|
14 | 14 |
|
15 | 15 | import re |
16 | 16 |
|
| 17 | +import pandas as pd |
| 18 | +import pyarrow as pa |
17 | 19 | import pytest |
18 | 20 |
|
19 | 21 | from bigframes.core import array_value, expression |
@@ -275,6 +277,58 @@ def test_engines_astype_from_json(scalars_array_value: array_value.ArrayValue, e |
275 | 277 | assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
276 | 278 |
|
277 | 279 |
|
| 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 | + |
278 | 332 | @pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
279 | 333 | def test_engines_astype_timedelta(scalars_array_value: array_value.ArrayValue, engine): |
280 | 334 | arr = apply_op( |
|
0 commit comments