|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import pandas as pd |
16 | | -import polars as pl |
17 | 16 | import pytest |
18 | 17 |
|
| 18 | +try: |
| 19 | + import polars as pl |
| 20 | + |
| 21 | + POLARS_INSTALLED = True |
| 22 | +except ImportError: |
| 23 | + POLARS_INSTALLED = False |
| 24 | + |
| 25 | +if not POLARS_INSTALLED: |
| 26 | + pytest.skip("polars is not installed", allow_module_level=True) |
| 27 | + |
| 28 | + |
19 | 29 | import bigframes as bf |
20 | 30 | import bigframes.core.compile.polars.compiler as polars_compiler |
21 | 31 | import bigframes.core.nodes as nodes |
@@ -48,21 +58,20 @@ def test_polars_parse_json(): |
48 | 58 | result_df = df.with_columns(result.alias("b")).collect() |
49 | 59 | # The result of json_decode is a struct |
50 | 60 | assert isinstance(result_df["b"][0], dict) |
51 | | - assert result_df["b"][0] == {"b": 2} |
| 61 | + assert result_df["b"][0]["b"] == 2 |
52 | 62 |
|
53 | 63 |
|
54 | | -@pytest.mark.skip(reason="Polars does not have json_extract on string expressions") |
55 | 64 | def test_polars_json_extract(): |
56 | 65 | """Test JSONExtract operation in Polars compiler.""" |
57 | 66 | compiler = polars_compiler.PolarsExpressionCompiler() |
58 | 67 | op = json_ops.JSONExtract(json_path="$.b") |
59 | 68 | input_expr = pl.lit('{"a": 1, "b": "hello"}', dtype=pl.String) |
60 | 69 | result = compiler.compile_op(op, input_expr) |
61 | 70 |
|
62 | | - df = pl.DataFrame({"a": ['{"b": "world"}']}).lazy() |
| 71 | + df = pl.DataFrame({"a": ['{"a": 1, "b": "hello"}']}).lazy() |
63 | 72 | result_df = df.with_columns(result.alias("b")).collect() |
64 | | - # json_extract returns a JSON encoded string |
65 | | - assert result_df["b"][0] == '"world"' |
| 73 | + # json_path_match returns the raw string value |
| 74 | + assert result_df["b"][0] == "hello" |
66 | 75 |
|
67 | 76 |
|
68 | 77 | def test_readlocal_with_json_column(polars_session): |
|
0 commit comments