Skip to content

[CALCITE-7482] Wrong variablesSet used when rewriting subquery in JOIN ON clause#4898

Merged
silundong merged 1 commit intoapache:mainfrom
silundong:issue-7482
Apr 22, 2026
Merged

[CALCITE-7482] Wrong variablesSet used when rewriting subquery in JOIN ON clause#4898
silundong merged 1 commit intoapache:mainfrom
silundong:issue-7482

Conversation

@silundong
Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7482

Changes Proposed

  1. When rewriting a subquery in the ON clause (SubQueryRemoveRule.matchJoin), after collecting the CorrelationId used in the subquery, CorrelationId that do not belong to the current scope are excluded.
  2. During initial plan generation, a lateral join is transformed into a Correlate. If at this point the right side contains a subquery that references variables from the left side (i.e., in the form of a nested correlated subquery), TopDownGeneralDecorrelator cannot be applied directly. This is because the subquery has not yet been fully removed — performing decorrelation prematurely would cause the free variables in the nested correlated subquery to be unable to locate their corresponding Correlate in subsequent processing. Like the initial plan below:
LogicalProject(ID=[$0], SAL=[$1], ID0=[$2], SAL0=[$3])
  LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{0}])
    LogicalValues(tuples=[[{ 1, 10 }, { 2, 20 }, { 3, 30 }]])
    LogicalProject(ID=[$0], SAL=[$1])
      LogicalJoin(condition=[EXISTS({
LogicalFilter(condition=[AND(=($0, $cor0.ID), =($1, $cor1.SAL))])     <- still have a free variable from $cor0
  LogicalValues(tuples=[[{ 2, 200 }, { 5, 50 }]])
})], joinType=[inner], variablesSet=[[$cor1]]) 
        LogicalValues(tuples=[[{ 2, 20 }, { 2, 200 }, { 3, 30 }]])
        LogicalValues(tuples=[[{ 3, 30 }, { 4, 40 }]])

@mihaibudiu
Copy link
Copy Markdown
Contributor

I will wait for the CI to pass to review this PR

@silundong
Copy link
Copy Markdown
Contributor Author

@mihaibudiu Thank you. That was a code style error.

@sonarqubecloud
Copy link
Copy Markdown

@silundong silundong added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Apr 21, 2026
@mihaibudiu
Copy link
Copy Markdown
Contributor

Well, I had approved anyway, feel free to merge

@silundong silundong merged commit 82e5ae7 into apache:main Apr 22, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants