Skip to content

Commit 93b02cd

Browse files
authored
fix: correct the numeric literal dtype (#365)
1 parent 747e5f6 commit 93b02cd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

bigframes/dtypes.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,29 @@ def literal_to_ibis_scalar(
378378
scalar_expr = ibis.literal(literal, ibis_dtypes.float64)
379379
elif scalar_expr.type().is_integer():
380380
scalar_expr = ibis.literal(literal, ibis_dtypes.int64)
381+
elif scalar_expr.type().is_decimal():
382+
precision = scalar_expr.type().precision
383+
scale = scalar_expr.type().scale
384+
if (not precision and not scale) or (
385+
precision and scale and scale <= 9 and precision + (9 - scale) <= 38
386+
):
387+
scalar_expr = ibis.literal(
388+
literal, ibis_dtypes.decimal(precision=38, scale=9)
389+
)
390+
elif precision and scale and scale <= 38 and precision + (38 - scale) <= 76:
391+
scalar_expr = ibis.literal(
392+
literal, ibis_dtypes.decimal(precision=76, scale=38)
393+
)
394+
else:
395+
raise TypeError(
396+
"BigQuery's decimal data type supports a maximum precision of 76 and a maximum scale of 38."
397+
f"Current precision: {precision}. Current scale: {scale}"
398+
)
381399

382400
# TODO(bmil): support other literals that can be coerced to compatible types
383401
if validate and (scalar_expr.type() not in BIGFRAMES_TO_IBIS.values()):
384402
raise ValueError(
385-
f"Literal did not coerce to a supported data type: {literal}. {constants.FEEDBACK_LINK}"
403+
f"Literal did not coerce to a supported data type: {scalar_expr.type()}. {constants.FEEDBACK_LINK}"
386404
)
387405

388406
return scalar_expr

tests/system/small/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,16 @@ def test_median(scalars_dfs):
12281228
assert pd_min < bf_result < pd_max
12291229

12301230

1231+
def test_numeric_literal(scalars_dfs):
1232+
scalars_df, _ = scalars_dfs
1233+
col_name = "numeric_col"
1234+
assert scalars_df[col_name].dtype == pd.ArrowDtype(pa.decimal128(38, 9))
1235+
bf_result = scalars_df[col_name] - scalars_df[col_name].median()
1236+
assert bf_result.size == scalars_df[col_name].size
1237+
# TODO(b/323387826): The precision increased by 1 unexpectedly.
1238+
# assert bf_result.dtype == pd.ArrowDtype(pa.decimal128(38, 9))
1239+
1240+
12311241
def test_repr(scalars_dfs):
12321242
scalars_df, scalars_pandas_df = scalars_dfs
12331243
if scalars_pandas_df.index.name != "rowindex":

0 commit comments

Comments
 (0)