Skip to content

Commit 9e928f9

Browse files
committed
tests: add engine tests for casting to json
1 parent b0ff718 commit 9e928f9

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

tests/system/small/engines/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def scalars_array_value(
7373
return ArrayValue.from_managed(managed_data_source, fake_session)
7474

7575

76+
@pytest.fixture(scope="module")
77+
def nested_array_value(
78+
nested_data_source: local_data.ManagedArrowTable, fake_session: bigframes.Session
79+
):
80+
return ArrayValue.from_managed(nested_data_source, fake_session)
81+
82+
7683
@pytest.fixture(scope="module")
7784
def zero_row_source() -> local_data.ManagedArrowTable:
7885
return local_data.ManagedArrowTable.from_pandas(pd.DataFrame({"a": [], "b": []}))

tests/system/small/engines/test_generic_ops.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import re
1616

17+
import pandas as pd
18+
import pyarrow as pa
1719
import pytest
1820

1921
from bigframes.core import array_value, expression
@@ -275,6 +277,58 @@ def test_engines_astype_from_json(scalars_array_value: array_value.ArrayValue, e
275277
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)
276278

277279

280+
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
281+
def test_engines_astype_to_json(scalars_array_value: array_value.ArrayValue, engine):
282+
exprs = [
283+
ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
284+
expression.deref("int64_col")
285+
),
286+
ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
287+
# Use a const since float to json has precision issues
288+
expression.const(5.2, bigframes.dtypes.FLOAT_DTYPE)
289+
),
290+
ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
291+
expression.deref("bool_col")
292+
),
293+
ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
294+
# Use a const since "str_col" has special chars.
295+
expression.const('"hello world"', bigframes.dtypes.STRING_DTYPE)
296+
),
297+
]
298+
arr, _ = scalars_array_value.compute_values(exprs)
299+
300+
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)
301+
302+
303+
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
304+
def test_engines_astype_struct_to_json(
305+
nested_array_value: array_value.ArrayValue, engine
306+
):
307+
json_data = [
308+
{"version": 1, "project": "pandas"},
309+
{"version": 2, "project": "numpy"},
310+
]
311+
exprs = [
312+
# ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
313+
# expression.deref("label")
314+
# ),
315+
# ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
316+
# expression.deref("address")
317+
# ),
318+
ops.AsTypeOp(to_type=bigframes.dtypes.JSON_DTYPE).as_expr(
319+
expression.const(
320+
json_data,
321+
pd.ArrowDtype(
322+
pa.struct([("version", pa.int64()), ("project", pa.string())])
323+
),
324+
)
325+
),
326+
]
327+
arr, _ = nested_array_value.compute_values(exprs)
328+
329+
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine)
330+
331+
278332
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
279333
def test_engines_astype_timedelta(scalars_array_value: array_value.ArrayValue, engine):
280334
arr = apply_op(

0 commit comments

Comments
 (0)