|
| 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 | +from typing import Literal |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from bigframes import operations as ops |
| 20 | +from bigframes.core import array_value, expression, ordering |
| 21 | +from bigframes.session import polars_executor |
| 22 | +from bigframes.testing.engine_utils import assert_equivalence_execution |
| 23 | + |
| 24 | +pytest.importorskip("polars") |
| 25 | + |
| 26 | +# Polars used as reference as its fast and local. Generally though, prefer gbq engine where they disagree. |
| 27 | +REFERENCE_ENGINE = polars_executor.PolarsExecutor() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 31 | +@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"]) |
| 32 | +def test_engines_join_on_key( |
| 33 | + scalars_array_value: array_value.ArrayValue, |
| 34 | + engine, |
| 35 | + join_type: Literal["inner", "outer", "left", "right"], |
| 36 | +): |
| 37 | + result, _ = scalars_array_value.relational_join( |
| 38 | + scalars_array_value, conditions=(("int64_col", "int64_col"),), type=join_type |
| 39 | + ) |
| 40 | + |
| 41 | + assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 45 | +@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"]) |
| 46 | +def test_engines_join_on_coerced_key( |
| 47 | + scalars_array_value: array_value.ArrayValue, |
| 48 | + engine, |
| 49 | + join_type: Literal["inner", "outer", "left", "right"], |
| 50 | +): |
| 51 | + result, _ = scalars_array_value.relational_join( |
| 52 | + scalars_array_value, conditions=(("int64_col", "float64_col"),), type=join_type |
| 53 | + ) |
| 54 | + |
| 55 | + assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 59 | +@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"]) |
| 60 | +def test_engines_join_multi_key( |
| 61 | + scalars_array_value: array_value.ArrayValue, |
| 62 | + engine, |
| 63 | + join_type: Literal["inner", "outer", "left", "right"], |
| 64 | +): |
| 65 | + l_input = scalars_array_value.order_by([ordering.ascending_over("float64_col")]) |
| 66 | + l_input, l_join_cols = scalars_array_value.compute_values( |
| 67 | + [ |
| 68 | + ops.mod_op.as_expr("int64_col", expression.const(2)), |
| 69 | + ops.invert_op.as_expr("bool_col"), |
| 70 | + ] |
| 71 | + ) |
| 72 | + r_input, r_join_cols = scalars_array_value.compute_values( |
| 73 | + [ops.mod_op.as_expr("int64_col", expression.const(3)), expression.const(True)] |
| 74 | + ) |
| 75 | + |
| 76 | + conditions = tuple((l_col, r_col) for l_col, r_col in zip(l_join_cols, r_join_cols)) |
| 77 | + |
| 78 | + result, _ = l_input.relational_join(r_input, conditions=conditions, type=join_type) |
| 79 | + |
| 80 | + assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 84 | +def test_engines_cross_join( |
| 85 | + scalars_array_value: array_value.ArrayValue, |
| 86 | + engine, |
| 87 | +): |
| 88 | + result, _ = scalars_array_value.relational_join(scalars_array_value, type="cross") |
| 89 | + |
| 90 | + assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine) |
0 commit comments