Skip to content

Commit b0a4348

Browse files
committed
comments
1 parent 32c6735 commit b0a4348

File tree

5 files changed

+21
-100
lines changed

5 files changed

+21
-100
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class Clamp extends EsqlScalarFunction implements SurrogateExpression {
3838
private final Expression field;
3939
private final Expression min;
4040
private final Expression max;
41-
private DataType dataType;
4241

4342
@FunctionInfo(
4443
returnType = { "double", "integer", "long", "double", "unsigned_long", "keyword", "ip", "boolean", "date", "version" },
@@ -99,36 +98,29 @@ protected TypeResolution resolveType() {
9998
fieldDataType.typeName()
10099
);
101100
if (resolution.unresolved()) {
102-
dataType = NULL;
103101
return resolution;
104102
}
105103
if (fieldDataType == NULL) {
106-
dataType = NULL;
107104
return new TypeResolution("'field' must not be null in clamp()");
108105
}
109106
for (Expression child : List.of(children().get(1), children().get(2))) {
110107
var childRes = TypeResolutions.isType(
111108
child,
112109
t -> t.isNumeric() ? fieldDataType.isNumeric() : t.noText() == fieldDataType,
113110
sourceText(),
114-
TypeResolutions.ParamOrdinal.SECOND,
111+
child == children().get(1) ? TypeResolutions.ParamOrdinal.SECOND : TypeResolutions.ParamOrdinal.THIRD,
115112
fieldDataType.typeName()
116113
);
117114
if (childRes.unresolved()) {
118-
dataType = NULL;
119115
return childRes;
120116
}
121117
}
122-
dataType = fieldDataType;
123118
return TypeResolution.TYPE_RESOLVED;
124119
}
125120

126121
@Override
127122
public DataType dataType() {
128-
if (dataType == null) {
129-
resolveType();
130-
}
131-
return dataType;
123+
return field.dataType();
132124
}
133125

134126
@Override

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/adsads

Lines changed: 0 additions & 36 deletions
This file was deleted.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/asdasd

Lines changed: 0 additions & 2 deletions
This file was deleted.

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

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2727
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast;
2828
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
29+
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
2930

3031
import java.io.IOException;
3132
import java.util.List;
@@ -38,8 +39,6 @@
3839
public class ClampMax extends EsqlScalarFunction {
3940
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "ClampMax", ClampMax::new);
4041

41-
private DataType dataType;
42-
4342
@FunctionInfo(
4443
returnType = { "double", "integer", "long", "double", "unsigned_long", "keyword", "ip", "boolean", "date", "version" },
4544
description = "Returns clamps the values of all input samples clamped to have an upper limit of max.",
@@ -79,10 +78,7 @@ public String getWriteableName() {
7978

8079
@Override
8180
public DataType dataType() {
82-
if (dataType == null) {
83-
resolveType();
84-
}
85-
return dataType;
81+
return children().getFirst().dataType();
8682
}
8783

8884
@Override
@@ -102,11 +98,9 @@ protected TypeResolution resolveType() {
10298
fieldDataType.typeName()
10399
);
104100
if (resolution.unresolved()) {
105-
dataType = NULL;
106101
return resolution;
107102
}
108103
if (fieldDataType == NULL) {
109-
dataType = NULL;
110104
return new TypeResolution("'field' must not be null in clamp()");
111105
}
112106
resolution = TypeResolutions.isType(
@@ -117,10 +111,8 @@ protected TypeResolution resolveType() {
117111
fieldDataType.typeName()
118112
);
119113
if (resolution.unresolved()) {
120-
dataType = NULL;
121114
return resolution;
122115
}
123-
dataType = fieldDataType;
124116
return TypeResolution.TYPE_RESOLVED;
125117
}
126118

@@ -149,26 +141,14 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
149141
? Cast.cast(source(), max.dataType(), outputType, toEvaluator.apply(max))
150142
: toEvaluator.apply(max);
151143

152-
if (dataType == DataType.BOOLEAN) {
153-
return new ClampMaxBooleanEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
154-
}
155-
if (dataType == DataType.DOUBLE) {
156-
return new ClampMaxDoubleEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
157-
}
158-
if (dataType == DataType.INTEGER) {
159-
return new ClampMaxIntegerEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
160-
}
161-
if (dataType == DataType.UNSIGNED_LONG
162-
|| dataType == DataType.LONG
163-
|| dataType == DataType.DATETIME
164-
|| dataType == DataType.DATE_NANOS) {
165-
return new ClampMaxLongEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
166-
}
167-
if (DataType.isString(dataType) || dataType == DataType.IP || dataType == DataType.VERSION || dataType == DataType.UNSUPPORTED) {
168-
169-
return new ClampMaxBytesRefEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
170-
}
171-
throw EsqlIllegalArgumentException.illegalDataType(dataType);
144+
return switch (PlannerUtils.toElementType(outputType)) {
145+
case BOOLEAN -> new ClampMaxBooleanEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
146+
case DOUBLE -> new ClampMaxDoubleEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
147+
case INT -> new ClampMaxIntegerEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
148+
case LONG -> new ClampMaxLongEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
149+
case BYTES_REF -> new ClampMaxBytesRefEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), maxF);
150+
default -> throw EsqlIllegalArgumentException.illegalDataType(outputType);
151+
};
172152
}
173153

174154
@Evaluator(extraName = "Boolean")

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2727
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast;
2828
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
29+
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
2930

3031
import java.io.IOException;
3132
import java.util.List;
@@ -148,29 +149,15 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
148149
var minF = outputType != min.dataType()
149150
? Cast.cast(source(), min.dataType(), outputType, toEvaluator.apply(min))
150151
: toEvaluator.apply(min);
151-
if (outputType == DataType.BOOLEAN) {
152-
return new ClampMinBooleanEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
153-
}
154-
if (outputType == DataType.DOUBLE) {
155-
return new ClampMinDoubleEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
156-
}
157-
if (outputType == DataType.INTEGER) {
158-
return new ClampMinIntegerEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
159-
}
160-
if (outputType == DataType.UNSIGNED_LONG
161-
|| outputType == DataType.LONG
162-
|| outputType == DataType.DATETIME
163-
|| outputType == DataType.DATE_NANOS) {
164-
return new ClampMinLongEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
165-
}
166-
if (DataType.isString(outputType)
167-
|| outputType == DataType.IP
168-
|| outputType == DataType.VERSION
169-
|| outputType == DataType.UNSUPPORTED) {
170152

171-
return new ClampMinBytesRefEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
172-
}
173-
throw EsqlIllegalArgumentException.illegalDataType(outputType);
153+
return switch (PlannerUtils.toElementType(outputType)) {
154+
case BOOLEAN -> new ClampMinBooleanEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
155+
case DOUBLE -> new ClampMinDoubleEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
156+
case INT -> new ClampMinIntegerEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
157+
case LONG -> new ClampMinLongEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
158+
case BYTES_REF -> new ClampMinBytesRefEvaluator.Factory(source(), toEvaluator.apply(children().get(0)), minF);
159+
default -> throw EsqlIllegalArgumentException.illegalDataType(outputType);
160+
};
174161
}
175162

176163
@Evaluator(extraName = "Boolean")

0 commit comments

Comments
 (0)