|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from bigframes.core import array_value |
| 18 | +import bigframes.operations as ops |
| 19 | +from bigframes.session import polars_executor |
| 20 | +from bigframes.testing.engine_utils import assert_equivalence_execution |
| 21 | + |
| 22 | +pytest.importorskip("polars") |
| 23 | + |
| 24 | +# Polars used as reference as its fast and local. Generally though, prefer gbq engine where they disagree. |
| 25 | +REFERENCE_ENGINE = polars_executor.PolarsExecutor() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 29 | +def test_engines_str_contains(scalars_array_value: array_value.ArrayValue, engine): |
| 30 | + arr, _ = scalars_array_value.compute_values( |
| 31 | + [ |
| 32 | + ops.StrContainsOp("(?i)hEllo").as_expr("string_col"), |
| 33 | + ops.StrContainsOp("Hello").as_expr("string_col"), |
| 34 | + ops.StrContainsOp("T").as_expr("string_col"), |
| 35 | + ops.StrContainsOp(".*").as_expr("string_col"), |
| 36 | + ] |
| 37 | + ) |
| 38 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 42 | +def test_engines_str_contains_regex( |
| 43 | + scalars_array_value: array_value.ArrayValue, engine |
| 44 | +): |
| 45 | + arr, _ = scalars_array_value.compute_values( |
| 46 | + [ |
| 47 | + ops.StrContainsRegexOp("(?i)hEllo").as_expr("string_col"), |
| 48 | + ops.StrContainsRegexOp("Hello").as_expr("string_col"), |
| 49 | + ops.StrContainsRegexOp("T").as_expr("string_col"), |
| 50 | + ops.StrContainsRegexOp(".*").as_expr("string_col"), |
| 51 | + ] |
| 52 | + ) |
| 53 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 57 | +def test_engines_str_startswith(scalars_array_value: array_value.ArrayValue, engine): |
| 58 | + arr, _ = scalars_array_value.compute_values( |
| 59 | + [ |
| 60 | + ops.StartsWithOp("He").as_expr("string_col"), |
| 61 | + ops.StartsWithOp("llo").as_expr("string_col"), |
| 62 | + ops.StartsWithOp(("He", "T", "ca")).as_expr("string_col"), |
| 63 | + ] |
| 64 | + ) |
| 65 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 69 | +def test_engines_str_endswith(scalars_array_value: array_value.ArrayValue, engine): |
| 70 | + arr, _ = scalars_array_value.compute_values( |
| 71 | + [ |
| 72 | + ops.EndsWithOp("!").as_expr("string_col"), |
| 73 | + ops.EndsWithOp("llo").as_expr("string_col"), |
| 74 | + ops.EndsWithOp(("He", "T", "ca")).as_expr("string_col"), |
| 75 | + ] |
| 76 | + ) |
| 77 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
0 commit comments