Skip to content

Commit

Permalink
fix(duckdb): generate SIMILAR TO instead of tilde to workaround sql…
Browse files Browse the repository at this point in the history
…glot issue
  • Loading branch information
cpcloud authored and jcrist committed Mar 24, 2023
1 parent 42f5987 commit 434da27
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions ibis/backends/duckdb/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def _array_filter(t, op):
ops.RegexReplace: fixed_arity(
lambda *args: sa.func.regexp_replace(*args, sa.text("'g'")), 3
),
ops.RegexSearch: fixed_arity(lambda x, y: x.op("SIMILAR TO")(y), 2),
ops.StringContains: fixed_arity(sa.func.contains, 2),
ops.ApproxMedian: reduction(
# without inline text, duckdb fails with
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
t0.*
FROM `ibis-gbq.ibis_gbq_testing.functional_alltypes` AS t0
WHERE
REGEXP_CONTAINS(t0.`string_col`, '0')
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
*
FROM functional_alltypes AS t0
WHERE
match(t0.string_col, '0')
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0.index,
t0."Unnamed: 0",
t0.id,
t0.bool_col,
t0.tinyint_col,
t0.smallint_col,
t0.int_col,
t0.bigint_col,
t0.float_col,
t0.double_col,
t0.date_string_col,
t0.string_col,
t0.timestamp_col,
t0.year,
t0.month
FROM functional_alltypes AS t0
WHERE
t0.string_col SIMILAR TO '0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT
t0.*
FROM ibis_testing.`functional_alltypes` AS t0
WHERE
t0.`string_col` RLIKE '0'
19 changes: 19 additions & 0 deletions ibis/backends/tests/snapshots/test_string/test_rlike/mysql/out.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0.`index`,
t0.`Unnamed: 0`,
t0.id,
t0.bool_col = 1 AS bool_col,
t0.tinyint_col,
t0.smallint_col,
t0.int_col,
t0.bigint_col,
t0.float_col,
t0.double_col,
t0.date_string_col,
t0.string_col,
t0.timestamp_col,
t0.year,
t0.month
FROM functional_alltypes AS t0
WHERE
REGEXP_LIKE(t0.string_col, '0')
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0.index,
t0."Unnamed: 0",
t0.id,
t0.bool_col,
t0.tinyint_col,
t0.smallint_col,
t0.int_col,
t0.bigint_col,
t0.float_col,
t0.double_col,
t0.date_string_col,
t0.string_col,
t0.timestamp_col,
t0.year,
t0.month
FROM functional_alltypes AS t0
WHERE
t0.string_col ~ '0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0."index",
t0."Unnamed: 0",
t0."id",
t0."bool_col",
t0."tinyint_col",
t0."smallint_col",
t0."int_col",
t0."bigint_col",
t0."float_col",
t0."double_col",
t0."date_string_col",
t0."string_col",
t0."timestamp_col",
t0."year",
t0."month"
FROM ibis_testing.voltrondataphillip."FUNCTIONAL_ALLTYPES" AS t0
WHERE
REGEXP_LIKE(t0."string_col", '0')
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0."index",
t0."Unnamed: 0",
t0.id,
t0.bool_col,
t0.tinyint_col,
t0.smallint_col,
t0.int_col,
t0.bigint_col,
t0.float_col,
t0.double_col,
t0.date_string_col,
t0.string_col,
STRFTIME('%Y-%m-%d %H:%M:%f', t0.timestamp_col) AS timestamp_col,
t0.year,
t0.month
FROM main.functional_alltypes AS t0
WHERE
_IBIS_SQLITE_REGEX_SEARCH(t0.string_col, '0')
19 changes: 19 additions & 0 deletions ibis/backends/tests/snapshots/test_string/test_rlike/trino/out.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT
t0.index,
t0."unnamed: 0",
t0.id,
t0.bool_col,
t0.tinyint_col,
t0.smallint_col,
t0.int_col,
t0.bigint_col,
t0.float_col,
t0.double_col,
t0.date_string_col,
t0.string_col,
t0.timestamp_col,
t0.year,
t0.month
FROM functional_alltypes AS t0
WHERE
REGEXP_POSITION(t0.string_col, '0') <> -1
20 changes: 20 additions & 0 deletions ibis/backends/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,23 @@ def test_multiple_subs(con):
expr = ibis.literal("foo").substitute(m)
result = con.execute(expr)
assert result == "FOO"


@pytest.mark.never(
["dask", "datafusion", "pandas", "polars", "pyspark"],
raises=NotImplementedError,
reason="not a SQL backend",
)
@pytest.mark.notyet(
["mssql"],
raises=OperationNotDefinedError,
reason="Doesn't support regular expressions",
)
@pytest.mark.notyet(
["druid"],
raises=ValueError,
reason="sqlglot doesn't support a druid dialect",
)
def test_rlike(snapshot, alltypes):
expr = alltypes[alltypes.string_col.rlike('0')]
snapshot.assert_match(str(ibis.to_sql(expr)), "out.sql")

0 comments on commit 434da27

Please sign in to comment.