Skip to content

Commit eb3489e

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

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

bigframes/dtypes.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,28 @@ 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 (precision, scale) == (76, 38):
385+
scalar_expr = ibis.literal(
386+
literal, ibis_dtypes.decimal(precision=76, scale=38)
387+
)
388+
elif (precision, scale) in ((38, 9), (None, None)):
389+
scalar_expr = ibis.literal(
390+
literal, ibis_dtypes.decimal(precision=38, scale=9)
391+
)
392+
else:
393+
raise TypeError(
394+
"BigQuery only supports decimal types with precision of 38 and "
395+
f"scale of 9 (NUMERIC) or precision of 76 and scale of 38 (BIGNUMERIC). "
396+
f"Current precision: {precision}. Current scale: {scale}"
397+
)
381398

382399
# TODO(bmil): support other literals that can be coerced to compatible types
383400
if validate and (scalar_expr.type() not in BIGFRAMES_TO_IBIS.values()):
384401
raise ValueError(
385-
f"Literal did not coerce to a supported data type: {literal}. {constants.FEEDBACK_LINK}"
402+
f"Literal did not coerce to a supported data type: {scalar_expr.type()}. {constants.FEEDBACK_LINK}"
386403
)
387404

388405
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)