Skip to content

Fix an aggregate over a whole-record argument planning incorrectly - #4501

Open
robert-brunel wants to merge 1 commit into
apple/robert-brunel/pullup-1from
apple/robert-brunel/pullup-2
Open

Fix an aggregate over a whole-record argument planning incorrectly#4501
robert-brunel wants to merge 1 commit into
apple/robert-brunel/pullup-1from
apple/robert-brunel/pullup-2

Conversation

@robert-brunel

@robert-brunel robert-brunel commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Fix incorrect plans for aggregates over a whole-record argument

  • Change Expression.pullUp() to simplify both sides before matching them, rather than only the candidate.
  • Change OrderByExpression.pullUp() the same way.

An aggregate whose argument effectively reconstructs a whole record, such as SELECT COUNT((T.*)) FROM T, failed to plan. In a grouped query the same aggregate value occurs twice, in the group-by expression that computes it and in the projection. In the projection, pullUp() has to replace the expression with a reference to the group-by output for the plan to stay correct. This is done by matching the two values structurally. Matching the unsimplified projection against the simplified candidate could not succeed for that argument, since the simplification rules canonicalize (T.*) to just T via Values.collapseSimpleSelectMaybe().

Fixes #4481.

* Change **Expression.pullUp()** to simplify _both_ sides before matching them, rather than only the candidate.
* Change **OrderByExpression.pullUp()** the same way, which keeps tolerating an ambiguous match as it did before.

An aggregate whose argument effectively reconstructs a whole record, such as `SELECT COUNT((T.*)) FROM T`, failed to plan. In a grouped query the same aggregate value occurs twice, in the group-by expression that computes it and in the projection, where `pullUp()` has to replace it with a reference to the group-by output by matching the two structurally. Matching the *unsimplified* projection against the simplified candidate could not succeed for that argument, since the simplification rules canonicalize it: `Values.collapseSimpleSelectMaybe()` collapses `(T.*)` to just `T`.

An `ORDER BY` over an aggregate remains unsupported either way; the whole-record spelling is now rejected as such instead of failing at evaluation time.

Fixes #4481.
@robert-brunel robert-brunel added the bug fix Change that fixes a bug label Aug 24, 2026
@robert-brunel robert-brunel self-assigned this Aug 24, 2026
@robert-brunel
robert-brunel marked this pull request as ready for review August 24, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix Change that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An aggregate such as COUNT((T.*)) over a whole-record argument produces an incorrect plan

1 participant