Skip to content

Commit f5821b9

Browse files
svranesevicalamb
andauthored
Support Duration in min/max agg functions (#15310)
* Support min/max agg functions on values of duration type * Add tests for GROUP BY as well --------- Co-authored-by: svranesevic <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent 97548a2 commit f5821b9

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

datafusion/functions-aggregate/src/min_max.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ mod min_max_bytes;
2222

2323
use arrow::array::{
2424
ArrayRef, BinaryArray, BinaryViewArray, BooleanArray, Date32Array, Date64Array,
25-
Decimal128Array, Decimal256Array, Float16Array, Float32Array, Float64Array,
26-
Int16Array, Int32Array, Int64Array, Int8Array, IntervalDayTimeArray,
25+
Decimal128Array, Decimal256Array, DurationMicrosecondArray, DurationMillisecondArray,
26+
DurationNanosecondArray, DurationSecondArray, Float16Array, Float32Array,
27+
Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, IntervalDayTimeArray,
2728
IntervalMonthDayNanoArray, IntervalYearMonthArray, LargeBinaryArray,
2829
LargeStringArray, StringArray, StringViewArray, Time32MillisecondArray,
2930
Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray,
@@ -518,6 +519,33 @@ macro_rules! min_max_batch {
518519
$OP
519520
)
520521
}
522+
DataType::Duration(TimeUnit::Second) => {
523+
typed_min_max_batch!($VALUES, DurationSecondArray, DurationSecond, $OP)
524+
}
525+
DataType::Duration(TimeUnit::Millisecond) => {
526+
typed_min_max_batch!(
527+
$VALUES,
528+
DurationMillisecondArray,
529+
DurationMillisecond,
530+
$OP
531+
)
532+
}
533+
DataType::Duration(TimeUnit::Microsecond) => {
534+
typed_min_max_batch!(
535+
$VALUES,
536+
DurationMicrosecondArray,
537+
DurationMicrosecond,
538+
$OP
539+
)
540+
}
541+
DataType::Duration(TimeUnit::Nanosecond) => {
542+
typed_min_max_batch!(
543+
$VALUES,
544+
DurationNanosecondArray,
545+
DurationNanosecond,
546+
$OP
547+
)
548+
}
521549
other => {
522550
// This should have been handled before
523551
return internal_err!(
@@ -1597,7 +1625,7 @@ mod tests {
15971625
assert_eq!(
15981626
min_res,
15991627
ScalarValue::IntervalYearMonth(Some(IntervalYearMonthType::make_value(
1600-
-2, 4
1628+
-2, 4,
16011629
)))
16021630
);
16031631

@@ -1609,7 +1637,7 @@ mod tests {
16091637
assert_eq!(
16101638
max_res,
16111639
ScalarValue::IntervalYearMonth(Some(IntervalYearMonthType::make_value(
1612-
5, 34
1640+
5, 34,
16131641
)))
16141642
);
16151643

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,38 @@ 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+
3839+
statement ok
3840+
drop table d;
3841+
38103842
# max_bool
38113843
statement ok
38123844
CREATE TABLE max_bool (value BOOLEAN);

0 commit comments

Comments
 (0)