Skip to content

Apache DataFusion SQL Query Engine

Clearly gate sliding SUM(DISTINCT) type support (#22866)

Kumar Ujjawal58e37a0b3af5
diff --git a/datafusion/functions-aggregate/src/sum.rs b/datafusion/functions-aggregate/src/sum.rs
index c3c2e5e0b9..1a1f9c59a2 100644
--- a/datafusion/functions-aggregate/src/sum.rs
+++ b/datafusion/functions-aggregate/src/sum.rs
@@ -303,13 +303,18 @@ impl AggregateUDFImpl for Sum {
args: AccumulatorArgs,
) -> Result<Box<dyn Accumulator>> {
if args.is_distinct {
- // distinct path: use our sliding‐window distinct‐sum
- macro_rules! helper_distinct {
- ($t:ty, $dt:expr) => {
- Ok(Box::new(SlidingDistinctSumAccumulator::try_new(&$dt)?))
- };
+ // distinct path: [`SlidingDistinctSumAccumulator`] only implements
+ // Int64, so gate the supported type here rather than dispatching
+ // through `downcast_sum!`, which accepts every SUM type
+ match args.return_field.data_type() {
+ DataType::Int64 => Ok(Box::new(SlidingDistinctSumAccumulator::try_new(
+ &DataType::Int64,
+ )?)),
+ _ => not_impl_err!(
+ "SUM(DISTINCT) over sliding window frames is only supported for Int64, got {}",
+ args.expr_fields[0].data_type()
+ ),
}
- downcast_sum!(args, helper_distinct)
} else {
// non‐distinct path: existing sliding sum
macro_rules! helper {
@@ -525,7 +530,9 @@ impl SlidingDistinctSumAccumulator {
pub fn try_new(data_type: &DataType) -> Result<Self> {
// TODO support other numeric types
if *data_type != DataType::Int64 {
- return exec_err!("SlidingDistinctSumAccumulator only supports Int64");
+ return exec_err!(
+ "SlidingDistinctSumAccumulator only supports Int64, got {data_type}"
+ );
}
Ok(Self {
counts: HashMap::default(),
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 59b43d6476..090f44f562 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -5981,6 +5981,41 @@ FROM table_distinct_sum_nulls;
5 5
+# SUM(DISTINCT) over sliding (bounded) window frames is only implemented
+# for Int64. Other SUM-supported input types must fail with a clear
+# capability error instead of an accumulator-internal one.
+statement ok
+CREATE TABLE table_distinct_sum_types(ts INT, f DOUBLE, d DECIMAL(10, 2)) AS VALUES
+ (1, 1.5, 1.50), (2, 2.5, 2.50), (3, 1.5, 1.50);
+
+query error DataFusion error: This feature is not implemented: SUM\(DISTINCT\) over sliding window frames is only supported for Int64, got Float64
+SELECT SUM(DISTINCT f) OVER (
+ ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
+) FROM table_distinct_sum_types;
+
+query error DataFusion error: This feature is not implemented: SUM\(DISTINCT\) over sliding window frames is only supported for Int64, got Decimal128\(10, 2\)
+SELECT SUM(DISTINCT d) OVER (
+ ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
+) FROM table_distinct_sum_types;
+
+query error DataFusion error: This feature is not implemented: SUM\(DISTINCT\) over sliding window frames is only supported for Int64, got UInt64
+SELECT SUM(DISTINCT arrow_cast(ts, 'UInt64')) OVER (
+ ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
+) FROM table_distinct_sum_types;
+
+# Unbounded frames take the regular distinct-sum path and keep
+# supporting all SUM input types.
+query R
+SELECT SUM(DISTINCT f) OVER (ORDER BY ts) FROM table_distinct_sum_types;
+----
+1.5
+4
+4
+
+statement ok
+DROP TABLE table_distinct_sum_types;
+
+
# FILTER clause with window functions
# Verify FILTER clause with non-aggregate window functions fails with a clear message