Skip to content

fix: preserve DISTINCT semantics in lateral decorrelation - #26060

Closed
mikamikasuki wants to merge 2 commits into
apache:mainfrom
mikamikasuki:fix/25808-lateral-distinct-decorrelation
Closed

mikamikasuki wants to merge 2 commits into
apache:mainfrom
mikamikasuki:fix/25808-lateral-distinct-decorrelation

Conversation

@mikamikasuki

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The query in #25808 returns the same (outer key, distinct value) row more than once. Moving a correlated filter above DISTINCT makes rows that differ only in the filtered inner column appear distinct, even though that column is not part of the visible result.

What changes are included in this PR?

For the supported INNER LATERAL shape, apply the correlated filter before the DISTINCT aggregation. A unique row number on each outer input row preserves bag semantics when the outer input itself contains duplicates. Unsupported plan shapes continue through the existing decorrelation path.

What is the testing strategy for this PR?

Added lateral_join.slt regression cases for the issue query, duplicate outer rows, and explicit JOIN LATERAL ... ON TRUE. The issue case fails on the base commit and passes with this change. The optimizer unit and integration tests, focused lateral join sqllogictests, the extended workspace test suite, strict optimizer clippy, formatting, and git diff --check passed.

Are there any user-facing changes?

The affected LATERAL DISTINCT query now returns the expected distinct rows.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 5, 2026
@alamb alamb added the Prolific Author author opened PRs or issues in more than 20 repositories in the last 30 days label Oct 22, 2026
@alamb

alamb commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution! I am closing this under DataFusion's AI Dump policy.

https://datafusion.apache.org/contributor-guide/ai-policy.html#what-is-an-ai-dump-and-why-it-is-not-helpful

This has the hallmarks of AI code that was not reviewed by the author: one of eight PRs opened in a single day, adding a 170-line special-case rewrite for one plan shape with many silent fallbacks, and a description listing local checks run.

If you would like to continue, please open a focused PR in your own words that you have reviewed and understand, and we are happy to help with review.

@alamb alamb closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules Prolific Author author opened PRs or issues in more than 20 repositories in the last 30 days sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: LATERAL with SELECT DISTINCT and a non-equality correlated filter returns duplicate rows

2 participants