Skip to content

Commit

Permalink
feat(sqlalchemy): implement ilike
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Mar 14, 2022
1 parent 527af3c commit 43996c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
12 changes: 7 additions & 5 deletions ibis/backends/base/sql/alchemy/registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import operator
from typing import Any, Dict

Expand Down Expand Up @@ -290,10 +291,10 @@ def unary(sa_func):
return fixed_arity(sa_func, 1)


def _string_like(t, expr):
arg, pattern, escape = expr.op().args
result = t.translate(arg).like(t.translate(pattern), escape=escape)
return result
def _string_like(method_name, t, expr):
op = expr.op()
method = getattr(t.translate(op.arg), method_name)
return method(t.translate(op.pattern), escape=op.escape)


def _startswith(t, expr):
Expand Down Expand Up @@ -482,7 +483,8 @@ def _sort_key(t, expr):
ops.StringAscii: unary(sa.func.ascii),
ops.StringLength: unary(sa.func.length),
ops.StringReplace: fixed_arity(sa.func.replace, 3),
ops.StringSQLLike: _string_like,
ops.StringSQLLike: functools.partial(_string_like, "like"),
ops.StringSQLILike: functools.partial(_string_like, "ilike"),
ops.StartsWith: _startswith,
ops.EndsWith: _endswith,
ops.StringConcat: varargs(sa.func.concat),
Expand Down
28 changes: 21 additions & 7 deletions ibis/backends/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,46 @@ def test_string_col_is_unicode(backend, alltypes, df):
[
"clickhouse",
"datafusion",
"duckdb",
"impala",
"mysql",
"postgres",
"pyspark",
"sqlite",
]
),
),
param(
lambda t: t.string_col.re_search(r'[[:digit:]]+'),
lambda t: t.string_col.str.contains(r'\d+'),
id='re_search_posix',
marks=pytest.mark.notimpl(["datafusion", "pyspark"]),
),
param(
lambda t: t.string_col.re_extract(r'([[:digit:]]+)', 0),
lambda t: t.string_col.str.extract(r'(\d+)', expand=False),
id='re_extract_posix',
marks=pytest.mark.notimpl(["mysql", "pyspark"]),
),
param(
lambda t: t.string_col.re_replace(r'[[:digit:]]+', 'a'),
lambda t: t.string_col.str.replace(r'\d+', 'a', regex=True),
id='re_replace_posix',
marks=pytest.mark.notimpl(['datafusion', "mysql", "pyspark"]),
),
param(
lambda t: t.string_col.re_search(r'\d+'),
lambda t: t.string_col.str.contains(r'\d+'),
id='re_search',
marks=pytest.mark.notimpl(["datafusion"]),
marks=pytest.mark.notimpl(["impala", "datafusion"]),
),
param(
lambda t: t.string_col.re_extract(r'(\d+)', 0),
lambda t: t.string_col.str.extract(r'(\d+)', expand=False),
id='re_extract',
marks=pytest.mark.notimpl(["mysql"]),
marks=pytest.mark.notimpl(["impala", "mysql"]),
),
param(
lambda t: t.string_col.re_replace(r'\d+', 'a'),
lambda t: t.string_col.str.replace(r'\d+', 'a', regex=True),
id='re_replace',
marks=pytest.mark.notimpl(['datafusion', "mysql"]),
marks=pytest.mark.notimpl(["impala", "datafusion", "mysql"]),
),
param(
lambda t: t.string_col.repeat(2),
Expand Down

0 comments on commit 43996c0

Please sign in to comment.