|
| 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, expression |
| 18 | +import bigframes.operations as ops |
| 19 | +import bigframes.operations.aggregations as agg_ops |
| 20 | +from bigframes.session import polars_executor |
| 21 | +from bigframes.testing.engine_utils import assert_equivalence_execution |
| 22 | + |
| 23 | +pytest.importorskip("polars") |
| 24 | + |
| 25 | +# Polars used as reference as its fast and local. Generally though, prefer gbq engine where they disagree. |
| 26 | +REFERENCE_ENGINE = polars_executor.PolarsExecutor() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 30 | +def test_engines_to_array_op(scalars_array_value: array_value.ArrayValue, engine): |
| 31 | + # Bigquery won't allow you to materialize arrays with null, so use non-nullable |
| 32 | + int64_non_null = ops.coalesce_op.as_expr("int64_col", expression.const(0)) |
| 33 | + bool_col_non_null = ops.coalesce_op.as_expr("bool_col", expression.const(False)) |
| 34 | + float_col_non_null = ops.coalesce_op.as_expr("float64_col", expression.const(0.0)) |
| 35 | + string_col_non_null = ops.coalesce_op.as_expr("string_col", expression.const("")) |
| 36 | + |
| 37 | + arr, _ = scalars_array_value.compute_values( |
| 38 | + [ |
| 39 | + ops.ToArrayOp().as_expr(int64_non_null), |
| 40 | + ops.ToArrayOp().as_expr( |
| 41 | + int64_non_null, bool_col_non_null, float_col_non_null |
| 42 | + ), |
| 43 | + ops.ToArrayOp().as_expr(string_col_non_null, string_col_non_null), |
| 44 | + ] |
| 45 | + ) |
| 46 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 50 | +def test_engines_array_reduce_op(arrays_array_value: array_value.ArrayValue, engine): |
| 51 | + arr, _ = arrays_array_value.compute_values( |
| 52 | + [ |
| 53 | + ops.ArrayReduceOp(agg_ops.SumOp()).as_expr("float_list_col"), |
| 54 | + ops.ArrayReduceOp(agg_ops.StdOp()).as_expr("float_list_col"), |
| 55 | + ops.ArrayReduceOp(agg_ops.MaxOp()).as_expr("date_list_col"), |
| 56 | + ops.ArrayReduceOp(agg_ops.CountOp()).as_expr("string_list_col"), |
| 57 | + ops.ArrayReduceOp(agg_ops.AnyOp()).as_expr("bool_list_col"), |
| 58 | + ] |
| 59 | + ) |
| 60 | + assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) |
0 commit comments