Skip to content

Commit 434da27

Browse files
cpcloudjcrist
authored andcommitted
fix(duckdb): generate SIMILAR TO instead of tilde to workaround sqlglot issue
1 parent 42f5987 commit 434da27

File tree

11 files changed

+150
-0
lines changed

11 files changed

+150
-0
lines changed

ibis/backends/duckdb/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def _array_filter(t, op):
313313
ops.RegexReplace: fixed_arity(
314314
lambda *args: sa.func.regexp_replace(*args, sa.text("'g'")), 3
315315
),
316+
ops.RegexSearch: fixed_arity(lambda x, y: x.op("SIMILAR TO")(y), 2),
316317
ops.StringContains: fixed_arity(sa.func.contains, 2),
317318
ops.ApproxMedian: reduction(
318319
# without inline text, duckdb fails with
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT
2+
t0.*
3+
FROM `ibis-gbq.ibis_gbq_testing.functional_alltypes` AS t0
4+
WHERE
5+
REGEXP_CONTAINS(t0.`string_col`, '0')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT
2+
*
3+
FROM functional_alltypes AS t0
4+
WHERE
5+
match(t0.string_col, '0')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0.index,
3+
t0."Unnamed: 0",
4+
t0.id,
5+
t0.bool_col,
6+
t0.tinyint_col,
7+
t0.smallint_col,
8+
t0.int_col,
9+
t0.bigint_col,
10+
t0.float_col,
11+
t0.double_col,
12+
t0.date_string_col,
13+
t0.string_col,
14+
t0.timestamp_col,
15+
t0.year,
16+
t0.month
17+
FROM functional_alltypes AS t0
18+
WHERE
19+
t0.string_col SIMILAR TO '0'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT
2+
t0.*
3+
FROM ibis_testing.`functional_alltypes` AS t0
4+
WHERE
5+
t0.`string_col` RLIKE '0'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0.`index`,
3+
t0.`Unnamed: 0`,
4+
t0.id,
5+
t0.bool_col = 1 AS bool_col,
6+
t0.tinyint_col,
7+
t0.smallint_col,
8+
t0.int_col,
9+
t0.bigint_col,
10+
t0.float_col,
11+
t0.double_col,
12+
t0.date_string_col,
13+
t0.string_col,
14+
t0.timestamp_col,
15+
t0.year,
16+
t0.month
17+
FROM functional_alltypes AS t0
18+
WHERE
19+
REGEXP_LIKE(t0.string_col, '0')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0.index,
3+
t0."Unnamed: 0",
4+
t0.id,
5+
t0.bool_col,
6+
t0.tinyint_col,
7+
t0.smallint_col,
8+
t0.int_col,
9+
t0.bigint_col,
10+
t0.float_col,
11+
t0.double_col,
12+
t0.date_string_col,
13+
t0.string_col,
14+
t0.timestamp_col,
15+
t0.year,
16+
t0.month
17+
FROM functional_alltypes AS t0
18+
WHERE
19+
t0.string_col ~ '0'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0."index",
3+
t0."Unnamed: 0",
4+
t0."id",
5+
t0."bool_col",
6+
t0."tinyint_col",
7+
t0."smallint_col",
8+
t0."int_col",
9+
t0."bigint_col",
10+
t0."float_col",
11+
t0."double_col",
12+
t0."date_string_col",
13+
t0."string_col",
14+
t0."timestamp_col",
15+
t0."year",
16+
t0."month"
17+
FROM ibis_testing.voltrondataphillip."FUNCTIONAL_ALLTYPES" AS t0
18+
WHERE
19+
REGEXP_LIKE(t0."string_col", '0')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0."index",
3+
t0."Unnamed: 0",
4+
t0.id,
5+
t0.bool_col,
6+
t0.tinyint_col,
7+
t0.smallint_col,
8+
t0.int_col,
9+
t0.bigint_col,
10+
t0.float_col,
11+
t0.double_col,
12+
t0.date_string_col,
13+
t0.string_col,
14+
STRFTIME('%Y-%m-%d %H:%M:%f', t0.timestamp_col) AS timestamp_col,
15+
t0.year,
16+
t0.month
17+
FROM main.functional_alltypes AS t0
18+
WHERE
19+
_IBIS_SQLITE_REGEX_SEARCH(t0.string_col, '0')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SELECT
2+
t0.index,
3+
t0."unnamed: 0",
4+
t0.id,
5+
t0.bool_col,
6+
t0.tinyint_col,
7+
t0.smallint_col,
8+
t0.int_col,
9+
t0.bigint_col,
10+
t0.float_col,
11+
t0.double_col,
12+
t0.date_string_col,
13+
t0.string_col,
14+
t0.timestamp_col,
15+
t0.year,
16+
t0.month
17+
FROM functional_alltypes AS t0
18+
WHERE
19+
REGEXP_POSITION(t0.string_col, '0') <> -1

ibis/backends/tests/test_string.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,23 @@ def test_multiple_subs(con):
861861
expr = ibis.literal("foo").substitute(m)
862862
result = con.execute(expr)
863863
assert result == "FOO"
864+
865+
866+
@pytest.mark.never(
867+
["dask", "datafusion", "pandas", "polars", "pyspark"],
868+
raises=NotImplementedError,
869+
reason="not a SQL backend",
870+
)
871+
@pytest.mark.notyet(
872+
["mssql"],
873+
raises=OperationNotDefinedError,
874+
reason="Doesn't support regular expressions",
875+
)
876+
@pytest.mark.notyet(
877+
["druid"],
878+
raises=ValueError,
879+
reason="sqlglot doesn't support a druid dialect",
880+
)
881+
def test_rlike(snapshot, alltypes):
882+
expr = alltypes[alltypes.string_col.rlike('0')]
883+
snapshot.assert_match(str(ibis.to_sql(expr)), "out.sql")

0 commit comments

Comments
 (0)