Skip to content

Commit

Permalink
tree: fix LIKE suboperator with NULL LHS
Browse files Browse the repository at this point in the history
If a NULL value was present in the LHS of a suboperator with a LIKE
comparison, then previously the server would crash with a panic. Now it
is handled and there are tests.

closes #40841

Release justification: Low impact bug fix that prevents a server crash.

Release note: None
  • Loading branch information
rafiss committed Sep 24, 2019
1 parent c2437cc commit 477d887
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2453,6 +2453,9 @@ func ConvertLikeToRegexp(
}

func matchLike(ctx *EvalContext, left, right Datum, caseInsensitive bool) (Datum, error) {
if left == DNull || right == DNull {
return DNull, nil
}
s, pattern := string(MustBeDString(left)), string(MustBeDString(right))
if len(s) == 0 {
// An empty string only matches with an empty pattern or a pattern
Expand Down
71 changes: 71 additions & 0 deletions pkg/sql/sem/tree/testdata/eval/any_some_all
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ eval
----
false

eval
'foo' LIKE ANY ARRAY[]
----
false

eval
'foo' LIKE ANY (ARRAY['bar', 'baz'])
----
false

eval
'foo' LIKE ANY (ARRAY['foo', 'bar', 'baz'])
----
true

eval
1+1 = SOME (ARRAY[1, 3, 4])
----
Expand All @@ -50,6 +65,21 @@ eval
----
true

eval
'foo' LIKE ALL ARRAY[]
----
true

eval
'foo' LIKE ALL (ARRAY['foo', 'bar', 'baz'])
----
false

eval
'foo' LIKE ALL (ARRAY['foo', 'foo'])
----
true

eval
1 = ANY (ARRAY[1, 2, NULL])
----
Expand Down Expand Up @@ -87,6 +117,11 @@ eval
----
NULL

eval
'foo' LIKE ANY(NULL::string[])
----
NULL

eval
NULL::int = ANY(NULL::int[])
----
Expand All @@ -102,6 +137,11 @@ NULL::int = ALL(NULL::int[])
----
NULL

eval
NULL::string LIKE ANY(NULL::string[])
----
NULL

# A null LHS has different behavior if the array is empty or not
eval
NULL::int = ANY(ARRAY[1,2])
Expand Down Expand Up @@ -133,6 +173,21 @@ NULL::int = ALL(ARRAY[]::int[])
----
true

eval
NULL::string LIKE ANY(ARRAY[]::string[])
----
false

eval
NULL::string LIKE SOME(ARRAY[]::string[])
----
false

eval
NULL::string LIKE ALL(ARRAY[]::string[])
----
true

eval
1 = ALL (ARRAY[1, 2, NULL])
----
Expand Down Expand Up @@ -232,3 +287,19 @@ eval
'aaa' NOT ILIKE ANY (ARRAY['%A%', '%A%'])
----
false

# Regression test for #40841 -- make sure LIKE can handle nulls.
eval
NULL::string LIKE ANY(ARRAY['bar', 'baz'])
----
NULL

eval
NULL::string ILIKE ANY(ARRAY['bar%', 'baz'])
----
NULL

eval
NULL::string LIKE ANY(ARRAY['bar', NULL])
----
NULL

0 comments on commit 477d887

Please sign in to comment.