Skip to content

Commit 873f01e

Browse files
ES|QL: Fix RLIKE folding with (unsupported) case insensitive pattern (#118454) (#118769) (#119292)
Co-authored-by: Luigi Dell'Aquila <[email protected]>
1 parent a528cc9 commit 873f01e

File tree

12 files changed

+66
-42
lines changed

12 files changed

+66
-42
lines changed

docs/changelog/118454.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 118454
2+
summary: Fix RLIKE folding with (unsupported) case insensitive pattern
3+
area: ES|QL
4+
type: bug
5+
issues: []

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/regex/RLike.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88

99
import org.elasticsearch.common.io.stream.StreamOutput;
1010
import org.elasticsearch.xpack.esql.core.expression.Expression;
11-
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1211
import org.elasticsearch.xpack.esql.core.tree.Source;
1312

1413
import java.io.IOException;
1514

16-
public class RLike extends RegexMatch<RLikePattern> {
15+
public abstract class RLike extends RegexMatch<RLikePattern> {
1716

1817
public RLike(Source source, Expression value, RLikePattern pattern) {
1918
super(source, value, pattern, false);
@@ -33,13 +32,4 @@ public String getWriteableName() {
3332
throw new UnsupportedOperationException();
3433
}
3534

36-
@Override
37-
protected NodeInfo<RLike> info() {
38-
return NodeInfo.create(this, RLike::new, field(), pattern(), caseInsensitive());
39-
}
40-
41-
@Override
42-
protected RLike replaceChild(Expression newChild) {
43-
return new RLike(source(), newChild, pattern(), caseInsensitive());
44-
}
4535
}

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/regex/RegexMatch.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
package org.elasticsearch.xpack.esql.core.expression.predicate.regex;
99

10-
import org.apache.lucene.util.BytesRef;
1110
import org.elasticsearch.xpack.esql.core.expression.Expression;
1211
import org.elasticsearch.xpack.esql.core.expression.Nullability;
1312
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
@@ -64,11 +63,7 @@ public boolean foldable() {
6463

6564
@Override
6665
public Boolean fold() {
67-
Object val = field().fold();
68-
if (val instanceof BytesRef br) {
69-
val = br.utf8ToString();
70-
}
71-
return RegexOperation.match(val, pattern().asJavaRegex());
66+
throw new UnsupportedOperationException();
7267
}
7368

7469
@Override

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/regex/WildcardLike.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88

99
import org.elasticsearch.common.io.stream.StreamOutput;
1010
import org.elasticsearch.xpack.esql.core.expression.Expression;
11-
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1211
import org.elasticsearch.xpack.esql.core.tree.Source;
1312

1413
import java.io.IOException;
1514

16-
public class WildcardLike extends RegexMatch<WildcardPattern> {
15+
public abstract class WildcardLike extends RegexMatch<WildcardPattern> {
1716

1817
public WildcardLike(Source source, Expression left, WildcardPattern pattern) {
1918
this(source, left, pattern, false);
@@ -33,14 +32,4 @@ public String getWriteableName() {
3332
throw new UnsupportedOperationException();
3433
}
3534

36-
@Override
37-
protected NodeInfo<WildcardLike> info() {
38-
return NodeInfo.create(this, WildcardLike::new, field(), pattern(), caseInsensitive());
39-
}
40-
41-
@Override
42-
protected WildcardLike replaceChild(Expression newLeft) {
43-
return new WildcardLike(source(), newLeft, pattern(), caseInsensitive());
44-
}
45-
4635
}

x-pack/plugin/esql-core/src/test/java/org/elasticsearch/xpack/esql/core/util/TestUtils.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
1212
import org.elasticsearch.xpack.esql.core.expression.Literal;
1313
import org.elasticsearch.xpack.esql.core.expression.predicate.Range;
14-
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLike;
15-
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLikePattern;
1614
import org.elasticsearch.xpack.esql.core.tree.Source;
1715
import org.elasticsearch.xpack.esql.core.type.DataType;
1816
import org.elasticsearch.xpack.esql.core.type.EsField;
@@ -46,10 +44,6 @@ public static Range rangeOf(Expression value, Expression lower, boolean includeL
4644
return new Range(EMPTY, value, lower, includeLower, upper, includeUpper, randomZone());
4745
}
4846

49-
public static RLike rlike(Expression left, String exp) {
50-
return new RLike(EMPTY, left, new RLikePattern(exp));
51-
}
52-
5347
public static FieldAttribute fieldAttribute() {
5448
return fieldAttribute(randomAlphaOfLength(10), randomFrom(DataType.types()));
5549
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/eval.csv-spec

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,39 @@ Mokhtar |Bernatsky |38992 |BM
601601
Parto |Bamford |61805 |BP
602602
Premal |Baek |52833 |BP
603603
;
604+
605+
606+
caseInsensitiveRegex
607+
from employees | where first_name RLIKE "(?i)geor.*" | keep first_name
608+
;
609+
610+
first_name:keyword
611+
;
612+
613+
614+
caseInsensitiveRegex2
615+
from employees | where first_name RLIKE "(?i)Geor.*" | keep first_name
616+
;
617+
618+
first_name:keyword
619+
;
620+
621+
622+
caseInsensitiveRegexFold
623+
required_capability: fixed_regex_fold
624+
row foo = "Bar" | where foo rlike "(?i)ba.*"
625+
;
626+
627+
foo:keyword
628+
;
629+
630+
631+
caseInsensitiveRegexFold2
632+
required_capability: fixed_regex_fold
633+
row foo = "Bar" | where foo rlike "(?i)Ba.*"
634+
;
635+
636+
foo:keyword
637+
;
638+
639+

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,12 @@ public enum Cap {
514514
/**
515515
* Fix for https://github.com/elastic/elasticsearch/issues/114714, again
516516
*/
517-
FIX_STATS_BY_FOLDABLE_EXPRESSION_2,;
517+
FIX_STATS_BY_FOLDABLE_EXPRESSION_2,
518+
519+
/**
520+
* Fix for regex folding with case-insensitive pattern https://github.com/elastic/elasticsearch/issues/118371
521+
*/
522+
FIXED_REGEX_FOLD;
518523

519524
private final boolean enabled;
520525

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/RLike.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String getWriteableName() {
7979
}
8080

8181
@Override
82-
protected NodeInfo<org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLike> info() {
82+
protected NodeInfo<RLike> info() {
8383
return NodeInfo.create(this, RLike::new, field(), pattern(), caseInsensitive());
8484
}
8585

@@ -93,6 +93,11 @@ protected TypeResolution resolveType() {
9393
return isString(field(), sourceText(), DEFAULT);
9494
}
9595

96+
@Override
97+
public Boolean fold() {
98+
return (Boolean) EvaluatorMapper.super.fold();
99+
}
100+
96101
@Override
97102
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
98103
return AutomataMatch.toEvaluator(source(), toEvaluator.apply(field()), pattern().createAutomaton());

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/WildcardLike.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ protected TypeResolution resolveType() {
9999
return isString(field(), sourceText(), DEFAULT);
100100
}
101101

102+
@Override
103+
public Boolean fold() {
104+
return (Boolean) EvaluatorMapper.super.fold();
105+
}
106+
102107
@Override
103108
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
104109
return AutomataMatch.toEvaluator(

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/ConstantFoldingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.And;
1818
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Not;
1919
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Or;
20-
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLike;
2120
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLikePattern;
22-
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardLike;
2321
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
2422
import org.elasticsearch.xpack.esql.core.type.DataType;
23+
import org.elasticsearch.xpack.esql.expression.function.scalar.string.RLike;
24+
import org.elasticsearch.xpack.esql.expression.function.scalar.string.WildcardLike;
2525
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Add;
2626
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Div;
2727
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Mod;

0 commit comments

Comments
 (0)