Skip to content

Commit

Permalink
Fix: handle NULL input for regex match operations (apache#12028)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuSen8891 authored Aug 17, 2024
1 parent 1f90b00 commit e84f343
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
12 changes: 8 additions & 4 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,14 @@ impl PhysicalExpr for BinaryExpr {
// Attempt to use special kernels if one input is scalar and the other is an array
let scalar_result = match (&lhs, &rhs) {
(ColumnarValue::Array(array), ColumnarValue::Scalar(scalar)) => {
// if left is array and right is literal - use scalar operations
self.evaluate_array_scalar(array, scalar.clone())?.map(|r| {
r.and_then(|a| to_result_type_array(&self.op, a, &result_type))
})
// if left is array and right is literal(not NULL) - use scalar operations
if scalar.is_null() {
None
} else {
self.evaluate_array_scalar(array, scalar.clone())?.map(|r| {
r.and_then(|a| to_result_type_array(&self.op, a, &result_type))
})
}
}
(_, _) => None, // default to array implementation
};
Expand Down
45 changes: 45 additions & 0 deletions datafusion/sqllogictest/test_files/regexp.slt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,51 @@ true
true
true

query B
SELECT str ~ NULL FROM t;
----
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL

query B
select str ~ right('foo', NULL) FROM t;
----
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL

query B
select right('foo', NULL) !~ str FROM t;
----
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL

query B
SELECT regexp_like('foobarbequebaz', '');
----
Expand Down

0 comments on commit e84f343

Please sign in to comment.