Skip to content

Commit 308fa1c

Browse files
committed
refactor: support agg_ops.ShiftOp for the sqlglot compiler
1 parent 7600001 commit 308fa1c

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ def _(
194194
return apply_window_if_present(sge.func("COUNT", sge.convert(1)), window)
195195

196196

197+
@UNARY_OP_REGISTRATION.register(agg_ops.ShiftOp)
198+
def _(
199+
op: agg_ops.ShiftOp,
200+
column: typed_expr.TypedExpr,
201+
window: typing.Optional[window_spec.WindowSpec] = None,
202+
) -> sge.Expression:
203+
if op.periods == 0: # No-op
204+
return column.expr
205+
if op.periods > 0:
206+
return apply_window_if_present(
207+
sge.func("LAG", column.expr, sge.convert(op.periods)), window
208+
)
209+
return apply_window_if_present(
210+
sge.func("LEAD", column.expr, sge.convert(-op.periods)), window
211+
)
212+
213+
197214
@UNARY_OP_REGISTRATION.register(agg_ops.SumOp)
198215
def _(
199216
op: agg_ops.SumOp,
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+
LAG(`bfcol_0`, 1) OVER (ORDER BY `bfcol_0` IS NULL ASC NULLS LAST, `bfcol_0` ASC NULLS LAST) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `lag`
13+
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+
LEAD(`bfcol_0`, 1) OVER (ORDER BY `bfcol_0` IS NULL ASC NULLS LAST, `bfcol_0` ASC NULLS LAST) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `lead`
13+
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+
`bfcol_0` AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `noop`
13+
FROM `bfcte_1`

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,33 @@ def test_rank(scalar_types_df: bpd.DataFrame, snapshot):
210210
snapshot.assert_match(sql, "out.sql")
211211

212212

213+
def test_shift(scalar_types_df: bpd.DataFrame, snapshot):
214+
col_name = "int64_col"
215+
bf_df = scalar_types_df[[col_name]]
216+
window = window_spec.WindowSpec(ordering=(ordering.ascending_over(col_name),))
217+
218+
# Test lag
219+
lag_op = agg_exprs.UnaryAggregation(
220+
agg_ops.ShiftOp(periods=1), expression.deref(col_name)
221+
)
222+
lag_sql = _apply_unary_window_op(bf_df, lag_op, window, "lag")
223+
snapshot.assert_match(lag_sql, "lag.sql")
224+
225+
# Test lead
226+
lead_op = agg_exprs.UnaryAggregation(
227+
agg_ops.ShiftOp(periods=-1), expression.deref(col_name)
228+
)
229+
lead_sql = _apply_unary_window_op(bf_df, lead_op, window, "lead")
230+
snapshot.assert_match(lead_sql, "lead.sql")
231+
232+
# Test no-op
233+
noop_op = agg_exprs.UnaryAggregation(
234+
agg_ops.ShiftOp(periods=0), expression.deref(col_name)
235+
)
236+
noop_sql = _apply_unary_window_op(bf_df, noop_op, window, "noop")
237+
snapshot.assert_match(noop_sql, "noop.sql")
238+
239+
213240
def test_sum(scalar_types_df: bpd.DataFrame, snapshot):
214241
bf_df = scalar_types_df[["int64_col", "bool_col"]]
215242
agg_ops_map = {

0 commit comments

Comments
 (0)