Skip to content

Commit 149ebc9

Browse files
committed
refactor: add agg_ops.DiffOp
1 parent 38bcf9a commit 149ebc9

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-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
@@ -151,6 +151,23 @@ def _(
151151
)
152152

153153

154+
@UNARY_OP_REGISTRATION.register(agg_ops.DiffOp)
155+
def _(
156+
op: agg_ops.DiffOp,
157+
column: typed_expr.TypedExpr,
158+
window: typing.Optional[window_spec.WindowSpec] = None,
159+
) -> sge.Expression:
160+
shift_op_impl = UNARY_OP_REGISTRATION[agg_ops.ShiftOp(0)]
161+
shifted = shift_op_impl(agg_ops.ShiftOp(op.periods), column, window)
162+
if column.dtype in (dtypes.BOOL_DTYPE, dtypes.INT_DTYPE, dtypes.FLOAT_DTYPE):
163+
if column.dtype == dtypes.BOOL_DTYPE:
164+
return sge.NEQ(this=column.expr, expression=shifted)
165+
else:
166+
return sge.Sub(this=column.expr, expression=shifted)
167+
else:
168+
raise TypeError(f"Cannot perform diff on type {column.dtype}")
169+
170+
154171
@UNARY_OP_REGISTRATION.register(agg_ops.MaxOp)
155172
def _(
156173
op: agg_ops.MaxOp,
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+
`bool_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
`bfcol_0` <> 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 `diff_bool`
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` - 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 `diff_int`
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
@@ -127,6 +127,28 @@ def test_dense_rank(scalar_types_df: bpd.DataFrame, snapshot):
127127
snapshot.assert_match(sql, "out.sql")
128128

129129

130+
def test_diff(scalar_types_df: bpd.DataFrame, snapshot):
131+
# Test integer
132+
int_col = "int64_col"
133+
bf_df_int = scalar_types_df[[int_col]]
134+
window = window_spec.WindowSpec(ordering=(ordering.ascending_over(int_col),))
135+
int_op = agg_exprs.UnaryAggregation(
136+
agg_ops.DiffOp(periods=1), expression.deref(int_col)
137+
)
138+
int_sql = _apply_unary_window_op(bf_df_int, int_op, window, "diff_int")
139+
snapshot.assert_match(int_sql, "diff_int.sql")
140+
141+
# Test boolean
142+
bool_col = "bool_col"
143+
bf_df_bool = scalar_types_df[[bool_col]]
144+
window = window_spec.WindowSpec(ordering=(ordering.ascending_over(bool_col),))
145+
bool_op = agg_exprs.UnaryAggregation(
146+
agg_ops.DiffOp(periods=1), expression.deref(bool_col)
147+
)
148+
bool_sql = _apply_unary_window_op(bf_df_bool, bool_op, window, "diff_bool")
149+
snapshot.assert_match(bool_sql, "diff_bool.sql")
150+
151+
130152
def test_first(scalar_types_df: bpd.DataFrame, snapshot):
131153
if sys.version_info < (3, 12):
132154
pytest.skip(

0 commit comments

Comments
 (0)