Skip to content

Commit 7276cdf

Browse files
authored
ESQL: Rework building evaluators (#113979) (#114139)
This reworks `EvaluatorMapper` to use a named interface instead of a `Function<Expression, EvalOperator.ExpressionEvaluator.Factory>`. It's shorter to type and it allows us to add extra information to the builder.
1 parent 2ef4f58 commit 7276cdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+90
-203
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/mapper/EvaluatorMapper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
import org.elasticsearch.xpack.esql.core.expression.Expression;
1515
import org.elasticsearch.xpack.esql.planner.Layout;
1616

17-
import java.util.function.Function;
18-
1917
import static org.elasticsearch.compute.data.BlockUtils.fromArrayRow;
2018
import static org.elasticsearch.compute.data.BlockUtils.toJavaObject;
2119

2220
/**
2321
* Expressions that have a mapping to an {@link ExpressionEvaluator}.
2422
*/
2523
public interface EvaluatorMapper {
24+
interface ToEvaluator {
25+
ExpressionEvaluator.Factory apply(Expression expression);
26+
}
27+
2628
/**
2729
* <p>
2830
* Note for implementors:
@@ -50,7 +52,7 @@ public interface EvaluatorMapper {
5052
* garbage. Or return an evaluator that throws when run.
5153
* </p>
5254
*/
53-
ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator);
55+
ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator);
5456

5557
/**
5658
* Fold using {@link #toEvaluator} so you don't need a "by hand"

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/Bucket.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.time.ZoneOffset;
4040
import java.util.ArrayList;
4141
import java.util.List;
42-
import java.util.function.Function;
4342

4443
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
4544
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
@@ -241,7 +240,7 @@ public boolean foldable() {
241240
}
242241

243242
@Override
244-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
243+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
245244
if (field.dataType() == DataType.DATETIME) {
246245
Rounding.Prepared preparedRounding;
247246
if (buckets.dataType().isWholeNumber()) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/Categorize.java

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

3737
import java.io.IOException;
3838
import java.util.List;
39-
import java.util.function.Function;
4039

4140
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
4241
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
@@ -107,7 +106,7 @@ static int process(
107106
}
108107

109108
@Override
110-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
109+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
111110
return new CategorizeEvaluator.Factory(
112111
source(),
113112
toEvaluator.apply(field),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class Case extends EsqlScalarFunction {
5050
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Case", Case::new);
5151

5252
record Condition(Expression condition, Expression value) {
53-
ConditionEvaluatorSupplier toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
53+
ConditionEvaluatorSupplier toEvaluator(ToEvaluator toEvaluator) {
5454
return new ConditionEvaluatorSupplier(condition.source(), toEvaluator.apply(condition), toEvaluator.apply(value));
5555
}
5656
}
@@ -311,7 +311,7 @@ private Expression finishPartialFold(List<Expression> newChildren) {
311311
}
312312

313313
@Override
314-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
314+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
315315
List<ConditionEvaluatorSupplier> conditionsFactories = conditions.stream().map(c -> c.toEvaluator(toEvaluator)).toList();
316316
ExpressionEvaluator.Factory elseValueFactory = toEvaluator.apply(elseValue);
317317
ElementType resultType = PlannerUtils.toElementType(dataType());

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

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

3131
import java.io.IOException;
3232
import java.util.List;
33-
import java.util.function.Function;
3433
import java.util.stream.Stream;
3534

3635
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
@@ -138,7 +137,7 @@ public boolean foldable() {
138137
}
139138

140139
@Override
141-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
140+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
142141
// force datatype initialization
143142
var dataType = dataType();
144143
ExpressionEvaluator.Factory[] factories = children().stream()

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

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

3131
import java.io.IOException;
3232
import java.util.List;
33-
import java.util.function.Function;
3433
import java.util.stream.Stream;
3534

3635
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
@@ -136,7 +135,7 @@ public boolean foldable() {
136135
}
137136

138137
@Override
139-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
138+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
140139
// force datatype initialization
141140
var dataType = dataType();
142141

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.Locale;
3535
import java.util.Map;
3636
import java.util.Set;
37-
import java.util.function.Function;
3837

3938
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isTypeOrUnionType;
4039

@@ -124,7 +123,7 @@ interface BuildFactory {
124123
protected abstract Map<DataType, BuildFactory> factories();
125124

126125
@Override
127-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
126+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
128127
return evaluator(toEvaluator.apply(field()));
129128
}
130129

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.IOException;
3030
import java.util.Base64;
3131
import java.util.List;
32-
import java.util.function.Function;
3332

3433
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
3534
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
@@ -96,9 +95,7 @@ static BytesRef process(BytesRef field, @Fixed(includeInToString = false, build
9695
}
9796

9897
@Override
99-
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(
100-
Function<Expression, EvalOperator.ExpressionEvaluator.Factory> toEvaluator
101-
) {
98+
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
10299
return switch (PlannerUtils.toElementType(field.dataType())) {
103100
case BYTES_REF -> new FromBase64Evaluator.Factory(source(), toEvaluator.apply(field), context -> new BytesRefBuilder());
104101
case NULL -> EvalOperator.CONSTANT_NULL_FACTORY;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.IOException;
3030
import java.util.Base64;
3131
import java.util.List;
32-
import java.util.function.Function;
3332

3433
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
3534
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
@@ -90,9 +89,7 @@ static BytesRef process(BytesRef field, @Fixed(includeInToString = false, build
9089
}
9190

9291
@Override
93-
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(
94-
Function<Expression, EvalOperator.ExpressionEvaluator.Factory> toEvaluator
95-
) {
92+
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
9693
return switch (PlannerUtils.toElementType(field.dataType())) {
9794
case BYTES_REF -> new ToBase64Evaluator.Factory(source(), toEvaluator.apply(field), context -> new BytesRefBuilder());
9895
case NULL -> EvalOperator.CONSTANT_NULL_FACTORY;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Map;
3838
import java.util.Set;
3939
import java.util.function.BiFunction;
40-
import java.util.function.Function;
4140

4241
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
4342
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
@@ -227,7 +226,7 @@ static int process(BytesRef unit, long startTimestamp, long endTimestamp) throws
227226
}
228227

229228
@Override
230-
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
229+
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
231230
ExpressionEvaluator.Factory startTimestampEvaluator = toEvaluator.apply(startTimestamp);
232231
ExpressionEvaluator.Factory endTimestampEvaluator = toEvaluator.apply(endTimestamp);
233232

0 commit comments

Comments
 (0)