Skip to content

Commit b6ddc6c

Browse files
authored
Backport GroupsAccumulator for Duration min/max agg (#288)
Co-authored-by: svranesevic <svranesevic@users.noreply.github.com>
1 parent 5623bcf commit b6ddc6c

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

datafusion/physical-expr/src/aggregate/min_max.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use crate::aggregate::groups_accumulator::prim_op::PrimitiveGroupsAccumulator;
2424
use crate::{AggregateExpr, PhysicalExpr};
2525
use arrow::compute;
2626
use arrow::datatypes::{
27-
DataType, Date32Type, Date64Type, Time32MillisecondType, Time32SecondType,
27+
DataType, Date32Type, Date64Type, DurationMicrosecondType, DurationMillisecondType,
28+
DurationNanosecondType, DurationSecondType, Time32MillisecondType, Time32SecondType,
2829
Time64MicrosecondType, Time64NanosecondType, TimeUnit, TimestampMicrosecondType,
2930
TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
3031
};
@@ -193,6 +194,7 @@ impl AggregateExpr for Max {
193194
| Time32(_)
194195
| Time64(_)
195196
| Timestamp(_, _)
197+
| Duration(_)
196198
)
197199
}
198200

@@ -241,6 +243,18 @@ impl AggregateExpr for Max {
241243
Timestamp(Nanosecond, _) => {
242244
instantiate_max_accumulator!(self, i64, TimestampNanosecondType)
243245
}
246+
Duration(Second) => {
247+
instantiate_max_accumulator!(self, i64, DurationSecondType)
248+
}
249+
Duration(Millisecond) => {
250+
instantiate_max_accumulator!(self, i64, DurationMillisecondType)
251+
}
252+
Duration(Microsecond) => {
253+
instantiate_max_accumulator!(self, i64, DurationMicrosecondType)
254+
}
255+
Duration(Nanosecond) => {
256+
instantiate_max_accumulator!(self, i64, DurationNanosecondType)
257+
}
244258
Decimal128(_, _) => {
245259
instantiate_max_accumulator!(self, i128, Decimal128Type)
246260
}
@@ -945,6 +959,7 @@ impl AggregateExpr for Min {
945959
| Time32(_)
946960
| Time64(_)
947961
| Timestamp(_, _)
962+
| Duration(_)
948963
)
949964
}
950965

@@ -992,6 +1007,18 @@ impl AggregateExpr for Min {
9921007
Timestamp(Nanosecond, _) => {
9931008
instantiate_min_accumulator!(self, i64, TimestampNanosecondType)
9941009
}
1010+
Duration(Second) => {
1011+
instantiate_min_accumulator!(self, i64, DurationSecondType)
1012+
}
1013+
Duration(Millisecond) => {
1014+
instantiate_min_accumulator!(self, i64, DurationMillisecondType)
1015+
}
1016+
Duration(Microsecond) => {
1017+
instantiate_min_accumulator!(self, i64, DurationMicrosecondType)
1018+
}
1019+
Duration(Nanosecond) => {
1020+
instantiate_min_accumulator!(self, i64, DurationNanosecondType)
1021+
}
9951022
Decimal128(_, _) => {
9961023
instantiate_min_accumulator!(self, i128, Decimal128Type)
9971024
}

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,8 +3734,8 @@ NULL NULL Row 2 Y
37343734
statement ok
37353735
create table d
37363736
as values
3737-
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)')),
3738-
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'));
3737+
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1),
3738+
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1);
37393739

37403740
query ????
37413741
SELECT min(column1), min(column2), min(column3), min(column4) FROM d;
@@ -3747,6 +3747,32 @@ SELECT max(column1), max(column2), max(column3), max(column4) FROM d;
37473747
----
37483748
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
37493749

3750+
# GROUP BY follows a different code path
3751+
query ????I
3752+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5;
3753+
----
3754+
0 days 0 hours 0 mins 1 secs 0 days 0 hours 0 mins 0.002 secs 0 days 0 hours 0 mins 0.000003 secs 0 days 0 hours 0 mins 0.000000004 secs 1
3755+
3756+
query ????I
3757+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5;
3758+
----
3759+
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
3760+
3761+
statement ok
3762+
INSERT INTO d VALUES
3763+
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1),
3764+
(arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1);
3765+
3766+
query ????I
3767+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
3768+
----
3769+
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
3770+
3771+
query ????I
3772+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
3773+
----
3774+
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
3775+
37503776
statement ok
37513777
drop table d;
37523778

0 commit comments

Comments
 (0)