Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bigframes/core/compile/sqlglot/expressions/generic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler

register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op
register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op
register_nary_op = scalar_compiler.scalar_op_compiler.register_nary_op
register_ternary_op = scalar_compiler.scalar_op_compiler.register_ternary_op

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


@register_binary_op(ops.coalesce_op)
Copy link
Copy Markdown
Contributor

@chelsea-lin chelsea-lin Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please enable the existing engine test test_engines_coalesce_op by adding "bq-sqlglot" to the "engine" parameters.

@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks.

def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
if left.expr == right.expr:
return left.expr
return sge.Coalesce(this=left.expr, expressions=[right.expr])


@register_nary_op(ops.RowKey)
def _(*values: TypedExpr) -> sge.Expression:
# All inputs into hash must be non-null or resulting hash will be null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WITH `bfcte_0` AS (
SELECT
`int64_col` AS `bfcol_0`,
`int64_too` AS `bfcol_1`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
`bfcol_0` AS `bfcol_2`,
COALESCE(`bfcol_1`, `bfcol_0`) AS `bfcol_3`
FROM `bfcte_0`
)
SELECT
`bfcol_2` AS `int64_col`,
`bfcol_3` AS `int64_too`
FROM `bfcte_1`
14 changes: 14 additions & 0 deletions tests/unit/core/compile/sqlglot/expressions/test_generic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ def test_case_when_op(scalar_types_df: bpd.DataFrame, snapshot):
snapshot.assert_match(sql, "out.sql")


def test_coalesce(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col", "int64_too"]]

sql = utils._apply_ops_to_sql(
bf_df,
[
ops.coalesce_op.as_expr("int64_col", "int64_col"),
ops.coalesce_op.as_expr("int64_too", "int64_col"),
],
["int64_col", "int64_too"],
)
snapshot.assert_match(sql, "out.sql")


def test_clip(scalar_types_df: bpd.DataFrame, snapshot):
op_expr = ops.clip_op.as_expr("rowindex", "int64_col", "int64_too")

Expand Down