Skip to content

Commit bed5b7b

Browse files
committed
refactor: add agg_ops.PopVarOp for the sqlglot compiler
1 parent 06a76b9 commit bed5b7b

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@ def _(
239239
return apply_window_if_present(sge.func("MIN", column.expr), window)
240240

241241

242+
@UNARY_OP_REGISTRATION.register(agg_ops.PopVarOp)
243+
def _(
244+
op: agg_ops.PopVarOp,
245+
column: typed_expr.TypedExpr,
246+
window: typing.Optional[window_spec.WindowSpec] = None,
247+
) -> sge.Expression:
248+
expr = column.expr
249+
if column.dtype == dtypes.BOOL_DTYPE:
250+
expr = sge.Cast(this=expr, to="INT64")
251+
252+
expr = sge.func("VAR_POP", expr)
253+
return apply_window_if_present(expr, window)
254+
255+
242256
@UNARY_OP_REGISTRATION.register(agg_ops.QuantileOp)
243257
def _(
244258
op: agg_ops.QuantileOp,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`bool_col` AS `bfcol_0`,
4+
`int64_col` AS `bfcol_1`
5+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6+
), `bfcte_1` AS (
7+
SELECT
8+
VAR_POP(`bfcol_1`) AS `bfcol_4`,
9+
VAR_POP(CAST(`bfcol_0` AS INT64)) AS `bfcol_5`
10+
FROM `bfcte_0`
11+
)
12+
SELECT
13+
`bfcol_4` AS `int64_col`,
14+
`bfcol_5` AS `bool_col`
15+
FROM `bfcte_1`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
*,
8+
CASE WHEN `bfcol_0` IS NULL THEN NULL ELSE VAR_POP(`bfcol_0`) OVER () END AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `agg_int64`
13+
FROM `bfcte_1`

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ def test_min(scalar_types_df: bpd.DataFrame, snapshot):
370370
snapshot.assert_match(sql_window_partition, "window_partition_out.sql")
371371

372372

373+
def test_pop_var(scalar_types_df: bpd.DataFrame, snapshot):
374+
col_names = ["int64_col", "bool_col"]
375+
bf_df = scalar_types_df[col_names]
376+
377+
agg_ops_map = {
378+
"int64_col": agg_ops.PopVarOp().as_expr("int64_col"),
379+
"bool_col": agg_ops.PopVarOp().as_expr("bool_col"),
380+
}
381+
sql = _apply_unary_agg_ops(
382+
bf_df, list(agg_ops_map.values()), list(agg_ops_map.keys())
383+
)
384+
snapshot.assert_match(sql, "out.sql")
385+
386+
# Window tests
387+
col_name = "int64_col"
388+
bf_df_int = scalar_types_df[[col_name]]
389+
agg_expr = agg_ops.PopVarOp().as_expr(col_name)
390+
window = window_spec.WindowSpec(ordering=(ordering.descending_over(col_name),))
391+
sql_window = _apply_unary_window_op(bf_df_int, agg_expr, window, "agg_int64")
392+
snapshot.assert_match(sql_window, "window_out.sql")
393+
394+
373395
def test_quantile(scalar_types_df: bpd.DataFrame, snapshot):
374396
col_name = "int64_col"
375397
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)