Skip to content

[CALCITE-7584] RelDecorrelator produces incorrect results for correlated LATERAL sub-queries with window functions#4997

Open
iwanttobepowerful wants to merge 1 commit into
apache:mainfrom
iwanttobepowerful:CALCITE-7584
Open

[CALCITE-7584] RelDecorrelator produces incorrect results for correlated LATERAL sub-queries with window functions#4997
iwanttobepowerful wants to merge 1 commit into
apache:mainfrom
iwanttobepowerful:CALCITE-7584

Conversation

@iwanttobepowerful

Copy link
Copy Markdown
Contributor

@iwanttobepowerful iwanttobepowerful requested review from mihaibudiu and suibianwanwank and removed request for mihaibudiu June 6, 2026 02:46
# Correlated LATERAL sub-query with a window expression.
# The equality predicate between the inner and outer query must remain applied
# after decorrelation.
SELECT e.ename, d.deptno, d.m

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iwanttobepowerful iwanttobepowerful force-pushed the CALCITE-7584 branch 2 times, most recently from cd476d3 to ac8b355 Compare June 6, 2026 03:58
.with(CalciteAssert.Config.REGULAR)
.query(sql)
.returnsUnordered("name=Bill; deptno=10; M=190",
"name=Bill; deptno=30; M=190",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iwanttobepowerful iwanttobepowerful force-pushed the CALCITE-7584 branch 5 times, most recently from 49d666d to 1621414 Compare June 6, 2026 16:14
* <p>3.3 Unnesting Rules
* (https://dl.gi.de/server/api/core/bitstreams/c1918e8c-6a87-4da2-930a-bfed289f2388/content)
*/
@Override public RexNode visitOver(RexOver over) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to conflict with #4998, so let's merge the other one first if it turns out to be right, so we can see the final shape of this function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the two fixes are related but apply to different shuttles. This PR updates visitOver in DecorrelateRexShuttle, while #4998 updates visitOver in RemoveCorrelationRexShuttle.

I also verified that if RemoveCorrelationForScalarAggregateRule is disabled in removeCorrelationViaRule(), the #4998 fix does not seem to be needed. So #4998 appears to address the rule-based decorrelation path, whereas this PR addresses the DecorrelateRexShuttle path.

Comment thread core/src/test/resources/sql/sub-query.iq Outdated
…ted LATERAL sub-queries with window functions
@sonarqubecloud

sonarqubecloud Bot commented Jun 8, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants