Skip to content

Commit 6ed22bb

Browse files
authored
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. -->
1 parent 5f22722 commit 6ed22bb

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

datafusion/functions-aggregate/src/sum.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ use arrow::array::{Array, ArrayRef, ArrowNativeTypeOp, ArrowNumericType, AsArray
2222
use arrow::datatypes::Field;
2323
use arrow::datatypes::{
2424
ArrowNativeType, DataType, Decimal128Type, Decimal256Type, Decimal32Type,
25-
Decimal64Type, FieldRef, Float64Type, Int64Type, UInt64Type,
26-
DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION, DECIMAL32_MAX_PRECISION,
27-
DECIMAL64_MAX_PRECISION,
25+
Decimal64Type, DurationMicrosecondType, DurationMillisecondType,
26+
DurationNanosecondType, DurationSecondType, FieldRef, Float64Type, Int64Type,
27+
TimeUnit, UInt64Type, DECIMAL128_MAX_PRECISION, DECIMAL256_MAX_PRECISION,
28+
DECIMAL32_MAX_PRECISION, DECIMAL64_MAX_PRECISION,
2829
};
2930
use datafusion_common::types::{
3031
logical_float64, logical_int16, logical_int32, logical_int64, logical_int8,
@@ -93,6 +94,27 @@ macro_rules! downcast_sum {
9394
DataType::Decimal256(_, _) => {
9495
$helper!(Decimal256Type, $args.return_field.data_type().clone())
9596
}
97+
DataType::Duration(TimeUnit::Second) => {
98+
$helper!(DurationSecondType, $args.return_field.data_type().clone())
99+
}
100+
DataType::Duration(TimeUnit::Millisecond) => {
101+
$helper!(
102+
DurationMillisecondType,
103+
$args.return_field.data_type().clone()
104+
)
105+
}
106+
DataType::Duration(TimeUnit::Microsecond) => {
107+
$helper!(
108+
DurationMicrosecondType,
109+
$args.return_field.data_type().clone()
110+
)
111+
}
112+
DataType::Duration(TimeUnit::Nanosecond) => {
113+
$helper!(
114+
DurationNanosecondType,
115+
$args.return_field.data_type().clone()
116+
)
117+
}
96118
_ => {
97119
not_impl_err!(
98120
"Sum not supported for {}: {}",
@@ -159,6 +181,9 @@ impl Sum {
159181
vec![TypeSignatureClass::Float],
160182
NativeType::Float64,
161183
)]),
184+
TypeSignature::Coercible(vec![Coercion::new_exact(
185+
TypeSignatureClass::Duration,
186+
)]),
162187
],
163188
Volatility::Immutable,
164189
),
@@ -208,6 +233,7 @@ impl AggregateUDFImpl for Sum {
208233
let new_precision = DECIMAL256_MAX_PRECISION.min(*precision + 10);
209234
Ok(DataType::Decimal256(new_precision, *scale))
210235
}
236+
DataType::Duration(time_unit) => Ok(DataType::Duration(*time_unit)),
211237
other => {
212238
exec_err!("[return_type] SUM not supported for {}", other)
213239
}

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,6 +4623,16 @@ SELECT max(column1), max(column2), max(column3), max(column4) FROM d;
46234623
----
46244624
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
46254625

4626+
query ????
4627+
SELECT avg(column1), avg(column2), avg(column3), avg(column4) FROM d;
4628+
----
4629+
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
4630+
4631+
query ????
4632+
SELECT sum(column1), sum(column2), sum(column3), sum(column4) FROM d;
4633+
----
4634+
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
4635+
46264636
# GROUP BY follows a different code path
46274637
query ????I
46284638
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
46344644
----
46354645
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
46364646

4647+
query ????I
4648+
SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d GROUP BY column5;
4649+
----
4650+
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
4651+
4652+
query ????I
4653+
SELECT sum(column1), sum(column2), sum(column3), sum(column4), column5 FROM d GROUP BY column5;
4654+
----
4655+
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
4656+
46374657
statement ok
46384658
INSERT INTO d VALUES
46394659
(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
46494669
----
46504670
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
46514671

4672+
query ????I
4673+
SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4674+
----
4675+
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
4676+
4677+
query ????I
4678+
SELECT sum(column1), sum(column2), sum(column3), sum(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4679+
----
4680+
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
4681+
46524682
statement ok
46534683
drop table d;
46544684

0 commit comments

Comments
 (0)