From f0a61471ae8ee1ad36b6eee73cfb7c783e3f072e Mon Sep 17 00:00:00 2001 From: Sergey Zhukov Date: Wed, 18 Feb 2026 16:27:59 +0400 Subject: [PATCH] Disallow order by within ordered-set aggregate functions argument lists(#18281) --- datafusion/sql/src/expr/function.rs | 6 ++++++ datafusion/sqllogictest/test_files/aggregate.slt | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/datafusion/sql/src/expr/function.rs b/datafusion/sql/src/expr/function.rs index 641f3bb8dcad1..2f41b88e8435b 100644 --- a/datafusion/sql/src/expr/function.rs +++ b/datafusion/sql/src/expr/function.rs @@ -542,6 +542,12 @@ impl SqlToRel<'_, S> { // accept a WITHIN GROUP clause. let supports_within_group = fm.supports_within_group_clause(); + if supports_within_group && !order_by.is_empty() { + return plan_err!( + "ORDER BY must be specified using WITHIN GROUP for ordered-set aggregate functions" + ); + } + if !within_group.is_empty() && !supports_within_group { return plan_err!( "WITHIN GROUP is only supported for ordered-set aggregate functions" diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt index 517467110fe6d..96ce4b7b77322 100644 --- a/datafusion/sqllogictest/test_files/aggregate.slt +++ b/datafusion/sqllogictest/test_files/aggregate.slt @@ -129,6 +129,14 @@ CREATE TABLE group_median_table_nullable ( # Error tests ####### +statement error ORDER BY must be specified using WITHIN GROUP +select quantile_cont(col0, 0.75 order by col0) +from values (1, 3), (2, 2), (3, 1) t(col0, col1); + +statement error ORDER BY must be specified using WITHIN GROUP +select quantile_cont(0.75 order by col0) +from values (1, 3), (2, 2), (3, 1) t(col0, col1); + statement error DataFusion error: Error during planning: WITHIN GROUP is only supported for ordered-set aggregate functions SELECT SUM(c2) WITHIN GROUP (ORDER BY c2) FROM aggregate_test_100