Skip to content

Commit b779c73

Browse files
committed
implement EndsWithOp
1 parent 9a448d6 commit b779c73

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
293293
return sge.Extract(this=sge.Identifier(this="DAYOFYEAR"), expression=expr.expr)
294294

295295

296+
@UNARY_OP_REGISTRATION.register(ops.EndsWithOp)
297+
def _(op: ops.EndsWithOp, expr: TypedExpr) -> sge.Expression:
298+
if not op.pat:
299+
return sge.false()
300+
301+
def to_endswith(pat: str) -> sge.Expression:
302+
return sge.func("ENDS_WITH", expr.expr, sge.convert(pat))
303+
304+
conditions = [to_endswith(pat) for pat in op.pat]
305+
return functools.reduce(lambda x, y: sge.Or(this=x, expression=y), conditions)
306+
307+
296308
@UNARY_OP_REGISTRATION.register(ops.exp_op)
297309
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
298310
return sge.Case(
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+
ENDS_WITH(`bfcol_0`, 'ab') OR ENDS_WITH(`bfcol_0`, 'cd') 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+
FALSE AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `string_col`
13+
FROM `bfcte_1`
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+
ENDS_WITH(`bfcol_0`, 'ab') 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
@@ -125,6 +125,18 @@ def test_dayofyear(scalar_types_df: bpd.DataFrame, snapshot):
125125
snapshot.assert_match(sql, "out.sql")
126126

127127

128+
def test_endswith(scalar_types_df: bpd.DataFrame, snapshot):
129+
bf_df = scalar_types_df[["string_col"]]
130+
sql = _apply_unary_op(bf_df, ops.EndsWithOp(pat=("ab",)), "string_col")
131+
snapshot.assert_match(sql, "single_pattern.sql")
132+
133+
sql = _apply_unary_op(bf_df, ops.EndsWithOp(pat=("ab", "cd")), "string_col")
134+
snapshot.assert_match(sql, "multiple_patterns.sql")
135+
136+
sql = _apply_unary_op(bf_df, ops.EndsWithOp(pat=()), "string_col")
137+
snapshot.assert_match(sql, "no_pattern.sql")
138+
139+
128140
def test_exp(scalar_types_df: bpd.DataFrame, snapshot):
129141
bf_df = scalar_types_df[["float64_col"]]
130142
sql = _apply_unary_op(bf_df, ops.exp_op, "float64_col")

0 commit comments

Comments
 (0)