|
| 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, identifiers, nodes |
| 18 | +import bigframes.operations.aggregations as agg_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_aggregate_size( |
| 30 | + scalars_array_value: array_value.ArrayValue, |
| 31 | + engine, |
| 32 | +): |
| 33 | + node = nodes.AggregateNode( |
| 34 | + scalars_array_value.node, |
| 35 | + aggregations=( |
| 36 | + ( |
| 37 | + expression.NullaryAggregation(agg_ops.SizeOp()), |
| 38 | + identifiers.ColumnId("size_op"), |
| 39 | + ), |
| 40 | + ( |
| 41 | + expression.UnaryAggregation( |
| 42 | + agg_ops.SizeUnaryOp(), expression.deref("string_col") |
| 43 | + ), |
| 44 | + identifiers.ColumnId("unary_size_op"), |
| 45 | + ), |
| 46 | + ), |
| 47 | + ) |
| 48 | + assert_equivalence_execution(node, REFERENCE_ENGINE, engine) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "grouping_cols", |
| 54 | + [ |
| 55 | + ["bool_col"], |
| 56 | + ["string_col", "int64_col"], |
| 57 | + ["date_col"], |
| 58 | + ["datetime_col"], |
| 59 | + ["timestamp_col"], |
| 60 | + ["bytes_col"], |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_engines_grouped_aggregate( |
| 64 | + scalars_array_value: array_value.ArrayValue, engine, grouping_cols |
| 65 | +): |
| 66 | + node = nodes.AggregateNode( |
| 67 | + scalars_array_value.node, |
| 68 | + aggregations=( |
| 69 | + ( |
| 70 | + expression.NullaryAggregation(agg_ops.SizeOp()), |
| 71 | + identifiers.ColumnId("size_op"), |
| 72 | + ), |
| 73 | + ( |
| 74 | + expression.UnaryAggregation( |
| 75 | + agg_ops.SizeUnaryOp(), expression.deref("string_col") |
| 76 | + ), |
| 77 | + identifiers.ColumnId("unary_size_op"), |
| 78 | + ), |
| 79 | + ), |
| 80 | + by_column_ids=tuple(expression.deref(id) for id in grouping_cols), |
| 81 | + ) |
| 82 | + assert_equivalence_execution(node, REFERENCE_ENGINE, engine) |
0 commit comments