Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 7f5a4b0

Browse files
committed
fix: load_table_from_dataframe for higher scale decimal
1 parent c9af8c1 commit 7f5a4b0

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

google/cloud/bigquery/_pandas_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ 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 (
519+
arrow_table.type.precision > 38 or arrow_table.type.scale > 9
520+
):
521+
detected_type = "BIGNUMERIC"
518522

519523
if detected_type is None:
520524
unknown_type_fields.append(field)

tests/unit/test_client.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8891,6 +8891,62 @@ 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(
8895+
pandas is None or PANDAS_INSTALLED_VERSION < PANDAS_MINIUM_VERSION,
8896+
"Only `pandas version >=1.0.0` supported",
8897+
)
8898+
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
8899+
def test_load_table_from_dataframe_w_higher_scale_decimal128_datatype(self):
8900+
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
8901+
from google.cloud.bigquery import job
8902+
from google.cloud.bigquery.schema import SchemaField
8903+
from decimal import Decimal
8904+
8905+
client = self._make_client()
8906+
dataframe = pandas.DataFrame(
8907+
{
8908+
"x": [
8909+
Decimal("0.12345678901234560000000000000000000000"),
8910+
Decimal("01234567890123456789012345678901234567.1234567891"),
8911+
]
8912+
}
8913+
)
8914+
load_patch = mock.patch(
8915+
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
8916+
)
8917+
8918+
get_table_patch = mock.patch(
8919+
"google.cloud.bigquery.client.Client.get_table",
8920+
autospec=True,
8921+
return_value=mock.Mock(schema=[SchemaField("x", "BIGNUMERIC", "NULLABLE")]),
8922+
)
8923+
8924+
with load_patch as load_table_from_file, get_table_patch:
8925+
client.load_table_from_dataframe(
8926+
dataframe, self.TABLE_REF, location=self.LOCATION
8927+
)
8928+
8929+
load_table_from_file.assert_called_once_with(
8930+
client,
8931+
mock.ANY,
8932+
self.TABLE_REF,
8933+
num_retries=_DEFAULT_NUM_RETRIES,
8934+
rewind=True,
8935+
size=mock.ANY,
8936+
job_id=mock.ANY,
8937+
job_id_prefix=None,
8938+
location=self.LOCATION,
8939+
project=None,
8940+
job_config=mock.ANY,
8941+
timeout=DEFAULT_TIMEOUT,
8942+
)
8943+
8944+
sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
8945+
assert sent_config.source_format == job.SourceFormat.PARQUET
8946+
assert tuple(sent_config.schema) == (
8947+
SchemaField("x", "BIGNUMERIC", "NULLABLE", None),
8948+
)
8949+
88948950
def test_load_table_from_json_basic_use(self):
88958951
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
88968952
from google.cloud.bigquery import job

0 commit comments

Comments
 (0)