-
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 | ||
| * 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); | ||
| 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 |
||
| subQuery = subQuery.clone(subQuery.getType(), clonedOperands); | ||
| } | ||
| final Set<CorrelationId> variablesSet = | ||
| RelOptUtil.getVariablesUsed(subQuery.rel); | ||
| 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) { | ||
|
|
||
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 code comment format here is strange.
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 variable name is big here, that's why it's going into it's doc. any suggestions on formatting?
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.
Indeed, I'll take a look at other Calcite code later to see if there are any good solutions.
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.
Perhaps changing to a shorter, more concise variable name would solve the problem. 🤔
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 get back on this tomorrow
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.
using a shorter variable name to make formatting of comments nice is not a good reason.
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.
Thanks for the reminder, I'll look into whether there's a better way to handle this tomorrow.