Skip to content

Commit e31f17a

Browse files
committed
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 (cherry picked from commit ec3481e)
1 parent 972e248 commit e31f17a

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
@@ -263,6 +263,9 @@ public LikePattern visitPattern(PatternContext ctx) {
263263
}
264264

265265
String pattern = string(ctx.value);
266+
if (pattern == null) {
267+
throw new ParsingException(source(ctx.value), "Pattern must not be [null]");
268+
}
266269
int pos = pattern.indexOf('*');
267270
if (pos >= 0) {
268271
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,14 +21,17 @@
2121
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.Equals;
2222
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.NotEquals;
2323
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.NullEquals;
24+
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
2425
import org.elasticsearch.xpack.sql.type.DataType;
2526

2627
import java.time.Duration;
2728
import java.time.Period;
2829
import java.time.temporal.TemporalAmount;
30+
import java.util.Collections;
2931
import java.util.Locale;
3032

3133
import static java.lang.String.format;
34+
import static org.elasticsearch.xpack.sql.type.DataType.KEYWORD;
3235
import static org.hamcrest.Matchers.startsWith;
3336

3437
public class ExpressionTests extends ESTestCase {
@@ -539,4 +542,11 @@ public void testCaseWithOperand() {
539542
assertEquals("WHEN 1 THEN 'one'", ifc.sourceText());
540543
assertEquals("many", c.elseResult().toString());
541544
}
545+
546+
public void testLikePatternWithNullParameterNotAllowed() {
547+
ParsingException e = expectThrows(ParsingException.class,
548+
() -> parser.createExpression("a LIKE ?",
549+
Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName, null))));
550+
assertEquals("line 1:9: Pattern must not be [null]", e.getMessage());
551+
}
542552
}

0 commit comments

Comments
 (0)