Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d472d2d

Browse filesBrowse files
authored
feat: convert BIGNUMERIC values to decimal objects (#414)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Towards #367 🦕
1 parent 985a8cf commit d472d2d
Copy full SHA for d472d2d

File tree

Expand file treeCollapse file tree

2 files changed

+67
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+67
-23
lines changed

‎google/cloud/bigquery/_helpers.py

Copy file name to clipboardExpand all lines: google/cloud/bigquery/_helpers.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def _record_from_json(value, field):
188188
"FLOAT": _float_from_json,
189189
"FLOAT64": _float_from_json,
190190
"NUMERIC": _decimal_from_json,
191+
"BIGNUMERIC": _decimal_from_json,
191192
"BOOLEAN": _bool_from_json,
192193
"BOOL": _bool_from_json,
193194
"STRING": _string_from_json,
@@ -347,6 +348,7 @@ def _time_to_json(value):
347348
"FLOAT": _float_to_json,
348349
"FLOAT64": _float_to_json,
349350
"NUMERIC": _decimal_to_json,
351+
"BIGNUMERIC": _decimal_to_json,
350352
"BOOLEAN": _bool_to_json,
351353
"BOOL": _bool_to_json,
352354
"BYTES": _bytes_to_json,

‎tests/unit/test_client.py

Copy file name to clipboardExpand all lines: tests/unit/test_client.py
+65-23Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6290,38 +6290,43 @@ def test_insert_rows_w_numeric(self):
62906290
creds = _make_credentials()
62916291
http = object()
62926292
client = self._make_one(project=project, credentials=creds, _http=http)
6293-
conn = client._connection = make_connection({})
62946293
table_ref = DatasetReference(project, ds_id).table(table_id)
6295-
schema = [SchemaField("account", "STRING"), SchemaField("balance", "NUMERIC")]
6296-
insert_table = table.Table(table_ref, schema=schema)
62976294
rows = [
62986295
("Savings", decimal.Decimal("23.47")),
62996296
("Checking", decimal.Decimal("1.98")),
63006297
("Mortgage", decimal.Decimal("-12345678909.87654321")),
63016298
]
6299+
schemas = [
6300+
[SchemaField("account", "STRING"), SchemaField("balance", "NUMERIC")],
6301+
[SchemaField("account", "STRING"), SchemaField("balance", "BIGNUMERIC")],
6302+
]
63026303

6303-
with mock.patch("uuid.uuid4", side_effect=map(str, range(len(rows)))):
6304-
errors = client.insert_rows(insert_table, rows)
6304+
for schema in schemas:
6305+
conn = client._connection = make_connection({})
63056306

6306-
self.assertEqual(len(errors), 0)
6307-
rows_json = [
6308-
{"account": "Savings", "balance": "23.47"},
6309-
{"account": "Checking", "balance": "1.98"},
6310-
{"account": "Mortgage", "balance": "-12345678909.87654321"},
6311-
]
6312-
sent = {
6313-
"rows": [
6314-
{"json": row, "insertId": str(i)} for i, row in enumerate(rows_json)
6307+
insert_table = table.Table(table_ref, schema=schema)
6308+
with mock.patch("uuid.uuid4", side_effect=map(str, range(len(rows)))):
6309+
errors = client.insert_rows(insert_table, rows)
6310+
6311+
self.assertEqual(len(errors), 0)
6312+
rows_json = [
6313+
{"account": "Savings", "balance": "23.47"},
6314+
{"account": "Checking", "balance": "1.98"},
6315+
{"account": "Mortgage", "balance": "-12345678909.87654321"},
63156316
]
6316-
}
6317-
conn.api_request.assert_called_once_with(
6318-
method="POST",
6319-
path="/projects/{}/datasets/{}/tables/{}/insertAll".format(
6320-
project, ds_id, table_id
6321-
),
6322-
data=sent,
6323-
timeout=None,
6324-
)
6317+
sent = {
6318+
"rows": [
6319+
{"json": row, "insertId": str(i)} for i, row in enumerate(rows_json)
6320+
]
6321+
}
6322+
conn.api_request.assert_called_once_with(
6323+
method="POST",
6324+
path="/projects/{}/datasets/{}/tables/{}/insertAll".format(
6325+
project, ds_id, table_id
6326+
),
6327+
data=sent,
6328+
timeout=None,
6329+
)
63256330

63266331
@unittest.skipIf(pandas is None, "Requires `pandas`")
63276332
def test_insert_rows_from_dataframe(self):
@@ -6915,6 +6920,43 @@ def test_list_rows_query_params(self):
69156920
test[1]["formatOptions.useInt64Timestamp"] = True
69166921
self.assertEqual(req[1]["query_params"], test[1], "for kwargs %s" % test[0])
69176922

6923+
def test_list_rows_w_numeric(self):
6924+
from google.cloud.bigquery.schema import SchemaField
6925+
from google.cloud.bigquery.table import Table
6926+
6927+
resource = {
6928+
"totalRows": 3,
6929+
"rows": [
6930+
{"f": [{"v": "-1.23456789"}, {"v": "-123456789.987654321"}]},
6931+
{"f": [{"v": None}, {"v": "3.141592653589793238462643383279502884"}]},
6932+
{"f": [{"v": "2718281828459045235360287471.352662497"}, {"v": None}]},
6933+
],
6934+
}
6935+
creds = _make_credentials()
6936+
http = object()
6937+
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
6938+
client._connection = make_connection(resource)
6939+
schema = [
6940+
SchemaField("num", "NUMERIC"),
6941+
SchemaField("bignum", "BIGNUMERIC"),
6942+
]
6943+
table = Table(self.TABLE_REF, schema=schema)
6944+
6945+
iterator = client.list_rows(table)
6946+
rows = list(iterator)
6947+
6948+
self.assertEqual(len(rows), 3)
6949+
self.assertEqual(rows[0]["num"], decimal.Decimal("-1.23456789"))
6950+
self.assertEqual(rows[0]["bignum"], decimal.Decimal("-123456789.987654321"))
6951+
self.assertIsNone(rows[1]["num"])
6952+
self.assertEqual(
6953+
rows[1]["bignum"], decimal.Decimal("3.141592653589793238462643383279502884")
6954+
)
6955+
self.assertEqual(
6956+
rows[2]["num"], decimal.Decimal("2718281828459045235360287471.352662497")
6957+
)
6958+
self.assertIsNone(rows[2]["bignum"])
6959+
69186960
def test_list_rows_repeated_fields(self):
69196961
from google.cloud.bigquery.schema import SchemaField
69206962

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.