generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
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 all commits
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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
| /* | ||
| * 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.Limit; | ||
| 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<Boolean, JsonSupportVisitorContext> { | ||
| @Override | ||
| public Boolean visit(Node node, JsonSupportVisitorContext context) { | ||
| // UnresolvedPlan can only have one child until joins are supported | ||
| return node.getChild().get(0).accept(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| protected Boolean defaultResult() { | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitLimit(Limit node, JsonSupportVisitorContext context) { | ||
| context.addToUnsupportedNodes("limit"); | ||
| return Boolean.FALSE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitAggregation(Aggregation node, JsonSupportVisitorContext context) { | ||
| if (!node.getGroupExprList().isEmpty()) { | ||
| context.addToUnsupportedNodes("aggregation"); | ||
| return Boolean.FALSE; | ||
| } | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitFunction(Function node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // queries with function calls are not supported. | ||
| context.addToUnsupportedNodes("functions"); | ||
| return Boolean.FALSE; | ||
| } | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitLiteral(Literal node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // queries with literal values are not supported | ||
| context.addToUnsupportedNodes("literal"); | ||
| return Boolean.FALSE; | ||
| } | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitCast(Cast node, JsonSupportVisitorContext context) { | ||
| // Supported if outside of Project | ||
| if (context.isVisitingProject()) { | ||
| // Queries with cast are not supported | ||
| context.addToUnsupportedNodes("cast"); | ||
| return Boolean.FALSE; | ||
| } | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean 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())) { | ||
| return node.getDelegated().accept(this, context); | ||
| } else { | ||
| context.addToUnsupportedNodes("alias"); | ||
| return Boolean.FALSE; | ||
| } | ||
| } | ||
| return Boolean.TRUE; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visitProject(Project node, JsonSupportVisitorContext context) { | ||
| Boolean isSupported = visit(node, context); | ||
|
|
||
| context.setVisitingProject(true); | ||
| isSupported = node.getProjectList().stream() | ||
| .allMatch(e -> e.accept(this, context)) && isSupported; | ||
| context.setVisitingProject(false); | ||
| return isSupported; | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.analysis; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * The context used for JsonSupportVisitor. | ||
| */ | ||
| public class JsonSupportVisitorContext { | ||
| @Getter | ||
| @Setter | ||
| private boolean isVisitingProject = false; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| private List<String> unsupportedNodes = new ArrayList<>(); | ||
|
|
||
| public void addToUnsupportedNodes(String unsupportedNode) { | ||
| unsupportedNodes.add(unsupportedNode); | ||
| } | ||
| } |
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
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
135 changes: 135 additions & 0 deletions
135
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,135 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.analysis; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 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.Limit; | ||
| import org.opensearch.sql.ast.tree.UnresolvedPlan; | ||
|
|
||
| class JsonSupportVisitorTest { | ||
| @Test | ||
| public void visitLiteralInProject() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.intLiteral(1)); | ||
| assertFalse(project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitLiteralOutsideProject() { | ||
| Literal intLiteral = AstDSL.intLiteral(1); | ||
| assertTrue(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"))); | ||
| assertFalse(project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitCastOutsideProject() { | ||
| UnresolvedExpression intCast = AstDSL.cast( | ||
| AstDSL.intLiteral(1), | ||
| AstDSL.stringLiteral("INT")); | ||
| assertTrue(intCast.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasInProject() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.alias("alias", AstDSL.intLiteral(1))); | ||
| assertFalse(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")); | ||
| assertFalse(project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasInProjectWithSupportedDelegated() { | ||
| UnresolvedPlan project = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.alias("alias", AstDSL.field("field"))); | ||
| assertTrue(project.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitAliasOutsideProject() { | ||
| UnresolvedExpression alias = AstDSL.alias("alias", AstDSL.intLiteral(1)); | ||
| assertTrue(alias.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitFunctionInProject() { | ||
| UnresolvedPlan function = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.function("abs", AstDSL.intLiteral(-1))); | ||
| assertFalse(function.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitFunctionOutsideProject() { | ||
| UnresolvedExpression function = AstDSL.function("abs", AstDSL.intLiteral(-1)); | ||
| assertTrue(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())); | ||
| assertFalse(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()); | ||
| assertTrue(aggregation.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitLimit() { | ||
| Limit limit = AstDSL.limit(AstDSL.relation("table", "table"), 10, 5); | ||
| assertFalse(limit.accept(new JsonSupportVisitor(), new JsonSupportVisitorContext())); | ||
| } | ||
|
|
||
| @Test | ||
| public void visitWithMultipleUnsupportedProjectNodes() { | ||
| UnresolvedPlan plan = AstDSL.project( | ||
| AstDSL.relation("table", "table"), | ||
| AstDSL.function("abs", AstDSL.intLiteral(-1)), | ||
| AstDSL.alias("alias", AstDSL.intLiteral(1))); | ||
| assertFalse(plan.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.
I'm thinking if any easy way to do this. As I understand, JSON format only makes sense if the physical query plan for a query only has a Project (item is regular field) and OpenSearchIndexScan. If so, can we do this validation 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.
+1, As I understand it, all unsupported cases will still fallback to the V1 engine, so there won't be a significant difference, right?