Skip to content

Commit

Permalink
fix(datafusion): ensure that non-matching re_search calls return bool…
Browse files Browse the repository at this point in the history
… values when patterns do not match
  • Loading branch information
cpcloud authored and kszucs committed Dec 6, 2023
1 parent 2a47119 commit 088b027
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion ibis/backends/datafusion/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis.backends.base.sqlglot import (
FALSE,
NULL,
AggGen,
F,
Expand Down Expand Up @@ -441,7 +442,16 @@ def string_find(op, *, arg, substr, start, end, **_):

@translate_val.register(ops.RegexSearch)
def regex_search(op, *, arg, pattern, **_):
return F.array_length(F.regexp_match(arg, pattern)) > 0
return if_(
sg.or_(arg.is_(NULL), pattern.is_(NULL)),
NULL,
F.coalesce(
# null is returned for non-matching patterns, so coalesce to false
# because that is the desired behavior for ops.RegexSearch
F.array_length(F.regexp_match(arg, pattern)) > 0,
FALSE,
),
)


@translate_val.register(ops.StringContains)
Expand Down
5 changes: 4 additions & 1 deletion ibis/backends/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib

import numpy as np
import pandas as pd
import pytest
import sqlalchemy as sa
Expand Down Expand Up @@ -1090,4 +1091,6 @@ def test_no_conditional_percent_escape(con, expr):
)
def test_non_match_regex_search_is_false(con):
expr = ibis.literal("foo").re_search("bar")
assert con.execute(expr) is False
result = con.execute(expr)
assert isinstance(result, (bool, np.bool_))
assert not result

0 comments on commit 088b027

Please sign in to comment.