Skip to content

Commit 3f392e4

Browse files
shruti2522emilk
authored andcommitted
GroupsAccumulator for Duration (apache#15322)
1 parent 26058ac commit 3f392e4

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

datafusion/functions-aggregate/src/min_max.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ use arrow::array::{
3232
};
3333
use arrow::compute;
3434
use arrow::datatypes::{
35-
DataType, Decimal128Type, Decimal256Type, Float16Type, Float32Type, Float64Type,
36-
Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, UInt64Type,
37-
UInt8Type,
35+
DataType, Decimal128Type, Decimal256Type, DurationMicrosecondType,
36+
DurationMillisecondType, DurationNanosecondType, DurationSecondType, Float16Type,
37+
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, IntervalUnit,
38+
UInt16Type, UInt32Type, UInt64Type, UInt8Type,
3839
};
39-
use arrow_schema::IntervalUnit;
4040
use datafusion_common::stats::Precision;
4141
use datafusion_common::{
4242
downcast_value, exec_err, internal_err, ColumnStatistics, DataFusionError, Result,
@@ -264,6 +264,7 @@ impl AggregateUDFImpl for Max {
264264
| Binary
265265
| LargeBinary
266266
| BinaryView
267+
| Duration(_)
267268
)
268269
}
269270

@@ -318,6 +319,18 @@ impl AggregateUDFImpl for Max {
318319
Timestamp(Nanosecond, _) => {
319320
primitive_max_accumulator!(data_type, i64, TimestampNanosecondType)
320321
}
322+
Duration(Second) => {
323+
primitive_max_accumulator!(data_type, i64, DurationSecondType)
324+
}
325+
Duration(Millisecond) => {
326+
primitive_max_accumulator!(data_type, i64, DurationMillisecondType)
327+
}
328+
Duration(Microsecond) => {
329+
primitive_max_accumulator!(data_type, i64, DurationMicrosecondType)
330+
}
331+
Duration(Nanosecond) => {
332+
primitive_max_accumulator!(data_type, i64, DurationNanosecondType)
333+
}
321334
Decimal128(_, _) => {
322335
primitive_max_accumulator!(data_type, i128, Decimal128Type)
323336
}
@@ -1091,6 +1104,7 @@ impl AggregateUDFImpl for Min {
10911104
| Binary
10921105
| LargeBinary
10931106
| BinaryView
1107+
| Duration(_)
10941108
)
10951109
}
10961110

@@ -1145,6 +1159,18 @@ impl AggregateUDFImpl for Min {
11451159
Timestamp(Nanosecond, _) => {
11461160
primitive_min_accumulator!(data_type, i64, TimestampNanosecondType)
11471161
}
1162+
Duration(Second) => {
1163+
primitive_min_accumulator!(data_type, i64, DurationSecondType)
1164+
}
1165+
Duration(Millisecond) => {
1166+
primitive_min_accumulator!(data_type, i64, DurationMillisecondType)
1167+
}
1168+
Duration(Microsecond) => {
1169+
primitive_min_accumulator!(data_type, i64, DurationMicrosecondType)
1170+
}
1171+
Duration(Nanosecond) => {
1172+
primitive_min_accumulator!(data_type, i64, DurationNanosecondType)
1173+
}
11481174
Decimal128(_, _) => {
11491175
primitive_min_accumulator!(data_type, i128, Decimal128Type)
11501176
}

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,52 @@ SELECT MIN(value), MAX(value) FROM timestampmicrosecond
38073807
statement ok
38083808
DROP TABLE timestampmicrosecond;
38093809

3810+
# min_duration, max_duration
3811+
statement ok
3812+
create table d
3813+
as values
3814+
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1),
3815+
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1);
3816+
3817+
query ????
3818+
SELECT min(column1), min(column2), min(column3), min(column4) FROM d;
3819+
----
3820+
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
3821+
3822+
query ????
3823+
SELECT max(column1), max(column2), max(column3), max(column4) FROM d;
3824+
----
3825+
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
3826+
3827+
# GROUP BY follows a different code path
3828+
query ????I
3829+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5;
3830+
----
3831+
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
3832+
3833+
query ????I
3834+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5;
3835+
----
3836+
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
3837+
3838+
statement ok
3839+
INSERT INTO d VALUES
3840+
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1),
3841+
(arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1);
3842+
3843+
query ????I
3844+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
3845+
----
3846+
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
3847+
3848+
query ????I
3849+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
3850+
----
3851+
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
3852+
3853+
statement ok
3854+
drop table d;
3855+
38103856
# max_bool
38113857
statement ok
38123858
CREATE TABLE max_bool (value BOOLEAN);

0 commit comments

Comments
 (0)