Skip to content

Commit 088b027

Browse files
cpcloudkszucs
authored andcommitted
fix(datafusion): ensure that non-matching re_search calls return bool values when patterns do not match
1 parent 2a47119 commit 088b027

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

ibis/backends/datafusion/compiler/values.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import ibis.expr.datatypes as dt
1212
import ibis.expr.operations as ops
1313
from ibis.backends.base.sqlglot import (
14+
FALSE,
1415
NULL,
1516
AggGen,
1617
F,
@@ -441,7 +442,16 @@ def string_find(op, *, arg, substr, start, end, **_):
441442

442443
@translate_val.register(ops.RegexSearch)
443444
def regex_search(op, *, arg, pattern, **_):
444-
return F.array_length(F.regexp_match(arg, pattern)) > 0
445+
return if_(
446+
sg.or_(arg.is_(NULL), pattern.is_(NULL)),
447+
NULL,
448+
F.coalesce(
449+
# null is returned for non-matching patterns, so coalesce to false
450+
# because that is the desired behavior for ops.RegexSearch
451+
F.array_length(F.regexp_match(arg, pattern)) > 0,
452+
FALSE,
453+
),
454+
)
445455

446456

447457
@translate_val.register(ops.StringContains)

ibis/backends/tests/test_string.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44

5+
import numpy as np
56
import pandas as pd
67
import pytest
78
import sqlalchemy as sa
@@ -1090,4 +1091,6 @@ def test_no_conditional_percent_escape(con, expr):
10901091
)
10911092
def test_non_match_regex_search_is_false(con):
10921093
expr = ibis.literal("foo").re_search("bar")
1093-
assert con.execute(expr) is False
1094+
result = con.execute(expr)
1095+
assert isinstance(result, (bool, np.bool_))
1096+
assert not result

0 commit comments

Comments
 (0)