diff --git a/datafusion/physical-expr/src/aggregate/min_max.rs b/datafusion/physical-expr/src/aggregate/min_max.rs index 6e29b8c41762..d538ecffef89 100644 --- a/datafusion/physical-expr/src/aggregate/min_max.rs +++ b/datafusion/physical-expr/src/aggregate/min_max.rs @@ -24,7 +24,8 @@ use crate::aggregate::groups_accumulator::prim_op::PrimitiveGroupsAccumulator; use crate::{AggregateExpr, PhysicalExpr}; use arrow::compute; use arrow::datatypes::{ - DataType, Date32Type, Date64Type, Time32MillisecondType, Time32SecondType, + DataType, Date32Type, Date64Type, DurationMicrosecondType, DurationMillisecondType, + DurationNanosecondType, DurationSecondType, Time32MillisecondType, Time32SecondType, Time64MicrosecondType, Time64NanosecondType, TimeUnit, TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType, }; @@ -193,6 +194,7 @@ impl AggregateExpr for Max { | Time32(_) | Time64(_) | Timestamp(_, _) + | Duration(_) ) } @@ -241,6 +243,18 @@ impl AggregateExpr for Max { Timestamp(Nanosecond, _) => { instantiate_max_accumulator!(self, i64, TimestampNanosecondType) } + Duration(Second) => { + instantiate_max_accumulator!(self, i64, DurationSecondType) + } + Duration(Millisecond) => { + instantiate_max_accumulator!(self, i64, DurationMillisecondType) + } + Duration(Microsecond) => { + instantiate_max_accumulator!(self, i64, DurationMicrosecondType) + } + Duration(Nanosecond) => { + instantiate_max_accumulator!(self, i64, DurationNanosecondType) + } Decimal128(_, _) => { instantiate_max_accumulator!(self, i128, Decimal128Type) } @@ -945,6 +959,7 @@ impl AggregateExpr for Min { | Time32(_) | Time64(_) | Timestamp(_, _) + | Duration(_) ) } @@ -992,6 +1007,18 @@ impl AggregateExpr for Min { Timestamp(Nanosecond, _) => { instantiate_min_accumulator!(self, i64, TimestampNanosecondType) } + Duration(Second) => { + instantiate_min_accumulator!(self, i64, DurationSecondType) + } + Duration(Millisecond) => { + instantiate_min_accumulator!(self, i64, DurationMillisecondType) + } + Duration(Microsecond) => { + instantiate_min_accumulator!(self, i64, DurationMicrosecondType) + } + Duration(Nanosecond) => { + instantiate_min_accumulator!(self, i64, DurationNanosecondType) + } Decimal128(_, _) => { instantiate_min_accumulator!(self, i128, Decimal128Type) } diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt index 20f9a311636c..c858c202f130 100644 --- a/datafusion/sqllogictest/test_files/aggregate.slt +++ b/datafusion/sqllogictest/test_files/aggregate.slt @@ -3734,8 +3734,8 @@ NULL NULL Row 2 Y statement ok create table d as values - (arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)')), - (arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)')); + (arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1), + (arrow_cast(11, 'Duration(Second)'),arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1); query ???? 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; ---- 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 +# GROUP BY follows a different code path +query ????I +SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5; +---- +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 + +query ????I +SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5; +---- +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 + +statement ok +INSERT INTO d VALUES + (arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1), + (arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1); + +query ????I +SELECT max(column1), max(column2), max(column3), max(column4), column5 FROM d GROUP BY column5 ORDER BY column5; +---- +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 + +query ????I +SELECT min(column1), min(column2), min(column3), min(column4), column5 FROM d GROUP BY column5 ORDER BY column5; +---- +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 + statement ok drop table d;