This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_ordered_unary_compiler.py
More file actions
80 lines (64 loc) · 2.59 KB
/
test_ordered_unary_compiler.py
File metadata and controls
80 lines (64 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import typing
import pytest
from bigframes.core import agg_expressions as agg_exprs
from bigframes.core import array_value, identifiers, nodes, ordering
from bigframes.operations import aggregations as agg_ops
import bigframes.pandas as bpd
pytest.importorskip("pytest_snapshot")
def _apply_ordered_unary_agg_ops(
obj: bpd.DataFrame,
ops_list: typing.Sequence[agg_exprs.UnaryAggregation],
new_names: typing.Sequence[str],
ordering_args: typing.Sequence[str],
) -> str:
ordering_exprs = tuple(ordering.ascending_over(arg) for arg in ordering_args)
aggs = [(op, identifiers.ColumnId(name)) for op, name in zip(ops_list, new_names)]
agg_node = nodes.AggregateNode(
obj._block.expr.node,
aggregations=tuple(aggs),
by_column_ids=(),
order_by=ordering_exprs,
)
result = array_value.ArrayValue(agg_node)
sql = result.session._executor.to_sql(result, enable_cache=False)
return sql
def test_array_agg(scalar_types_df: bpd.DataFrame, snapshot):
# TODO: Verify "NULL LAST" syntax issue on Python < 3.12
if sys.version_info < (3, 12):
pytest.skip(
"Skipping test due to inconsistent SQL formatting on Python < 3.12.",
)
col_name = "int64_col"
bf_df = scalar_types_df[[col_name]]
agg_expr = agg_ops.ArrayAggOp().as_expr(col_name)
sql = _apply_ordered_unary_agg_ops(
bf_df, [agg_expr], [col_name], ordering_args=[col_name]
)
snapshot.assert_match(sql, "out.sql")
def test_string_agg(scalar_types_df: bpd.DataFrame, snapshot):
# TODO: Verify "NULL LAST" syntax issue on Python < 3.12
if sys.version_info < (3, 12):
pytest.skip(
"Skipping test due to inconsistent SQL formatting on Python < 3.12.",
)
col_name = "string_col"
bf_df = scalar_types_df[[col_name]]
agg_expr = agg_ops.StringAggOp(sep=",").as_expr(col_name)
sql = _apply_ordered_unary_agg_ops(
bf_df, [agg_expr], [col_name], ordering_args=[col_name]
)
snapshot.assert_match(sql, "out.sql")