-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for correlated sub-queries in Filter #5036
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
Open
Dwrite
wants to merge
5
commits into
apache:main
Choose a base branch
from
Dwrite:CALCITE-7505
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−10
Open
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for correlated sub-queries in Filter #5036
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57d62e1
[CALCITE-7505] RelToSqlConverter produces duplicate FROM aliases for …
675a4ce
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for co…
8801140
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for co…
419bab8
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for co…
df14a28
[CALCITE-7505] RelToSqlConverter fails to alias outer relation for co…
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
Some comments aren't visible on the classic Files Changed page.
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
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.
The JavaDoc above says: "a correlated variable defined by the given input"
Where is this check validating this fact?
(I am not sure how "defined by an input" is defined)
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.
Sorry, that's my english fault, The wording is inaccurate. The method does not actually verify that the correlation variable is defined by the given input; it only checks for a correlated reference with a matching row type. I'll update the Javadoc to reflect what it actually does.
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.
There can be many variables with the same type, please explain why your approach is correct.
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @mihaibudiu
Thanks for the question. During our test case execution, we observed that planner rules like FilterIntoJoinRule( test case with testFilterCorrelateMissingVariableCor) can reposition or push down a Filter. This optimization often causes the Filter's variablesSet to become stale, meaning the variable's rowType no longer matches the schema of the new underlying input.
Therefore, relying solely on correlIds.contains(v.id) is insufficient. We need the rowType equality check to ensure that this Filter remains the true binding point of the correlation variable post-optimization. If the types mismatch, it indicates the Filter has been relocated, and we should not trigger resetAliasForCorrelation.
Regarding the structure check, rather than a generic BiRel, I specifically restrict this validation to Join and LogicalCorrelate operators. These two operators represent the exact semantic boundaries where correlation aliases are defined and resolved. Checking for Join and LogicalCorrelate allows us to correctly determine whether the alias needs to be appended, ensuring correctness without interfering with other relational nodes.
Let me know if this clarifies the design choice!
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.
It looks to me that you are describing a bug, I think having a wrong type on the correlated variables should be normally impossible - whoever rewrote the filter should have adjusted the variable types too. Am I wrong? If not, can you make sure that there is an issue filed for this?
Can a variable have the right type and also be obsolete at the same time?
Also, you can probably add an explanation here as a comment about why the type is needed.
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.
You are correct, and I appreciate the sharp observation.
The rowType matching check is not a reliable discriminator. It was
introduced as an empirical workaround based on observed behavior in
specific test cases, not derived from a sound semantic invariant. As
you pointed out, a variable can have the correct type and still be
obsolete, so the check is not sound in general.
After mirroring the same variablesSet approach from Project to Filter,
all tests pass except for two cases that involve HepPlanner rules
(FilterIntoJoinRule-- testFilterCorrelateMissingVariableCor() and FilterCorrelateRule testFilterIntoJoinMissingVariableCor()
). I will investigate why
these two rules produce incorrect behavior and follow up.
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.
Hi @mihaibudiu
I add new code
!(definedHere.isEmpty()) && e.getInput().getInputs().size() <= 1. and here's explaination.The purpose of calling
resetAliasForCorrelationinvisit(Filter)/visit(Project)is narrow: give
e.getInput()a canonical alias so that a correlatedsub-query embedded in this node's condition/projects can resolve
$corX.fieldagainst that alias.This is only safe/necessary when
e.getInput()'s rendered SQL alreadyrepresents a single addressable relation:
e.getInput().getInputs().size() <= 1(TableScan, Values, Project,Filter, Aggregate, ...): these nodes always render as a single
identity -- either a bare table reference or a freshly wrapped SELECT
derived table. Assigning an alias here is safe, and is exactly the
step the original bug was missing.
e.getInput().getInputs().size() > 1(Join, Correlate):visit(Join)already builds a multi-alias context via
joinContext(leftContext, rightContext)(e.g.{EMP: ..., DEPT: ...}),keeping each side independently addressable -- which is exactly what a
correlated sub-query needs to resolve
$corX.field. CallingresetAliasForCorrelationhere would collapse that multi-alias contextinto a single alias, destroying field resolution that was already
correct (this is the root cause of the
DEPTNO0leakage observed inthe FilterIntoJoinRule / FilterCorrelateRule regression cases).
Scope: this check only governs whether
visit(Filter)/visit(Project)should invoke
resetAliasForCorrelationfor the purpose of correlationvariable binding. It says nothing about the general contract of
resetAliasForCorrelationitself, and does not affect other call sites(e.g.
visitAntiOrSemiJoin, which legitimately calls it on amulti-alias Result for an unrelated purpose -- wrapping a complex
anti-join input). That is why the check belongs at this call site rather
than as an assertion inside
resetAliasForCorrelation: an assertioninside the shared method would incorrectly impose this call site's
precondition on every caller.
Verified against three concrete scenarios:
(2 inputs) -> pushed=false, correct (avoids DEPTNO0 leakage)
has 1 input -> pushed=true, correct