Skip to content

Commit 85c3d83

Browse files
committed
casting
1 parent bfbd48c commit 85c3d83

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/math.csv-spec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,10 +1846,10 @@ emp_no:integer | l1:double | l2:double
18461846
copySignWithMixedNumericValues
18471847
required_capability: mixed_numeric_types_in_case_copy_sign
18481848

1849-
ROW cs1=COPY_SIGN(10.0, -5.0), cs2=COPY_SIGN(10.0, null);
1849+
ROW cs1=COPY_SIGN(10.0, -5.0), cs2=COPY_SIGN(10.0, null), cs3=COPY_SIGN(1, -5.0);
18501850

1851-
cs1:double | cs2:double
1852-
-5.0 | null
1851+
cs1:double | cs2:double | cs3:long
1852+
-5.0 | null | -1
18531853
;
18541854

18551855
copySignWithMixedNumericValuesWithMV
@@ -1862,7 +1862,7 @@ FROM employees
18621862
| LIMIT 3
18631863
;
18641864

1865-
emp_no:integer | salary:long | salary_change:double | cs1:double
1865+
emp_no:integer | salary:long | salary_change:double | cs1:long
18661866
10001 | 57305 | 1.19 | 57305
18671867
10002 | 56371 | -7.23 | -56371
18681868
10003 | 61805 | 4.0 | 61805

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ private static FunctionDefinition[][] functions() {
322322
def(Exp.class, Exp::new, "exp"),
323323
def(Floor.class, Floor::new, "floor"),
324324
def(Greatest.class, Greatest::new, "greatest"),
325-
def(CopySign.class, CopySign::new, "copy_sign"),
325+
def(CopySign.class, bi(CopySign::new), "copy_sign"),
326326
def(Hypot.class, Hypot::new, "hypot"),
327327
def(Log.class, Log::new, "log"),
328328
def(Log10.class, Log10::new, "log10"),

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.xpack.esql.expression.function.Param;
2323
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2424
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
25-
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
2625

2726
import java.io.IOException;
2827
import 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

Comments
 (0)