2626import org .elasticsearch .xpack .esql .expression .function .scalar .EsqlScalarFunction ;
2727import org .elasticsearch .xpack .esql .expression .function .scalar .math .Cast ;
2828import org .elasticsearch .xpack .esql .io .stream .PlanStreamInput ;
29+ import org .elasticsearch .xpack .esql .planner .PlannerUtils ;
2930
3031import java .io .IOException ;
3132import java .util .List ;
3839public 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" )
0 commit comments