|
| 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 itertools |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from bigframes.core import array_value |
| 20 | +import bigframes.operations as ops |
| 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 | +def apply_op_pairwise( |
| 31 | + array: array_value.ArrayValue, op: ops.BinaryOp, excluded_cols=[] |
| 32 | +) -> array_value.ArrayValue: |
| 33 | + exprs = [] |
| 34 | + for l_arg, r_arg in itertools.permutations(array.column_ids, 2): |
| 35 | + if (l_arg in excluded_cols) or (r_arg in excluded_cols): |
| 36 | + continue |
| 37 | + try: |
| 38 | + _ = op.output_type( |
| 39 | + array.get_column_type(l_arg), array.get_column_type(r_arg) |
| 40 | + ) |
| 41 | + exprs.append(op.as_expr(l_arg, r_arg)) |
| 42 | + except TypeError: |
| 43 | + continue |
| 44 | + assert len(exprs) > 0 |
| 45 | + new_arr, _ = array.compute_values(exprs) |
| 46 | + return new_arr |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "op", |
| 52 | + [ |
| 53 | + ops.and_op, |
| 54 | + ops.or_op, |
| 55 | + ops.xor_op, |
| 56 | + ], |
| 57 | +) |
| 58 | +def test_engines_project_boolean_op( |
| 59 | + scalars_array_value: array_value.ArrayValue, engine, op |
| 60 | +): |
| 61 | + # exclude string cols as does not contain dates |
| 62 | + # bool col actually doesn't work properly for bq engine |
| 63 | + arr = apply_op_pairwise(scalars_array_value, op, excluded_cols=["string_col"]) |
| 64 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
0 commit comments