Skip to content

Commit f0b6e5a

Browse files
Address code review feedback part 2 (still needs UT with the flag set to false)
1 parent 359a7c6 commit f0b6e5a

File tree

12 files changed

+97
-51
lines changed

12 files changed

+97
-51
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/querydsl/query/WildcardQuery.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ public class WildcardQuery extends Query {
1616

1717
private final String field, query;
1818
private final boolean caseInsensitive;
19+
private final boolean forceStringMatch;
1920

20-
public WildcardQuery(Source source, String field, String query) {
21-
this(source, field, query, false);
22-
}
23-
24-
public WildcardQuery(Source source, String field, String query, boolean caseInsensitive) {
21+
public WildcardQuery(Source source, String field, String query, boolean caseInsensitive, boolean forceStringMatch) {
2522
super(source);
2623
this.field = field;
2724
this.query = query;
2825
this.caseInsensitive = caseInsensitive;
26+
this.forceStringMatch = forceStringMatch;
2927
}
3028

3129
public String field() {
@@ -42,7 +40,7 @@ public Boolean caseInsensitive() {
4240

4341
@Override
4442
protected QueryBuilder asBuilder() {
45-
WildcardQueryBuilder wb = new WildcardQueryBuilder(field, query, true);
43+
WildcardQueryBuilder wb = new WildcardQueryBuilder(field, query, forceStringMatch);
4644
// ES does not allow case_insensitive to be set to "false", it should be either "true" or not specified
4745
return caseInsensitive == false ? wb : wb.caseInsensitive(caseInsensitive);
4846
}

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/ConfigurationTestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public static Configuration randomConfiguration(String query, Map<String, Map<St
7272
profile,
7373
tables,
7474
System.nanoTime(),
75-
false
75+
false,
76+
true // stringLikeOnIndex, default to true for tests
7677
);
7778
}
7879

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/EsqlTestUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ public static Configuration configuration(QueryPragmas pragmas, String query) {
429429
false,
430430
TABLES,
431431
System.nanoTime(),
432-
false
432+
false,
433+
true // stringLikeOnIndex, default to true for tests
433434
);
434435
}
435436

@@ -863,7 +864,7 @@ static Version randomVersion() {
863864
}
864865

865866
public static WildcardLike wildcardLike(Expression left, String exp) {
866-
return new WildcardLike(EMPTY, left, new WildcardPattern(exp));
867+
return new WildcardLike(EMPTY, left, new WildcardPattern(exp), false, TEST_CFG);
867868
}
868869

869870
public static RLike rlike(Expression left, String exp) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHand
151151
// TODO: Get the real FoldContext here
152152
var wildcardQuery = "*" + QueryParser.escape(BytesRefs.toString(suffix.fold(FoldContext.small())));
153153

154-
return new WildcardQuery(source(), fieldName, wildcardQuery);
154+
return new WildcardQuery(source(), fieldName, wildcardQuery, false, false);
155155
}
156156

157157
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHand
148148
// TODO: Get the real FoldContext here
149149
var wildcardQuery = QueryParser.escape(BytesRefs.toString(prefix.fold(FoldContext.small()))) + "*";
150150

151-
return new WildcardQuery(source(), fieldName, wildcardQuery);
151+
return new WildcardQuery(source(), fieldName, wildcardQuery, false, false);
152152
}
153153

154154
@Override

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
2424
import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates;
2525
import org.elasticsearch.xpack.esql.planner.TranslatorHandler;
26+
import org.elasticsearch.xpack.esql.session.Configuration;
2627

2728
import java.io.IOException;
2829

@@ -33,6 +34,7 @@ public class WildcardLike extends RegexMatch<WildcardPattern> {
3334
WildcardLike::new
3435
);
3536
public static final String NAME = "LIKE";
37+
public final Configuration configuration;
3638

3739
@FunctionInfo(returnType = "boolean", description = """
3840
Use `LIKE` to filter data based on string patterns using wildcards. `LIKE`
@@ -60,21 +62,24 @@ also act on a constant (literal) expression. The right-hand side of the operator
6062
public WildcardLike(
6163
Source source,
6264
@Param(name = "str", type = { "keyword", "text" }, description = "A literal expression.") Expression left,
63-
@Param(name = "pattern", type = { "keyword", "text" }, description = "Pattern.") WildcardPattern pattern
65+
@Param(name = "pattern", type = { "keyword", "text" }, description = "Pattern.") WildcardPattern pattern,
66+
@Param(name = "configuration", type = "configuration", description = "configuration") Configuration configuration
6467
) {
65-
this(source, left, pattern, false);
68+
this(source, left, pattern, false, configuration);
6669
}
6770

68-
public WildcardLike(Source source, Expression left, WildcardPattern pattern, boolean caseInsensitive) {
71+
public WildcardLike(Source source, Expression left, WildcardPattern pattern, boolean caseInsensitive, Configuration configuration) {
6972
super(source, left, pattern, caseInsensitive);
73+
this.configuration = configuration;
7074
}
7175

7276
private WildcardLike(StreamInput in) throws IOException {
7377
this(
7478
Source.readFrom((PlanStreamInput) in),
7579
in.readNamedWriteable(Expression.class),
7680
new WildcardPattern(in.readString()),
77-
deserializeCaseInsensitivity(in)
81+
deserializeCaseInsensitivity(in),
82+
Configuration.readConfigurationHelper(in)
7883
);
7984
}
8085

@@ -84,6 +89,7 @@ public void writeTo(StreamOutput out) throws IOException {
8489
out.writeNamedWriteable(field());
8590
out.writeString(pattern().pattern());
8691
serializeCaseInsensitivity(out);
92+
Configuration.writeConfigurationHelper(out, configuration);
8793
}
8894

8995
@Override
@@ -98,12 +104,12 @@ public String getWriteableName() {
98104

99105
@Override
100106
protected NodeInfo<WildcardLike> info() {
101-
return NodeInfo.create(this, WildcardLike::new, field(), pattern(), caseInsensitive());
107+
return NodeInfo.create(this, WildcardLike::new, field(), pattern(), caseInsensitive(), configuration);
102108
}
103109

104110
@Override
105111
protected WildcardLike replaceChild(Expression newLeft) {
106-
return new WildcardLike(source(), newLeft, pattern(), caseInsensitive());
112+
return new WildcardLike(source(), newLeft, pattern(), caseInsensitive(), configuration);
107113
}
108114

109115
@Override
@@ -120,6 +126,12 @@ public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHand
120126

121127
// TODO: see whether escaping is needed
122128
private Query translateField(String targetFieldName) {
123-
return new WildcardQuery(source(), targetFieldName, pattern().asLuceneWildcard(), caseInsensitive());
129+
return new WildcardQuery(
130+
source(),
131+
targetFieldName,
132+
pattern().asLuceneWildcard(),
133+
caseInsensitive(),
134+
configuration.stringLikeOnIndex()
135+
);
124136
}
125137
}

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public WildcardLikeList(StreamInput in) throws IOException {
7272
in.readNamedWriteable(Expression.class),
7373
new WildcardPatternList(in),
7474
deserializeCaseInsensitivity(in),
75-
readConfiguration(in)
75+
Configuration.readConfigurationHelper(in)
7676
);
7777
}
7878

@@ -82,32 +82,7 @@ public void writeTo(StreamOutput out) throws IOException {
8282
out.writeNamedWriteable(field());
8383
pattern().writeTo(out);
8484
serializeCaseInsensitivity(out);
85-
writeConfiguration(out);
86-
87-
}
88-
89-
static Configuration readConfiguration(StreamInput in) throws IOException {
90-
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_FIXED_INDEX_LIKE)) {
91-
boolean hasConfiguration = in.readBoolean();
92-
if (hasConfiguration) {
93-
return Configuration.readFrom(in);
94-
} else {
95-
return null; // For backward compatibility, configuration is not serialized before this version
96-
}
97-
} else {
98-
return null; // For backward compatibility, configuration is not serialized before this version
99-
}
100-
}
101-
102-
void writeConfiguration(StreamOutput out) throws IOException {
103-
if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_FIXED_INDEX_LIKE)) {
104-
boolean hasConfiguration = configuration != null;
105-
out.writeBoolean(hasConfiguration);
106-
if (hasConfiguration) {
107-
configuration.writeTo(out);
108-
}
109-
}
110-
// else don't write configuration for backward compatibility
85+
Configuration.writeConfigurationHelper(out, configuration);
11186
}
11287

11388
@Override
@@ -146,6 +121,11 @@ public Translatable translatable(LucenePushdownPredicates pushdownPredicates) {
146121
*/
147122
@Override
148123
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
124+
if(configuration != null && configuration.stringLikeOnIndex() == false) {
125+
throw new IllegalArgumentException(
126+
"LIKE with LIST cannot be used with string_like_on_index enabled. Use LIKE instead."
127+
);
128+
}
149129
var field = field();
150130
LucenePushdownPredicates.checkIsPushableAttribute(field);
151131
String targetFieldName = handler.nameOf(field instanceof FieldAttribute fa ? fa.exactAttribute() : field);

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/ReplaceStringCasingWithInsensitiveRegexMatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static Expression insensitiveRegexMatch(RegexMatch<? extends StringPatte
4747
wildcardLike.source(),
4848
unwrapCase(wildcardLike.field()),
4949
wildcardLike.pattern(),
50-
true
50+
wildcardLike.configuration
5151
);
5252
default -> regexMatch;
5353
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public Expression visitLikeExpression(EsqlBaseParser.LikeExpressionContext ctx)
762762
Literal patternLiteral = visitString(ctx.string());
763763
try {
764764
WildcardPattern pattern = new WildcardPattern(BytesRefs.toString(patternLiteral.fold(FoldContext.small())));
765-
WildcardLike result = new WildcardLike(source, left, pattern);
765+
WildcardLike result = new WildcardLike(source, left, pattern, configuration);
766766
return ctx.NOT() == null ? result : new Not(source, result);
767767
} catch (InvalidArgumentException e) {
768768
throw new ParsingException(source, "Invalid pattern for LIKE [{}]: [{}]", patternLiteral, e.getMessage());
@@ -779,7 +779,7 @@ public Expression visitLikeListExpression(EsqlBaseParser.LikeListExpressionConte
779779
.toList();
780780
// for now we will use the old WildcardLike function for one argument case to allow compatibility in mixed version deployments
781781
Expression e = wildcardPatterns.size() == 1
782-
? new WildcardLike(source, left, wildcardPatterns.getFirst())
782+
? new WildcardLike(source, left, wildcardPatterns.getFirst(), configuration)
783783
: new WildcardLikeList(source, left, new WildcardPatternList(wildcardPatterns), configuration);
784784
return ctx.NOT() == null ? e : new Not(source, e);
785785
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ public class EsqlPlugin extends Plugin implements ActionPlugin, ExtensiblePlugin
116116
Setting.Property.Dynamic
117117
);
118118

119+
public static final Setting<Boolean> ESQL_STRING_LIKE_ON_INDEX = Setting.boolSetting(
120+
"esql.query.string_like_on_index",
121+
true,
122+
Setting.Property.NodeScope,
123+
Setting.Property.Dynamic
124+
);
125+
119126
public static final Setting<TimeValue> ESQL_QUERYLOG_THRESHOLD_WARN_SETTING = Setting.timeSetting(
120127
"esql.querylog.threshold.warn",
121128
TimeValue.timeValueMillis(-1),
@@ -263,7 +270,8 @@ public List<Setting<?>> getSettings() {
263270
ESQL_QUERYLOG_THRESHOLD_WARN_SETTING,
264271
ESQL_QUERYLOG_INCLUDE_USER_SETTING,
265272
DEFAULT_DATA_PARTITIONING,
266-
STORED_FIELDS_SEQUENTIAL_PROPORTION
273+
STORED_FIELDS_SEQUENTIAL_PROPORTION,
274+
ESQL_STRING_LIKE_ON_INDEX
267275
);
268276
}
269277

0 commit comments

Comments
 (0)