-
Notifications
You must be signed in to change notification settings - Fork 144
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
PPL: Add json_extract
function
#3262
base: main
Are you sure you want to change the base?
PPL: Add json_extract
function
#3262
Conversation
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Co-authored-by: Andrew Carbonetto <[email protected]> Signed-off-by: kenrickyap <[email protected]>
….java Co-authored-by: Andrew Carbonetto <[email protected]> Signed-off-by: kenrickyap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
…nsearch-project-sql into feature/json-valid Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
would be ideal to merge:
|
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.
Reviewed to 0f0b8d4a26fa47d2f9ae190d9050d9d87b2ab37c
.
core/src/main/java/org/opensearch/sql/expression/json/JsonFunctions.java
Show resolved
Hide resolved
Signed-off-by: 14yapkc1 <[email protected]>
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.
Reviewed to f030057
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Show resolved
Hide resolved
Signed-off-by: 14yapkc1 <[email protected]>
Signed-off-by: 14yapkc1 <[email protected]> # Conflicts: # core/src/main/java/org/opensearch/sql/expression/DSL.java # core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java # core/src/main/java/org/opensearch/sql/expression/json/JsonFunctions.java # core/src/main/java/org/opensearch/sql/utils/JsonUtils.java # core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java # core/src/test/java/org/opensearch/sql/expression/operator/convert/TypeCastOperatorTest.java # docs/user/ppl/functions/json.rst # integ-test/src/test/java/org/opensearch/sql/ppl/JsonFunctionsIT.java # ppl/src/main/antlr/OpenSearchPPLLexer.g4 # ppl/src/main/antlr/OpenSearchPPLParser.g4
Signed-off-by: 14yapkc1 <[email protected]> Signed-off-by: Kenrick Yap <[email protected]>
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.
Reviewed to 2b08007
.
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
…pensearch-project-sql into feature/json-extract
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
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.
Reviewed to e57fa21
.
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
Outdated
Show resolved
Hide resolved
…tionsTest.java Co-authored-by: Taylor Curran <[email protected]> Signed-off-by: kenrickyap <[email protected]>
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.
Reviewed to dfe5d3a
. Looks good to me.
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.
Reviewed to dfe5d3a
. Looks good to me.
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.
can u plz add license header ?
thanks
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.
added
Signed-off-by: Kenrick Yap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
dfe5d3a
to
75e9cc3
Compare
Signed-off-by: kenrickyap <[email protected]>
Signed-off-by: Kenrick Yap <[email protected]>
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 few comments to address
private DefaultFunctionResolver jsonExtract() { | ||
return define( | ||
BuiltinFunctionName.JSON_EXTRACT.getName(), | ||
impl(JsonUtils::extractJson, UNDEFINED, STRING, 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.
this means we only accept a single path
Splunk function takes a variable length of paths: https://docs.splunk.com/Documentation/SCS/current/SearchReference/JSONFunctions#json_extract.28.26lt.3Bjson.26gt.3B.2C_.26lt.3Bpaths.26gt.3B.29
try { | ||
Object results = JsonPath.parse(jsonString).read(jsonPath); | ||
return ExprValueUtils.fromObjectValue(results); | ||
} catch (PathNotFoundException e) { |
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.
} catch (PathNotFoundException e) { | |
} catch (PathNotFoundException ignored) { |
return ExprValueUtils.fromObjectValue(results); | ||
} catch (PathNotFoundException e) { | ||
return LITERAL_NULL; | ||
} catch (InvalidPathException e) { |
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 don't reuse variable names:
} catch (InvalidPathException e) { | |
} catch (InvalidPathException invalidPathException) { |
} catch (InvalidPathException e) { | ||
final String errorFormat = "JSON path '%s' is not valid. Error details: %s"; | ||
throw new SemanticCheckException(String.format(errorFormat, path, e.getMessage()), e); | ||
} catch (InvalidJsonException e) { |
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 don't reuse variable names:
} catch (InvalidJsonException e) { | |
} catch (InvalidJsonException invalidJsonException) { |
@Test | ||
void json_extract_search() { | ||
ExprValue expected = new ExprIntegerValue(1); | ||
execute_extract_json(expected, "{\"a\":1}", "$.a"); |
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.
let's add a test for each scalar type: boolean, double, string, integer (already have) and null (not exactly the same case as the test case below).
|
||
@Test | ||
void json_extract_search_arrays_out_of_bound() { | ||
execute_extract_json(LITERAL_NULL, "{\"a\":[1,2,3]}", "$.a[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.
move to `json_extract_returns_null
// null json | ||
assertEquals( | ||
LITERAL_NULL, | ||
DSL.jsonExtract(DSL.literal(LITERAL_NULL), DSL.literal(new ExprStringValue("$.a"))) |
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.
DSL.jsonExtract(DSL.literal(LITERAL_NULL), DSL.literal(new ExprStringValue("$.a"))) | |
DSL.jsonExtract(DSL.literal(LITERAL_NULL), DSL.literal("$.a")) |
// missing json | ||
assertEquals( | ||
LITERAL_MISSING, | ||
DSL.jsonExtract(DSL.literal(LITERAL_MISSING), DSL.literal(new ExprStringValue("$.a"))) |
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.
DSL.jsonExtract(DSL.literal(LITERAL_MISSING), DSL.literal(new ExprStringValue("$.a"))) | |
DSL.jsonExtract(DSL.literal(LITERAL_MISSING), DSL.literal("$.a")) |
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.
same for rest of file...
Description
Implements json_extract function that would be useful to extract specific objects and scalars from an existing json_object.
Related Issues
Resolves #3211
Follow up issue for #3027
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.