Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions datafusion/physical-plan/src/aggregates/group_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
//! [`GroupValues`] trait for storing and interning group keys

use arrow::array::types::{
Date32Type, Date64Type, Decimal128Type, Time32MillisecondType, Time32SecondType,
Time64MicrosecondType, Time64NanosecondType, TimestampMicrosecondType,
TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
Date32Type, Date64Type, Decimal128Type, Int8Type, Int16Type, Int32Type, Int64Type,
Time32MillisecondType, Time32SecondType, Time64MicrosecondType, Time64NanosecondType,
TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType,
TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
};
use arrow::array::{ArrayRef, downcast_primitive};
use arrow::datatypes::{DataType, SchemaRef, TimeUnit};
Expand All @@ -41,15 +42,15 @@ pub(crate) use single_group_by::primitive::HashValue;
use crate::aggregates::{
group_values::single_group_by::{
boolean::GroupValuesBoolean, bytes::GroupValuesBytes,
bytes_view::GroupValuesBytesView, primitive::GroupValuesPrimitive,
bytes_view::GroupValuesBytesView, dictionary::GroupValuesDictionary,
primitive::GroupValuesPrimitive,
},
order::GroupOrdering,
};

mod metrics;
mod null_builder;

pub(crate) use metrics::GroupByMetrics;
mod null_builder;

/// Stores the group values during hash aggregation.
///
Expand Down Expand Up @@ -196,6 +197,45 @@ pub fn new_group_values(
DataType::Boolean => {
return Ok(Box::new(GroupValuesBoolean::new()));
}
DataType::Dictionary(key_type, value_type) => {
if supported_single_dictionary_value(value_type) {
return match key_type.as_ref() {
DataType::Int8 => Ok(Box::new(
GroupValuesDictionary::<Int8Type>::new(value_type),
)),
DataType::Int16 => Ok(Box::new(
GroupValuesDictionary::<Int16Type>::new(value_type),
)),
DataType::Int32 => Ok(Box::new(
GroupValuesDictionary::<Int32Type>::new(value_type),
)),
DataType::Int64 => Ok(Box::new(
GroupValuesDictionary::<Int64Type>::new(value_type),
)),
DataType::UInt8 => Ok(Box::new(
GroupValuesDictionary::<UInt8Type>::new(value_type),
)),
DataType::UInt16 => {
Ok(Box::new(GroupValuesDictionary::<UInt16Type>::new(
value_type,
)))
}
DataType::UInt32 => {
Ok(Box::new(GroupValuesDictionary::<UInt32Type>::new(
value_type,
)))
}
DataType::UInt64 => {
Ok(Box::new(GroupValuesDictionary::<UInt64Type>::new(
value_type,
)))
}
_ => Err(datafusion_common::DataFusionError::NotImplemented(
format!("Unsupported dictionary key type: {key_type:?}"),
)),
};
}
}
_ => {}
}
}
Expand All @@ -210,3 +250,18 @@ pub fn new_group_values(
Ok(Box::new(GroupValuesRows::try_new(schema)?))
}
}

fn supported_single_dictionary_value(t: &DataType) -> bool {
match t {
DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => true,
DataType::List(field)
if matches!(
field.data_type(),
DataType::Utf8 | DataType::Utf8View | DataType::LargeUtf8
) =>
{
true
}
_ => false,
}
}
Comment on lines +254 to +267
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the most common types, but why not support other types? Other types would fall under the slow path no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is intended to serve as a proof of concept that dictionary encoding works and improves efficiency. It makes sense to start with string types since that is what dictionary arrays are most commonly used with and where they provide the most benefit. A follow up issue can be created to support additional types such as LargeUtf8, LargeList, and numeric values ,which should be as minimal as adding the relevant branches in get_raw_bytes and sentinel_repr. This is also related to how sentinel representations of types work but I think thats worth a separate comment.

Loading
Loading