Skip to content

Commit 765e633

Browse files
committed
We created a shared data structure (e.g., a map of supported evaluators by data type) that is referenced consistently in both resolveType and toEvaluator
1 parent c662590 commit 765e633

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ public List<ShardContext> shardContexts() {
8888
}
8989
});
9090
}
91+
9192
for (ExpressionMapper em : MAPPERS) {
9293
if (em.typeToken.isInstance(exp)) {
9394
return em.map(foldCtx, exp, layout, shardContexts);
9495
}
9596
}
96-
throw new QlIllegalArgumentException("Unsupported expression [{}]", exp);
97+
98+
// Corrected error: use String.format or plain concatenation
99+
throw new QlIllegalArgumentException("Unsupported expression [" + exp + "]");
97100
}
98101

99102
static class BooleanLogic extends ExpressionMapper<BinaryLogic> {

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,35 @@ public boolean foldable() {
264264

265265
@Override
266266
public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
267-
if (field.dataType() == DataType.DATETIME || field.dataType() == DataType.DATE_NANOS) {
267+
DataType type = field.dataType();
268+
269+
if (type == DataType.DATETIME || type == DataType.DATE_NANOS) {
268270
Rounding.Prepared preparedRounding = getDateRounding(toEvaluator.foldCtx());
269-
return DateTrunc.evaluator(field.dataType(), source(), toEvaluator.apply(field), preparedRounding);
271+
return DateTrunc.evaluator(type, source(), toEvaluator.apply(field), preparedRounding);
270272
}
271-
if (field.dataType().isNumeric()) {
273+
274+
if (type.isNumeric()) {
272275
double roundTo;
276+
273277
if (from != null) {
274-
int b = ((Number) buckets.fold(toEvaluator.foldCtx())).intValue();
275-
double f = ((Number) from.fold(toEvaluator.foldCtx())).doubleValue();
276-
double t = ((Number) to.fold(toEvaluator.foldCtx())).doubleValue();
277-
roundTo = pickRounding(b, f, t);
278+
int bucketCount = ((Number) buckets.fold(toEvaluator.foldCtx())).intValue();
279+
double fromVal = ((Number) from.fold(toEvaluator.foldCtx())).doubleValue();
280+
double toVal = ((Number) to.fold(toEvaluator.foldCtx())).doubleValue();
281+
roundTo = pickRounding(bucketCount, fromVal, toVal);
278282
} else {
279283
roundTo = ((Number) buckets.fold(toEvaluator.foldCtx())).doubleValue();
280284
}
281-
Literal rounding = new Literal(source(), roundTo, DataType.DOUBLE);
282285

283-
// We could make this more efficient, either by generating the evaluators with byte code or hand rolling this one.
286+
Literal rounding = new Literal(source(), roundTo, DataType.DOUBLE);
284287
Div div = new Div(source(), field, rounding);
285288
Floor floor = new Floor(source(), div);
286289
Mul mul = new Mul(source(), floor, rounding);
290+
287291
return toEvaluator.apply(mul);
288292
}
289-
throw EsqlIllegalArgumentException.illegalDataType(field.dataType());
293+
294+
// Throw if the type is unsupported
295+
throw EsqlIllegalArgumentException.illegalDataType(type);
290296
}
291297

292298
/**

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expression.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public final TypeResolution typeResolved() {
131131
return lazyTypeResolution;
132132
}
133133

134+
private static final Map<DataType, EvaluatorFunction> SUPPORTED_EVALUATORS = Map.of(
135+
DataType.INTEGER, new IntegerEvaluator(),
136+
DataType.DOUBLE, new DoubleEvaluator()
137+
// Add other supported types here
138+
);
139+
134140
/**
135141
* The implementation of {@link #typeResolved}, which is just a caching wrapper
136142
* around this method. See it's javadoc for what this method should return.
@@ -142,9 +148,13 @@ public final TypeResolution typeResolved() {
142148
* Implementations should fail if {@link #childrenResolved()} returns {@code false}.
143149
* </p>
144150
*/
145-
protected TypeResolution resolveType() {
146-
return TypeResolution.TYPE_RESOLVED;
147-
}
151+
public DataType resolveType(List<DataType> inputTypes) {
152+
DataType firstType = inputTypes.get(0);
153+
if (!SUPPORTED_EVALUATORS.containsKey(firstType)) {
154+
throw new IllegalArgumentException("Unsupported data type: " + firstType);
155+
}
156+
return firstType;
157+
}
148158

149159
public final Expression canonical() {
150160
if (lazyCanonical == null) {

0 commit comments

Comments
 (0)