Skip to content

Commit f3b2006

Browse files
authored
Add PI and TAU functions (ESQL-1357)
Adds functions for the constants `PI` and it's big brother `TAU`.
1 parent f294be3 commit f3b2006

File tree

13 files changed

+318
-28
lines changed

13 files changed

+318
-28
lines changed

docs/reference/esql/esql-functions.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ these functions:
3232
* <<esql-mv_min>>
3333
* <<esql-mv_sum>>
3434
* <<esql-now>>
35+
* <<esql-pi>>
3536
* <<esql-pow>>
3637
* <<esql-round>>
3738
* <<esql-split>>
3839
* <<esql-starts_with>>
3940
* <<esql-substring>>
41+
* <<esql-tau>>
4042
* <<esql-to_boolean>>
4143
* <<esql-to_datetime>>
4244
* <<esql-to_double>>
@@ -69,11 +71,13 @@ include::functions/mv_median.asciidoc[]
6971
include::functions/mv_min.asciidoc[]
7072
include::functions/mv_sum.asciidoc[]
7173
include::functions/now.asciidoc[]
74+
include::functions/pi.asciidoc[]
7275
include::functions/pow.asciidoc[]
7376
include::functions/round.asciidoc[]
7477
include::functions/split.asciidoc[]
7578
include::functions/starts_with.asciidoc[]
7679
include::functions/substring.asciidoc[]
80+
include::functions/tau.asciidoc[]
7781
include::functions/to_boolean.asciidoc[]
7882
include::functions/to_datetime.asciidoc[]
7983
include::functions/to_double.asciidoc[]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[esql-pi]]
2+
=== `PI`
3+
The {wikipedia}/Pi[ratio] of a circle's circumference to its diameter.
4+
5+
[source.merge.styled,esql]
6+
----
7+
include::{esql-specs}/math.csv-spec[tag=pi]
8+
----
9+
[%header.monospaced.styled,format=dsv,separator=|]
10+
|===
11+
include::{esql-specs}/math.csv-spec[tag=pi-result]
12+
|===
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[esql-tau]]
2+
=== `TAU`
3+
The https://tauday.com/tau-manifesto[ratio] of a circle's circumference to its radius.
4+
5+
[source.merge.styled,esql]
6+
----
7+
include::{esql-specs}/math.csv-spec[tag=tau]
8+
----
9+
[%header.monospaced.styled,format=dsv,separator=|]
10+
|===
11+
include::{esql-specs}/math.csv-spec[tag=tau-result]
12+
|===

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,27 @@ row ul = [18446744073709551615, 0, 1, 9223372036854775807, 9223372036854775808,
566566
mv_median(ul):ul
567567
4611686018427387904
568568
;
569+
570+
pi
571+
// tag::pi[]
572+
ROW PI()
573+
// end::pi[]
574+
;
575+
576+
// tag::pi-result[]
577+
PI():double
578+
3.141592653589793
579+
// end::pi-result[]
580+
;
581+
582+
tau
583+
// tag::tau[]
584+
ROW TAU()
585+
// end::tau[]
586+
;
587+
588+
// tag::tau-result[]
589+
TAU():double
590+
6.283185307179586
591+
// end::tau-result[]
592+
;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ mv_min |mv_min(arg1)
4141
mv_sum |mv_sum(arg1)
4242
now |now()
4343
percentile |percentile(arg1, arg2)
44+
pi |pi()
4445
pow |pow(arg1, arg2)
4546
round |round(arg1, arg2)
4647
split |split(arg1, arg2)
4748
starts_with |starts_with(arg1, arg2)
4849
substring |substring(arg1, arg2, arg3)
4950
sum |sum(arg1)
51+
tau |tau()
5052
to_bool |to_bool(arg1)
5153
to_boolean |to_boolean(arg1)
5254
to_datetime |to_datetime(arg1)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import org.elasticsearch.xpack.esql.expression.function.scalar.math.IsFinite;
3838
import org.elasticsearch.xpack.esql.expression.function.scalar.math.IsInfinite;
3939
import org.elasticsearch.xpack.esql.expression.function.scalar.math.IsNaN;
40+
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Pi;
4041
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Pow;
4142
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Round;
43+
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Tau;
4244
import org.elasticsearch.xpack.esql.expression.function.scalar.metadata.Metadata;
4345
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvAvg;
4446
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvConcat;
@@ -90,8 +92,10 @@ private FunctionDefinition[][] functions() {
9092
def(IsFinite.class, IsFinite::new, "is_finite"),
9193
def(IsInfinite.class, IsInfinite::new, "is_infinite"),
9294
def(IsNaN.class, IsNaN::new, "is_nan"),
95+
def(Pi.class, Pi::new, "pi"),
96+
def(Pow.class, Pow::new, "pow"),
9397
def(Round.class, Round::new, "round"),
94-
def(Pow.class, Pow::new, "pow") },
98+
def(Tau.class, Tau::new, "tau") },
9599
// string
96100
new FunctionDefinition[] {
97101
def(Length.class, Length::new, "length"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
9+
10+
import org.elasticsearch.xpack.ql.expression.Expression;
11+
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
12+
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
13+
import org.elasticsearch.xpack.ql.tree.NodeInfo;
14+
import org.elasticsearch.xpack.ql.tree.Source;
15+
import org.elasticsearch.xpack.ql.type.DataType;
16+
import org.elasticsearch.xpack.ql.type.DataTypes;
17+
18+
/**
19+
* Function that emits Euler's number.
20+
*/
21+
public abstract class DoubleConstantFunction extends ScalarFunction {
22+
protected DoubleConstantFunction(Source source) {
23+
super(source);
24+
}
25+
26+
@Override
27+
public final boolean foldable() {
28+
return true;
29+
}
30+
31+
@Override
32+
public final DataType dataType() {
33+
return DataTypes.DOUBLE;
34+
}
35+
36+
@Override
37+
public final ScriptTemplate asScript() {
38+
throw new UnsupportedOperationException();
39+
}
40+
41+
@Override
42+
protected final NodeInfo<? extends Expression> info() {
43+
return NodeInfo.create(this);
44+
}
45+
}

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,25 @@
88
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
99

1010
import org.elasticsearch.xpack.ql.expression.Expression;
11-
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
12-
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
13-
import org.elasticsearch.xpack.ql.tree.NodeInfo;
1411
import org.elasticsearch.xpack.ql.tree.Source;
15-
import org.elasticsearch.xpack.ql.type.DataType;
16-
import org.elasticsearch.xpack.ql.type.DataTypes;
1712

1813
import java.util.List;
1914

2015
/**
2116
* Function that emits Euler's number.
2217
*/
23-
public class E extends ScalarFunction {
18+
public class E extends DoubleConstantFunction {
2419
public E(Source source) {
2520
super(source);
2621
}
2722

28-
@Override
29-
public boolean foldable() {
30-
return true;
31-
}
32-
3323
@Override
3424
public Object fold() {
3525
return Math.E;
3626
}
3727

38-
@Override
39-
public DataType dataType() {
40-
return DataTypes.DOUBLE;
41-
}
42-
43-
@Override
44-
public ScriptTemplate asScript() {
45-
throw new UnsupportedOperationException();
46-
}
47-
4828
@Override
4929
public Expression replaceChildren(List<Expression> newChildren) {
5030
return new E(source());
5131
}
52-
53-
@Override
54-
protected NodeInfo<? extends Expression> info() {
55-
return NodeInfo.create(this);
56-
}
5732
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
9+
10+
import org.elasticsearch.xpack.ql.expression.Expression;
11+
import org.elasticsearch.xpack.ql.tree.Source;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Function that emits pi.
17+
*/
18+
public class Pi extends DoubleConstantFunction {
19+
public Pi(Source source) {
20+
super(source);
21+
}
22+
23+
@Override
24+
public Object fold() {
25+
return Math.PI;
26+
}
27+
28+
@Override
29+
public Expression replaceChildren(List<Expression> newChildren) {
30+
return new Pi(source());
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
9+
10+
import org.elasticsearch.xpack.ql.expression.Expression;
11+
import org.elasticsearch.xpack.ql.tree.Source;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Function that emits tau, also known as 2 * pi.
17+
*/
18+
public class Tau extends DoubleConstantFunction {
19+
public static final double TAU = Math.PI * 2;
20+
21+
public Tau(Source source) {
22+
super(source);
23+
}
24+
25+
@Override
26+
public Object fold() {
27+
return TAU;
28+
}
29+
30+
@Override
31+
public Expression replaceChildren(List<Expression> newChildren) {
32+
return new Tau(source());
33+
}
34+
}

0 commit comments

Comments
 (0)