This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 6ed22bb7cd implement sum for durations (#18853)
6ed22bb7cd is described below
commit 6ed22bb7cd2d8f078743b00dbc0616341871d463
Author: Vedic Chawla <[email protected]>
AuthorDate: Sat Nov 22 20:03:22 2025 +0530
implement sum for durations (#18853)
## 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 #18771 .
## Rationale for this change
<!--
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?
<!--
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?
<!--
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?
<!--
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 | 32 +++++++++++++++++++++---
datafusion/sqllogictest/test_files/aggregate.slt | 30 ++++++++++++++++++++++
2 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/datafusion/functions-aggregate/src/sum.rs
b/datafusion/functions-aggregate/src/sum.rs
index d726b9fad1..d40709a467 100644
--- a/datafusion/functions-aggregate/src/sum.rs
+++ b/datafusion/functions-aggregate/src/sum.rs
@@ -22,9 +22,10 @@ use arrow::array::{Array, ArrayRef, ArrowNativeTypeOp,
ArrowNumericType, AsArray
use arrow::datatypes::Field;
use arrow::datatypes::{
ArrowNativeType, DataType, Decimal128Type, Decimal256Type, Decimal32Type,
- Decimal64Type, FieldRef, Float64Type, Int64Type, UInt64Type,
- DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION,
DECIMAL32_MAX_PRECISION,
- DECIMAL64_MAX_PRECISION,
+ Decimal64Type, DurationMicrosecondType, DurationMillisecondType,
+ DurationNanosecondType, DurationSecondType, FieldRef, Float64Type,
Int64Type,
+ TimeUnit, UInt64Type, DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION,
+ DECIMAL32_MAX_PRECISION, DECIMAL64_MAX_PRECISION,
};
use datafusion_common::types::{
logical_float64, logical_int16, logical_int32, logical_int64, logical_int8,
@@ -93,6 +94,27 @@ macro_rules! downcast_sum {
DataType::Decimal256(_, _) => {
$helper!(Decimal256Type,
$args.return_field.data_type().clone())
}
+ DataType::Duration(TimeUnit::Second) => {
+ $helper!(DurationSecondType,
$args.return_field.data_type().clone())
+ }
+ DataType::Duration(TimeUnit::Millisecond) => {
+ $helper!(
+ DurationMillisecondType,
+ $args.return_field.data_type().clone()
+ )
+ }
+ DataType::Duration(TimeUnit::Microsecond) => {
+ $helper!(
+ DurationMicrosecondType,
+ $args.return_field.data_type().clone()
+ )
+ }
+ DataType::Duration(TimeUnit::Nanosecond) => {
+ $helper!(
+ DurationNanosecondType,
+ $args.return_field.data_type().clone()
+ )
+ }
_ => {
not_impl_err!(
"Sum not supported for {}: {}",
@@ -159,6 +181,9 @@ impl Sum {
vec![TypeSignatureClass::Float],
NativeType::Float64,
)]),
+ TypeSignature::Coercible(vec![Coercion::new_exact(
+ TypeSignatureClass::Duration,
+ )]),
],
Volatility::Immutable,
),
@@ -208,6 +233,7 @@ impl AggregateUDFImpl for Sum {
let new_precision = DECIMAL256_MAX_PRECISION.min(*precision +
10);
Ok(DataType::Decimal256(new_precision, *scale))
}
+ DataType::Duration(time_unit) =>
Ok(DataType::Duration(*time_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 83faae0db5..a1b868b0b0 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -4623,6 +4623,16 @@ SELECT max(column1), max(column2), max(column3),
max(column4) FROM d;
----
0 days 0 hours 0 mins 11 secs 0 days 0 hours 0 mins 0.022 secs 0 days 0 hours
0 mins 0.000033 secs 0 days 0 hours 0 mins 0.000000044 secs
+query ????
+SELECT avg(column1), avg(column2), avg(column3), avg(column4) FROM d;
+----
+0 days 0 hours 0 mins 6 secs 0 days 0 hours 0 mins 0.012 secs 0 days 0 hours 0
mins 0.000018 secs 0 days 0 hours 0 mins 0.000000024 secs
+
+query ????
+SELECT sum(column1), sum(column2), sum(column3), sum(column4) FROM d;
+----
+0 days 0 hours 0 mins 12 secs 0 days 0 hours 0 mins 0.024 secs 0 days 0 hours
0 mins 0.000036 secs 0 days 0 hours 0 mins 0.000000048 secs
+
# GROUP BY follows a different code path
query ????I
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d
GROUP BY column5;
@@ -4634,6 +4644,16 @@ SELECT max(column1), max(column2), max(column3),
max(column4), column5 FROM d GR
----
0 days 0 hours 0 mins 11 secs 0 days 0 hours 0 mins 0.022 secs 0 days 0 hours
0 mins 0.000033 secs 0 days 0 hours 0 mins 0.000000044 secs 1
+query ????I
+SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d
GROUP BY column5;
+----
+0 days 0 hours 0 mins 6 secs 0 days 0 hours 0 mins 0.012 secs 0 days 0 hours 0
mins 0.000018 secs 0 days 0 hours 0 mins 0.000000024 secs 1
+
+query ????I
+SELECT sum(column1), sum(column2), sum(column3), sum(column4), column5 FROM d
GROUP BY column5;
+----
+0 days 0 hours 0 mins 12 secs 0 days 0 hours 0 mins 0.024 secs 0 days 0 hours
0 mins 0.000036 secs 0 days 0 hours 0 mins 0.000000048 secs 1
+
statement ok
INSERT INTO d VALUES
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'),
arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'),
1),
@@ -4649,6 +4669,16 @@ SELECT min(column1), min(column2), min(column3),
min(column4), column5 FROM d GR
----
0 days 0 hours 0 mins 0 secs 0 days 0 hours 0 mins 0.001 secs 0 days 0 hours 0
mins 0.000003 secs 0 days 0 hours 0 mins 0.000000002 secs 1
+query ????I
+SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d
GROUP BY column5 ORDER BY column5;
+----
+0 days 0 hours 0 mins 3 secs 0 days 0 hours 0 mins 0.008 secs 0 days 0 hours 0
mins 0.000012 secs 0 days 0 hours 0 mins 0.000000014 secs 1
+
+query ????I
+SELECT sum(column1), sum(column2), sum(column3), sum(column4), column5 FROM d
GROUP BY column5 ORDER BY column5;
+----
+0 days 0 hours 0 mins 15 secs 0 days 0 hours 0 mins 0.034 secs 0 days 0 hours
0 mins 0.000048 secs 0 days 0 hours 0 mins 0.000000058 secs 1
+
statement ok
drop table d;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]