From 0fba04daee688427270ef00d0be081b836297044 Mon Sep 17 00:00:00 2001 From: Chelsea Lin Date: Thu, 11 Sep 2025 11:10:57 -0700 Subject: [PATCH] Revert "feat: support to cast struct to json (#2067)" This reverts commit b0ff718a04fadda33cfa3613b1d02822cde34bc2. --- .../ibis_compiler/scalar_op_registry.py | 4 +--- bigframes/dtypes.py | 9 +++---- bigframes/operations/generic_ops.py | 2 -- tests/system/small/test_series.py | 24 ------------------- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/bigframes/core/compile/ibis_compiler/scalar_op_registry.py b/bigframes/core/compile/ibis_compiler/scalar_op_registry.py index 804ee6f926..044fc90306 100644 --- a/bigframes/core/compile/ibis_compiler/scalar_op_registry.py +++ b/bigframes/core/compile/ibis_compiler/scalar_op_registry.py @@ -1023,8 +1023,6 @@ def astype_op_impl(x: ibis_types.Value, op: ops.AsTypeOp): x, ibis_dtypes.string, safe=op.safe ) return parse_json_in_safe(x_str) if op.safe else parse_json(x_str) - if x.type().is_struct(): - return to_json_string(typing.cast(ibis_types.StructValue, x)) if x.type() == ibis_dtypes.json: if to_type == ibis_dtypes.int64: @@ -2071,7 +2069,7 @@ def json_extract_string_array( # type: ignore[empty-body] @ibis_udf.scalar.builtin(name="to_json_string") def to_json_string( # type: ignore[empty-body] - json_obj, + json_obj: ibis_dtypes.JSON, ) -> ibis_dtypes.String: """Convert JSON to STRING.""" diff --git a/bigframes/dtypes.py b/bigframes/dtypes.py index ae68dbe7d3..ef1b9e7871 100644 --- a/bigframes/dtypes.py +++ b/bigframes/dtypes.py @@ -641,9 +641,6 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]: return BIGFRAMES_STRING_TO_BIGFRAMES[ typing.cast(DtypeString, str(dtype_string)) ] - if isinstance(dtype_string, str) and dtype_string.lower() == "json": - return JSON_DTYPE - raise TypeError( textwrap.dedent( f""" @@ -655,9 +652,9 @@ def _dtype_from_string(dtype_string: str) -> typing.Optional[Dtype]: The following pandas.ExtensionDtype are supported: pandas.BooleanDtype(), pandas.Float64Dtype(), pandas.Int64Dtype(), pandas.StringDtype(storage="pyarrow"), - pandas.ArrowDtype(pa.date32()), pandas.ArrowDtype(pa.time64("us")), - pandas.ArrowDtype(pa.timestamp("us")), - pandas.ArrowDtype(pa.timestamp("us", tz="UTC")). + pd.ArrowDtype(pa.date32()), pd.ArrowDtype(pa.time64("us")), + pd.ArrowDtype(pa.timestamp("us")), + pd.ArrowDtype(pa.timestamp("us", tz="UTC")). {constants.FEEDBACK_LINK} """ ) diff --git a/bigframes/operations/generic_ops.py b/bigframes/operations/generic_ops.py index ea25086aa9..d6155a770c 100644 --- a/bigframes/operations/generic_ops.py +++ b/bigframes/operations/generic_ops.py @@ -324,8 +324,6 @@ def _valid_cast(src: dtypes.Dtype, dst: dtypes.Dtype): if not _valid_cast(src_dtype, dst_dtype): return False return True - if dtypes.is_struct_like(src) and dst == dtypes.JSON_DTYPE: - return True return _valid_scalar_cast(src, dst) diff --git a/tests/system/small/test_series.py b/tests/system/small/test_series.py index 70dcafdb22..165e3b6df0 100644 --- a/tests/system/small/test_series.py +++ b/tests/system/small/test_series.py @@ -3866,30 +3866,6 @@ def test_string_astype_timestamp(): pd.testing.assert_series_equal(bf_result, pd_result, check_index_type=False) -def test_struct_astype_json(): - """See internal issue 444196993.""" - s = series.Series( - [ - {"version": 1, "project": "pandas"}, - {"version": 2, "project": "numpy"}, - ] - ) - assert dtypes.is_struct_like(s.dtype) - - expected = series.Series(s, dtype=dtypes.JSON_DTYPE) - assert expected.dtype == dtypes.JSON_DTYPE - - result = s.astype("json") - pd.testing.assert_series_equal( - result.to_pandas(), expected.to_pandas(), check_index_type=False - ) - - result = s.astype(dtypes.JSON_DTYPE) - pd.testing.assert_series_equal( - result.to_pandas(), expected.to_pandas(), check_index_type=False - ) - - def test_timestamp_astype_string(): bf_series = series.Series( [