Skip to content

Commit

Permalink
SQL: Fix NPE for parameterized LIKE/RLIKE (#53573)
Browse files Browse the repository at this point in the history
Fix NPE when `null` is passed as a parameter for a parameterized
pattern of LIKE/RLIKE. e.g.: `field LIKE ?` params=[null]`
Check for null pattern in LIKE/RLIKE as for RLIKE (RegexpQuery) we
get an IllegalArgumentExpression from Lucence but for LIKE
(WildcardQuery) we get an NPE.

Fixes: #53557
(cherry picked from commit ec3481e)
  • Loading branch information
matriv committed Mar 16, 2020
1 parent 972e248 commit e31f17a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ public LikePattern visitPattern(PatternContext ctx) {
}

String pattern = string(ctx.value);
if (pattern == null) {
throw new ParsingException(source(ctx.value), "Pattern must not be [null]");
}
int pos = pattern.indexOf('*');
if (pos >= 0) {
throw new ParsingException(source(ctx.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.NotEquals;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.NullEquals;
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
import org.elasticsearch.xpack.sql.type.DataType;

import java.time.Duration;
import java.time.Period;
import java.time.temporal.TemporalAmount;
import java.util.Collections;
import java.util.Locale;

import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.type.DataType.KEYWORD;
import static org.hamcrest.Matchers.startsWith;

public class ExpressionTests extends ESTestCase {
Expand Down Expand Up @@ -539,4 +542,11 @@ public void testCaseWithOperand() {
assertEquals("WHEN 1 THEN 'one'", ifc.sourceText());
assertEquals("many", c.elseResult().toString());
}

public void testLikePatternWithNullParameterNotAllowed() {
ParsingException e = expectThrows(ParsingException.class,
() -> parser.createExpression("a LIKE ?",
Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName, null))));
assertEquals("line 1:9: Pattern must not be [null]", e.getMessage());
}
}

0 comments on commit e31f17a

Please sign in to comment.