-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable GroupValueBytesView
for aggregation with StringView types
#11519
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd3ca4b
add functions
XiangpengHao 012bd59
Update `string-view` branch to arrow-rs main (#10966)
alamb 4673554
merge
XiangpengHao 4b39020
update cast
XiangpengHao 80e8e06
consistent dep
XiangpengHao fb0266e
fix ci
XiangpengHao 61d113d
avoid unused dep
XiangpengHao b920216
update dep
XiangpengHao 18daf7e
update
XiangpengHao 03934da
fix cargo check
XiangpengHao 2944acd
better group value view aggregation
XiangpengHao 28426ff
update
XiangpengHao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
datafusion/physical-plan/src/aggregates/group_values/bytes_view.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::aggregates::group_values::GroupValues; | ||
use arrow_array::{Array, ArrayRef, RecordBatch}; | ||
use datafusion_expr::EmitTo; | ||
use datafusion_physical_expr::binary_map::OutputType; | ||
use datafusion_physical_expr_common::binary_view_map::ArrowBytesViewMap; | ||
|
||
/// A [`GroupValues`] storing single column of Utf8View/BinaryView values | ||
/// | ||
/// This specialization is significantly faster than using the more general | ||
/// purpose `Row`s format | ||
pub struct GroupValuesBytesView { | ||
/// Map string/binary values to group index | ||
map: ArrowBytesViewMap<usize>, | ||
/// The total number of groups so far (used to assign group_index) | ||
num_groups: usize, | ||
} | ||
|
||
impl GroupValuesBytesView { | ||
pub fn new(output_type: OutputType) -> Self { | ||
Self { | ||
map: ArrowBytesViewMap::new(output_type), | ||
num_groups: 0, | ||
} | ||
} | ||
} | ||
|
||
impl GroupValues for GroupValuesBytesView { | ||
fn intern( | ||
&mut self, | ||
cols: &[ArrayRef], | ||
groups: &mut Vec<usize>, | ||
) -> datafusion_common::Result<()> { | ||
assert_eq!(cols.len(), 1); | ||
|
||
// look up / add entries in the table | ||
let arr = &cols[0]; | ||
|
||
groups.clear(); | ||
self.map.insert_if_new( | ||
arr, | ||
// called for each new group | ||
|_value| { | ||
// assign new group index on each insert | ||
let group_idx = self.num_groups; | ||
self.num_groups += 1; | ||
group_idx | ||
}, | ||
// called for each group | ||
|group_idx| { | ||
groups.push(group_idx); | ||
}, | ||
); | ||
|
||
// ensure we assigned a group to for each row | ||
assert_eq!(groups.len(), arr.len()); | ||
Ok(()) | ||
} | ||
|
||
fn size(&self) -> usize { | ||
self.map.size() + std::mem::size_of::<Self>() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.num_groups == 0 | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.num_groups | ||
} | ||
|
||
fn emit(&mut self, emit_to: EmitTo) -> datafusion_common::Result<Vec<ArrayRef>> { | ||
// Reset the map to default, and convert it into a single array | ||
let map_contents = self.map.take().into_state(); | ||
|
||
let group_values = match emit_to { | ||
EmitTo::All => { | ||
self.num_groups -= map_contents.len(); | ||
map_contents | ||
} | ||
EmitTo::First(n) if n == self.len() => { | ||
self.num_groups -= map_contents.len(); | ||
map_contents | ||
} | ||
EmitTo::First(n) => { | ||
// if we only wanted to take the first n, insert the rest back | ||
// into the map we could potentially avoid this reallocation, at | ||
// the expense of much more complex code. | ||
// see https://github.com/apache/datafusion/issues/9195 | ||
let emit_group_values = map_contents.slice(0, n); | ||
let remaining_group_values = | ||
map_contents.slice(n, map_contents.len() - n); | ||
|
||
self.num_groups = 0; | ||
let mut group_indexes = vec![]; | ||
self.intern(&[remaining_group_values], &mut group_indexes)?; | ||
|
||
// Verify that the group indexes were assigned in the correct order | ||
assert_eq!(0, group_indexes[0]); | ||
|
||
emit_group_values | ||
} | ||
}; | ||
|
||
Ok(vec![group_values]) | ||
} | ||
|
||
fn clear_shrink(&mut self, _batch: &RecordBatch) { | ||
// in theory we could potentially avoid this reallocation and clear the | ||
// contents of the maps, but for now we just reset the map from the beginning | ||
self.map.take(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏