-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(udf): POC faster min max accumulator #12677
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
Closed
Closed
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
571013d
feat: wip: working on it
devanbenz f7634e1
feat: working on it
devanbenz 6d522fa
feat(udf): POC for native min max accumulators
devanbenz bd9ea7d
feat: revert some changes
devanbenz fe64903
feat: add BinaryView to groups accum supported
devanbenz fbdf867
feat: revert some changes while testing
devanbenz 0a93803
feat: rename file
devanbenz 4819521
chore: add license header
devanbenz 6f048fd
feat: clippy + fmt + check
devanbenz 1cefad5
chore: fmt
devanbenz a60d599
feat: fix max accum
devanbenz 268aa91
chore: fix emit_to calls
devanbenz 77b980f
fix: rm not needed import
devanbenz 660eaa4
feat: moves all functionality to single primitive strings function
devanbenz 7205cca
feat: rename to string_op
devanbenz a52cefe
fix: need to implement own accumulator
devanbenz a0a1572
fix: fmt
devanbenz 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 hidden or 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 |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
|
||
pub mod count_distinct; | ||
pub mod groups_accumulator; | ||
pub mod min_max; |
17 changes: 17 additions & 0 deletions
17
datafusion/functions-aggregate-common/src/aggregate/min_max.rs
This file contains hidden or 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,17 @@ | ||
// 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 | ||
// "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. | ||
pub mod groups_accumulator_max_view; | ||
pub mod groups_accumulator_min_view; |
203 changes: 203 additions & 0 deletions
203
datafusion/functions-aggregate-common/src/aggregate/min_max/groups_accumulator_max_view.rs
This file contains hidden or 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,203 @@ | ||
// 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 | ||
// "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 arrow::array::{Array, ArrayRef, AsArray, BinaryViewBuilder, BooleanArray}; | ||
use datafusion_common::{DataFusionError, Result}; | ||
use datafusion_expr_common::groups_accumulator::{EmitTo, GroupsAccumulator}; | ||
use std::sync::Arc; | ||
|
||
pub struct GroupsAccumulatorMaxStringView { | ||
states: Vec<String>, | ||
} | ||
|
||
impl Default for GroupsAccumulatorMaxStringView { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl GroupsAccumulatorMaxStringView { | ||
pub fn new() -> Self { | ||
Self { states: Vec::new() } | ||
} | ||
} | ||
|
||
impl GroupsAccumulator for GroupsAccumulatorMaxStringView { | ||
fn update_batch( | ||
&mut self, | ||
values: &[ArrayRef], | ||
group_indices: &[usize], | ||
opt_filter: Option<&BooleanArray>, | ||
total_num_groups: usize, | ||
) -> Result<()> { | ||
if self.states.len() < total_num_groups { | ||
self.states.resize(total_num_groups, String::new()); | ||
} | ||
|
||
let input_array = &values[0]; | ||
|
||
for (i, &group_index) in group_indices.iter().enumerate() { | ||
if let Some(filter) = opt_filter { | ||
if !filter.value(i) { | ||
continue; | ||
} | ||
} | ||
|
||
if input_array.is_null(i) { | ||
continue; | ||
} | ||
|
||
let value = input_array.as_binary_view().value(i); | ||
|
||
let value_str = std::str::from_utf8(value).map_err(|e| { | ||
DataFusionError::Execution(format!( | ||
"could not build utf8 from binary view {}", | ||
e | ||
)) | ||
})?; | ||
|
||
if self.states[group_index].is_empty() { | ||
self.states[group_index] = value_str.to_string(); | ||
} else { | ||
let curr_value_bytes = self.states[group_index].as_bytes(); | ||
if value > curr_value_bytes { | ||
self.states[group_index] = value_str.parse().unwrap(); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> { | ||
let states = emit_to.take_needed(&mut self.states); | ||
|
||
let mut builder = BinaryViewBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
let array = Arc::new(builder.finish()) as ArrayRef; | ||
Ok(array) | ||
} | ||
|
||
fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> { | ||
let states = emit_to.take_needed(&mut self.states); | ||
|
||
let mut builder = BinaryViewBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
let array = Arc::new(builder.finish()) as ArrayRef; | ||
Ok(vec![array]) | ||
} | ||
|
||
fn merge_batch( | ||
&mut self, | ||
values: &[ArrayRef], | ||
group_indices: &[usize], | ||
opt_filter: Option<&BooleanArray>, | ||
total_num_groups: usize, | ||
) -> Result<()> { | ||
if self.states.len() < total_num_groups { | ||
self.states.resize(total_num_groups, String::new()); | ||
} | ||
|
||
let input_array = &values[0]; | ||
|
||
for (i, &group_index) in group_indices.iter().enumerate() { | ||
if let Some(filter) = opt_filter { | ||
if !filter.value(i) { | ||
continue; | ||
} | ||
} | ||
|
||
if input_array.is_null(i) { | ||
continue; | ||
} | ||
|
||
let value = input_array.as_binary_view().value(i); | ||
|
||
let value_str = std::str::from_utf8(value).map_err(|e| { | ||
DataFusionError::Execution(format!( | ||
"could not build utf8 from binary view {}", | ||
e | ||
)) | ||
})?; | ||
|
||
if self.states[group_index].is_empty() { | ||
self.states[group_index] = value_str.to_string(); | ||
} else { | ||
let curr_value_bytes = self.states[group_index].as_bytes(); | ||
if value > curr_value_bytes { | ||
self.states[group_index] = value_str.parse().unwrap(); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn convert_to_state( | ||
&self, | ||
values: &[ArrayRef], | ||
opt_filter: Option<&BooleanArray>, | ||
) -> Result<Vec<ArrayRef>> { | ||
let input_array = &values[0]; | ||
|
||
if opt_filter.is_none() { | ||
return Ok(vec![Arc::<dyn arrow::array::Array>::clone(input_array)]); | ||
} | ||
|
||
let filter = opt_filter.unwrap(); | ||
|
||
let mut builder = BinaryViewBuilder::new(); | ||
|
||
for i in 0..values.len() { | ||
let value = input_array.as_binary_view().value(i); | ||
|
||
if !filter.value(i) { | ||
builder.append_null(); | ||
continue; | ||
} | ||
|
||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value); | ||
} | ||
} | ||
|
||
let array = Arc::new(builder.finish()) as ArrayRef; | ||
Ok(vec![array]) | ||
} | ||
|
||
fn supports_convert_to_state(&self) -> bool { | ||
true | ||
} | ||
|
||
fn size(&self) -> usize { | ||
self.states.iter().map(|s| s.len()).sum() | ||
} | ||
} |
Oops, something went wrong.
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.
I think much of the filter logic is handled by
accumulate_indices
datafusion/datafusion/functions-aggregate/src/count.rs
Lines 417 to 424 in f54712d
You could likely avoid much of this repetition (and likely it would be faster)
It woudl also be nice to avoid the duplication between min /max by using generics. Here is how the primitive one does it (passes in a comparison function)
https://github.com/apache/datafusion/blob/main/datafusion/functions-aggregate/src/min_max.rs#L119
Uh oh!
There was an error while loading. Please reload this page.
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.
Okay so I would probably want to do something like this?
And then make this generic. I.E. I can pass a generic function in instead of:
Afterwards I can likely use a const generic for deciding how to down-cast here with string array or string view?
let value = input_array.as_binary_view().value(i);