sql: preserve explicit ROW constructors during tuple flattening - #174679
Open
Alignyx wants to merge 1 commit into
Open
sql: preserve explicit ROW constructors during tuple flattening#174679Alignyx wants to merge 1 commit into
Alignyx wants to merge 1 commit into
Conversation
Tuple list flattening treated explicit ROW constructors like syntactic parenthesized lists. This turned GROUP BY ROW() into an empty grouping set and exposed fields of ROW(a, b) as individual grouping columns. Honor Tuple.Row at both top-level and recursive flattening, while retaining existing recursive expansion for syntactic parenthesized lists. Fixes cockroachdb#173264 Release note (bug fix): GROUP BY now treats explicit ROW(...) constructors as single composite grouping expressions.
|
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
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.
Summary
Fixes #173264.
Explicit ROW constructors are scalar composite expressions, but clause tuple flattening treated them like syntactic parenthesized expression lists. This caused two incorrect GROUP BY behaviors:
Syntactic lists such as GROUP BY (a, b) still need to flatten recursively, so the fix must retain the parser distinction between those lists and explicit ROW constructors.
Root cause
The parser already records this distinction in tree.Tuple.Row: explicit ROW(...) syntax sets Row to true, while a parenthesized list leaves it false.
After type resolution, optbuilder calls flattenTuples while expanding GROUP BY and related clause expression lists. Both flattenTuples and its recursive helper previously expanded every tree.Tuple without consulting Row. Consequently:
The semantic distinction was therefore lost during optbuilder clause expansion, before physical planning or aggregate execution.
Fix
Tuple flattening now checks Tuple.Row at both the top-level and recursive boundaries:
Because the helper is shared by GROUP BY, statement ORDER BY, aggregate-internal ORDER BY, and window PARTITION BY / ORDER BY processing, this preserves the same explicit-ROW boundary consistently across all callers.
Tests
Added a regression subtest covering:
Validation included the regression in all 12 applicable logic-test configurations, the complete local aggregate logic file, and the full optbuilder data-driven test suite.
Release note (bug fix): GROUP BY now treats explicit ROW(...) constructors as single composite grouping expressions.