-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7442] Getting Wrong index of Correlated variable inside Subquery after FilterJoinRule #4840
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
base: main
Are you sure you want to change the base?
[CALCITE-7442] Getting Wrong index of Correlated variable inside Subquery after FilterJoinRule #4840
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2957,7 +2957,8 @@ public static boolean classifyFilters( | |
| joinFields, | ||
| nTotalFields, | ||
| leftFields, | ||
| filter); | ||
| filter, | ||
| joinRel.getInput(0)); | ||
|
|
||
| leftFilters.add(shiftedFilter); | ||
| } | ||
|
|
@@ -2975,7 +2976,8 @@ public static boolean classifyFilters( | |
| joinFields, | ||
| nTotalFields, | ||
| rightFields, | ||
| filter); | ||
| filter, | ||
| joinRel.getInput(1)); | ||
| rightFilters.add(shiftedFilter); | ||
| } | ||
| filtersToRemove.add(filter); | ||
|
|
@@ -3079,7 +3081,8 @@ public static boolean classifyFilters( | |
| joinFields, | ||
| nTotalFields, | ||
| leftFields, | ||
| filter); | ||
| filter, | ||
| joinRel.getInput(0)); | ||
|
|
||
| leftFilters.add(shiftedFilter); | ||
| } | ||
|
|
@@ -3105,7 +3108,8 @@ public static boolean classifyFilters( | |
| joinFields, | ||
| nTotalFields, | ||
| rightFields, | ||
| filter); | ||
| filter, | ||
| joinRel.getInput(1)); | ||
| rightFilters.add(shiftedFilter); | ||
| } | ||
| filtersToRemove.add(filter); | ||
|
|
@@ -3140,7 +3144,8 @@ private static RexNode shiftFilter( | |
| List<RelDataTypeField> joinFields, | ||
| int nTotalFields, | ||
| List<RelDataTypeField> rightFields, | ||
| RexNode filter) { | ||
| RexNode filter, | ||
| RelNode child) { | ||
| int[] adjustments = new int[nTotalFields]; | ||
| for (int i = start; i < end; i++) { | ||
| adjustments[i] = offset; | ||
|
|
@@ -3150,7 +3155,9 @@ private static RexNode shiftFilter( | |
| rexBuilder, | ||
| joinFields, | ||
| rightFields, | ||
| adjustments)); | ||
| adjustments, | ||
| offset, | ||
| child)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -4752,6 +4759,17 @@ public ImmutableBitSet build() { | |
| } | ||
| return super.visitCall(call); | ||
| } | ||
|
|
||
| @Override public Void visitSubQuery(RexSubQuery subQuery) { | ||
| final Set<CorrelationId> variablesSet = RelOptUtil.getVariablesUsed(subQuery.rel); | ||
| for (CorrelationId id : variablesSet) { | ||
| ImmutableBitSet requiredColumns = RelOptUtil.correlationColumns(id, subQuery.rel); | ||
| for (int index : requiredColumns) { | ||
| bitBuilder.set(index); | ||
| } | ||
| } | ||
| return super.visitSubQuery(subQuery); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -4766,6 +4784,8 @@ public static class RexInputConverter extends RexShuttle { | |
| private final @Nullable List<RelDataTypeField> rightDestFields; | ||
| private final int nLeftDestFields; | ||
| private final int[] adjustments; | ||
| private final int offset; | ||
| private final @Nullable RelNode correlateVariableChild; | ||
|
|
||
| /** | ||
| * Creates a RexInputConverter. | ||
|
|
@@ -4784,14 +4804,23 @@ public static class RexInputConverter extends RexShuttle { | |
| * @param rightDestFields in the case where the destination is a join, | ||
| * these are the fields from the right join input | ||
| * @param adjustments the amount to adjust each field by | ||
| * @param offset the amount to shift field accesses by when | ||
| * rewriting correlated subqueries | ||
| * @param correlateVariableChild the child relation providing the | ||
|
Member
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. The code comment format here is strange.
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. the variable name is big here, that's why it's going into it's doc. any suggestions on formatting?
Member
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. Indeed, I'll take a look at other Calcite code later to see if there are any good solutions.
Member
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. Perhaps changing to a shorter, more concise variable name would solve the problem. 🤔
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. got it, will get back on this tomorrow
Contributor
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. using a shorter variable name to make formatting of comments nice is not a good reason.
Member
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. Thanks for the reminder, I'll look into whether there's a better way to handle this tomorrow. |
||
| * correlated variable; if non-null, subqueries | ||
| * referencing a correlation variable will have | ||
| * their field accesses shifted by {@code offset} | ||
| * relative to this child | ||
| */ | ||
| private RexInputConverter( | ||
| RexBuilder rexBuilder, | ||
| @Nullable List<RelDataTypeField> srcFields, | ||
| @Nullable List<RelDataTypeField> destFields, | ||
| @Nullable List<RelDataTypeField> leftDestFields, | ||
| @Nullable List<RelDataTypeField> rightDestFields, | ||
| int[] adjustments) { | ||
| int[] adjustments, | ||
| int offset, | ||
| @Nullable RelNode correlateVariableChild) { | ||
yashlimbad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.rexBuilder = rexBuilder; | ||
| this.srcFields = srcFields; | ||
| this.destFields = destFields; | ||
|
|
@@ -4804,6 +4833,8 @@ private RexInputConverter( | |
| assert destFields == null; | ||
| nLeftDestFields = leftDestFields.size(); | ||
| } | ||
| this.offset = offset; | ||
| this.correlateVariableChild = correlateVariableChild; | ||
| } | ||
|
|
||
| public RexInputConverter( | ||
|
|
@@ -4818,22 +4849,61 @@ public RexInputConverter( | |
| null, | ||
| leftDestFields, | ||
| rightDestFields, | ||
| adjustments); | ||
| adjustments, | ||
| 0, | ||
| null); | ||
| } | ||
|
|
||
| public RexInputConverter( | ||
| RexBuilder rexBuilder, | ||
| @Nullable List<RelDataTypeField> srcFields, | ||
| @Nullable List<RelDataTypeField> destFields, | ||
| int[] adjustments) { | ||
| this(rexBuilder, srcFields, destFields, null, null, adjustments); | ||
| this(rexBuilder, srcFields, destFields, null, null, adjustments, 0, null); | ||
| } | ||
|
|
||
| public RexInputConverter( | ||
| RexBuilder rexBuilder, | ||
| @Nullable List<RelDataTypeField> srcFields, | ||
| int[] adjustments) { | ||
| this(rexBuilder, srcFields, null, null, null, adjustments); | ||
| this(rexBuilder, srcFields, null, null, null, adjustments, 0, null); | ||
| } | ||
|
|
||
| public RexInputConverter( | ||
| RexBuilder rexBuilder, | ||
| @Nullable List<RelDataTypeField> srcFields, | ||
| @Nullable List<RelDataTypeField> destFields, | ||
| int[] adjustments, | ||
| int offset, | ||
| RelNode child) { | ||
| this(rexBuilder, srcFields, destFields, null, null, adjustments, offset, child); | ||
| } | ||
|
|
||
| @Override public RexNode visitSubQuery(RexSubQuery subQuery) { | ||
| boolean[] update = {false}; | ||
| List<RexNode> clonedOperands = visitList(subQuery.operands, update); | ||
|
Member
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.
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. yes, accepts list of boolean and updates 0th index to true if updated. |
||
| if (update[0]) { | ||
|
Member
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. I suspect there might be an issue with the EXISTS subquery, but I'm not entirely sure. Could you add a similar test?
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. checking
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. EXISTS fails similar to IN clause, nice catch! thanks for this. will work on it
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. fixed and added test
Member
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. Besides update[0], is there a better representation? Could you explain why it must be update[0]? |
||
| subQuery = subQuery.clone(subQuery.getType(), clonedOperands); | ||
| } | ||
| final Set<CorrelationId> variablesSet = | ||
| RelOptUtil.getVariablesUsed(subQuery.rel); | ||
|
Contributor
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. Similar to the concern at
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. correlation ID belongs to outer scope, that's the point of adding it. please check the test |
||
| if (!variablesSet.isEmpty() && correlateVariableChild != null) { | ||
| for (CorrelationId id : variablesSet) { | ||
| RelNode newSubQueryRel = | ||
| subQuery.rel.accept(new RelHomogeneousShuttle() { | ||
| @Override public RelNode visit(RelNode other) { | ||
| RelNode node = | ||
| RexUtil.shiftFieldAccess(rexBuilder, other, id, | ||
| correlateVariableChild, offset); | ||
| return super.visit(node); | ||
| } | ||
| }); | ||
| if (newSubQueryRel != subQuery.rel) { | ||
| subQuery = subQuery.clone(newSubQueryRel); | ||
| } | ||
| } | ||
| } | ||
| return subQuery; | ||
| } | ||
|
|
||
| @Override public RexNode visitInputRef(RexInputRef var) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import org.apache.calcite.plan.RelOptUtil; | ||
| import org.apache.calcite.plan.RelRule; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.core.CorrelationId; | ||
| import org.apache.calcite.rel.core.Filter; | ||
| import org.apache.calcite.rel.core.Join; | ||
| import org.apache.calcite.rel.core.JoinRelType; | ||
|
|
@@ -29,7 +30,9 @@ | |
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.rex.RexInputRef; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.rex.RexSubQuery; | ||
| import org.apache.calcite.rex.RexUtil; | ||
| import org.apache.calcite.rex.RexVisitorImpl; | ||
| import org.apache.calcite.sql.SqlKind; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.tools.RelBuilder; | ||
|
|
@@ -198,14 +201,43 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter, | |
| return; | ||
| } | ||
|
|
||
| Set<CorrelationId> leftVariablesSet = new LinkedHashSet<>(); | ||
| Set<CorrelationId> rightVariablesSet = new LinkedHashSet<>(); | ||
|
|
||
| for (RexNode condition : leftFilters) { | ||
| condition.accept(new RexVisitorImpl<Void>(true) { | ||
| @Override public Void visitSubQuery(RexSubQuery subQuery) { | ||
| leftVariablesSet.addAll(RelOptUtil.getVariablesUsed(subQuery.rel)); | ||
| return super.visitSubQuery(subQuery); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| for (RexNode condition : rightFilters) { | ||
| condition.accept(new RexVisitorImpl<Void>(true) { | ||
| @Override public Void visitSubQuery(RexSubQuery subQuery) { | ||
| rightVariablesSet.addAll(RelOptUtil.getVariablesUsed(subQuery.rel)); | ||
| return super.visitSubQuery(subQuery); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| ImmutableSet.Builder<CorrelationId> newJoinCorrelationIds = ImmutableSet.builder(); | ||
| for (CorrelationId correlationId : join.getVariablesSet()) { | ||
| if (!leftVariablesSet.contains(correlationId) | ||
| && !rightVariablesSet.contains(correlationId)) { | ||
| newJoinCorrelationIds.add(correlationId); | ||
| } | ||
| } | ||
|
|
||
| // create Filters on top of the children if any filters were | ||
| // pushed to them | ||
| final RexBuilder rexBuilder = join.getCluster().getRexBuilder(); | ||
| final RelBuilder relBuilder = call.builder(); | ||
| final RelNode leftRel = | ||
| relBuilder.push(join.getLeft()).filter(leftFilters).build(); | ||
| relBuilder.push(join.getLeft()).filter(leftVariablesSet, leftFilters).build(); | ||
| final RelNode rightRel = | ||
| relBuilder.push(join.getRight()).filter(rightFilters).build(); | ||
| relBuilder.push(join.getRight()).filter(rightVariablesSet, rightFilters).build(); | ||
|
Contributor
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. If the initial ON condition contains two correlated subqueries, one that cannot be pushed down and the other is pushed down to the right, could the following situation occur? Two
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 code to remove pushed down variable set from join's variable set
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. some tests are failing, investigating them. will get back on it tomorrow
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. fixed and pushed |
||
|
|
||
| // create the new join node referencing the new children and | ||
| // containing its new join filters (if there are any) | ||
|
|
@@ -233,7 +265,8 @@ protected void perform(RelOptRuleCall call, @Nullable Filter filter, | |
| leftRel, | ||
| rightRel, | ||
| joinType, | ||
| join.isSemiJoinDone()); | ||
| join.isSemiJoinDone(), | ||
| newJoinCorrelationIds.build()); | ||
| call.getPlanner().onCopy(join, newJoinRel); | ||
| if (!leftFilters.isEmpty() && filter != null) { | ||
| call.getPlanner().onCopy(filter, leftRel); | ||
|
|
||
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.
If I understand correctly, the purpose here is to obtain the indices of the free variables in the subquery. My concern is:
InputFinderanalyzes the expressions of aRelNode, and there should be at most oneCorrelationIdthat belongs to thatRelNode(at least during the rule rewriting phase). Here, callingRelOptUtil.getVariablesUsedreturns a set ofCorrelationId, which may include CorrelationIds that do not belong to thisRelNode(i.e., the free variables in the subquery come from an outer scope). While it may be difficult to construct such a case,InputFinder.analyzeis a public method, and we don't know how users will call it in their systems.If we want InputFinder to consider the indices of free variables in the subquery, perhaps we should add a field to record the variablesSet of the
RelNode.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 whole purpose is to find correlationID right? a correlationID doesn't belong to that particular RelNode anyways, it belongs to whichever other subtree it's correlation variable is pointing to.
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.
Just to avoid any misunderstanding, let me explain again. For example:
There is a
Filter(condition=[subquery], variablesSet=[cor2]), and the subquery has two free variables:cor2.idandcor1.name(assume thatcor1belongs to an outer scope). If we useInputFinderto analyze theFiltercondition,InputFindershould record the index ofcor2.idand ignorecor1.name.As I mentioned below, similar logic should also apply to
RexInputConverter.visitSubQuery.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.
got it, will come up with some unit test for this
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.
verified a scenario where nested subquery has 2 correlated variable which belongs to inner scope and outer scope.
exaple query:
plan
when
FilterJoinRuleis called, it's called on main query scope's join which hasvariablesSet=[[$cor0]]so when
InputFinderis used on the condition of join which contains subquery the methodRelOptUtil.getVariablesUsedwill return$cor0and not all variables; this is already handled so we will not get both variables here. HenceInputFinderandRexInputConverterwill get correct variableSet.and if we are concerned about outer subquery getting variableSet of all correlationId then also we shouldn't be worries because it will not be queried directly unless we decorrelate all nested subqueries.