Skip to content

Commit b9c8be0

Browse files
Gaurang033Linchin
andauthored
fix: load_table_from_dataframe for higher scale decimal (#1703)
* fix: load_table_from_dataframe for higher scale decimal * Update test_client.py * fix test_load_table_from_dataframe_w_higher_scale_decimal128_datatype --------- Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
1 parent fdd580f commit b9c8be0

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

google/cloud/bigquery/_pandas_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import warnings
2424
from typing import Any, Union
2525

26+
2627
from google.cloud.bigquery import _pyarrow_helpers
2728
from google.cloud.bigquery import _versions_helpers
2829
from google.cloud.bigquery import schema
@@ -485,7 +486,6 @@ def augment_schema(dataframe, current_bq_schema):
485486
# pytype: disable=attribute-error
486487
augmented_schema = []
487488
unknown_type_fields = []
488-
489489
for field in current_bq_schema:
490490
if field.field_type is not None:
491491
augmented_schema.append(field)
@@ -515,6 +515,8 @@ def augment_schema(dataframe, current_bq_schema):
515515
else:
516516
detected_mode = field.mode
517517
detected_type = _pyarrow_helpers.arrow_scalar_ids_to_bq(arrow_table.type.id)
518+
if detected_type == "NUMERIC" and arrow_table.type.scale > 9:
519+
detected_type = "BIGNUMERIC"
518520

519521
if detected_type is None:
520522
unknown_type_fields.append(field)

tests/unit/test_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8891,6 +8891,49 @@ def test_load_table_from_dataframe_with_csv_source_format(self):
88918891
sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
88928892
assert sent_config.source_format == job.SourceFormat.CSV
88938893

8894+
@unittest.skipIf(pandas is None, "Requires `pandas`")
8895+
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
8896+
def test_load_table_from_dataframe_w_higher_scale_decimal128_datatype(self):
8897+
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
8898+
from google.cloud.bigquery import job
8899+
from google.cloud.bigquery.schema import SchemaField
8900+
from decimal import Decimal
8901+
8902+
client = self._make_client()
8903+
dataframe = pandas.DataFrame({"x": [Decimal("0.1234567891")]})
8904+
load_patch = mock.patch(
8905+
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
8906+
)
8907+
8908+
get_table_patch = mock.patch(
8909+
"google.cloud.bigquery.client.Client.get_table", autospec=True
8910+
)
8911+
with load_patch as load_table_from_file, get_table_patch:
8912+
client.load_table_from_dataframe(
8913+
dataframe, self.TABLE_REF, location=self.LOCATION
8914+
)
8915+
8916+
load_table_from_file.assert_called_once_with(
8917+
client,
8918+
mock.ANY,
8919+
self.TABLE_REF,
8920+
num_retries=_DEFAULT_NUM_RETRIES,
8921+
rewind=True,
8922+
size=mock.ANY,
8923+
job_id=mock.ANY,
8924+
job_id_prefix=None,
8925+
location=self.LOCATION,
8926+
project=None,
8927+
job_config=mock.ANY,
8928+
timeout=DEFAULT_TIMEOUT,
8929+
)
8930+
8931+
sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
8932+
assert sent_config.source_format == job.SourceFormat.PARQUET
8933+
assert tuple(sent_config.schema) == (
8934+
SchemaField("x", "BIGNUMERIC", "NULLABLE", None),
8935+
)
8936+
88948937
def test_load_table_from_json_basic_use(self):
88958938
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
88968939
from google.cloud.bigquery import job

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy