Skip to content

Commit 9260877

Browse files
fix system tests
1 parent c01b99b commit 9260877

3 files changed

Lines changed: 10 additions & 20 deletions

File tree

packages/bigframes/bigframes/core/compile/ibis_compiler/scalar_op_registry.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,20 +1168,7 @@ def parse_json_op_impl(x: ibis_types.Value, op: ops.ParseJSON):
11681168
def to_json_op_impl(x: ibis_types.Value, op: ops.ToJSON):
11691169
if x.type() == ibis_dtypes.string:
11701170
return parse_json_in_safe(x) if op.safe else parse_json(x)
1171-
if x.type() == ibis_dtypes.bool:
1172-
x_bool = typing.cast(
1173-
ibis_types.StringValue,
1174-
bigframes.core.compile.ibis_types.cast_ibis_value(
1175-
x, ibis_dtypes.string, safe=op.safe
1176-
),
1177-
).lower()
1178-
return parse_json_in_safe(x_bool) if op.safe else parse_json(x_bool)
1179-
if x.type() in (ibis_dtypes.int64, ibis_dtypes.float64):
1180-
x_str = bigframes.core.compile.ibis_types.cast_ibis_value(
1181-
x, ibis_dtypes.string, safe=op.safe
1182-
)
1183-
return parse_json_in_safe(x_str) if op.safe else parse_json(x_str)
1184-
raise TypeError(f"Cannot cast to JSON from type {x.type()}")
1171+
return to_json(x)
11851172

11861173

11871174
@scalar_op_compiler.register_unary_op(ops.JSONDecode, pass_op=True)

packages/bigframes/bigframes/core/compile/polars/compiler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,13 @@ def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
484484

485485
@compile_op.register(json_ops.ToJSON)
486486
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
487-
# Polars represents JSON as string, so to_json is cast to String
488-
return input.cast(pl.String())
487+
# Polars represents JSON as string, so to_json is cast to String.
488+
# Handle null values by mapping them to JSON 'null' representation.
489+
return (
490+
pl.when(input.is_null())
491+
.then(pl.lit("null"))
492+
.otherwise(input.cast(pl.String()))
493+
)
489494

490495
@compile_op.register(arr_ops.ToArrayOp)
491496
def _(self, op: ops.ToArrayOp, *inputs: pl.Expr) -> pl.Expr:

packages/bigframes/bigframes/core/compile/sqlglot/expressions/json_ops.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ def _(expr: TypedExpr, op: ops.ToJSON) -> sge.Expression:
7878
if from_type == dtypes.STRING_DTYPE:
7979
func_name = "SAFE.PARSE_JSON" if op.safe else "PARSE_JSON"
8080
return sge.func(func_name, sg_expr)
81-
if from_type in (dtypes.INT_DTYPE, dtypes.BOOL_DTYPE, dtypes.FLOAT_DTYPE):
82-
sg_expr = sge.Cast(this=sg_expr, to="STRING")
83-
return sge.func("PARSE_JSON", sg_expr)
84-
raise TypeError(f"Cannot cast from {from_type} to {dtypes.JSON_DTYPE}")
81+
else:
82+
return sge.func("TO_JSON", sg_expr)
8583

8684

8785
@register_unary_op(ops.JSONDecode, pass_op=True)

0 commit comments

Comments
 (0)