Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cbrt function to the PPL #171

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
24 changes: 24 additions & 0 deletions docs/user/ppl/functions/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,27 @@ Example::
| 2.0 | 2.1 |
+-----------+--------------+

CBRT

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the functions in here might be organized in Alphabetical order? It might be good to move this function documentation up.

----

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)`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use source=people, do we just get a single row of results?

Copy link
Author

@margarit-h margarit-h Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we get 12 rows, locations seems to be the shortest.
Here is the people:
opensearchsql> source=people | 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 = 12/12
+-----------+---------------+-------------+
| CBRT(8) | CBRT(9.261) | CBRT(-27) |
|-----------+---------------+-------------|
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
| 2.0 | 2.1 | -3.0 |
+-----------+---------------+-------------+

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 |
+-----------+---------------+-------------+
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ DC: 'DC';

// BASIC FUNCTIONS
ABS: 'ABS';
CBRT: 'CBRT';
CEIL: 'CEIL';
CEILING: 'CEILING';
CONV: 'CONV';
Expand Down
2 changes: 1 addition & 1 deletion ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Expand Down