You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/06/13 14:12:47 UTC

(datafusion) 01/01: Clearly gate sliding SUM(DISTINCT) type support (#22866)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-22866-3bece3dde20cf8973ded8a8dc44978bda64cf640
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 58e37a0b3af584761164edb831522e3cc9aee8d0
Author: Kumar Ujjawal <uj...@gmail.com>
AuthorDate: Sat Jun 13 19:42:24 2026 +0530

    Clearly gate sliding SUM(DISTINCT) type support (#22866)
    
    ## Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax. For example
    `Closes #123` indicates that this PR will close issue #123.
    -->
    
    - Closes #22820.
    
    ## Rationale for this change
    
    Sliding `SUM(DISTINCT)` only supports `Int64`, but it was routed through
    the wider `SUM` type dispatch path. This made unsupported types fail
    with a less clear accumulator error.
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    ## What changes are included in this PR?
    
    This PR adds an explicit `Int64` gate for sliding `SUM(DISTINCT)`.
    
    Unsupported types now return a clear feature error that names the
    operation and type. The existing `Int64` path is unchanged.
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    ## Are these changes tested?
    
    Yes
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    ## Are there any user-facing changes?
    
    No public API change
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
---
 datafusion/functions-aggregate/src/sum.rs     | 21 ++++++++++------
 datafusion/sqllogictest/test_files/window.slt | 35 +++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 7 deletions(-)

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org