-
Notifications
You must be signed in to change notification settings - Fork 34
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
P1 antlr grammar - update lambda & join #874
Merged
YANG-DB
merged 15 commits into
opensearch-project:p1-antlr-grammar
from
YANG-DB:p1-antlr-grammar
Nov 5, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ff222c9
update antlr grammar for (future) P1 command syntax
YANG-DB 281c641
add trendline command
YANG-DB 596c069
add expand command
YANG-DB 4439f06
add geoip command
YANG-DB b71a9ed
PPl `flatten` command (#784)
lukasz-soszynski-eliatra 950009b
Extract source table names from mv query (#854)
seankao-az faae818
Fallback to internal scheduler when index creation failed (#850)
noCharger bdb4848
New trendline ppl command (SMA only) (#833)
salyh 8f3405b
update iplocation antlr
YANG-DB c2acd9b
Merge branch 'main' into p1-antlr-grammar
YANG-DB 6b44cb3
update scala fmt style
YANG-DB b183b4a
`cidrmatch` ppl command add logical tests and docs (#865)
YANG-DB aaba489
Support Lambda and add related array functions (#864)
qianheng-aws f5f05f8
Merge branch 'main' into p1-antlr-grammar
YANG-DB bd13a64
Merge remote-tracking branch 'origin/p1-antlr-grammar' into p1-antlr-…
YANG-DB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
## Lambda Functions | ||
|
||
### `FORALL` | ||
|
||
**Description** | ||
|
||
`forall(array, lambda)` Evaluates whether a lambda predicate holds for all elements in the array. | ||
|
||
**Argument type:** ARRAY, LAMBDA | ||
|
||
**Return type:** BOOLEAN | ||
|
||
Returns `TRUE` if all elements in the array satisfy the lambda predicate, otherwise `FALSE`. | ||
|
||
Example: | ||
|
||
os> source=people | eval array = json_array(1, -1, 2), result = forall(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| false | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(1, 3, 2), result = forall(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| true | | ||
+-----------+ | ||
|
||
**Note:** The lambda expression can access the nested fields of the array elements. This applies to all lambda functions introduced in this document. | ||
|
||
Consider constructing the following array: | ||
|
||
array = [ | ||
{"a":1, "b":1}, | ||
{"a":-1, "b":2} | ||
] | ||
|
||
and perform lambda functions against the nested fields `a` or `b`. See the examples: | ||
|
||
os> source=people | eval array = json_array(json_object("a", 1, "b", 1), json_object("a" , -1, "b", 2)), result = forall(array, x -> x.a > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| false | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(json_object("a", 1, "b", 1), json_object("a" , -1, "b", 2)), result = forall(array, x -> x.b > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| true | | ||
+-----------+ | ||
|
||
### `EXISTS` | ||
|
||
**Description** | ||
|
||
`exists(array, lambda)` Evaluates whether a lambda predicate holds for one or more elements in the array. | ||
|
||
**Argument type:** ARRAY, LAMBDA | ||
|
||
**Return type:** BOOLEAN | ||
|
||
Returns `TRUE` if at least one element in the array satisfies the lambda predicate, otherwise `FALSE`. | ||
|
||
Example: | ||
|
||
os> source=people | eval array = json_array(1, -1, 2), result = exists(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| true | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(-1, -3, -2), result = exists(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| false | | ||
+-----------+ | ||
|
||
|
||
### `FILTER` | ||
|
||
**Description** | ||
|
||
`filter(array, lambda)` Filters the input array using the given lambda function. | ||
|
||
**Argument type:** ARRAY, LAMBDA | ||
|
||
**Return type:** ARRAY | ||
|
||
An ARRAY that contains all elements in the input array that satisfy the lambda predicate. | ||
|
||
Example: | ||
|
||
os> source=people | eval array = json_array(1, -1, 2), result = filter(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| [1, 2] | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(-1, -3, -2), result = filter(array, x -> x > 0) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| [] | | ||
+-----------+ | ||
|
||
### `TRANSFORM` | ||
|
||
**Description** | ||
|
||
`transform(array, lambda)` Transform elements in an array using the lambda transform function. The second argument implies the index of the element if using binary lambda function. This is similar to a `map` in functional programming. | ||
|
||
**Argument type:** ARRAY, LAMBDA | ||
|
||
**Return type:** ARRAY | ||
|
||
An ARRAY that contains the result of applying the lambda transform function to each element in the input array. | ||
|
||
Example: | ||
|
||
os> source=people | eval array = json_array(1, 2, 3), result = transform(array, x -> x + 1) | fields result | ||
fetched rows / total rows = 1/1 | ||
+--------------+ | ||
| result | | ||
+--------------+ | ||
| [2, 3, 4] | | ||
+--------------+ | ||
|
||
os> source=people | eval array = json_array(1, 2, 3), result = transform(array, (x, i) -> x + i) | fields result | ||
fetched rows / total rows = 1/1 | ||
+--------------+ | ||
| result | | ||
+--------------+ | ||
| [1, 3, 5] | | ||
+--------------+ | ||
|
||
### `REDUCE` | ||
|
||
**Description** | ||
|
||
`reduce(array, start, merge_lambda, finish_lambda)` Applies a binary merge lambda function to a start value and all elements in the array, and reduces this to a single state. The final state is converted into the final result by applying a finish lambda function. | ||
|
||
**Argument type:** ARRAY, ANY, LAMBDA, LAMBDA | ||
|
||
**Return type:** ANY | ||
|
||
The final result of applying the lambda functions to the start value and the input array. | ||
|
||
Example: | ||
|
||
os> source=people | eval array = json_array(1, 2, 3), result = reduce(array, 0, (acc, x) -> acc + x) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| 6 | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(1, 2, 3), result = reduce(array, 10, (acc, x) -> acc + x) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| 16 | | ||
+-----------+ | ||
|
||
os> source=people | eval array = json_array(1, 2, 3), result = reduce(array, 0, (acc, x) -> acc + x, acc -> acc * 10) | fields result | ||
fetched rows / total rows = 1/1 | ||
+-----------+ | ||
| result | | ||
+-----------+ | ||
| 60 | | ||
+-----------+ |
132 changes: 132 additions & 0 deletions
132
...integration/scala/org/opensearch/flint/spark/ppl/FlintSparkPPLLambdaFunctionITSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.ppl | ||
|
||
import org.apache.spark.sql.{QueryTest, Row} | ||
import org.apache.spark.sql.functions.{col, to_json} | ||
import org.apache.spark.sql.streaming.StreamTest | ||
|
||
class FlintSparkPPLLambdaFunctionITSuite | ||
extends QueryTest | ||
with LogicalPlanTestUtils | ||
with FlintPPLSuite | ||
with StreamTest { | ||
|
||
/** Test table and index name */ | ||
private val testTable = "spark_catalog.default.flint_ppl_test" | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
// Create test table | ||
createNullableJsonContentTable(testTable) | ||
} | ||
|
||
protected override def afterEach(): Unit = { | ||
super.afterEach() | ||
// Stop all streaming jobs if any | ||
spark.streams.active.foreach { job => | ||
job.stop() | ||
job.awaitTermination() | ||
} | ||
} | ||
|
||
test("test forall()") { | ||
val frame = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,0,-1,1.1,-0.11), result = forall(array, x -> x > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(false)), frame) | ||
|
||
val frame2 = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,0,-1,1.1,-0.11), result = forall(array, x -> x > -10) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(true)), frame2) | ||
|
||
val frame3 = sql(s""" | ||
| source = $testTable | eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = forall(array, x -> x.a > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(false)), frame3) | ||
|
||
val frame4 = sql(s""" | ||
| source = $testTable | eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = exists(array, x -> x.b < 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(true)), frame4) | ||
} | ||
|
||
test("test exists()") { | ||
val frame = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,0,-1,1.1,-0.11), result = exists(array, x -> x > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(true)), frame) | ||
|
||
val frame2 = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,0,-1,1.1,-0.11), result = exists(array, x -> x > 10) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(false)), frame2) | ||
|
||
val frame3 = sql(s""" | ||
| source = $testTable | eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = exists(array, x -> x.a > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(true)), frame3) | ||
|
||
val frame4 = sql(s""" | ||
| source = $testTable | eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = exists(array, x -> x.b > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(false)), frame4) | ||
|
||
} | ||
|
||
test("test filter()") { | ||
val frame = sql(s""" | ||
| source = $testTable| eval array = json_array(1,2,0,-1,1.1,-0.11), result = filter(array, x -> x > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(Seq(1, 2, 1.1))), frame) | ||
|
||
val frame2 = sql(s""" | ||
| source = $testTable| eval array = json_array(1,2,0,-1,1.1,-0.11), result = filter(array, x -> x > 10) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(Seq())), frame2) | ||
|
||
val frame3 = sql(s""" | ||
| source = $testTable| eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = filter(array, x -> x.a > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
|
||
assertSameRows(Seq(Row("""[{"a":1,"b":-1}]""")), frame3.select(to_json(col("result")))) | ||
|
||
val frame4 = sql(s""" | ||
| source = $testTable| eval array = json_array(json_object("a",1,"b",-1),json_object("a",-1,"b",-1)), result = filter(array, x -> x.b > 0) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row("""[]""")), frame4.select(to_json(col("result")))) | ||
} | ||
|
||
test("test transform()") { | ||
val frame = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,3), result = transform(array, x -> x + 1) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(Seq(2, 3, 4))), frame) | ||
|
||
val frame2 = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,3), result = transform(array, (x, y) -> x + y) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(Seq(1, 3, 5))), frame2) | ||
} | ||
|
||
test("test reduce()") { | ||
val frame = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,3), result = reduce(array, 0, (acc, x) -> acc + x) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(6)), frame) | ||
|
||
val frame2 = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,3), result = reduce(array, 1, (acc, x) -> acc + x) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(7)), frame2) | ||
|
||
val frame3 = sql(s""" | ||
| source = $testTable | eval array = json_array(1,2,3), result = reduce(array, 0, (acc, x) -> acc + x, acc -> acc * 10) | head 1 | fields result | ||
| """.stripMargin) | ||
assertSameRows(Seq(Row(60)), frame3) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this commented out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not supported yet by the lambda command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented
EXISTS
out because the it is an existed keyword. Just add it here as a placeholder. I think this line can be deleted to reduce confusion.