Skip to content

Commit

Permalink
feat(bigquery): Add parsing support for STRPOS(...) (#4378)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD authored Nov 12, 2024
1 parent 358cafa commit 72ffdcb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
timestrtotime_sql,
ts_or_ds_add_cast,
unit_to_var,
str_position_sql,
)
from sqlglot.helper import seq_get, split_num_words
from sqlglot.tokens import TokenType
Expand Down Expand Up @@ -492,6 +493,7 @@ class Parser(parser.Parser):
this=seq_get(args, 0),
expression=seq_get(args, 1) or exp.Literal.string(","),
),
"STRPOS": exp.StrPosition.from_arg_list,
"TIME": _build_time,
"TIME_ADD": build_date_delta_with_interval(exp.TimeAdd),
"TIME_SUB": build_date_delta_with_interval(exp.TimeSub),
Expand Down Expand Up @@ -845,6 +847,7 @@ class Generator(generator.Generator):
"DETERMINISTIC" if e.name == "IMMUTABLE" else "NOT DETERMINISTIC"
),
exp.String: rename_func("STRING"),
exp.StrPosition: str_position_sql,
exp.StrToDate: _str_to_datetime_sql,
exp.StrToTime: _str_to_datetime_sql,
exp.TimeAdd: date_add_interval_sql("TIME", "ADD"),
Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,14 @@ def test_bigquery(self):
"snowflake": """SELECT TRANSFORM(GET_PATH(PARSE_JSON('{"arr": [1, "a"]}'), 'arr'), x -> CAST(x AS VARCHAR))""",
},
)
self.validate_all(
"SELECT STRPOS('foo@example.com', '@')",
write={
"bigquery": "SELECT STRPOS('foo@example.com', '@')",
"duckdb": "SELECT STRPOS('foo@example.com', '@')",
"snowflake": "SELECT POSITION('@', 'foo@example.com')",
},
)

def test_errors(self):
with self.assertRaises(TokenError):
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,7 @@ def test_operators(self):
"duckdb": "STRPOS(haystack, needle)",
"postgres": "STRPOS(haystack, needle)",
"presto": "STRPOS(haystack, needle)",
"bigquery": "STRPOS(haystack, needle)",
"spark": "LOCATE(needle, haystack)",
"clickhouse": "position(haystack, needle)",
"snowflake": "POSITION(needle, haystack)",
Expand Down

0 comments on commit 72ffdcb

Please sign in to comment.