Skip to content

Commit fb775bd

Browse files
committed
Support for Avg duration
1 parent 83ce79c commit fb775bd

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

datafusion/functions-aggregate/src/average.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl AggregateUDFImpl for Avg {
171171
fn groups_accumulator_supported(&self, args: AccumulatorArgs) -> bool {
172172
matches!(
173173
args.return_type,
174-
DataType::Float64 | DataType::Decimal128(_, _)
174+
DataType::Float64 | DataType::Decimal128(_, _) | DataType::Duration(_)
175175
)
176176
}
177177

@@ -232,6 +232,49 @@ impl AggregateUDFImpl for Avg {
232232
)))
233233
}
234234

235+
(Duration(time_unit), Duration(_)) => match time_unit {
236+
TimeUnit::Second => {
237+
let avg_fn = move |sum: i64, count: u64| Ok(sum / count as i64);
238+
Ok(Box::new(
239+
AvgGroupsAccumulator::<DurationSecondType, _>::new(
240+
&data_type,
241+
args.return_type,
242+
avg_fn,
243+
),
244+
))
245+
}
246+
TimeUnit::Millisecond => {
247+
let avg_fn = move |sum: i64, count: u64| Ok(sum / count as i64);
248+
Ok(Box::new(
249+
AvgGroupsAccumulator::<DurationMillisecondType, _>::new(
250+
&data_type,
251+
args.return_type,
252+
avg_fn,
253+
),
254+
))
255+
}
256+
TimeUnit::Microsecond => {
257+
let avg_fn = move |sum: i64, count: u64| Ok(sum / count as i64);
258+
Ok(Box::new(
259+
AvgGroupsAccumulator::<DurationMicrosecondType, _>::new(
260+
&data_type,
261+
args.return_type,
262+
avg_fn,
263+
),
264+
))
265+
}
266+
TimeUnit::Nanosecond => {
267+
let avg_fn = move |sum: i64, count: u64| Ok(sum / count as i64);
268+
Ok(Box::new(
269+
AvgGroupsAccumulator::<DurationNanosecondType, _>::new(
270+
&data_type,
271+
args.return_type,
272+
avg_fn,
273+
),
274+
))
275+
}
276+
},
277+
235278
_ => not_impl_err!(
236279
"AvgGroupsAccumulator for ({} --> {})",
237280
&data_type,

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,6 +4969,34 @@ select count(distinct column1), count(distinct column2) from dict_test group by
49694969
statement ok
49704970
drop table dict_test;
49714971

4972+
# avg_duartion
4973+
4974+
statement ok
4975+
create table d as values
4976+
(arrow_cast(1, 'Duration(Second)'), arrow_cast(2, 'Duration(Millisecond)'), arrow_cast(3, 'Duration(Microsecond)'), arrow_cast(4, 'Duration(Nanosecond)'), 1),
4977+
(arrow_cast(11, 'Duration(Second)'), arrow_cast(22, 'Duration(Millisecond)'), arrow_cast(33, 'Duration(Microsecond)'), arrow_cast(44, 'Duration(Nanosecond)'), 1);
4978+
4979+
query ????
4980+
SELECT avg(column1), avg(column2), avg(column3), avg(column4) FROM d;
4981+
----
4982+
0 days 0 hours 0 mins 6 secs 0 days 0 hours 0 mins 0.012 secs 0 days 0 hours 0 mins 0.000018 secs 0 days 0 hours 0 mins 0.000000024 secs
4983+
4984+
query ????I
4985+
SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d GROUP BY column5;
4986+
----
4987+
0 days 0 hours 0 mins 6 secs 0 days 0 hours 0 mins 0.012 secs 0 days 0 hours 0 mins 0.000018 secs 0 days 0 hours 0 mins 0.000000024 secs 1
4988+
4989+
INSERT INTO d VALUES
4990+
(arrow_cast(3, 'Duration(Second)'), arrow_cast(1, 'Duration(Millisecond)'), arrow_cast(7, 'Duration(Microsecond)'), arrow_cast(2, 'Duration(Nanosecond)'), 1),
4991+
(arrow_cast(0, 'Duration(Second)'), arrow_cast(9, 'Duration(Millisecond)'), arrow_cast(5, 'Duration(Microsecond)'), arrow_cast(8, 'Duration(Nanosecond)'), 1);
4992+
4993+
query ????I
4994+
SELECT avg(column1), avg(column2), avg(column3), avg(column4), column5 FROM d GROUP BY column5 ORDER BY column5;
4995+
----
4996+
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
4997+
4998+
statement ok
4999+
drop table d;
49725000

49735001
# Prepare the table with dictionary values for testing
49745002
statement ok

0 commit comments

Comments
 (0)