diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 9c26427143a..3a325143cef 100644 --- a/docs/user/dql/functions.rst +++ b/docs/user/dql/functions.rst @@ -215,8 +215,7 @@ Argument type: INTEGER/LONG/FLOAT/DOUBLE Return type: DOUBLE -(Non-negative) INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE -(Negative) INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE +INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE Example:: diff --git a/docs/user/ppl/functions/math.rst b/docs/user/ppl/functions/math.rst index d1801877787..69f94091e7f 100644 --- a/docs/user/ppl/functions/math.rst +++ b/docs/user/ppl/functions/math.rst @@ -662,3 +662,27 @@ Example:: | 2.0 | 2.1 | +-----------+--------------+ +CBRT +---- + +Description +>>>>>>>>>>> + +Usage: Calculates the cube root of a number + +Argument type: INTEGER/LONG/FLOAT/DOUBLE + +Return type DOUBLE: + +INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + +Example:: + + opensearchsql> source=location | eval `CBRT(8)` = CBRT(8), `CBRT(9.261)` = CBRT(9.261), `CBRT(-27)` = CBRT(-27) | fields `CBRT(8)`, `CBRT(9.261)`, `CBRT(-27)`; + fetched rows / total rows = 2/2 + +-----------+---------------+-------------+ + | CBRT(8) | CBRT(9.261) | CBRT(-27) | + |-----------+---------------+-------------| + | 2.0 | 2.1 | -3.0 | + | 2.0 | 2.1 | -3.0 | + +-----------+---------------+-------------+ diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/MathematicalFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/MathematicalFunctionIT.java index 1da8164c440..f6fe93dc5f8 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/MathematicalFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/MathematicalFunctionIT.java @@ -7,6 +7,7 @@ package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_CALCS; import static org.opensearch.sql.util.MatcherUtils.closeTo; import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.schema; @@ -24,6 +25,7 @@ public class MathematicalFunctionIT extends PPLIntegTestCase { public void init() throws IOException { loadIndex(Index.BANK); loadIndex(Index.BANK_WITH_NULL_VALUES); + loadIndex(Index.CALCS); } @Test @@ -262,6 +264,22 @@ public void testSqrt() throws IOException { rows(5.830951894845301)); } + @Test + public void testCbrt() throws IOException { + JSONObject result = + executeQuery( + String.format( + "source=%s | eval f = cbrt(num3) | fields f", TEST_INDEX_CALCS)); + verifySchema(result, schema("f", null, "double")); + verifyDataRows(result, + closeTo(Math.cbrt(-11.52)), closeTo(Math.cbrt(-9.31)), closeTo(Math.cbrt(-12.17)), + closeTo(Math.cbrt(-7.25)), closeTo(Math.cbrt(12.93)), closeTo(Math.cbrt(-19.96)), + closeTo(Math.cbrt(10.93)), closeTo(Math.cbrt(3.64)), closeTo(Math.cbrt(-13.38)), + closeTo(Math.cbrt(-10.56)), closeTo(Math.cbrt(-4.79)), closeTo(Math.cbrt(-10.81)), + closeTo(Math.cbrt(-6.62)), closeTo(Math.cbrt(-18.43)), closeTo(Math.cbrt(6.84)), + closeTo(Math.cbrt(-10.98)), closeTo(Math.cbrt(-2.6))); + } + @Test public void testTruncate() throws IOException { JSONObject result = diff --git a/ppl/src/main/antlr/OpenSearchPPLLexer.g4 b/ppl/src/main/antlr/OpenSearchPPLLexer.g4 index e0aeb0ac47f..a601a547eea 100644 --- a/ppl/src/main/antlr/OpenSearchPPLLexer.g4 +++ b/ppl/src/main/antlr/OpenSearchPPLLexer.g4 @@ -201,6 +201,7 @@ DC: 'DC'; // BASIC FUNCTIONS ABS: 'ABS'; +CBRT: 'CBRT'; CEIL: 'CEIL'; CEILING: 'CEILING'; CONV: 'CONV'; diff --git a/ppl/src/main/antlr/OpenSearchPPLParser.g4 b/ppl/src/main/antlr/OpenSearchPPLParser.g4 index 0dd30ddcd79..9a414d9bacd 100644 --- a/ppl/src/main/antlr/OpenSearchPPLParser.g4 +++ b/ppl/src/main/antlr/OpenSearchPPLParser.g4 @@ -417,7 +417,7 @@ relevanceArgValue ; mathematicalFunctionBase - : ABS | CEIL | CEILING | CONV | CRC32 | E | EXP | FLOOR | LN | LOG | LOG10 | LOG2 | MOD | PI |POW | POWER + : ABS | CBRT | CEIL | CEILING | CONV | CRC32 | E | EXP | FLOOR | LN | LOG | LOG10 | LOG2 | MOD | PI |POW | POWER | RAND | ROUND | SIGN | SQRT | TRUNCATE | trigonometricFunctionName ;