Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 9a448d6

Browse files
committed
implement StartsWithOp
1 parent 9ed0078 commit 9a448d6

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import functools
1718
import typing
1819

1920
import pandas as pd
@@ -633,6 +634,18 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
633634
)
634635

635636

637+
@UNARY_OP_REGISTRATION.register(ops.StartsWithOp)
638+
def _(op: ops.StartsWithOp, expr: TypedExpr) -> sge.Expression:
639+
if not op.pat:
640+
return sge.false()
641+
642+
def to_startswith(pat: str) -> sge.Expression:
643+
return sge.func("STARTS_WITH", expr.expr, sge.convert(pat))
644+
645+
conditions = [to_startswith(pat) for pat in op.pat]
646+
return functools.reduce(lambda x, y: sge.Or(this=x, expression=y), conditions)
647+
648+
636649
@UNARY_OP_REGISTRATION.register(ops.StrStripOp)
637650
def _(op: ops.StrStripOp, expr: TypedExpr) -> sge.Expression:
638651
return sge.Trim(this=sge.convert(op.to_strip), 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+
STARTS_WITH(`bfcol_0`, 'ab') OR STARTS_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`
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+
STARTS_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
@@ -501,6 +501,18 @@ def test_sqrt(scalar_types_df: bpd.DataFrame, snapshot):
501501
snapshot.assert_match(sql, "out.sql")
502502

503503

504+
def test_startswith(scalar_types_df: bpd.DataFrame, snapshot):
505+
bf_df = scalar_types_df[["string_col"]]
506+
sql = _apply_unary_op(bf_df, ops.StartsWithOp(pat=("ab",)), "string_col")
507+
snapshot.assert_match(sql, "single_pattern.sql")
508+
509+
sql = _apply_unary_op(bf_df, ops.StartsWithOp(pat=("ab", "cd")), "string_col")
510+
snapshot.assert_match(sql, "multiple_patterns.sql")
511+
512+
sql = _apply_unary_op(bf_df, ops.StartsWithOp(pat=()), "string_col")
513+
snapshot.assert_match(sql, "no_pattern.sql")
514+
515+
504516
def test_str_get(scalar_types_df: bpd.DataFrame, snapshot):
505517
bf_df = scalar_types_df[["string_col"]]
506518
sql = _apply_unary_op(bf_df, ops.StrGetOp(1), "string_col")

0 commit comments

Comments
 (0)