Skip to content

Commit bacab0e

Browse files
committed
refactor: support agg_ops.AnyValueOp in sqlglot compiler
1 parent 9130a61 commit bacab0e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

bigframes/core/compile/sqlglot/aggregations/unary_compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ def _(
6969
return sge.func("APPROX_TOP_COUNT", column.expr, sge.convert(op.number))
7070

7171

72+
@UNARY_OP_REGISTRATION.register(agg_ops.AnyValueOp)
73+
def _(
74+
op: agg_ops.AnyValueOp,
75+
column: typed_expr.TypedExpr,
76+
window: typing.Optional[window_spec.WindowSpec] = None,
77+
) -> sge.Expression:
78+
return apply_window_if_present(sge.func("ANY_VALUE", column.expr), window)
79+
80+
7281
@UNARY_OP_REGISTRATION.register(agg_ops.CountOp)
7382
def _(
7483
op: agg_ops.CountOp,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`int64_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
ANY_VALUE(`bfcol_0`) AS `bfcol_1`
8+
FROM `bfcte_0`
9+
)
10+
SELECT
11+
`bfcol_1` AS `int64_col`
12+
FROM `bfcte_1`

tests/unit/core/compile/sqlglot/aggregations/test_unary_compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def test_approx_top_count(scalar_types_df: bpd.DataFrame, snapshot):
8787
snapshot.assert_match(sql, "out.sql")
8888

8989

90+
def test_any_value(scalar_types_df: bpd.DataFrame, snapshot):
91+
col_name = "int64_col"
92+
bf_df = scalar_types_df[[col_name]]
93+
agg_expr = agg_ops.AnyValueOp().as_expr(col_name)
94+
sql = _apply_unary_agg_ops(bf_df, [agg_expr], [col_name])
95+
96+
snapshot.assert_match(sql, "out.sql")
97+
98+
9099
def test_count(scalar_types_df: bpd.DataFrame, snapshot):
91100
col_name = "int64_col"
92101
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)