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

Support Lambda and add related array functions #864

Merged
merged 13 commits into from
Nov 5, 2024

Conversation

qianheng-aws
Copy link
Contributor

@qianheng-aws qianheng-aws commented Nov 1, 2024

Description

  • Support parsing lambda expression.

  • Add below array related function which need a lambda function as its argument:

    • json_array_any_match() -> exists(ARRAY, Lambda)
    • json_array_all_match() -> forAll(ARRAY, Lambda)
    • json_array_filter() -> filter(ARRAY, Lambda)
    • json_array_map() -> transform(ARRAY, Lambda)
    • json_array_reduce() -> reduce(ARRAY, start, Lambda, Lambda)

Examples:
Exists:

... | eval a = json_array(1, -1, 2), b = exists(a, x -> x > 0) | head 1 | fields b

# return True

ForAll:

... | eval a = json_array(1, -1, 2), b = forall(a, x -> x > 0) | head 1 | fields b

# return False

Filter:

... | eval a = json_array(1, -1, 2), b = filter(a, x -> x > 0) | head 1 | fields b

# return `[1, 2]`

Transform:

... | eval a = json_array(1, 2, 3), b = transform(a, x -> x + 1) | head 1 | fields b

# return `[2, 3, 4]`

... | eval a = json_array(1, 2, 3), b = transform(a, (x + i) -> x + i) | head 1 | fields b

# return `[1, 3, 5]`

Reduce:

... | eval a = json_array(1, 2, 3), b = reduce(a, 0, (acc, x) -> acc + x) | head 1 | fields b

# return `6`

... | eval a = json_array(1, 2, 3), b = reduce(a, 10, (acc, x) -> acc + x) | head 1 | fields b

# return `16`

... | eval a = json_array(1, 2, 3), b = reduce(a, 0, (acc, x) -> acc + x, acc -> acc * 10) | head 1 | fields b

# return `60`

Related Issues

Partial resolve #863

Check List

  • Updated documentation (docs/ppl-lang/README.md)
  • Implemented unit tests
  • Implemented tests for combination with other commands
  • New added source code should include a copyright header
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Heng Qian <qianheng@amazon.com>
val logPlan =
planTransformer.visit(
plan(pplParser,
"""source=t | eval a = json_array(1, 2, 3), b = json_array_all_match(a, x -> x > 0)""".stripMargin),
Copy link
Member

Choose a reason for hiding this comment

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

Need a test for a = json_array(json_object(..), json_object(..), json_object(..)).
For example:

a = [
  {id:8, uid:1},
  {id:7, uid:9},
  {id:7, uid:1},
  ...
]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in IT

Copy link
Member

@LantaoJin LantaoJin Nov 4, 2024

Choose a reason for hiding this comment

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

a = [
  {id:8, uid:1},
  {id:7, uid:9},
  {id:7, uid:1},
  ...
]

Could you add this json structure example to user doc ppl-lambda.md? it could help user to understand the case.

(PS: please keep review conversations open, it could help reviewers to remember the context and join conversation)

Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
@@ -179,6 +185,18 @@ public interface BuiltinFunctionTransformer {
args -> {
return ToUTCTimestamp$.MODULE$.apply(CurrentTimestamp$.MODULE$.apply(), CurrentTimeZone$.MODULE$.apply());
})
.put(
JSON_ARRAY_ALL_MATCH,
args -> ArrayForAll$.MODULE$.apply(args.get(0), args.get(1))
Copy link
Member

Choose a reason for hiding this comment

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

Can we accept both ARRAY and JSON ARRAY STRING?

Copy link
Member

Choose a reason for hiding this comment

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

Em, I think we can keep this lambda functions and remove the JSON_ARRAY_ prefix since they seem common functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to use the original function name as spark

@YANG-DB YANG-DB added Lang:PPL Pipe Processing Language support 0.6 labels Nov 1, 2024
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
…tion

# Conflicts:
#	ppl-spark-integration/src/main/java/org/opensearch/sql/ppl/CatalystExpressionVisitor.java
@qianheng-aws qianheng-aws marked this pull request as ready for review November 4, 2024 07:23
@qianheng-aws qianheng-aws changed the title json function enhancement Support Lambda and add related array functions Nov 4, 2024
@LantaoJin
Copy link
Member

LantaoJin commented Nov 4, 2024

Thanks @qianheng-aws , I think we could separate the lambda functions and json functions (from tests to documentations). Another thought is will we add json_array_xx(jsonString, lambdaFunction) for the original request?

@LantaoJin
Copy link
Member

LantaoJin commented Nov 4, 2024

Another thought is will we add json_array_xx(jsonString, lambdaFunction) for the original request?

Discussed offline. Let's enhance the json_array() instead of adding json_array_xx for now. As an example:

| eval a = json_array("[1, -1, 2]"), b = filter(a, x -> x > 0)

@qianheng-aws qianheng-aws mentioned this pull request Nov 4, 2024
5 tasks
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Copy link
Member

@LantaoJin LantaoJin left a comment

Choose a reason for hiding this comment

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

@YANG-DB please review this PR and its description, the function names are not exactly same with the original ones in original issue. please double confirm that's fine to you.

Signed-off-by: Heng Qian <qianheng@amazon.com>
@LantaoJin LantaoJin merged commit aaba489 into opensearch-project:main Nov 5, 2024
4 checks passed
@LantaoJin
Copy link
Member

LantaoJin commented Nov 5, 2024

To avoid conflicts with #870, I merged this first. @YANG-DB please provide your review and feedback, we still could discuss and address them later.

YANG-DB added a commit that referenced this pull request Nov 5, 2024
* update antlr grammar for (future) P1 command syntax

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* add trendline command

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* add expand command

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* add geoip command

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* PPl `flatten` command (#784)

* The flatten command implemented

Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>

* The flatten command integration tests were extended with additional checks for logical plans.

Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>

* flatten, added more tests related to plan translation and integration tests

Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>

* Flatten command added to command names list.

Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>

---------

Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>

* Extract source table names from mv query (#854)

* add sourceTables to MV index metadata properties

Signed-off-by: Sean Kao <seankao@amazon.com>

* parse source tables from mv query

Signed-off-by: Sean Kao <seankao@amazon.com>

* test cases for parse source tables from mv query

Signed-off-by: Sean Kao <seankao@amazon.com>

* use constant for metadata cache version

Signed-off-by: Sean Kao <seankao@amazon.com>

* write source tables to metadata cache

Signed-off-by: Sean Kao <seankao@amazon.com>

* address comment

Signed-off-by: Sean Kao <seankao@amazon.com>

* generate source tables for old mv without new prop

Signed-off-by: Sean Kao <seankao@amazon.com>

* syntax fix

Signed-off-by: Sean Kao <seankao@amazon.com>

---------

Signed-off-by: Sean Kao <seankao@amazon.com>

* Fallback to internal scheduler when index creation failed (#850)

* Fallback to internal scheduler when index creation failed

Signed-off-by: Louis Chu <clingzhi@amazon.com>

* Fix IT

Signed-off-by: Louis Chu <clingzhi@amazon.com>

* Fix IOException

Signed-off-by: Louis Chu <clingzhi@amazon.com>

---------

Signed-off-by: Louis Chu <clingzhi@amazon.com>

* New trendline ppl command (SMA only) (#833)

* WIP trendline command

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* wip

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* trendline supports sorting

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* run scalafmtAll

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* return null when there are too few data points

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* sbt scalafmtAll

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* Remove WMA references

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* trendline - sortByField as Optional<Field>

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* introduce TrendlineStrategy

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* keywordsCanBeId -> replace SMA with trendlineType

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* handle trendline alias as qualifiedName instead of fieldExpression

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* Add docs

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Make alias optional

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Adapt tests for optional alias

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Adden logical plan unittests

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Add missing license headers

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Fix docs

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* numberOfDataPoints must be 1 or greater

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Rename TrendlineStrategy to  TrendlineCatalystUtils

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Validate TrendlineType early and pass around enum type

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Add trendline chaining test

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Fix compile errors

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Fix imports

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

* Fix imports

Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>

---------

Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>
Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>
Co-authored-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>

* update iplocation antlr

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* update scala fmt style

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* `cidrmatch` ppl command add logical tests and docs (#865)

* update logical tests and docs

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* update scala fmt style

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* fix type error

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

---------

Signed-off-by: YANGDB <yang.db.dev@gmail.com>

* Support Lambda and add related array functions (#864)

* json function enhancement

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add JavaToScalaTransformer

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Apply scalafmtAll

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Address comments

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add IT and change to use the same function name as spark

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Address comments

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add document and separate lambda functions from json functions

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add lambda functions transform and reduce

Signed-off-by: Heng Qian <qianheng@amazon.com>

* polish lambda function document

Signed-off-by: Heng Qian <qianheng@amazon.com>

* polish lambda function document

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Minor fix

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Minor change to polish the documents

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: YANGDB <yang.db.dev@gmail.com>
Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>
Signed-off-by: Sean Kao <seankao@amazon.com>
Signed-off-by: Louis Chu <clingzhi@amazon.com>
Signed-off-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>
Signed-off-by: Hendrik Saly <hendrik.saly@eliatra.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Co-authored-by: lukasz-soszynski-eliatra <110241464+lukasz-soszynski-eliatra@users.noreply.github.com>
Co-authored-by: Sean Kao <seankao@amazon.com>
Co-authored-by: Louis Chu <clingzhi@amazon.com>
Co-authored-by: Hendrik Saly <hendrik.saly@eliatra.com>
Co-authored-by: Kacper Trochimiak <kacper.trochimiak@eliatra.com>
Co-authored-by: qianheng <qianheng@amazon.com>
kenrickyap pushed a commit to Bit-Quill/opensearch-spark that referenced this pull request Dec 11, 2024
* json function enhancement

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add JavaToScalaTransformer

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Apply scalafmtAll

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Address comments

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add IT and change to use the same function name as spark

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Address comments

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add document and separate lambda functions from json functions

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add lambda functions transform and reduce

Signed-off-by: Heng Qian <qianheng@amazon.com>

* polish lambda function document

Signed-off-by: Heng Qian <qianheng@amazon.com>

* polish lambda function document

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Minor fix

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Minor change to polish the documents

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: Heng Qian <qianheng@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.6 Lang:PPL Pipe Processing Language support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Enhance JSON array functions
3 participants