Skip to content

Commit 484c1bd

Browse files
svranesevicavantgardnerio
authored andcommitted
Backport GroupsAccumulator for Duration min/max agg (#288) (apache#15322) v47
1 parent 511739e commit 484c1bd

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
@@ -32,9 +32,10 @@ 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, UInt16Type,
38+
UInt32Type, UInt64Type, UInt8Type,
3839
};
3940
use arrow_schema::IntervalUnit;
4041
use datafusion_common::stats::Precision;
@@ -264,6 +265,7 @@ impl AggregateUDFImpl for Max {
264265
| Binary
265266
| LargeBinary
266267
| BinaryView
268+
| Duration(_)
267269
)
268270
}
269271

@@ -318,6 +320,18 @@ impl AggregateUDFImpl for Max {
318320
Timestamp(Nanosecond, _) => {
319321
primitive_max_accumulator!(data_type, i64, TimestampNanosecondType)
320322
}
323+
Duration(Second) => {
324+
primitive_max_accumulator!(data_type, i64, DurationSecondType)
325+
}
326+
Duration(Millisecond) => {
327+
primitive_max_accumulator!(data_type, i64, DurationMillisecondType)
328+
}
329+
Duration(Microsecond) => {
330+
primitive_max_accumulator!(data_type, i64, DurationMicrosecondType)
331+
}
332+
Duration(Nanosecond) => {
333+
primitive_max_accumulator!(data_type, i64, DurationNanosecondType)
334+
}
321335
Decimal128(_, _) => {
322336
primitive_max_accumulator!(data_type, i128, Decimal128Type)
323337
}
@@ -1118,6 +1132,7 @@ impl AggregateUDFImpl for Min {
11181132
| Binary
11191133
| LargeBinary
11201134
| BinaryView
1135+
| Duration(_)
11211136
)
11221137
}
11231138

@@ -1172,6 +1187,18 @@ impl AggregateUDFImpl for Min {
11721187
Timestamp(Nanosecond, _) => {
11731188
primitive_min_accumulator!(data_type, i64, TimestampNanosecondType)
11741189
}
1190+
Duration(Second) => {
1191+
primitive_min_accumulator!(data_type, i64, DurationSecondType)
1192+
}
1193+
Duration(Millisecond) => {
1194+
primitive_min_accumulator!(data_type, i64, DurationMillisecondType)
1195+
}
1196+
Duration(Microsecond) => {
1197+
primitive_min_accumulator!(data_type, i64, DurationMicrosecondType)
1198+
}
1199+
Duration(Nanosecond) => {
1200+
primitive_min_accumulator!(data_type, i64, DurationNanosecondType)
1201+
}
11751202
Decimal128(_, _) => {
11761203
primitive_min_accumulator!(data_type, i128, Decimal128Type)
11771204
}

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4339,8 +4339,8 @@ NULL NULL Row 2 Y
43394339
statement ok
43404340
create table d
43414341
as values
4342-
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)')),
4343-
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'));
4342+
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1),
4343+
(arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1);
43444344

43454345
query ????
43464346
SELECT min(column1), min(column2), min(column3), min(column4) FROM d;
@@ -4352,6 +4352,32 @@ SELECT max(column1), max(column2), max(column3), max(column4) FROM d;
43524352
----
43534353
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
43544354

4355+
# GROUP BY follows a different code path
4356+
query ????I
4357+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5;
4358+
----
4359+
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
4360+
4361+
query ????I
4362+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5;
4363+
----
4364+
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
4365+
4366+
statement ok
4367+
INSERT INTO d VALUES
4368+
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1),
4369+
(arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1);
4370+
4371+
query ????I
4372+
SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4373+
----
4374+
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
4375+
4376+
query ????I
4377+
SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4378+
----
4379+
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
4380+
43554381
statement ok
43564382
drop table d;
43574383

0 commit comments

Comments
 (0)