-
Notifications
You must be signed in to change notification settings - Fork 216
Add JSON Support to V2 Engine (#217) #1464
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
core/src/main/java/org/opensearch/sql/analysis/JsonSupportVisitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.analysis; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
| import org.opensearch.sql.ast.Node; | ||
| import org.opensearch.sql.ast.expression.Alias; | ||
| import org.opensearch.sql.ast.expression.Cast; | ||
| import org.opensearch.sql.ast.expression.Function; | ||
| import org.opensearch.sql.ast.expression.Literal; | ||
| import org.opensearch.sql.ast.tree.Aggregation; | ||
| import org.opensearch.sql.ast.tree.Project; | ||
|
|
||
| /** | ||
| * This visitor's sole purpose is to throw UnsupportedOperationExceptions | ||
| * for unsupported features in the new engine when JSON format is specified. | ||
| * Unsupported features in V2 are ones the produce results that differ from | ||
| * legacy results. | ||
| */ | ||
| public class JsonSupportVisitor extends AbstractNodeVisitor<Void, JsonSupportVisitorContext> { | ||
| @Override | ||
| public Void visit(Node node, JsonSupportVisitorContext context) { | ||
| visitChildren(node, context); | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitChildren(Node node, JsonSupportVisitorContext context) { | ||
| for (Node child : node.getChild()) { | ||
| child.accept(this, context); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitAggregation(Aggregation node, JsonSupportVisitorContext context) { | ||
| if (!node.getGroupExprList().isEmpty()) { | ||
| throw new UnsupportedOperationException( | ||
| "Queries with aggregation are not yet supported with json format in the new engine"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitFunction(Function node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // queries with function calls are not supported. | ||
| throw new UnsupportedOperationException( | ||
| "Queries with functions are not yet supported with json format in the new engine"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitLiteral(Literal node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // queries with literal values are not supported | ||
| throw new UnsupportedOperationException( | ||
| "Queries with literals are not yet supported with json format in the new engine"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitCast(Cast node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // Queries with cast are not supported | ||
| throw new UnsupportedOperationException( | ||
| "Queries with casts are not yet supported with json format in the new engine"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitAlias(Alias node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // Alias node is accepted if it does not have a user-defined alias | ||
| // and if the delegated expression is accepted. | ||
| if (StringUtils.isEmpty(node.getAlias())) { | ||
| node.getDelegated().accept(this, context); | ||
| } else { | ||
| throw new UnsupportedOperationException( | ||
| "Queries with aliases are not yet supported with json format in the new engine"); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Void visitProject(Project node, JsonSupportVisitorContext context) { | ||
| visit(node, context); | ||
|
|
||
| context.setVisitingProject(true); | ||
| node.getProjectList().forEach(e -> e.accept(this, context)); | ||
| context.setVisitingProject(false); | ||
| return null; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
core/src/main/java/org/opensearch/sql/analysis/JsonSupportVisitorContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.analysis; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * The context used for JsonSupportVisitor. | ||
| */ | ||
| public class JsonSupportVisitorContext { | ||
| @Getter | ||
| @Setter | ||
| private boolean isVisitingProject = false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -45,4 +45,9 @@ public ExecutionEngine.Schema schema() { | |||||
| throw new IllegalStateException(String.format("[BUG] schema can been only applied to " | ||||||
| + "ProjectOperator, instead of %s", toString())); | ||||||
| } | ||||||
|
|
||||||
| public String getRawResponse() { | ||||||
| return getChild().stream().map(PhysicalPlan::getRawResponse) | ||||||
| .filter(r -> r != null && !r.isEmpty()).findFirst().orElse(""); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in 4e692fe |
||||||
| } | ||||||
| } | ||||||
125 changes: 125 additions & 0 deletions
125
core/src/test/java/org/opensearch/sql/analysis/JsonSupportVisitorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.analysis; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.opensearch.sql.ast.dsl.AstDSL.qualifiedName; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opensearch.sql.ast.dsl.AstDSL; | ||
| import org.opensearch.sql.ast.expression.Literal; | ||
| import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
| import org.opensearch.sql.ast.tree.UnresolvedPlan; | ||
|
|
||
| class JsonSupportVisitorTest { | ||
| @Test | ||
| public void visitLiteralInProject() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.intLiteral(1)); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitLiteralOutsideProject() { | ||
| Literal intLiteral = AstDSL.intLiteral(1); | ||
| assertNull(intLiteral.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitCastInProject() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.cast(AstDSL.intLiteral(1), AstDSL.stringLiteral("INT"))); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitCastOutsideProject() { | ||
| UnresolvedExpression intCast = AstDSL.cast( | ||
| AstDSL.intLiteral(1), | ||
| AstDSL.stringLiteral("INT")); | ||
| assertNull(intCast.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasInProject() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.alias("alias", AstDSL.intLiteral(1))); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasInProjectWithUnsupportedDelegated() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.alias("alias", AstDSL.intLiteral(1), "alias")); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasInProjectWithSupportedDelegated() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.alias("alias", AstDSL.field("field"))); | ||
| assertNull(project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasOutsideProject() { | ||
| UnresolvedExpression alias = AstDSL.alias("alias", AstDSL.intLiteral(1)); | ||
| assertNull(alias.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitFunctionInProject() { | ||
| UnresolvedPlan function = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.function("abs", AstDSL.intLiteral(-1))); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> function.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitFunctionOutsideProject() { | ||
| UnresolvedExpression function = AstDSL.function("abs", AstDSL.intLiteral(-1)); | ||
| assertNull(function.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAggregationWithGroupExprList() { | ||
| UnresolvedPlan projectAggr = AstDSL.project(AstDSL.agg( | ||
| AstDSL.relation("table", "table"), | ||
| Collections.emptyList(), | ||
| Collections.emptyList(), | ||
| ImmutableList.of(AstDSL.alias("alias", qualifiedName("integer_value"))), | ||
| Collections.emptyList())); | ||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> projectAggr.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAggregationWithAggExprList() { | ||
| UnresolvedPlan aggregation = AstDSL.agg( | ||
| AstDSL.relation("table", "table"), | ||
| ImmutableList.of( | ||
| AstDSL.alias("AVG(alias)", | ||
| AstDSL.aggregate("AVG", | ||
| qualifiedName("integer_value")))), | ||
| Collections.emptyList(), | ||
| Collections.emptyList(), | ||
| Collections.emptyList()); | ||
| assertNull(aggregation.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is this code using
Void,return nulland raising exceptions instead of just returningboolean?The visitor answers a yes or no question. Using
booleanis simpler and more natural here.Using exceptions for flow control is not a recommended practice. SQL plugin does that with fallback to legacy, but that's no great -- let's not add more.
The only thing it would lose is knowing which element caused validation to fail. That seems ok in this case -- the error message will be slightly worse.
If you do want to maintain the error message, you could return
Optional<String>. Then for supported queriesJsonSupportVisitor.visitwill returnOptional.empty(). Otherwise it returns the label of the first element that caused validation to fail. That's enough information to throw exception with the same message. As a bonus, throw will happen in one place now.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.
The Boolean return type is a good change.
I'm not convinced that removing the Exception throws in this instance is the right way. Try...Catch blocks are slightly more expensive in processing time, but then we have the satisfaction of knowing that the NodeVisitor is interrupted.
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.
Updated in 4e692fe