-
Notifications
You must be signed in to change notification settings - Fork 156
add json functions #3559
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
base: main
Are you sure you want to change the base?
add json functions #3559
Conversation
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Why we didn't leverage the Calcite builtin JSON functions, for instance |
I have listed the functions above and try to reuse calcite code |
Signed-off-by: xinyual <[email protected]>
import org.opensearch.sql.ppl.JsonFunctionsIT; | ||
|
||
@Ignore |
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 ignore IT?
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.
Previous V2 json return the undefined type while we return a string instead.
docs/user/ppl/functions/json.rst
Outdated
|
||
Usage: `json_array_length(value)` parse the string to json array and return size, if can't be parsed, return null | ||
|
||
Argument type: value: STRING |
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.
Argument type: A JSON array
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.
Already update
docs/user/ppl/functions/json.rst
Outdated
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_array_length(value)` parse the string to json array and return size, if can't be parsed, return null |
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.
NULL is returned in case of any other valid JSON string, NULL or an invalid JSON.
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.
Already update
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.
Update doc,
json_array_length(value)
parse the string to json array and return size,, null is returned in case of any other valid JSON string, null or an invalid JSON.
| JSON_KEYS | ||
| JSON_SET | ||
| JSON_DELETE | ||
| JSON_APPEND | ||
| JSON_EXTEND |
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.
add docs for these functions.
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.
Already update it.
docs/user/ppl/functions/json.rst
Outdated
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_extract(json_string, path1, path2, ...)` it first transfer json_string to json, then extract value using paths. If only one path, return the value, otherwise, return the list of values. If one path cannot find value, return null as the result for this path. The path use "{<index>}" to represent index for array, "{}" means "{*}". |
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.
it first transfer json_string to json, then extract value using paths. If only one path, return the value, otherwise, return the list of values.
Extracts values using the specified JSON paths. If only one path is provided, it returns a single value. If multiple paths are provided, it returns a JSON Array
in the order of the paths.
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.
Already update it.
docs/user/ppl/functions/json.rst
Outdated
|
||
Example:: | ||
|
||
> source=json_test | eval extract = json_extract('{\"a\": [{\"b\": 1}, {\"b\": 2}]}', 'a{}.b') | head 1 | fields extract |
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.
\"
escaple is not necessary?
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.
Yes. I update it.
docs/user/ppl/functions/json.rst
Outdated
+---------------------------------+ | ||
| test_json_array | | ||
|---------------------------------| | ||
| [[1,2],[{\"b\": 1}, {\"b\": 2}]]| |
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.
[[1,2],[{\"b\": 1}, {\"b\": 2}]]
is not a valid JSON array.
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.
I update it. Please check the doc.
Signed-off-by: xinyual <[email protected]>
docs/user/ppl/functions/json.rst
Outdated
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_array_length(value)` parse the string to json array and return size, if can't be parsed, return null |
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.
Update doc,
json_array_length(value)
parse the string to json array and return size,, null is returned in case of any other valid JSON string, null or an invalid JSON.
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_extract(json_string, path1, path2, ...)` Extracts values using the specified JSON paths. If only one path is provided, it returns a single value. If multiple paths are provided, it returns a JSON Array in the order of the paths. If one path cannot find value, return null as the result for this path. The path use "{<index>}" to represent index for array, "{}" means "{*}". |
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.
Is JSON Path standard? add a section to explain it?
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 a standard json path. I would a section in this doc to describe this.
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.
Already add the section at the top of doc. Please review it.
|
||
Example:: | ||
|
||
> source=json_test | eval delete = json_delete('{"a": [{"b": 1}, {"b": 2}]}', 'a{0}.b') | head 1 | fields delete |
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.
json_delete('{"a": [{"b": 1}, {"b": 2}]}', 'a{0}.b') should delete is {"b": 1}? and keep {"b": 2}
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.
Yes. Write wrong result, already fix it.
| {"a": [{"b": 3}]} | | ||
+-------------------------+ | ||
|
||
> source=json_test | eval jsonSet = json_set('{"a": [{"b": 1}, {"b": 2}]}', 'a{0}.b', 3, 'a{1}.b', 4) | head 1 | fields jsonSet |
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.
'a{1}.b', 4). should change {"b":2} to {"b":4}
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.
Write wrong result, already fix it.
| {"a": [{"b": 1}, 3]} | | ||
+-------------------------+ | ||
|
||
> source=json_test | eval jsonAppend = json_append('{"a": [{"b": 1}, {"b": 2}]}', 'a{0}.b', 3, 'a{1}.b', 4) | head 1 | fields jsonAppend |
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.
a{0}.b is not an array, should skip append?
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.
Yes. Write wrong query, already fix it.
| {"a": [{"b": 1}, 3]} | | ||
+-------------------------+ | ||
|
||
> source=json_test | eval jsonExtend = json_extend('{"a": [{"b": 1}, {"b": 2}]}', 'a{0}.b', 3, 'a{1}.b', 4) | head 1 | fields jsonExtend |
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.
a{0}.b is not an array, should skip append?
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.
Yes. Write wrong query, already fix it.
|
||
Example:: | ||
|
||
> source=json_test | eval jsonKeys = json_keys('{"a": 1, "b": 2}') | head 1 | fields jsonKeys |
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.
what if json object is nested {"a": {"b", 1}, "b":1}
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.
Only the top level key will be returned. Still ["a", "b"] in this case. I update doc and add this case.
docs/user/ppl/functions/json.rst
Outdated
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_keys(json_string)` Return the key list of the json_string as a string if it's an object json string. Otherwise, return null. |
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.
"Return the key list of the json_string as a string if it's an object json string." -> Return the key list of the Json object as a Json array.
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.
Done.
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java
Outdated
Show resolved
Hide resolved
---------- | ||
|
||
Description | ||
>>>>>>>>>>> |
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.
please add Version
and Limitation
section for all new added functions.
Limitation: Only works when plugins.calcite.enabled=true
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.
Done.
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
CI failures |
Force push to revert useless change and resolve DCO problems. |
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Description
Here we add json related functions to align with spark
Here is the argument and description of functions
Related Issues
Resolves #[Issue number to be closed when this PR is merged]
#3565
Check List
--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.