Skip to content

Commit ec3481e

Browse files
authored
SQL: Fix NPE for parameterized LIKE/RLIKE (#53573)
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
1 parent 7e0a3c4 commit ec3481e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ public LikePattern visitPattern(PatternContext ctx) {
264264
}
265265

266266
String pattern = string(ctx.value);
267+
if (pattern == null) {
268+
throw new ParsingException(source(ctx.value), "Pattern must not be [null]");
269+
}
267270
int pos = pattern.indexOf('*');
268271
if (pos >= 0) {
269272
throw new ParsingException(source(ctx.value),

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Add;
2222
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Mul;
2323
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Sub;
24+
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
2425

2526
import java.time.Duration;
2627
import java.time.Period;
2728
import java.time.temporal.TemporalAmount;
29+
import java.util.Collections;
2830
import java.util.Locale;
2931

3032
import static java.lang.String.format;
3133
import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN;
3234
import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE;
3335
import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER;
36+
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
3437
import static org.elasticsearch.xpack.ql.type.DataTypes.LONG;
3538
import static org.hamcrest.Matchers.startsWith;
3639

@@ -542,4 +545,11 @@ public void testCaseWithOperand() {
542545
assertEquals("WHEN 1 THEN 'one'", ifc.sourceText());
543546
assertEquals("many", c.elseResult().toString());
544547
}
548+
549+
public void testLikePatternWithNullParameterNotAllowed() {
550+
ParsingException e = expectThrows(ParsingException.class,
551+
() -> parser.createExpression("a LIKE ?",
552+
Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName(), null))));
553+
assertEquals("line 1:9: Pattern must not be [null]", e.getMessage());
554+
}
545555
}

0 commit comments

Comments
 (0)