From 5c49bcf1ec9fe5af350dd13d6b52646049a769aa Mon Sep 17 00:00:00 2001 From: virajchogle Date: Tue, 2 Jun 2026 15:11:31 -0400 Subject: [PATCH 1/2] sql: reject WITHIN GROUP on non-ordered-set aggregates WITHIN GROUP (ORDER BY ...) was silently ignored on non-ordered-set functions. Match PostgreSQL by rejecting with SQLSTATE 42809. Fixes #171236 Release note (bug fix): WITHIN GROUP on a function that is not an ordered-set aggregate now returns an error matching PostgreSQL. --- pkg/sql/logictest/testdata/logic_test/aggregate | 12 ++++++++++++ pkg/sql/opt/optbuilder/scope.go | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/sql/logictest/testdata/logic_test/aggregate b/pkg/sql/logictest/testdata/logic_test/aggregate index 32ebd8e22046..84126a1be40f 100644 --- a/pkg/sql/logictest/testdata/logic_test/aggregate +++ b/pkg/sql/logictest/testdata/logic_test/aggregate @@ -3567,6 +3567,18 @@ SELECT percentile_disc(0.50) FROM osagg statement error ordered-set aggregations must have a WITHIN GROUP clause containing one ORDER BY column SELECT percentile_cont(0.50) FROM osagg +# Test that WITHIN GROUP is rejected on functions that are not ordered-set +# aggregates (#171236). Covers non-aggregate scalar builtins and general +# aggregates. +statement error pgcode 42809 function concat_ws is not an ordered set aggregate +SELECT concat_ws(',', 'a', 'b') WITHIN GROUP (ORDER BY 1) + +statement error pgcode 42809 function abs is not an ordered set aggregate +SELECT abs(-5) WITHIN GROUP (ORDER BY 1) + +statement error pgcode 42809 function sum is not an ordered set aggregate +SELECT sum(f) WITHIN GROUP (ORDER BY f) FROM osagg + # Tests for min/max on collated strings. statement ok CREATE TABLE t_collate (x STRING COLLATE en_us); diff --git a/pkg/sql/opt/optbuilder/scope.go b/pkg/sql/opt/optbuilder/scope.go index 2904a8a97875..717158d250f1 100644 --- a/pkg/sql/opt/optbuilder/scope.go +++ b/pkg/sql/opt/optbuilder/scope.go @@ -1162,6 +1162,17 @@ func (s *scope) VisitPre(expr tree.Expr) (recurse bool, newExpr tree.Expr) { panic(err) } + // WITHIN GROUP is only valid on ordered-set aggregate functions. + if t.AggType == tree.OrderedSetAgg { + if _, found := isOrderedSetAggregate(def); !found { + panic(pgerror.Newf( + pgcode.WrongObjectType, + "function %s is not an ordered set aggregate", + def.Name, + )) + } + } + if isGenerator(def) && s.replaceSRFs { expr = s.replaceSRF(t, def) break From e42b3b4079284a63aab51c658b5bb042604dfcfb Mon Sep 17 00:00:00 2001 From: virajchogle Date: Tue, 9 Jun 2026 13:20:52 -0400 Subject: [PATCH 2/2] sql: fix SHOW ENUMS delegate to use valid PG syntax The SHOW ENUMS delegate used array_agg(...) WITHIN GROUP (ORDER BY ...), which was silently accepted before but is now rejected. Switch to array_agg(x ORDER BY y), same semantics with valid PG syntax. Release note: None --- pkg/sql/delegate/show_enums.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sql/delegate/show_enums.go b/pkg/sql/delegate/show_enums.go index 1cdef444b12d..fed267364ebc 100644 --- a/pkg/sql/delegate/show_enums.go +++ b/pkg/sql/delegate/show_enums.go @@ -39,7 +39,7 @@ func (d *delegator) delegateShowEnums(n *tree.ShowEnums) (tree.Statement, error) WITH enums(enumtypid, values) AS ( SELECT enums.enumtypid AS enumtypid, - array_agg(enums.enumlabel) WITHIN GROUP (ORDER BY (enumsortorder)) AS values + array_agg(enums.enumlabel ORDER BY enums.enumsortorder) AS values FROM %[1]s.pg_catalog.pg_enum AS enums GROUP BY enumtypid )