Skip to content

Commit ad9ecc6

Browse files
committed
figured types out and set it up
1 parent 255d5a9 commit ad9ecc6

File tree

2 files changed

+28
-23
lines changed
  • x-pack/plugin/esql
    • qa/testFixtures/src/main/resources
    • src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math

2 files changed

+28
-23
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,26 +1844,26 @@ emp_no:integer | l1:double | l2:double
18441844
;
18451845

18461846
copySignWithMixedNumericValues
1847-
required_capability: mixed_numeric_types_in_case_copy_sign
1847+
required_capability: mixed_numeric_types_in_case_greatest_least
18481848

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

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

18551855
copySignWithMixedNumericValuesWithMV
18561856
required_capability: mixed_numeric_types_in_case_greatest_least
18571857

18581858
FROM employees
1859-
| EVAL cs1 = COPY_SIGN(salary, salary_change)
1859+
| EVAL cs1 = COPY_SIGN(salary, LEAST(salary_change))
18601860
| KEEP emp_no, salary, salary_change, cs1
18611861
| SORT emp_no
18621862
| LIMIT 3
18631863
;
18641864

1865-
emp_no:integer | salary:long | salary_change:double | cs1:long
1866-
10001 | 57305 | 1.19 | 57305
1867-
10002 | 56371 | -7.23 | -56371
1868-
10003 | 61805 | 4.0 | 61805
1865+
emp_no:integer | salary:integer | salary_change:double | cs1:integer
1866+
10001 | 57305 | 1.19 | 57305
1867+
10002 | 56371 | [-7.23, 11.17] | -56371
1868+
10003 | 61805 | [12.82, 14.68] | 61805
18691869
;

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,23 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
146146
throw new EsqlIllegalArgumentException("Unsupported data type [{}] for function [{}]", dataType(), NAME);
147147
}
148148
var sign = children().get(1);
149-
var signFactory = toEvaluator.apply(sign);
150149
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(source(), sign.dataType(), dataType, signFactory);
154-
}
155-
return FACTORY_PROVIDERS.get(dataType).create(source(), toEvaluator.apply(magnitude), signFactory);
150+
return FACTORY_PROVIDERS.get(dataType)
151+
.create(
152+
source(),
153+
toEvaluator.apply(magnitude),
154+
// We always cast the sign to double for processing, as the sign can be any numeric type.
155+
Cast.cast(source(), sign.dataType(), DataType.DOUBLE, toEvaluator.apply(sign))
156+
);
156157
}
157158

158159
@Evaluator(extraName = "Float")
159-
static float processFloat(float magnitude, float sign) {
160-
return Math.copySign(magnitude, sign);
160+
static float processFloat(float magnitude, double sign) {
161+
if (sign < 0) {
162+
return magnitude < 0 ? magnitude : -magnitude;
163+
} else {
164+
return magnitude < 0 ? -magnitude : magnitude;
165+
}
161166
}
162167

163168
@Evaluator(extraName = "Double")
@@ -166,20 +171,20 @@ static double processDouble(double magnitude, double sign) {
166171
}
167172

168173
@Evaluator(extraName = "Long")
169-
static long processLong(long magnitude, long sign) {
174+
static long processLong(long magnitude, double sign) {
170175
if (sign < 0) {
171-
return -Math.abs(magnitude);
176+
return magnitude < 0 ? magnitude : -magnitude;
172177
} else {
173-
return Math.abs(magnitude);
178+
return magnitude < 0 ? -magnitude : magnitude;
174179
}
175180
}
176181

177182
@Evaluator(extraName = "Integer")
178-
static int processInteger(int magnitude, int sign) {
183+
static int processInteger(int magnitude, double sign) {
179184
if (sign < 0) {
180-
return -Math.abs(magnitude);
185+
return magnitude < 0 ? magnitude : -magnitude;
181186
} else {
182-
return Math.abs(magnitude);
187+
return magnitude < 0 ? -magnitude : magnitude;
183188
}
184189
}
185190
}

0 commit comments

Comments
 (0)