Skip to content

Commit 2a53ce5

Browse files
svranesevicavantgardnerio
authored andcommitted
Backport GroupsAccumulator for Duration min/max agg (#288)
1 parent 3795bb2 commit 2a53ce5

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

datafusion/functions-aggregate/src/min_max.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ use arrow::array::{
3131
};
3232
use arrow::compute;
3333
use arrow::datatypes::{
34-
DataType, Decimal128Type, Decimal256Type, Float16Type, Float32Type, Float64Type,
35-
Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, UInt64Type,
36-
UInt8Type,
34+
DataType, Decimal128Type, Decimal256Type, DurationMicrosecondType,
35+
DurationMillisecondType, DurationNanosecondType, DurationSecondType, Float16Type,
36+
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
37+
UInt32Type, UInt64Type, UInt8Type,
3738
};
3839
use arrow_schema::IntervalUnit;
3940
use datafusion_common::stats::Precision;
@@ -240,6 +241,7 @@ impl AggregateUDFImpl for Max {
240241
| Binary
241242
| LargeBinary
242243
| BinaryView
244+
| Duration(_)
243245
)
244246
}
245247

@@ -294,6 +296,18 @@ impl AggregateUDFImpl for Max {
294296
Timestamp(Nanosecond, _) => {
295297
primitive_max_accumulator!(data_type, i64, TimestampNanosecondType)
296298
}
299+
Duration(Second) => {
300+
primitive_max_accumulator!(data_type, i64, DurationSecondType)
301+
}
302+
Duration(Millisecond) => {
303+
primitive_max_accumulator!(data_type, i64, DurationMillisecondType)
304+
}
305+
Duration(Microsecond) => {
306+
primitive_max_accumulator!(data_type, i64, DurationMicrosecondType)
307+
}
308+
Duration(Nanosecond) => {
309+
primitive_max_accumulator!(data_type, i64, DurationNanosecondType)
310+
}
297311
Decimal128(_, _) => {
298312
primitive_max_accumulator!(data_type, i128, Decimal128Type)
299313
}
@@ -1098,6 +1112,7 @@ impl AggregateUDFImpl for Min {
10981112
| Binary
10991113
| LargeBinary
11001114
| BinaryView
1115+
| Duration(_)
11011116
)
11021117
}
11031118

@@ -1152,6 +1167,18 @@ impl AggregateUDFImpl for Min {
11521167
Timestamp(Nanosecond, _) => {
11531168
primitive_min_accumulator!(data_type, i64, TimestampNanosecondType)
11541169
}
1170+
Duration(Second) => {
1171+
primitive_min_accumulator!(data_type, i64, DurationSecondType)
1172+
}
1173+
Duration(Millisecond) => {
1174+
primitive_min_accumulator!(data_type, i64, DurationMillisecondType)
1175+
}
1176+
Duration(Microsecond) => {
1177+
primitive_min_accumulator!(data_type, i64, DurationMicrosecondType)
1178+
}
1179+
Duration(Nanosecond) => {
1180+
primitive_min_accumulator!(data_type, i64, DurationNanosecondType)
1181+
}
11551182
Decimal128(_, _) => {
11561183
primitive_min_accumulator!(data_type, i128, Decimal128Type)
11571184
}

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,8 +4232,8 @@ NULL NULL Row 2 Y
42324232
statement ok
42334233
create table d
42344234
as values
4235-
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)')),
4236-
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'));
4235+
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1),
4236+
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1);
42374237

42384238
query ????
42394239
SELECT min(column1), min(column2), min(column3), min(column4) FROM d;
@@ -4245,6 +4245,32 @@ SELECT max(column1), max(column2), max(column3), max(column4) FROM d;
42454245
----
42464246
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
42474247

4248+
# GROUP BY follows a different code path
4249+
query ????I
4250+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5;
4251+
----
4252+
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
4253+
4254+
query ????I
4255+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5;
4256+
----
4257+
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
4258+
4259+
statement ok
4260+
INSERT INTO d VALUES
4261+
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1),
4262+
(arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1);
4263+
4264+
query ????I
4265+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4266+
----
4267+
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
4268+
4269+
query ????I
4270+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4271+
----
4272+
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
4273+
42484274
statement ok
42494275
drop table d;
42504276

0 commit comments

Comments
 (0)