fix(optimizer)!: don't fan out EXISTS decorrelation over grouped subqueries - #8318
Merged
Conversation
geooo109
reviewed
Sep 8, 2026
Contributor
SQLGlot Integration Test Results✅ All tests passedComparing:
Overallmain: 182937 total, 163862 passed (pass rate: 89.6%) sqlglot:jo/exists_group_by_decorrelation: 170743 total, 152710 passed (pass rate: 89.4%) Transitions: Dialect pair changes: 0 previous results not found, 3 current results not found ✅ All tests passed |
…ueries Unnesting an EXISTS subquery with its own GROUP BY appended the join key to the existing grouping, so the derived table was no longer unique on it and the LEFT JOIN emitted one outer row per group. A plain GROUP BY, DISTINCT or ORDER BY can't change whether the subquery returns rows (we already bail earlier for ORDER BY + LIMIT), so they're dropped before decorrelating. Since HAVING and QUALIFY do filter the groups, and grouping sets yield a row even on empty input, those subqueries are no longer unnested. See also: #8317 (comment).
georgesittas
force-pushed
the
jo/exists_group_by_decorrelation
branch
from
September 8, 2026 15:52
87962f9 to
79b1e16
Compare
geooo109
approved these changes
Sep 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Unnesting an EXISTS subquery with its own GROUP BY appended the join key to the existing grouping, so the derived table was no longer unique on it and the LEFT JOIN emitted one outer row per group. A plain GROUP BY, DISTINCT or ORDER BY can't change whether the subquery returns rows (we already bail earlier for ORDER BY + LIMIT), so they're dropped before decorrelating. Since HAVING and QUALIFY do filter the groups, and grouping sets yield a row even on empty input, those subqueries are no longer unnested.
See also: #8317 (comment).