Skip to content

Commit a61bb09

Browse files
committed
chore: Migrate coalesce_op operator to SQLGlot
Migrated the coalesce_op operator from Ibis to SQLGlot.
1 parent 3de8995 commit a61bb09

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

bigframes/core/compile/sqlglot/expressions/generic_ops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler
2525

2626
register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op
27+
register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op
2728
register_nary_op = scalar_compiler.scalar_op_compiler.register_nary_op
2829
register_ternary_op = scalar_compiler.scalar_op_compiler.register_ternary_op
2930

@@ -159,6 +160,13 @@ def _(*cases_and_outputs: TypedExpr) -> sge.Expression:
159160
)
160161

161162

163+
@register_binary_op(ops.coalesce_op)
164+
def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
165+
if left.expr == right.expr:
166+
return left.expr
167+
return sge.Coalesce(this=left.expr, expressions=[right.expr])
168+
169+
162170
@register_nary_op(ops.RowKey)
163171
def _(*values: TypedExpr) -> sge.Expression:
164172
# All inputs into hash must be non-null or resulting hash will be null
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`int64_col` AS `bfcol_0`,
4+
`int64_too` AS `bfcol_1`
5+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6+
), `bfcte_1` AS (
7+
SELECT
8+
*,
9+
COALESCE(`bfcol_0`, `bfcol_1`) AS `bfcol_2`
10+
FROM `bfcte_0`
11+
)
12+
SELECT
13+
`bfcol_2` AS `int64_col`
14+
FROM `bfcte_1`

tests/unit/core/compile/sqlglot/expressions/test_generic_ops.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ def test_case_when_op(scalar_types_df: bpd.DataFrame, snapshot):
209209
snapshot.assert_match(sql, "out.sql")
210210

211211

212+
def test_coalesce(scalar_types_df: bpd.DataFrame, snapshot):
213+
bf_df = scalar_types_df[["int64_col", "int64_too"]]
214+
sql = utils._apply_binary_op(bf_df, ops.coalesce_op, "int64_col", "int64_too")
215+
216+
snapshot.assert_match(sql, "out.sql")
217+
218+
212219
def test_clip(scalar_types_df: bpd.DataFrame, snapshot):
213220
op_expr = ops.clip_op.as_expr("rowindex", "int64_col", "int64_too")
214221

0 commit comments

Comments
 (0)