Skip to content

Commit

Permalink
Support Duration and Union types in ScalarValue::iter_to_array (#10139)
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 authored Apr 21, 2024
1 parent 16e3831 commit eb72deb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 16 additions & 3 deletions datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,18 @@ impl ScalarValue {
tz
)
}
DataType::Duration(TimeUnit::Second) => {
build_array_primitive!(DurationSecondArray, DurationSecond)
}
DataType::Duration(TimeUnit::Millisecond) => {
build_array_primitive!(DurationMillisecondArray, DurationMillisecond)
}
DataType::Duration(TimeUnit::Microsecond) => {
build_array_primitive!(DurationMicrosecondArray, DurationMicrosecond)
}
DataType::Duration(TimeUnit::Nanosecond) => {
build_array_primitive!(DurationNanosecondArray, DurationNanosecond)
}
DataType::Interval(IntervalUnit::DayTime) => {
build_array_primitive!(IntervalDayTimeArray, IntervalDayTime)
}
Expand Down Expand Up @@ -1605,7 +1617,10 @@ impl ScalarValue {
let arrays = arrays.iter().map(|a| a.as_ref()).collect::<Vec<_>>();
arrow::compute::concat(arrays.as_slice())?
}
DataType::List(_) | DataType::LargeList(_) | DataType::Struct(_) => {
DataType::List(_)
| DataType::LargeList(_)
| DataType::Struct(_)
| DataType::Union(_, _) => {
let arrays = scalars.map(|s| s.to_array()).collect::<Result<Vec<_>>>()?;
let arrays = arrays.iter().map(|a| a.as_ref()).collect::<Vec<_>>();
arrow::compute::concat(arrays.as_slice())?
Expand Down Expand Up @@ -1673,8 +1688,6 @@ impl ScalarValue {
| DataType::Time32(TimeUnit::Nanosecond)
| DataType::Time64(TimeUnit::Second)
| DataType::Time64(TimeUnit::Millisecond)
| DataType::Duration(_)
| DataType::Union(_, _)
| DataType::Map(_, _)
| DataType::RunEndEncoded(_, _)
| DataType::Utf8View
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,12 @@ SELECT avg(nanos), avg(micros), avg(millis), avg(secs) FROM t
statement error DataFusion error: Error during planning: No function matches the given name and argument types 'AVG\(Timestamp\(Nanosecond, None\)\)'\. You might need to add explicit type casts\.
SELECT tag, avg(nanos), avg(micros), avg(millis), avg(secs) FROM t GROUP BY tag ORDER BY tag;

# aggregate_duration_array_agg
query T?
SELECT tag, array_agg(millis - arrow_cast(secs, 'Timestamp(Millisecond, None)')) FROM t GROUP BY tag ORDER BY tag;
----
X [0 days 0 hours 0 mins 0.011 secs, 0 days 0 hours 0 mins 0.123 secs]
Y [, 0 days 0 hours 0 mins 0.432 secs]

statement ok
drop table t_source;
Expand Down

0 comments on commit eb72deb

Please sign in to comment.