2222import org .elasticsearch .xpack .esql .expression .function .Param ;
2323import org .elasticsearch .xpack .esql .expression .function .scalar .EsqlScalarFunction ;
2424import org .elasticsearch .xpack .esql .io .stream .PlanStreamInput ;
25- import org .elasticsearch .xpack .esql .type .EsqlDataTypeConverter ;
2625
2726import java .io .IOException ;
2827import java .util .Arrays ;
@@ -50,7 +49,11 @@ EvalOperator.ExpressionEvaluator.Factory create(
5049 DataType .FLOAT ,
5150 CopySignFloatEvaluator .Factory ::new ,
5251 DataType .DOUBLE ,
53- CopySignDoubleEvaluator .Factory ::new
52+ CopySignDoubleEvaluator .Factory ::new ,
53+ DataType .LONG ,
54+ CopySignLongEvaluator .Factory ::new ,
55+ DataType .INTEGER ,
56+ CopySignIntegerEvaluator .Factory ::new
5457 );
5558
5659 private DataType dataType ;
@@ -107,7 +110,6 @@ public Expression replaceChildren(List<Expression> newChildren) {
107110
108111 @ Override
109112 public DataType dataType () {
110-
111113 if (dataType == null ) {
112114 resolveType ();
113115 }
@@ -127,25 +129,8 @@ public TypeResolution resolveType() {
127129 if (sign .dataType ().isNumeric () == false ) {
128130 return new TypeResolution ("Sign must be a numeric type" );
129131 }
130- dataType = EsqlDataTypeConverter .commonType (magnitude .dataType (), sign .dataType ());
131- // TypeResolution resolution = TypeResolutions.isType(
132- // magnitude,
133- // t -> t == commonType,
134- // sourceText(),
135- // TypeResolutions.ParamOrdinal.fromIndex(1),
136- // magnitude.dataType().typeName()
137- // );
138- // if (resolution.unresolved()) {
139- // throw new EsqlIllegalArgumentException(
140- // "Magnitude [{}] is not compatible with sign [{}] for function [{}] - common type is [{}]",
141- // magnitude.dataType(),
142- // sign.dataType(),
143- // NAME
144- // ,commonType
145- // );
146- //// return resolution;
147- // }
148- // dataType = commonType;
132+ // The return type is the same as the magnitude type, so we can use it directly.
133+ dataType = magnitude .dataType ();
149134 return TypeResolution .TYPE_RESOLVED ;
150135 }
151136
@@ -157,12 +142,17 @@ public boolean foldable() {
157142 @ Override
158143 public EvalOperator .ExpressionEvaluator .Factory toEvaluator (ToEvaluator toEvaluator ) {
159144 var dataType = dataType ();
160- if (FACTORY_PROVIDERS .containsKey (dataType )) {
161- return FACTORY_PROVIDERS .get (dataType )
162- .create (source (), toEvaluator .apply (children ().get (0 )), toEvaluator .apply (children ().get (1 )));
163- } else {
164- throw new EsqlIllegalArgumentException ("Unsupported data type [{}] for function [{}]" , dataType , NAME );
145+ if (!FACTORY_PROVIDERS .containsKey (dataType )) {
146+ throw new EsqlIllegalArgumentException ("Unsupported data type [{}] for function [{}]" , dataType (), NAME );
147+ }
148+ var sign = children ().get (1 );
149+ var signFactory = toEvaluator .apply (sign );
150+ var magnitude = children ().get (0 );
151+ if (sign .dataType () != dataType ) {
152+ // If the sign is not the same type as the magnitude, we need to convert it.
153+ signFactory = Cast .cast (sign .source (), sign .dataType (), dataType , signFactory );
165154 }
155+ return FACTORY_PROVIDERS .get (dataType ).create (source (), toEvaluator .apply (magnitude ), signFactory );
166156 }
167157
168158 @ Evaluator (extraName = "Float" )
0 commit comments