Skip to content

sql: reject WITHIN GROUP on non-ordered-set aggregates#171369

Open
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:within-group-non-ordered-set-171236
Open

sql: reject WITHIN GROUP on non-ordered-set aggregates#171369
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:within-group-non-ordered-set-171236

Conversation

@virajchogle
Copy link
Copy Markdown
Contributor

Fixes #171236

WITHIN GROUP (ORDER BY ...) is only valid on ordered-set aggregate functions (percentile_disc, percentile_cont). CockroachDB silently ignored the clause on general aggregates (sum, avg, ...) and non-aggregate scalar functions (concat_ws, abs, ...), diverging from PostgreSQL's behavior.

This rejects the misuse in scope.VisitPre against the isOrderedSetAggregate allowlist, using pgcode.WrongObjectType (SQLSTATE 42809) to match PostgreSQL.

Epic: none

Release note (bug fix): WITHIN GROUP on a function that is not an ordered-set aggregate now returns an error matching PostgreSQL.

WITHIN GROUP (ORDER BY ...) was silently ignored on non-ordered-set
functions. Match PostgreSQL by rejecting with SQLSTATE 42809.

Fixes cockroachdb#171236

Release note (bug fix): WITHIN GROUP on a function that is not an
ordered-set aggregate now returns an error matching PostgreSQL.
@virajchogle virajchogle requested a review from a team as a code owner June 2, 2026 19:19
@virajchogle virajchogle requested review from michae2 and removed request for a team June 2, 2026 19:19
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Jun 2, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Jun 2, 2026

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.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jun 2, 2026
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

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

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ordinary functions accept WITHIN GROUP (ORDER BY ...) without error

2 participants