Skip to content

Commit 38ccfbb

Browse files
committed
ESQL: Small fixes for COPY_SIGN
Adds some small fixes to `COPY_SIGN` and some docs and tests. The fixes are mostly supporting the literal `NULL` in parameters. `COPY_SIGN` supports `float` arguments, but the tests don't cover it because we don't build `float` blocks. Otherwise, the tests are fairly standard.
1 parent 785803b commit 38ccfbb

File tree

13 files changed

+415
-7
lines changed

13 files changed

+415
-7
lines changed

docs/reference/query-languages/esql/_snippets/functions/description/copy_sign.md

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/_snippets/functions/layout/copy_sign.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/_snippets/functions/parameters/copy_sign.md

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/_snippets/functions/types/copy_sign.md

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/_snippets/lists/math-functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [`ATAN2`](../../functions-operators/math-functions.md#esql-atan2)
66
* [`CBRT`](../../functions-operators/math-functions.md#esql-cbrt)
77
* [`CEIL`](../../functions-operators/math-functions.md#esql-ceil)
8+
* [`COPY_SIGN`](../../functions-operators/math-functions.md#esql-copy-sign)
89
* [`COS`](../../functions-operators/math-functions.md#esql-cos)
910
* [`COSH`](../../functions-operators/math-functions.md#esql-cosh)
1011
* [`E`](../../functions-operators/math-functions.md#esql-e)

docs/reference/query-languages/esql/functions-operators/math-functions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ mapped_pages:
3333
:::{include} ../_snippets/functions/layout/ceil.md
3434
:::
3535

36+
:::{include} ../_snippets/functions/layout/copy_sign.md
37+
:::
38+
3639
:::{include} ../_snippets/functions/layout/cos.md
3740
:::
3841

docs/reference/query-languages/esql/images/functions/copy_sign.svg

Lines changed: 1 addition & 0 deletions
Loading

docs/reference/query-languages/esql/kibana/definition/functions/copy_sign.json

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/kibana/docs/functions/copy_sign.md

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
1616
import org.elasticsearch.xpack.esql.core.expression.Expression;
1717
import org.elasticsearch.xpack.esql.core.expression.Expressions;
18+
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
1819
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1920
import org.elasticsearch.xpack.esql.core.tree.Source;
2021
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -63,18 +64,18 @@ EvalOperator.ExpressionEvaluator.Factory create(
6364
@FunctionInfo(
6465
description = "Returns a value with the magnitude of the first argument and the sign of the second argument. "
6566
+ "This function is similar to Java's Math.copySign(double magnitude, double sign).",
66-
returnType = { "double", "float" }
67+
returnType = { "double", "integer", "long" }
6768
)
6869
public CopySign(
6970
Source source,
7071
@Param(
7172
name = "magnitude",
72-
type = { "double", "float", "integer", "long" },
73+
type = { "double", "integer", "long" },
7374
description = "The expression providing the magnitude of the result. Must be a numeric type."
7475
) Expression magnitude,
7576
@Param(
7677
name = "sign",
77-
type = { "double", "float", "integer", "long" },
78+
type = { "double", "integer", "long" },
7879
description = "The expression providing the sign of the result. Must be a numeric type."
7980
) Expression sign
8081
) {
@@ -125,11 +126,25 @@ public TypeResolution resolveType() {
125126
}
126127
var magnitude = children().get(0);
127128
var sign = children().get(1);
128-
if (magnitude.dataType().isNumeric() == false) {
129-
return new TypeResolution("Magnitude must be a numeric type");
129+
TypeResolution resolution = TypeResolutions.isType(
130+
magnitude,
131+
t -> t.isNumeric() && t != DataType.UNSIGNED_LONG,
132+
sourceText(),
133+
TypeResolutions.ParamOrdinal.FIRST,
134+
"numeric"
135+
);
136+
if (resolution.unresolved()) {
137+
return resolution;
130138
}
131-
if (sign.dataType().isNumeric() == false) {
132-
return new TypeResolution("Sign must be a numeric type");
139+
resolution = TypeResolutions.isType(
140+
sign,
141+
t -> t.isNumeric() && t != DataType.UNSIGNED_LONG,
142+
sourceText(),
143+
TypeResolutions.ParamOrdinal.SECOND,
144+
"numeric"
145+
);
146+
if (resolution.unresolved()) {
147+
return resolution;
133148
}
134149
// The return type is the same as the magnitude type, so we can use it directly.
135150
dataType = magnitude.dataType();

0 commit comments

Comments
 (0)