From d363f60b47259f32753115bfce586f6ee5e1414b Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Wed, 23 Nov 2022 08:10:24 -0800 Subject: [PATCH 1/3] Add cbrt function to the PPL (#171) * Add cbrt function to the PPL Signed-off-by: Margarit Hakobyan --- docs/user/dql/functions.rst | 3 +-- docs/user/ppl/functions/math.rst | 24 +++++++++++++++++++ .../sql/ppl/MathematicalFunctionIT.java | 13 ++++++++++ ppl/src/main/antlr/OpenSearchPPLLexer.g4 | 1 + ppl/src/main/antlr/OpenSearchPPLParser.g4 | 2 +- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/user/dql/functions.rst b/docs/user/dql/functions.rst index 9c26427143..3a325143ce 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 d180187778..69f94091e7 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 1da8164c44..78a83b77ad 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 @@ -262,6 +262,19 @@ public void testSqrt() throws IOException { rows(5.830951894845301)); } + @Test + public void testCbrt() throws IOException { + JSONObject result = + executeQuery( + String.format( + "source=%s | eval f = cbrt(age) | fields f", TEST_INDEX_BANK)); + verifySchema(result, schema("f", null, "double")); + verifyDataRows(result, + rows(3.174802103936399), rows(3.3019272488946267), rows(3.0365889718756627), + rows(3.2075343299958265), rows(3.3019272488946267), rows(3.391211443014167), + rows(3.2396118012774835)); + } + @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 e0aeb0ac47..a601a547ee 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 0dd30ddcd7..9a414d9bac 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 ; From b60195c90a24932f36c65c9c12c869b63ab8b5de Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Wed, 23 Nov 2022 16:32:04 -0800 Subject: [PATCH 2/3] Address PR review feedback Signed-off-by: Margarit Hakobyan --- .../sql/ppl/MathematicalFunctionIT.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 78a83b77ad..54ad7666f6 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 @@ -273,6 +275,19 @@ public void testCbrt() throws IOException { rows(3.174802103936399), rows(3.3019272488946267), rows(3.0365889718756627), rows(3.2075343299958265), rows(3.3019272488946267), rows(3.391211443014167), rows(3.2396118012774835)); + + 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 From 77de5e53e04e701e14124222614826169bf57191 Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Mon, 28 Nov 2022 10:44:33 -0800 Subject: [PATCH 3/3] Address PR review comments Signed-off-by: Margarit Hakobyan --- .../sql/ppl/MathematicalFunctionIT.java | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) 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 54ad7666f6..f6fe93dc5f 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 @@ -267,19 +267,9 @@ public void testSqrt() throws IOException { @Test public void testCbrt() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s | eval f = cbrt(age) | fields f", TEST_INDEX_BANK)); - verifySchema(result, schema("f", null, "double")); - verifyDataRows(result, - rows(3.174802103936399), rows(3.3019272488946267), rows(3.0365889718756627), - rows(3.2075343299958265), rows(3.3019272488946267), rows(3.391211443014167), - rows(3.2396118012774835)); - - result = - executeQuery( - String.format( - "source=%s | eval f = cbrt(num3) | fields f", TEST_INDEX_CALCS)); + 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)),