-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add JsonStringArrayExtractScalar function #22379
Conversation
a8abebd
to
f314662
Compare
core/trino-main/src/main/java/io/trino/metadata/SystemFunctionBundle.java
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
continue; | ||
} | ||
JsonParser jsonParser = parser.readValueAsTree().traverse(); | ||
jsonParser.nextToken(); |
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.
shouldn't you check token is not 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.
all requierd checks are within extractor.
...rino-main/src/main/java/io/trino/sql/ir/optimizer/rule/SpecializeTransformWithJsonParse.java
Outdated
Show resolved
Hide resolved
...rino-main/src/main/java/io/trino/sql/ir/optimizer/rule/SpecializeTransformWithJsonParse.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
append(path.getScalarExtractor().extract(jsonParser), elementBuilder); | ||
} | ||
catch (JsonParseException e) { | ||
append(null, elementBuilder); |
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.
How does that comply with javadoc: https://trino.io/docs/current/functions/json.html#json_extract_scalar
What happens if malformed value is used? Maybe javadoc should be changed?
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 depends, if the malformed value is json, is scalar or array/object.
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
...rino-main/src/main/java/io/trino/sql/ir/optimizer/rule/SpecializeTransformWithJsonParse.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
66e1707
to
935b149
Compare
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.
lgtm % comments
core/trino-main/src/main/java/io/trino/operator/scalar/JsonStringArrayExtractScalar.java
Show resolved
Hide resolved
void testEmptyArrayExtract() | ||
{ | ||
Result result = extract("[]", "$.name"); | ||
assertThat(result.value()).isEqualTo(List.of()); |
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.
isEqualTo(List.of());
-> isEmpty()
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.
result.value
is Object
and it will require to cast -> assertThat((List<?>) result.value()).isEmpty();
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java
Outdated
Show resolved
Hide resolved
504f718
to
6612645
Compare
01cd6f3
to
d794dd8
Compare
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java
Outdated
Show resolved
Hide resolved
f978d51
to
95923ec
Compare
core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/sql/query/QueryAssertions.java
Outdated
Show resolved
Hide resolved
Optimzie common case of transform(cast(json_parse(varchar_col) as array<json>), json -> json_extract_scalar(json, jsonPath)) case to be single and more performant call of JsonStringArrayExtract.
@Test | ||
void testEmptySliceExtract() | ||
{ | ||
assertTrinoExceptionThrownBy(() -> assertions.expression("transform(cast(json_parse(a) as array<json>), x -> json_extract_scalar(\"x\", '$.name'))") |
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.
Angle brackets for parametric types are deprecated. Use parenthesis instead: array(json)
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Show resolved
Hide resolved
core/trino-main/src/test/java/io/trino/operator/scalar/TestJsonStringArrayExtractScalar.java
Show resolved
Hide resolved
return new Result(full.type(), full.value, full.expression); | ||
} | ||
|
||
private static Optional<Expression> extractExpressionWithBindings(Plan plan) |
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's the purpose of this method and the one below?
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.
To get final expression how value was produced.
@@ -883,7 +906,7 @@ public ExpressionAssert assertThat() | |||
.withRepresentation(ExpressionAssert.TYPE_RENDERER); | |||
} | |||
|
|||
public record Result(Type type, Object value) {} | |||
public record Result(Type type, Object value, Optional<Expression> expression) {} |
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 is the optional expression?
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.
Extracted expression from plan to check how value was produced. For example, if transformation was involved or not.
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.
When would it be empty?
Also, this change to QueryAssertions doesn't follow the intent and spirit of that API and abstraction. It should be changed to work with AssertJ like this:
assertThat(assertions.expression("..."))
.matches(<ir expression>)
Description
Optimize common case of
transform(cast(json_parse), json_extract_scalar)
to be single and more performant call ofJsonStringArrayExtractScalar
.Example query:
With change:
Without:
Additional context and related issues
Release notes
( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(X) Release notes are required, with the following suggested text: