Skip to content

Commit 5665696

Browse files
committed
chore: implement ReplaceStrOp and RegexReplaceStrOp
1 parent 1785c30 commit 5665696

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def _(op: ops.StrContainsOp, expr: TypedExpr) -> sge.Expression:
222222
def _(op: ops.StrRepeatOp, expr: TypedExpr) -> sge.Expression:
223223
return sge.Repeat(this=expr.expr, times=sge.convert(op.repeats))
224224

225+
225226
@UNARY_OP_REGISTRATION.register(ops.date_op)
226227
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
227228
return sge.Date(this=expr.expr)
@@ -560,6 +561,24 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
560561
return sge.Extract(this=sge.Identifier(this="QUARTER"), expression=expr.expr)
561562

562563

564+
@UNARY_OP_REGISTRATION.register(ops.ReplaceStrOp)
565+
def _(op: ops.ReplaceStrOp, expr: TypedExpr) -> sge.Expression:
566+
return sge.Replace(
567+
this=expr.expr,
568+
old=sge.convert(op.pat),
569+
new=sge.convert(op.repl),
570+
)
571+
572+
573+
@UNARY_OP_REGISTRATION.register(ops.RegexReplaceStrOp)
574+
def _(op: ops.RegexReplaceStrOp, expr: TypedExpr) -> sge.Expression:
575+
return sge.RegexpReplace(
576+
this=expr.expr,
577+
expression=sge.convert(op.pat),
578+
replacement=sge.convert(op.repl),
579+
)
580+
581+
563582
@UNARY_OP_REGISTRATION.register(ops.reverse_op)
564583
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
565584
return sge.func("REVERSE", expr.expr)
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+
REGEXP_REPLACE(`bfcol_0`, 'e', 'a') AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_col`
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+
`string_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
REPLACE(`bfcol_0`) 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,18 @@ def test_quarter(scalar_types_df: bpd.DataFrame, snapshot):
431431
snapshot.assert_match(sql, "out.sql")
432432

433433

434+
def test_replace_str(scalar_types_df: bpd.DataFrame, snapshot):
435+
bf_df = scalar_types_df[["string_col"]]
436+
sql = _apply_unary_op(bf_df, ops.ReplaceStrOp("e", "a"), "string_col")
437+
snapshot.assert_match(sql, "out.sql")
438+
439+
440+
def test_regex_replace_str(scalar_types_df: bpd.DataFrame, snapshot):
441+
bf_df = scalar_types_df[["string_col"]]
442+
sql = _apply_unary_op(bf_df, ops.RegexReplaceStrOp(r"e", "a"), "string_col")
443+
snapshot.assert_match(sql, "out.sql")
444+
445+
434446
def test_reverse(scalar_types_df: bpd.DataFrame, snapshot):
435447
bf_df = scalar_types_df[["string_col"]]
436448
sql = _apply_unary_op(bf_df, ops.reverse_op, "string_col")

0 commit comments

Comments
 (0)