Skip to content

Commit 02fa399

Browse files
committed
feat(query): support group by variant
1 parent e105352 commit 02fa399

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/query/expression/src/kernels/group_by_hash.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl HashMethod for HashMethodSingleString {
170170
fn build_keys_iter<'a>(&self, key_state: &'a KeysState) -> Result<Self::HashKeyIter<'a>> {
171171
match key_state {
172172
KeysState::Column(Column::String(col)) => Ok(col.iter()),
173+
KeysState::Column(Column::Variant(col)) => Ok(col.iter()),
173174
_ => unreachable!(),
174175
}
175176
}

src/query/service/src/pipelines/processors/transforms/group_by/aggregator_groups_builder.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ pub struct SerializedKeysGroupColumnsBuilder<'a> {
8181

8282
impl<'a> SerializedKeysGroupColumnsBuilder<'a> {
8383
pub fn create(capacity: usize, data_capacity: usize, params: &AggregatorParams) -> Self {
84-
let (single_builder, data) =
85-
if params.group_data_types.len() == 1 && params.group_data_types[0].is_string() {
86-
(
87-
Some(StringColumnBuilder::with_capacity(capacity, data_capacity)),
88-
vec![],
89-
)
90-
} else {
91-
(None, Vec::with_capacity(capacity))
92-
};
84+
let (single_builder, data) = if params.group_data_types.len() == 1
85+
&& (params.group_data_types[0].is_string() || params.group_data_types[0].is_variant())
86+
{
87+
(
88+
Some(StringColumnBuilder::with_capacity(capacity, data_capacity)),
89+
vec![],
90+
)
91+
} else {
92+
(None, Vec::with_capacity(capacity))
93+
};
9394

9495
Self {
9596
data,
@@ -115,7 +116,11 @@ impl<'a> GroupColumnsBuilder for SerializedKeysGroupColumnsBuilder<'a> {
115116
fn finish(mut self) -> Result<Vec<Column>> {
116117
if let Some(builder) = self.single_builder.take() {
117118
let col = builder.build();
118-
return Ok(vec![Column::String(col)]);
119+
match self.group_data_types[0] {
120+
DataType::String => return Ok(vec![Column::String(col)]),
121+
DataType::Variant => return Ok(vec![Column::Variant(col)]),
122+
_ => {}
123+
}
119124
}
120125

121126
let rows = self.data.len();

tests/sqllogictests/suites/base/03_common/03_0003_select_group_by

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,20 @@ select (number % 3)::Decimal(19, 2) c, to_string(number % 3) d, count() from num
189189
0.00 0 34
190190
1.00 1 33
191191
2.00 2 33
192+
193+
194+
## group by variant https://github.com/datafuselabs/databend/issues/10603
195+
196+
query TI
197+
select number::Variant a, count() from numbers(3) group by a order by a;
198+
----
199+
0 1
200+
1 1
201+
2 1
202+
203+
query TI
204+
select number::Variant a, number as b, count() from numbers(3) group by a, b order by a;
205+
----
206+
0 0 1
207+
1 1 1
208+
2 2 1

0 commit comments

Comments
 (0)