Skip to content

Commit 7d7b5d4

Browse files
committed
MapOp compiler
1 parent 364aaef commit 7d7b5d4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,17 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
455455
return sge.Lower(this=expr.expr)
456456

457457

458+
@UNARY_OP_REGISTRATION.register(ops.MapOp)
459+
def _(op: ops.MapOp, expr: TypedExpr) -> sge.Expression:
460+
return sge.Case(
461+
this=expr.expr,
462+
ifs=[
463+
sge.If(this=sge.convert(key), true=sge.convert(value))
464+
for key, value in op.mappings
465+
],
466+
)
467+
468+
458469
@UNARY_OP_REGISTRATION.register(ops.minute_op)
459470
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
460471
return sge.Extract(this=sge.Identifier(this="MINUTE"), expression=expr.expr)
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+
`string_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
CASE `bfcol_0` WHEN 'value1' THEN 'mapped1' END AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_col`
13+
FROM `bfcte_1`

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ def test_lower(scalar_types_df: bpd.DataFrame, snapshot):
391391
snapshot.assert_match(sql, "out.sql")
392392

393393

394+
def test_map(scalar_types_df: bpd.DataFrame, snapshot):
395+
bf_df = scalar_types_df[["string_col"]]
396+
sql = _apply_unary_op(
397+
bf_df, ops.MapOp(mappings=(("value1", "mapped1"),)), "string_col"
398+
)
399+
400+
snapshot.assert_match(sql, "out.sql")
401+
402+
394403
def test_lstrip(scalar_types_df: bpd.DataFrame, snapshot):
395404
bf_df = scalar_types_df[["string_col"]]
396405
sql = _apply_unary_op(bf_df, ops.StrLstripOp(" "), "string_col")

0 commit comments

Comments
 (0)