Skip to content

Apache DataFusion SQL Query Engine

feat(functions-aggregate): support sum(interval) (#23177)

Subham Singhal77545a4f2734
diff --git a/datafusion/functions-aggregate/src/sum.rs b/datafusion/functions-aggregate/src/sum.rs
index 1a1f9c59a2..8d1df28559 100644
--- a/datafusion/functions-aggregate/src/sum.rs
+++ b/datafusion/functions-aggregate/src/sum.rs
@@ -24,7 +24,8 @@ use arrow::datatypes::{
DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION, DataType, Decimal32Type,
Decimal64Type, Decimal128Type, Decimal256Type, DurationMicrosecondType,
DurationMillisecondType, DurationNanosecondType, DurationSecondType, FieldRef,
- Float64Type, Int64Type, TimeUnit, UInt64Type,
+ Float64Type, Int64Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit,
+ IntervalYearMonthType, TimeUnit, UInt64Type,
};
use datafusion_common::hash_utils::RandomState;
use datafusion_common::internal_err;
@@ -117,6 +118,21 @@ macro_rules! downcast_sum {
$args.return_field.data_type().clone()
)
}
+ DataType::Interval(IntervalUnit::YearMonth) => {
+ $helper!(
+ IntervalYearMonthType,
+ $args.return_field.data_type().clone()
+ )
+ }
+ DataType::Interval(IntervalUnit::DayTime) => {
+ $helper!(IntervalDayTimeType, $args.return_field.data_type().clone())
+ }
+ DataType::Interval(IntervalUnit::MonthDayNano) => {
+ $helper!(
+ IntervalMonthDayNanoType,
+ $args.return_field.data_type().clone()
+ )
+ }
_ => {
not_impl_err!(
"Sum not supported for {}: {}",
@@ -186,6 +202,9 @@ impl Sum {
TypeSignature::Coercible(vec![Coercion::new_exact(
TypeSignatureClass::Duration,
)]),
+ TypeSignature::Coercible(vec![Coercion::new_exact(
+ TypeSignatureClass::Interval,
+ )]),
],
Volatility::Immutable,
),
@@ -232,6 +251,7 @@ impl AggregateUDFImpl for Sum {
Ok(DataType::Decimal256(new_precision, *scale))
}
DataType::Duration(time_unit) => Ok(DataType::Duration(*time_unit)),
+ DataType::Interval(interval_unit) => Ok(DataType::Interval(*interval_unit)),
other => {
exec_err!("[return_type] SUM not supported for {}", other)
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index 18c09acf08..9c9d38c297 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -6551,6 +6551,99 @@ c NULL 2
statement ok
drop table dn;
+# sum_interval
+# Component-wise sum across all three Interval variants (matches PostgreSQL).
+
+# Basic Interval(MonthDayNano): the issue's repro.
+# (0 mons, 0 days, 1s) + (12 mons, 0, 0) + (1 mon, 0, 0) = (13 mons, 0, 1s)
+query T?
+SELECT arrow_typeof(sum(v)), sum(v) FROM (VALUES
+ (interval '1 second'),
+ (interval '1 year'),
+ (interval '1 month')) t(v);
+----
+Interval(MonthDayNano) 13 mons 1.000000000 secs
+
+# NULLs are skipped.
+query ?
+SELECT sum(v) FROM (VALUES
+ (interval '1 day'),
+ (NULL),
+ (interval '2 days')) t(v);
+----
+3 days
+
+# Empty input → NULL.
+query ?
+SELECT sum(v) FROM (VALUES (interval '1 day')) t(v) WHERE 1 = 0;
+----
+NULL
+
+# GROUP BY exercises the PrimitiveGroupsAccumulator path.
+query I? rowsort
+SELECT k, sum(v) FROM (VALUES
+ (1, interval '1 day'),
+ (1, interval '2 days'),
+ (2, interval '1 month')) t(k, v)
+GROUP BY k;
+----
+1 3 days
+2 1 mons
+
+# Interval(YearMonth) via cast.
+query T?
+SELECT arrow_typeof(sum(v)), sum(v) FROM (VALUES
+ (arrow_cast('1 year', 'Interval(YearMonth)')),
+ (arrow_cast('6 months', 'Interval(YearMonth)'))) t(v);
+----
+Interval(YearMonth) 1 years 6 mons
+
+# Interval(DayTime) via cast.
+query T?
+SELECT arrow_typeof(sum(v)), sum(v) FROM (VALUES
+ (arrow_cast('1 day', 'Interval(DayTime)')),
+ (arrow_cast('1 day', 'Interval(DayTime)'))) t(v);
+----
+Interval(DayTime) 2 days
+
+# Sliding window sum on intervals.
+query ??
+SELECT v, sum(v) OVER (ORDER BY v ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+FROM (VALUES
+ (interval '1 day'),
+ (interval '2 days'),
+ (interval '3 days')) t(v);
+----
+1 days 1 days
+2 days 3 days
+3 days 5 days
+
+# DISTINCT sum drops duplicates (DistinctSumAccumulator path).
+query ?
+SELECT sum(DISTINCT v) FROM (VALUES
+ (interval '1 day'),
+ (interval '1 day'),
+ (interval '2 days')) t(v);
+----
+3 days
+
+# SUM(col + interval_lit) — exercises the simplify_expr_op_literal path.
+query ?
+SELECT sum(v + interval '1 day') FROM (VALUES
+ (interval '1 day'),
+ (interval '2 days'),
+ (interval '3 days')) t(v);
+----
+9 days
+
+# Negative intervals: component-wise wrapping_add over signed i32/i64.
+query ?
+SELECT sum(v) FROM (VALUES
+ (interval '1 day'),
+ (interval '-3 days')) t(v);
+----
+-2 days
+
# Prepare the table with dictionary values for testing
statement ok
CREATE TABLE value(x bigint) AS VALUES (1), (2), (3), (1), (3), (4), (5), (2);