Skip to content

Commit

Permalink
Add ES|QL signum function (#106866)
Browse files Browse the repository at this point in the history
* Add ES|QL signum function

* Update docs/changelog/106866.yaml

* Skip csv tests for versions older than 8.14

* Reference layout docs file and fix instructions for adding functions

* Break csv specs by param type

* More tests
  • Loading branch information
ioanatia authored Apr 4, 2024
1 parent 996a164 commit 7b25421
Show file tree
Hide file tree
Showing 22 changed files with 864 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/106866.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 106866
summary: Add ES|QL signum function
area: ES|QL
type: enhancement
issues: []
5 changes: 5 additions & 0 deletions docs/reference/esql/functions/description/signum.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Description*

Returns the sign of the given number. It returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
13 changes: 13 additions & 0 deletions docs/reference/esql/functions/examples/signum.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Example*

[source.merge.styled,esql]
----
include::{esql-specs}/math.csv-spec[tag=signum]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/math.csv-spec[tag=signum-result]
|===

15 changes: 15 additions & 0 deletions docs/reference/esql/functions/layout/signum.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

[discrete]
[[esql-signum]]
=== `SIGNUM`

*Syntax*

[.text-center]
image::esql/functions/signature/signum.svg[Embedded,opts=inline]

include::../parameters/signum.asciidoc[]
include::../description/signum.asciidoc[]
include::../types/signum.asciidoc[]
include::../examples/signum.asciidoc[]
2 changes: 2 additions & 0 deletions docs/reference/esql/functions/math-functions.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* <<esql-pi>>
* <<esql-pow>>
* <<esql-round>>
* <<esql-signum>>
* <<esql-sin>>
* <<esql-sinh>>
* <<esql-sqrt>>
Expand All @@ -46,6 +47,7 @@ include::layout/log10.asciidoc[]
include::pi.asciidoc[]
include::pow.asciidoc[]
include::round.asciidoc[]
include::layout/signum.asciidoc[]
include::layout/sin.asciidoc[]
include::layout/sinh.asciidoc[]
include::sqrt.asciidoc[]
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/esql/functions/parameters/signum.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Parameters*

`number`::
Numeric expression. If `null`, the function returns `null`.
1 change: 1 addition & 0 deletions docs/reference/esql/functions/signature/signum.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/reference/esql/functions/types/signum.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Supported types*

[%header.monospaced.styled,format=dsv,separator=|]
|===
number | result
double | double
integer | double
long | double
unsigned_long | double
|===
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,50 @@ required_feature: esql.agg_values
[1.56, 1.78] | Tech Lead
[1.7, 1.83, 2.05] | null
;

signumOfPositiveDouble#[skip:-8.13.99,reason:new scalar function added in 8.14]
row d = to_double(100) | eval s = signum(d);

d:double | s:double
100 | 1.0
;

signumOfNegativeDouble#[skip:-8.13.99,reason:new scalar function added in 8.14]
row d = to_double(-100) | eval s = signum(d);

d:double | s:double
-100 | -1.0
;

signumOfZeroDouble#[skip:-8.13.99,reason:new scalar function added in 8.14]
row d = to_double(0) | eval s = signum(d);

d:double | s:double
0 | 0.0
;

signumWithEvalWhereAndStats#[skip:-8.13.99,reason:new scalar function added in 8.14]

from employees
| where emp_no <= 10009
| eval s = signum(mv_min(salary_change))
| where signum(mv_max(salary_change)) >= 0
| STATS x = AVG(signum(60000 - salary));

x:double
0.14285714285714285
;

signumWithEvalAndSort#[skip:-8.13.99,reason:new scalar function added in 8.14]
from employees
| eval s = signum(mv_min(salary_change))
| where signum(mv_max(salary_change)) >= 0
| keep s, emp_no, salary, salary_change
| sort s, emp_no
| limit 3;

s:double | emp_no:integer | salary:integer | salary_change:double
-1.0 | 10002 | 56371 | [-7.23, 11.17]
-1.0 | 10004 | 36174 | [-0.35, 1.13, 3.65, 13.48]
-1.0 | 10005 | 63528 | [-2.14, 13.07]
;
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,70 @@ required_feature: esql.agg_values
[3, 5] | Tech Lead
[1, 4] | null
;

signumOfPositiveInteger#[skip:-8.13.99,reason:new scalar function added in 8.14]
row i = 100 | eval s = signum(i);

i:integer | s:double
100 | 1.0
;

signumOfNegativeInteger#[skip:-8.13.99,reason:new scalar function added in 8.14]
row i = -100 | eval s = signum(i);

i:integer | s:double
-100 | -1.0
;

signumOfZeroInteger#[skip:-8.13.99,reason:new scalar function added in 8.14]
row i = 0 | eval s = signum(i);

i:integer | s:double
0 | 0.0
;

signumOfPositiveLong#[skip:-8.13.99,reason:new scalar function added in 8.14]
row l = to_long(100) | eval s = signum(l);

l:long | s:double
100 | 1.0
;

signumOfNegativeLong#[skip:-8.13.99,reason:new scalar function added in 8.14]
row l = to_long(-100) | eval s = signum(l);

l:long | s:double
-100 | -1.0
;

signumOfZeroLong#[skip:-8.13.99,reason:new scalar function added in 8.14]
row l = to_long(0) | eval s = signum(l);

l:long | s:double
0 | 0.0
;

signumWithEvalWhereAndStats#[skip:-8.13.99,reason:new scalar function added in 8.14]

from employees
| eval s = signum(mv_min(salary_change.int))
| where signum(mv_max(salary_change.int)) >= 0
| STATS x=AVG(signum(60000 - salary));

x:double
0.5409836065573771
;

signumWithEvalAndSort#[skip:-8.13.99,reason:new scalar function added in 8.14]
from employees
| eval s = signum(60000 - salary)
| where signum(salary - 55000) >= 0
| keep s, emp_no, salary
| sort s DESC, salary ASC
| limit 3;

s:double | emp_no:integer | salary:integer
1.0 | 10052 | 55360
1.0 | 10002 | 56371
1.0 | 10041 | 56415
;
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,19 @@ i:ul | c:ul | f:ul
1000000000000000000 | 1000000000000000000 | 1000000000000000000
;

signum#[skip:-8.13.99,reason:new scalar function added in 8.14]
// tag::signum[]
ROW d = 100.0
| EVAL s = SIGNUM(d)
// end::signum[]
;

// tag::signum-result[]
d: double | s:double
100 | 1.0
// end::signum-result[]
;

sqrt
// tag::sqrt[]
ROW d = 100.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ double pi()
"keyword right(string:keyword|text, length:integer)"
"double round(number:double, ?decimals:integer)"
"keyword|text rtrim(string:keyword|text)"
"double signum(number:double|integer|long|unsigned_long)"
"double sin(angle:double|integer|long|unsigned_long)"
"double sinh(angle:double|integer|long|unsigned_long)"
"keyword split(string:keyword|text, delim:keyword|text)"
Expand Down Expand Up @@ -165,6 +166,7 @@ replace |[string, regex, newString] |["keyword|text", "keyword|te
right |[string, length] |["keyword|text", integer] |[, ]
round |[number, decimals] |[double, integer] |[The numeric value to round, The number of decimal places to round to. Defaults to 0.]
rtrim |string |"keyword|text" |[""]
signum |number |"double|integer|long|unsigned_long" |"Numeric expression. If `null`, the function returns `null`."
sin |angle |"double|integer|long|unsigned_long" |An angle, in radians. If `null`, the function returns `null`.
sinh |angle |"double|integer|long|unsigned_long" |An angle, in radians. If `null`, the function returns `null`.
split |[string, delim] |["keyword|text", "keyword|text"] |[, ]
Expand Down Expand Up @@ -273,6 +275,7 @@ replace |The function substitutes in the string any match of the regular e
right |Return the substring that extracts length chars from the string starting from the right.
round |Rounds a number to the closest number with the specified number of digits.
rtrim |Removes trailing whitespaces from a string.
signum |Returns the sign of the given number. It returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
sin |Returns ths {wikipedia}/Sine_and_cosine[Sine] trigonometric function of an angle.
sinh |Returns the {wikipedia}/Hyperbolic_functions[hyperbolic sine] of an angle.
split |Split a single valued string into multiple strings.
Expand Down Expand Up @@ -382,6 +385,7 @@ replace |keyword
right |keyword |[false, false] |false |false
round |double |[false, true] |false |false
rtrim |"keyword|text" |false |false |false
signum |double |false |false |false
sin |double |false |false |false
sinh |double |false |false |false
split |keyword |[false, false] |false |false
Expand Down Expand Up @@ -443,5 +447,5 @@ countFunctions#[skip:-8.13.99]
meta functions | stats a = count(*), b = count(*), c = count(*) | mv_expand c;

a:long | b:long | c:long
99 | 99 | 99
100 | 100 | 100
;
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,34 @@ warning:Line 1:22: java.lang.IllegalArgumentException: single-value function enc
bytes_in:ul | rad:double
16002960716282089759 | 2.79304354566432608E17
;

signumOfPositiveUnsignedLong#[skip:-8.13.99,reason:new scalar function added in 8.14]
row l = to_ul(100) | eval s = signum(l);

l:ul | s:double
100 | 1.0
;

signumOfZeroUnsignedLong#[skip:-8.13.99,reason:new scalar function added in 8.14]
row l = to_ul(0) | eval s = signum(l);

l:ul | s:double
0 | 0.0
;

signumWithEvalAndWhere#[skip:-8.13.99,reason:new scalar function added in 8.14]

from ul_logs |
where signum(bytes_in) >= 0.0 |
eval s = signum(bytes_out) |
keep s, bytes_in, bytes_out |
sort bytes_out, s |
limit 2;

warning:Line 2:7: evaluation of [signum(bytes_in)] failed, treating result as null. Only first 20 failures recorded.
warning:Line 2:7: java.lang.IllegalArgumentException: single-value function encountered multi-value

s:double | bytes_in:ul | bytes_out:ul
1.0 | 1957665857956635540 | 352442273299370793
1.0 | 2408213296071189837 | 419872666232023984
;
Loading

0 comments on commit 7b25421

Please sign in to comment.