Skip to content

Commit 9a3f8d1

Browse files
alambcomphead
andauthored
Minor: Encapsulate type check in GroupValuesColumn, avoid panic (#12620)
* Encapsulate type check in GroupValuesColumn, avoid panic * Update datafusion/physical-plan/src/aggregates/group_values/column_wise.rs Co-authored-by: Oleks V <[email protected]> * Clarify what supported means * return not implemented error * Fixup doc link --------- Co-authored-by: Oleks V <[email protected]>
1 parent 524e56d commit 9a3f8d1

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

datafusion/physical-plan/src/aggregates/group_values/column.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use arrow::datatypes::{
2727
};
2828
use arrow::record_batch::RecordBatch;
2929
use arrow_array::{Array, ArrayRef};
30-
use arrow_schema::{DataType, SchemaRef};
30+
use arrow_schema::{DataType, Schema, SchemaRef};
3131
use datafusion_common::hash_utils::create_hashes;
32-
use datafusion_common::{DataFusionError, Result};
32+
use datafusion_common::{not_impl_err, DataFusionError, Result};
3333
use datafusion_execution::memory_pool::proxy::{RawTableAllocExt, VecAllocExt};
3434
use datafusion_expr::EmitTo;
3535
use datafusion_physical_expr::binary_map::OutputType;
@@ -67,6 +67,7 @@ pub struct GroupValuesColumn {
6767
}
6868

6969
impl GroupValuesColumn {
70+
/// Create a new instance of GroupValuesColumn if supported for the specified schema
7071
pub fn try_new(schema: SchemaRef) -> Result<Self> {
7172
let map = RawTable::with_capacity(0);
7273
Ok(Self {
@@ -78,6 +79,41 @@ impl GroupValuesColumn {
7879
random_state: Default::default(),
7980
})
8081
}
82+
83+
/// Returns true if [`GroupValuesColumn`] supported for the specified schema
84+
pub fn supported_schema(schema: &Schema) -> bool {
85+
schema
86+
.fields()
87+
.iter()
88+
.map(|f| f.data_type())
89+
.all(Self::supported_type)
90+
}
91+
92+
/// Returns true if the specified data type is supported by [`GroupValuesColumn`]
93+
///
94+
/// In order to be supported, there must be a specialized implementation of
95+
/// [`GroupColumn`] for the data type, instantiated in [`Self::intern`]
96+
fn supported_type(data_type: &DataType) -> bool {
97+
matches!(
98+
*data_type,
99+
DataType::Int8
100+
| DataType::Int16
101+
| DataType::Int32
102+
| DataType::Int64
103+
| DataType::UInt8
104+
| DataType::UInt16
105+
| DataType::UInt32
106+
| DataType::UInt64
107+
| DataType::Float32
108+
| DataType::Float64
109+
| DataType::Utf8
110+
| DataType::LargeUtf8
111+
| DataType::Binary
112+
| DataType::LargeBinary
113+
| DataType::Date32
114+
| DataType::Date64
115+
)
116+
}
81117
}
82118

83119
impl GroupValues for GroupValuesColumn {
@@ -154,7 +190,9 @@ impl GroupValues for GroupValuesColumn {
154190
let b = ByteGroupValueBuilder::<i64>::new(OutputType::Binary);
155191
v.push(Box::new(b) as _)
156192
}
157-
dt => todo!("{dt} not impl"),
193+
dt => {
194+
return not_impl_err!("{dt} not supported in GroupValuesColumn")
195+
}
158196
}
159197
}
160198
self.group_values = v;

datafusion/physical-plan/src/aggregates/group_values/mod.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,9 @@ pub fn new_group_values(schema: SchemaRef) -> Result<Box<dyn GroupValues>> {
9696
}
9797
}
9898

99-
if schema
100-
.fields()
101-
.iter()
102-
.map(|f| f.data_type())
103-
.all(has_row_like_feature)
104-
{
99+
if GroupValuesColumn::supported_schema(schema.as_ref()) {
105100
Ok(Box::new(GroupValuesColumn::try_new(schema)?))
106101
} else {
107102
Ok(Box::new(GroupValuesRows::try_new(schema)?))
108103
}
109104
}
110-
111-
fn has_row_like_feature(data_type: &DataType) -> bool {
112-
matches!(
113-
*data_type,
114-
DataType::Int8
115-
| DataType::Int16
116-
| DataType::Int32
117-
| DataType::Int64
118-
| DataType::UInt8
119-
| DataType::UInt16
120-
| DataType::UInt32
121-
| DataType::UInt64
122-
| DataType::Float32
123-
| DataType::Float64
124-
| DataType::Utf8
125-
| DataType::LargeUtf8
126-
| DataType::Binary
127-
| DataType::LargeBinary
128-
| DataType::Date32
129-
| DataType::Date64
130-
)
131-
}

0 commit comments

Comments
 (0)