Skip to content

Commit c301a3b

Browse files
Implement review suggestions
1 parent 094c98f commit c301a3b

File tree

38 files changed

+103
-179
lines changed

38 files changed

+103
-179
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Literal.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ public Literal(Source source, Object value, DataType dataType) {
5858

5959
private boolean noPlainStrings(Object value, DataType dataType) {
6060
if (dataType == KEYWORD || dataType == TEXT || dataType == VERSION) {
61-
if (value instanceof String) {
62-
return false;
63-
}
64-
if (value instanceof Collection<?> c) {
65-
return c.stream().allMatch(x -> noPlainStrings(x, dataType));
66-
}
61+
return switch (value) {
62+
case String s -> false;
63+
case Collection<?> c -> c.stream().allMatch(x -> noPlainStrings(x, dataType));
64+
default -> true;
65+
};
6766
}
6867
return true;
6968
}
@@ -146,6 +145,7 @@ public String toString() {
146145
str = BytesRefs.toString(value);
147146
} else if (dataType == VERSION && value instanceof BytesRef br) {
148147
str = new Version(br).toString();
148+
// TODO review how we manage IPs: https://github.com/elastic/elasticsearch/issues/129605
149149
// } else if (dataType == IP && value instanceof BytesRef ip) {
150150
// str = DocValueFormat.IP.format(ip);
151151
} else {
@@ -186,6 +186,10 @@ public static Literal of(Expression source, Object value) {
186186
return new Literal(source.source(), value, source.dataType());
187187
}
188188

189+
public static Literal keyword(Source source, String literal) {
190+
return new Literal(source, BytesRefs.toBytesRef(literal), KEYWORD);
191+
}
192+
189193
/**
190194
* Not all literal values are currently supported in StreamInput/StreamOutput as generic values.
191195
* This mapper allows for addition of new and interesting values without (yet) adding to StreamInput/Output.

x-pack/plugin/esql-core/src/test/java/org/elasticsearch/xpack/esql/core/expression/function/scalar/FunctionTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static Literal l(Object value) {
2020
}
2121

2222
public static Literal l(Object value, DataType type) {
23-
if (value instanceof String && (type == DataType.TEXT || type == DataType.KEYWORD)) {
23+
if ((type == DataType.TEXT || type == DataType.KEYWORD) && value instanceof String) {
2424
value = BytesRefs.toBytesRef(value);
2525
}
2626
return new Literal(EMPTY, value, type);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Literal of(Source source, Object value) {
4343
return (Literal) value;
4444
}
4545
DataType type = DataType.fromJava(value);
46-
if (value instanceof String && (type == TEXT || type == KEYWORD)) {
46+
if ((type == TEXT || type == KEYWORD) && value instanceof String) {
4747
value = BytesRefs.toBytesRef(value);
4848
}
4949
return new Literal(source, value, DataType.fromJava(value));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ eth0 |gamma |fe80::cae2:65ff:fece:feb9
398398
lo0 |gamma |fe80::cae2:65ff:fece:feb9 |fe81::cae2:65ff:fece:feb9
399399
;
400400

401-
cicdirMatchEqualsInsOrsIPs
401+
cidrMatchEqualsInsOrsIPs
402402
required_capability: combine_disjunctive_cidrmatches
403403

404404
FROM hosts

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
package org.elasticsearch.xpack.esql.analysis;
99

10-
import org.apache.lucene.util.BytesRef;
1110
import org.elasticsearch.common.logging.HeaderWarning;
1211
import org.elasticsearch.common.logging.LoggerMessageFormat;
1312
import org.elasticsearch.common.lucene.BytesRefs;
@@ -320,7 +319,7 @@ protected LogicalPlan rule(Enrich plan, AnalyzerContext context) {
320319
// the policy does not exist
321320
return plan;
322321
}
323-
final String policyName = ((BytesRef) plan.policyName().fold(FoldContext.small() /* TODO remove me */)).utf8ToString();
322+
final String policyName = BytesRefs.toString(plan.policyName().fold(FoldContext.small() /* TODO remove me */));
324323
final var resolved = context.enrichResolution().getResolvedPolicy(policyName, plan.mode());
325324
if (resolved != null) {
326325
var policy = new EnrichPolicy(resolved.matchType(), null, List.of(), resolved.matchField(), resolved.enrichFields());
@@ -1579,7 +1578,7 @@ private static UnresolvedAttribute unresolvedAttribute(Expression value, String
15791578
private static Expression castStringLiteralToTemporalAmount(Expression from) {
15801579
try {
15811580
TemporalAmount result = maybeParseTemporalAmount(
1582-
((BytesRef) from.fold(FoldContext.small() /* TODO remove me */)).utf8ToString().strip()
1581+
BytesRefs.toString(from.fold(FoldContext.small() /* TODO remove me */)).strip()
15831582
);
15841583
if (result == null) {
15851584
return from;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/Count.java

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

1010
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1111
import org.elasticsearch.common.io.stream.StreamInput;
12-
import org.elasticsearch.common.lucene.BytesRefs;
1312
import org.elasticsearch.compute.aggregation.AggregatorFunctionSupplier;
1413
import org.elasticsearch.compute.aggregation.CountAggregatorFunction;
1514
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
@@ -164,7 +163,7 @@ public Expression surrogate() {
164163
return new Mul(
165164
s,
166165
new Coalesce(s, new MvCount(s, field), List.of(new Literal(s, 0, DataType.INTEGER))),
167-
new Count(s, new Literal(s, BytesRefs.toBytesRef(StringUtils.WILDCARD), DataType.KEYWORD))
166+
new Count(s, Literal.keyword(s, StringUtils.WILDCARD))
168167
);
169168
}
170169

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/Sum.java

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

99
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1010
import org.elasticsearch.common.io.stream.StreamInput;
11-
import org.elasticsearch.common.lucene.BytesRefs;
1211
import org.elasticsearch.compute.aggregation.AggregatorFunctionSupplier;
1312
import org.elasticsearch.compute.aggregation.SumDoubleAggregatorFunctionSupplier;
1413
import org.elasticsearch.compute.aggregation.SumIntAggregatorFunctionSupplier;
@@ -144,8 +143,6 @@ public Expression surrogate() {
144143
}
145144

146145
// SUM(const) is equivalent to MV_SUM(const)*COUNT(*).
147-
return field.foldable()
148-
? new Mul(s, new MvSum(s, field), new Count(s, new Literal(s, BytesRefs.toBytesRef(StringUtils.WILDCARD), DataType.KEYWORD)))
149-
: null;
146+
return field.foldable() ? new Mul(s, new MvSum(s, field), new Count(s, Literal.keyword(s, StringUtils.WILDCARD))) : null;
150147
}
151148
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
14+
import org.elasticsearch.common.lucene.BytesRefs;
1415
import org.elasticsearch.common.time.DateUtils;
1516
import org.elasticsearch.compute.ann.Evaluator;
1617
import org.elasticsearch.compute.ann.Fixed;
@@ -317,7 +318,7 @@ private ExpressionEvaluator.Factory toEvaluator(
317318

318319
if (unit.foldable()) {
319320
try {
320-
Part datePartField = Part.resolve(((BytesRef) unit.fold(toEvaluator.foldCtx())).utf8ToString());
321+
Part datePartField = Part.resolve(BytesRefs.toString(unit.fold(toEvaluator.foldCtx())));
321322
return constantFactory.build(source(), datePartField, startTimestampEvaluator, endTimestampEvaluator);
322323
} catch (IllegalArgumentException e) {
323324
throw new InvalidArgumentException("invalid unit format for [{}]: {}", sourceText(), e.getMessage());

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public class MvSort extends EsqlScalarFunction implements OptionalArgument, Post
6565

6666
private final Expression field, order;
6767

68-
private static final Literal ASC = new Literal(Source.EMPTY, BytesRefs.toBytesRef("ASC"), DataType.KEYWORD);
69-
private static final Literal DESC = new Literal(Source.EMPTY, BytesRefs.toBytesRef("DESC"), DataType.KEYWORD);
68+
private static final Literal ASC = Literal.keyword(Source.EMPTY, "ASC");
69+
private static final Literal DESC = Literal.keyword(Source.EMPTY, "DESC");
7070

7171
private static final String INVALID_ORDER_ERROR = "Invalid order value in [{}], expected one of [{}, {}] but got [{}]";
7272

@@ -157,13 +157,12 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
157157
sourceText(),
158158
BytesRefs.toString(ASC.value()),
159159
BytesRefs.toString(DESC.value()),
160-
((BytesRef) order.fold(toEvaluator.foldCtx())).utf8ToString()
160+
BytesRefs.toString(order.fold(toEvaluator.foldCtx()))
161161
)
162162
);
163163
}
164164
if (order != null && order.foldable()) {
165-
ordering = ((BytesRef) order.fold(toEvaluator.foldCtx())).utf8ToString()
166-
.equalsIgnoreCase(((BytesRef) ASC.value()).utf8ToString());
165+
ordering = BytesRefs.toString(order.fold(toEvaluator.foldCtx())).equalsIgnoreCase(BytesRefs.toString(ASC.value()));
167166
}
168167

169168
return switch (PlannerUtils.toElementType(field.dataType())) {
@@ -245,9 +244,9 @@ public void postOptimizationVerification(Failures failures) {
245244
order,
246245
INVALID_ORDER_ERROR,
247246
sourceText(),
248-
((BytesRef) ASC.value()).utf8ToString(),
249-
((BytesRef) DESC.value()).utf8ToString(),
250-
((BytesRef) order.fold(FoldContext.small() /* TODO remove me */)).utf8ToString()
247+
BytesRefs.toString(ASC.value()),
248+
BytesRefs.toString(DESC.value()),
249+
BytesRefs.toString(order.fold(FoldContext.small() /* TODO remove me */))
251250
)
252251
);
253252
}
@@ -264,8 +263,8 @@ private boolean isValidOrder() {
264263
o = os;
265264
}
266265
if (o == null
267-
|| o.equalsIgnoreCase(((BytesRef) ASC.value()).utf8ToString()) == false
268-
&& o.equalsIgnoreCase(((BytesRef) DESC.value()).utf8ToString()) == false) {
266+
|| o.equalsIgnoreCase(BytesRefs.toString(ASC.value())) == false
267+
&& o.equalsIgnoreCase(BytesRefs.toString(DESC.value())) == false) {
269268
isValidOrder = false;
270269
}
271270
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
15+
import org.elasticsearch.common.lucene.BytesRefs;
1516
import org.elasticsearch.compute.ann.Evaluator;
1617
import org.elasticsearch.compute.ann.Fixed;
1718
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
@@ -199,7 +200,7 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
199200
if (regex.foldable() && regex.dataType() == DataType.KEYWORD) {
200201
Pattern regexPattern;
201202
try {
202-
regexPattern = Pattern.compile(((BytesRef) regex.fold(toEvaluator.foldCtx())).utf8ToString());
203+
regexPattern = Pattern.compile(BytesRefs.toString(regex.fold(toEvaluator.foldCtx())));
203204
} catch (PatternSyntaxException pse) {
204205
// TODO this is not right (inconsistent). See also https://github.com/elastic/elasticsearch/issues/100038
205206
// this should generate a header warning and return null (as do the rest of this functionality in evaluators),

0 commit comments

Comments
 (0)