@@ -922,35 +922,6 @@ def astype_op_impl(x: ibis_types.Value, op: ops.AsTypeOp):
922922 elif to_type == ibis_dtypes .time :
923923 return x_converted .time ()
924924
925- if to_type == ibis_dtypes .json :
926- if x .type () == ibis_dtypes .string :
927- return parse_json_in_safe (x ) if op .safe else parse_json (x )
928- if x .type () == ibis_dtypes .bool :
929- x_bool = typing .cast (
930- ibis_types .StringValue ,
931- bigframes .core .compile .ibis_types .cast_ibis_value (
932- x , ibis_dtypes .string , safe = op .safe
933- ),
934- ).lower ()
935- return parse_json_in_safe (x_bool ) if op .safe else parse_json (x_bool )
936- if x .type () in (ibis_dtypes .int64 , ibis_dtypes .float64 ):
937- x_str = bigframes .core .compile .ibis_types .cast_ibis_value (
938- x , ibis_dtypes .string , safe = op .safe
939- )
940- return parse_json_in_safe (x_str ) if op .safe else parse_json (x_str )
941-
942- if x .type () == ibis_dtypes .json :
943- if to_type == ibis_dtypes .int64 :
944- return cast_json_to_int64_in_safe (x ) if op .safe else cast_json_to_int64 (x )
945- if to_type == ibis_dtypes .float64 :
946- return (
947- cast_json_to_float64_in_safe (x ) if op .safe else cast_json_to_float64 (x )
948- )
949- if to_type == ibis_dtypes .bool :
950- return cast_json_to_bool_in_safe (x ) if op .safe else cast_json_to_bool (x )
951- if to_type == ibis_dtypes .string :
952- return cast_json_to_string_in_safe (x ) if op .safe else cast_json_to_string (x )
953-
954925 # TODO: either inline this function, or push rest of this op into the function
955926 return bigframes .core .compile .ibis_types .cast_ibis_value (x , to_type , safe = op .safe )
956927
@@ -1193,9 +1164,27 @@ def parse_json_op_impl(x: ibis_types.Value, op: ops.ParseJSON):
11931164 return parse_json (json_str = x )
11941165
11951166
1196- @scalar_op_compiler .register_unary_op (ops .ToJSON )
1197- def to_json_op_impl (json_obj : ibis_types .Value ):
1198- return to_json (json_obj = json_obj )
1167+ @scalar_op_compiler .register_unary_op (ops .ToJSON , pass_op = True )
1168+ def to_json_op_impl (x : ibis_types .Value , op : ops .ToJSON ):
1169+ if x .type () == ibis_dtypes .string :
1170+ return parse_json_in_safe (x ) if op .safe else parse_json (x )
1171+ return x .isnull ().ifelse (ibis .null ().cast (ibis_dtypes .json ), to_json (x ))
1172+
1173+
1174+ @scalar_op_compiler .register_unary_op (ops .JSONDecode , pass_op = True )
1175+ def json_decode_op_impl (x : ibis_types .Value , op : ops .JSONDecode ):
1176+ to_type = bigframes .core .compile .ibis_types .bigframes_dtype_to_ibis_dtype (
1177+ op .to_type
1178+ )
1179+ if to_type == ibis_dtypes .int64 :
1180+ return cast_json_to_int64_in_safe (x ) if op .safe else cast_json_to_int64 (x )
1181+ if to_type == ibis_dtypes .float64 :
1182+ return cast_json_to_float64_in_safe (x ) if op .safe else cast_json_to_float64 (x )
1183+ if to_type == ibis_dtypes .bool :
1184+ return cast_json_to_bool_in_safe (x ) if op .safe else cast_json_to_bool (x )
1185+ if to_type == ibis_dtypes .string :
1186+ return cast_json_to_string_in_safe (x ) if op .safe else cast_json_to_string (x )
1187+ raise TypeError (f"Cannot cast from JSON to type { to_type } " )
11991188
12001189
12011190@scalar_op_compiler .register_unary_op (ops .ToJSONString )
0 commit comments