@@ -32,12 +32,13 @@ use crate::aggregates::group_values::multi_group_by::{
3232use arrow:: array:: { Array , ArrayRef , BooleanBufferBuilder } ;
3333use arrow:: compute:: cast;
3434use arrow:: datatypes:: {
35- BinaryViewType , DataType , Date32Type , Date64Type , Decimal128Type , Field , Float32Type ,
36- Float64Type , Int8Type , Int16Type , Int32Type , Int64Type , Schema , SchemaRef ,
37- StringViewType , Time32MillisecondType , Time32SecondType , Time64MicrosecondType ,
38- Time64NanosecondType , TimeUnit , TimestampMicrosecondType , TimestampMillisecondType ,
39- TimestampNanosecondType , TimestampSecondType , UInt8Type , UInt16Type , UInt32Type ,
40- UInt64Type ,
35+ BinaryViewType , DataType , Date32Type , Date64Type , Decimal128Type ,
36+ DurationMicrosecondType , DurationMillisecondType , DurationNanosecondType ,
37+ DurationSecondType , Field , Float32Type , Float64Type , Int8Type , Int16Type , Int32Type ,
38+ Int64Type , Schema , SchemaRef , StringViewType , Time32MillisecondType ,
39+ Time32SecondType , Time64MicrosecondType , Time64NanosecondType , TimeUnit ,
40+ TimestampMicrosecondType , TimestampMillisecondType , TimestampNanosecondType ,
41+ TimestampSecondType , UInt8Type , UInt16Type , UInt32Type , UInt64Type ,
4142} ;
4243use datafusion_common:: hash_utils:: RandomState ;
4344use datafusion_common:: hash_utils:: create_hashes;
@@ -952,6 +953,9 @@ fn group_column_supported_type(data_type: &DataType) -> bool {
952953 | DataType :: Time64 ( TimeUnit :: Microsecond )
953954 | DataType :: Time64 ( TimeUnit :: Nanosecond )
954955 | DataType :: Timestamp ( _, _)
956+ // All four Duration units are valid Arrow types (unlike Time32 /
957+ // Time64), so every unit is handled by the dispatcher below.
958+ | DataType :: Duration ( _)
955959 | DataType :: Utf8View
956960 | DataType :: BinaryView
957961 | DataType :: Boolean
@@ -1031,6 +1035,20 @@ fn make_group_column(field: &Field) -> Result<Box<dyn GroupColumn>> {
10311035 instantiate_primitive ! ( v, nullable, TimestampNanosecondType , data_type)
10321036 }
10331037 } ,
1038+ DataType :: Duration ( t) => match t {
1039+ TimeUnit :: Second => {
1040+ instantiate_primitive ! ( v, nullable, DurationSecondType , data_type)
1041+ }
1042+ TimeUnit :: Millisecond => {
1043+ instantiate_primitive ! ( v, nullable, DurationMillisecondType , data_type)
1044+ }
1045+ TimeUnit :: Microsecond => {
1046+ instantiate_primitive ! ( v, nullable, DurationMicrosecondType , data_type)
1047+ }
1048+ TimeUnit :: Nanosecond => {
1049+ instantiate_primitive ! ( v, nullable, DurationNanosecondType , data_type)
1050+ }
1051+ } ,
10341052 DataType :: Decimal128 ( _, _) => {
10351053 instantiate_primitive ! ( v, nullable, Decimal128Type , data_type)
10361054 }
@@ -1259,7 +1277,10 @@ enum Nulls {
12591277mod tests {
12601278 use std:: { collections:: HashMap , sync:: Arc } ;
12611279
1262- use arrow:: array:: { ArrayRef , Int64Array , RecordBatch , StringArray , StringViewArray } ;
1280+ use arrow:: array:: {
1281+ Array , ArrayRef , DurationMicrosecondArray , Int64Array , RecordBatch , StringArray ,
1282+ StringViewArray ,
1283+ } ;
12631284 use arrow:: datatypes:: { DataType , Field , Schema , SchemaRef } ;
12641285 use arrow:: { compute:: concat_batches, util:: pretty:: pretty_format_batches} ;
12651286 use datafusion_common:: utils:: proxy:: HashTableAllocExt ;
@@ -1309,6 +1330,10 @@ mod tests {
13091330 DataType :: Time64 ( arrow:: datatypes:: TimeUnit :: Microsecond ) ,
13101331 DataType :: Time64 ( arrow:: datatypes:: TimeUnit :: Nanosecond ) ,
13111332 DataType :: Timestamp ( arrow:: datatypes:: TimeUnit :: Nanosecond , None ) ,
1333+ DataType :: Duration ( arrow:: datatypes:: TimeUnit :: Second ) ,
1334+ DataType :: Duration ( arrow:: datatypes:: TimeUnit :: Millisecond ) ,
1335+ DataType :: Duration ( arrow:: datatypes:: TimeUnit :: Microsecond ) ,
1336+ DataType :: Duration ( arrow:: datatypes:: TimeUnit :: Nanosecond ) ,
13121337 ] ;
13131338
13141339 for dt in & supported_cases {
@@ -1352,6 +1377,56 @@ mod tests {
13521377 }
13531378 }
13541379
1380+ /// End-to-end coverage for `Duration` group keys: a `Duration` column stays
1381+ /// on the `GroupValuesColumn` fast path, deduplicates equal durations
1382+ /// (including nulls), and round-trips with its `Duration` output type
1383+ /// preserved (not the bare `i64` native). The four units share one builder,
1384+ /// so a single unit exercises the dispatcher; the fuzz above covers routing
1385+ /// for every unit.
1386+ #[ test]
1387+ fn test_group_values_column_duration ( ) {
1388+ use arrow:: datatypes:: TimeUnit ;
1389+
1390+ let schema = Arc :: new ( Schema :: new ( vec ! [ Field :: new(
1391+ "d" ,
1392+ DataType :: Duration ( TimeUnit :: Microsecond ) ,
1393+ true ,
1394+ ) ] ) ) ;
1395+ assert ! ( supported_schema( & schema) ) ;
1396+ let mut group_values =
1397+ GroupValuesColumn :: < false > :: try_new ( Arc :: clone ( & schema) ) . unwrap ( ) ;
1398+
1399+ // Two distinct durations and a null with repeats: row 3 repeats row 0,
1400+ // row 4 repeats the null of row 1.
1401+ let input: ArrayRef = Arc :: new ( DurationMicrosecondArray :: from ( vec ! [
1402+ Some ( 10 ) ,
1403+ None ,
1404+ Some ( 20 ) ,
1405+ Some ( 10 ) ,
1406+ None ,
1407+ ] ) ) ;
1408+ let mut groups = Vec :: new ( ) ;
1409+ group_values. intern ( & [ input] , & mut groups) . unwrap ( ) ;
1410+ assert_eq ! ( groups, vec![ 0 , 1 , 2 , 0 , 1 ] ) ;
1411+
1412+ let emitted = group_values. emit ( EmitTo :: All ) . unwrap ( ) ;
1413+ assert_eq ! ( emitted. len( ) , 1 ) ;
1414+ // The emitted key keeps its Duration type, not the bare i64 native.
1415+ assert_eq ! (
1416+ emitted[ 0 ] . data_type( ) ,
1417+ & DataType :: Duration ( TimeUnit :: Microsecond )
1418+ ) ;
1419+ let actual = emitted[ 0 ]
1420+ . as_any ( )
1421+ . downcast_ref :: < DurationMicrosecondArray > ( )
1422+ . expect ( "emitted column should be a DurationMicrosecondArray" ) ;
1423+ // Three groups in first-seen order: 10, null, 20.
1424+ assert_eq ! ( actual. len( ) , 3 ) ;
1425+ assert_eq ! ( actual. value( 0 ) , 10 ) ;
1426+ assert ! ( actual. is_null( 1 ) ) ;
1427+ assert_eq ! ( actual. value( 2 ) , 20 ) ;
1428+ }
1429+
13551430 #[ test]
13561431 fn supported_schema_rejects_mix_of_supported_and_unsupported ( ) {
13571432 // One Float16 column among supported columns flips the whole
0 commit comments