Skip to content

Commit 66d4be7

Browse files
committed
refactor: add agg_ops.ApproxTopCountOp to sqlglot compiler
1 parent 1bde1b1 commit 66d4be7

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ def _(
5858
)
5959

6060

61+
@UNARY_OP_REGISTRATION.register(agg_ops.ApproxTopCountOp)
62+
def _(
63+
op: agg_ops.ApproxTopCountOp,
64+
column: typed_expr.TypedExpr,
65+
window: typing.Optional[window_spec.WindowSpec] = None,
66+
) -> sge.Expression:
67+
if window is not None:
68+
raise NotImplementedError("Approx top count with windowing is not supported.")
69+
return sge.func("APPROX_TOP_COUNT", column.expr, sge.convert(op.number))
70+
71+
6172
@UNARY_OP_REGISTRATION.register(agg_ops.CountOp)
6273
def _(
6374
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+
APPROX_TOP_COUNT(`bfcol_0`, 10) 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
@@ -53,6 +53,15 @@ def test_approx_quartiles(scalar_types_df: bpd.DataFrame, snapshot):
5353
snapshot.assert_match(sql, "out.sql")
5454

5555

56+
def test_approx_top_count(scalar_types_df: bpd.DataFrame, snapshot):
57+
col_name = "int64_col"
58+
bf_df = scalar_types_df[[col_name]]
59+
agg_expr = agg_ops.ApproxTopCountOp(number=10).as_expr(col_name)
60+
sql = _apply_unary_agg_ops(bf_df, [agg_expr], [col_name])
61+
62+
snapshot.assert_match(sql, "out.sql")
63+
64+
5665
def test_count(scalar_types_df: bpd.DataFrame, snapshot):
5766
col_name = "int64_col"
5867
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)