generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Implement ppl scalar subquery command with Calcite #3392
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
Merged
LantaoJin
merged 3 commits into
opensearch-project:feature/calcite-engine
from
LantaoJin:pr/issues/3361
Mar 7, 2025
Merged
Changes from 2 commits
Commits
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
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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/org/opensearch/sql/ast/expression/subquery/ScalarSubquery.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,34 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression.subquery; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
| import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
| import org.opensearch.sql.ast.tree.UnresolvedPlan; | ||
|
|
||
| @Getter | ||
| @ToString | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @RequiredArgsConstructor | ||
| public class ScalarSubquery extends UnresolvedExpression { | ||
| private final UnresolvedPlan query; | ||
|
|
||
| @Override | ||
| public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
| return nodeVisitor.visitScalarSubquery(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| public List<UnresolvedExpression> getChild() { | ||
| return ImmutableList.of(); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import static org.opensearch.sql.ast.tree.Sort.SortOrder.DESC; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.Iterables; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
@@ -31,14 +32,15 @@ | |
| import org.apache.calcite.util.Holder; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
| import org.opensearch.sql.ast.Node; | ||
| import org.opensearch.sql.ast.expression.AllFields; | ||
| import org.opensearch.sql.ast.expression.Argument; | ||
| import org.opensearch.sql.ast.expression.Compare; | ||
| import org.opensearch.sql.ast.expression.Field; | ||
| import org.opensearch.sql.ast.expression.Not; | ||
| import org.opensearch.sql.ast.expression.Let; | ||
| import org.opensearch.sql.ast.expression.QualifiedName; | ||
| import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
| import org.opensearch.sql.ast.expression.subquery.ExistsSubquery; | ||
| import org.opensearch.sql.ast.expression.subquery.ScalarSubquery; | ||
| import org.opensearch.sql.ast.tree.Aggregation; | ||
| import org.opensearch.sql.ast.tree.Eval; | ||
| import org.opensearch.sql.ast.tree.Filter; | ||
|
|
@@ -92,13 +94,14 @@ private RelBuilder scan(RelOptTable tableSchema, CalcitePlanContext context) { | |
| public RelNode visitFilter(Filter node, CalcitePlanContext context) { | ||
| visitChildren(node, context); | ||
| boolean containsExistsSubquery = containsExistsSubquery(node.getCondition()); | ||
| boolean containsScalarSubquery = containsScalarSubquery(node.getCondition()); | ||
| final Holder<@Nullable RexCorrelVariable> v = Holder.empty(); | ||
| if (containsExistsSubquery) { | ||
| if (containsExistsSubquery || containsScalarSubquery) { | ||
| context.relBuilder.variable(v::set); | ||
| context.pushCorrelVar(v.get()); | ||
| } | ||
| RexNode condition = rexVisitor.analyze(node.getCondition(), context); | ||
| if (containsExistsSubquery) { | ||
| if (containsExistsSubquery || containsScalarSubquery) { | ||
| context.relBuilder.filter(ImmutableList.of(v.get().id), condition); | ||
| context.popCorrelVar(); | ||
| } else { | ||
|
|
@@ -107,15 +110,38 @@ public RelNode visitFilter(Filter node, CalcitePlanContext context) { | |
| return context.relBuilder.peek(); | ||
| } | ||
|
|
||
| private boolean containsExistsSubquery(Object condition) { | ||
| if (condition instanceof ExistsSubquery) { | ||
| private boolean containsExistsSubquery(Node expr) { | ||
| if (expr == null) { | ||
| return false; | ||
| } | ||
| if (expr instanceof ExistsSubquery) { | ||
| return true; | ||
| } | ||
| if (expr instanceof Let l) { | ||
| return containsExistsSubquery(l.getExpression()); | ||
| } | ||
| for (Node child : expr.getChild()) { | ||
| if (containsExistsSubquery(child)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean containsScalarSubquery(Node expr) { | ||
| if (expr == null) { | ||
| return false; | ||
| } | ||
| if (expr instanceof ScalarSubquery) { | ||
| return true; | ||
| } | ||
| if (condition instanceof Not n) { | ||
| return containsExistsSubquery(n.getExpression()); | ||
| if (expr instanceof Let l) { | ||
| return containsScalarSubquery(l.getExpression()); | ||
| } | ||
| if (condition instanceof Compare c) { | ||
| return containsExistsSubquery(c.getLeft()) || containsExistsSubquery(c.getRight()); | ||
| for (Node child : expr.getChild()) { | ||
| if (containsScalarSubquery(child)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
|
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. Should we support more cases than Not, Let and Compare?
Member
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. Sure, addressed in latest commit, please check it again |
||
| } | ||
|
|
@@ -187,8 +213,26 @@ public RelNode visitEval(Eval node, CalcitePlanContext context) { | |
| node.getExpressionList().stream() | ||
| .map( | ||
| expr -> { | ||
| boolean containsExistsSubquery = containsExistsSubquery(expr); | ||
| boolean containsScalarSubquery = containsScalarSubquery(expr); | ||
| final Holder<@Nullable RexCorrelVariable> v = Holder.empty(); | ||
| if (containsExistsSubquery || containsScalarSubquery) { | ||
| context.relBuilder.variable(v::set); | ||
| context.pushCorrelVar(v.get()); | ||
| } | ||
| RexNode eval = rexVisitor.analyze(expr, context); | ||
| context.relBuilder.projectPlus(eval); | ||
| if (containsExistsSubquery || containsScalarSubquery) { | ||
| // RelBuilder.projectPlus doesn't have a parameter with variablesSet: | ||
| // projectPlus(Iterable<CorrelationId> variablesSet, RexNode... nodes) | ||
| context.relBuilder.project( | ||
| Iterables.concat(context.relBuilder.fields(), ImmutableList.of(eval)), | ||
| ImmutableList.of(), | ||
| false, | ||
| ImmutableList.of(v.get().id)); | ||
| context.popCorrelVar(); | ||
| } else { | ||
| context.relBuilder.projectPlus(eval); | ||
| } | ||
| return eval; | ||
| }) | ||
| .collect(Collectors.toList()); | ||
|
|
||
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.
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.
How about combining this 2 flags together? Will we use them separately in specific cases?
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