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

Enable concat() string function to support multiple string arguments #1279

Conversation

margarit-h
Copy link
Contributor

@margarit-h margarit-h commented Jan 13, 2023

  • Refactor concat() to support multiple string arguments

Signed-off-by: Margarit Hakobyan [email protected]

Description

These changes enable concat() string function to accept more than two arguments.
Usage: CONCAT(str1, str2, ...., str_n) adds two or more strings together. Expects 1-9 arguments. If any of the expressions is a NULL value, it returns NULL.

Argument type: STRING, STRING, ...., STRING

Return type: STRING

Example::

os> SELECT CONCAT('hello ', 'whole ', 'world', '!'), CONCAT('hello', 'world'), CONCAT('hello', null)
fetched rows / total rows = 1/1
+--------------------------------------------+----------------------------+-------------------------+
| CONCAT('hello ', 'whole ', 'world', '!')   | CONCAT('hello', 'world')   | CONCAT('hello', null)   |
|--------------------------------------------+----------------------------+-------------------------|
| hello whole world!                         | helloworld                 | null                    |
+--------------------------------------------+----------------------------+-------------------------+

If more than 9 arguments are passed, error will be returned as follows:

opensearchsql> SELECT CONCAT('arg1 ', 'arg2 ', 'arg3 ', 'arg4  ', 'arg5  ', 'arg6 ', 'arg7 ', 'arg8 ', 'arg9 ', 'arg10  ');                           
{'reason': 'Invalid SQL query', 'details': 'concat function expected 1-9 arguments, but got 10', 'type': 'ExpressionEvaluationException'}

If no arguments are passed, error will be returned as follows:

opensearchsql> SELECT CONCAT();                                                                                                                       
{'reason': 'Invalid SQL query', 'details': 'concat function expected 1-9 arguments, but got 0', 'type': 'ExpressionEvaluationException'}

Issues Resolved

#1053

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • 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.

@margarit-h margarit-h requested a review from a team as a code owner January 13, 2023 17:06
@Yury-Fridlyand Yury-Fridlyand added enhancement New feature or request backport 2.x labels Jan 13, 2023
#200)

* Refactor concat() to support multiple string arguments

Signed-off-by: Margarit Hakobyan <[email protected]>
@margarit-h margarit-h force-pushed the integ-concat-support-many-args branch from 26869fa to 1f924f5 Compare January 13, 2023 17:21
@codecov-commenter
Copy link

codecov-commenter commented Jan 13, 2023

Codecov Report

Merging #1279 (0654fbb) into main (c6a59f7) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##               main    #1279      +/-   ##
============================================
- Coverage     98.35%   98.35%   -0.01%     
- Complexity     3609     3611       +2     
============================================
  Files           344      343       -1     
  Lines          8946     8932      -14     
  Branches        569      576       +7     
============================================
- Hits           8799     8785      -14     
  Misses          142      142              
  Partials          5        5              
Flag Coverage Δ
sql-engine 98.35% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...expression/function/BuiltinFunctionRepository.java 100.00% <100.00%> (ø)
...l/expression/function/DefaultFunctionResolver.java 100.00% <100.00%> (ø)
...rch/sql/expression/function/FunctionSignature.java 100.00% <100.00%> (ø)
...g/opensearch/sql/expression/text/TextFunction.java 100.00% <100.00%> (ø)
...ript/aggregation/dsl/AggregationBuilderHelper.java 100.00% <0.00%> (ø)
...l/filesystem/streaming/FileSystemStreamSource.java

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@dai-chen
Copy link
Collaborator

Relate to issue #1053 ?

YANG-DB
YANG-DB previously approved these changes Jan 13, 2023
Signed-off-by: Margarit Hakobyan <[email protected]>
return (args) -> {
if (Arrays.stream(args).anyMatch(ExprValue::isMissing)) {
return ExprValueUtils.missingValue();
} else if (Arrays.stream(args).anyMatch(ExprValue::isNull)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

else not needed here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in bed81bf

return ExprValueUtils.missingValue();
} else if (Arrays.stream(args).anyMatch(ExprValue::isNull)) {
return ExprValueUtils.nullValue();
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

else not needed here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in bed81bf

Signed-off-by: Margarit Hakobyan <[email protected]>
@Yury-Fridlyand
Copy link
Collaborator

We need limit amount of arguments, because it may cause DOS or security breach.
Just tested a concat query with 10k arguments - it works fine.

Signed-off-by: Margarit Hakobyan <[email protected]>
Signed-off-by: Margarit Hakobyan <[email protected]>
@dai-chen
Copy link
Collaborator

I followed the idea of using our existing ARRAY type. Some quick test show it worked. Please let me know if it can work for your PR and anything I missed. Thanks!

Changes for tests

Commit: dai-chen@2f4b960

  1. Let ARRAY match all unresolved arguments
  2. Avoid implicit cast handle vararg function signature
  3. Add simple concat function impl

Test

$ curl localhost:9200/_plugins/_sql/ -X POST -H 'Content-Type: application/json' -d'{"query": "select concat(1, 2, 3)" }'
{
  "schema": [
    {
      "name": "concat(1, 2, 3)",
      "type": "keyword"
    }
  ],
  "datarows": [
    [
      "123"
    ]
  ],
  "total": 1,
  "size": 1,
  "status": 200
}

$ curl localhost:9200/_plugins/_sql/ -X POST -H 'Content-Type: application/json' -d'{"query": "select concat('\''hello'\'', '\''world'\'', '\''test'\'', '\''test'\'')" }'
{
  "schema": [
    {
      "name": "concat('hello', 'world', 'test', 'test')",
      "type": "keyword"
    }
  ],
  "datarows": [
    [
      "helloworldtesttest"
    ]
  ],
  "total": 1,
  "size": 1,
  "status": 200
}

Signed-off-by: Margarit Hakobyan <[email protected]>
Signed-off-by: Margarit Hakobyan <[email protected]>
Comment on lines +160 to +162
return define(concatFuncName, funcName ->
Pair.of(
new FunctionSignature(concatFuncName, Collections.singletonList(ARRAY)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks @dai-chen for the idea!

docs/user/dql/functions.rst Outdated Show resolved Hide resolved
docs/user/ppl/functions/string.rst Outdated Show resolved Hide resolved

Return type: STRING

Example::

os> source=people | eval `CONCAT('hello', 'world')` = CONCAT('hello', 'world') | fields `CONCAT('hello', 'world')`
os> source=people | eval `CONCAT('hello', 'world')` = CONCAT('hello', 'world'), `CONCAT('hello ', 'whole ', 'world', '!')` = CONCAT('hello ', 'whole ', 'world', '!') | fields `CONCAT('hello', 'world')`, `CONCAT('hello ', 'whole ', 'world', '!')`
Copy link
Collaborator

Choose a reason for hiding this comment

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

a NULL test for PPL?

Signed-off-by: Margarit Hakobyan <[email protected]>
@dai-chen dai-chen linked an issue Jan 25, 2023 that may be closed by this pull request
Copy link
Collaborator

@dai-chen dai-chen left a comment

Choose a reason for hiding this comment

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

Thanks for the changes!

@Yury-Fridlyand Yury-Fridlyand merged commit 45fc371 into opensearch-project:main Jan 27, 2023
@Yury-Fridlyand Yury-Fridlyand deleted the integ-concat-support-many-args branch January 27, 2023 18:09
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jan 27, 2023
#1279)

* Enable `concat()` string function to support multiple string arguments (#200)

Signed-off-by: Margarit Hakobyan <[email protected]>
(cherry picked from commit 45fc371)
dai-chen pushed a commit that referenced this pull request Jan 27, 2023
#1279) (#1297)

* Enable `concat()` string function to support multiple string arguments (#200)

Signed-off-by: Margarit Hakobyan <[email protected]>
(cherry picked from commit 45fc371)

Co-authored-by: Margarit Hakobyan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] CONCAT on more than two strings
7 participants