Skip to content

Commit 68108ac

Browse files
committed
fix: correct the numeric literal dtype
1 parent ffb0d15 commit 68108ac

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

bigframes/dtypes.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,27 @@ 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 or precision <= 38) and (not scale or scale <= 9):
385+
scalar_expr = ibis.literal(
386+
literal, ibis_dtypes.decimal(precision=38, scale=9)
387+
)
388+
elif precision <= 76 and scale <= 38:
389+
scalar_expr = ibis.literal(
390+
literal, ibis_dtypes.decimal(precision=76, scale=38)
391+
)
392+
else:
393+
raise TypeError(
394+
"BigQuery only support decimal type with precision up to 76 and scale "
395+
f"up to 38. Current precision: {precision}. Current scale: {scale}"
396+
)
381397

382398
# TODO(bmil): support other literals that can be coerced to compatible types
383399
if validate and (scalar_expr.type() not in BIGFRAMES_TO_IBIS.values()):
384400
raise ValueError(
385-
f"Literal did not coerce to a supported data type: {literal}. {constants.FEEDBACK_LINK}"
401+
f"Literal did not coerce to a supported data type: {scalar_expr.type()}. {constants.FEEDBACK_LINK}"
386402
)
387403

388404
return scalar_expr

tests/system/small/test_series.py

Lines changed: 10 additions & 0 deletions
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)