-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(udf): POC faster min max accumulator #12677
Closed
Closed
Changes from all 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 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
249 changes: 249 additions & 0 deletions
249
datafusion/functions-aggregate-common/src/aggregate/groups_accumulator/string_op.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,249 @@ | ||
// 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, BinaryBuilder, BinaryViewBuilder, BooleanArray, | ||
}; | ||
use datafusion_common::{DataFusionError, Result}; | ||
use datafusion_expr_common::groups_accumulator::{EmitTo, GroupsAccumulator}; | ||
use std::sync::Arc; | ||
|
||
pub struct StringGroupsAccumulator<F, const VIEW: bool> { | ||
states: Vec<String>, | ||
fun: F, | ||
} | ||
|
||
impl<F, const VIEW: bool> StringGroupsAccumulator<F, VIEW> | ||
where | ||
F: Fn(&[u8], &[u8]) -> bool + Send + Sync, | ||
{ | ||
pub fn new(s_fn: F) -> Self { | ||
Self { | ||
states: Vec::new(), | ||
fun: s_fn, | ||
} | ||
} | ||
} | ||
|
||
impl<F, const VIEW: bool> GroupsAccumulator for StringGroupsAccumulator<F, VIEW> | ||
where | ||
F: Fn(&[u8], &[u8]) -> bool + Send + Sync, | ||
{ | ||
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() { | ||
invoke_accumulator::<F, VIEW>(self, input_array, opt_filter, group_index, i) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> { | ||
let states = emit_to.take_needed(&mut self.states); | ||
|
||
let array = if VIEW { | ||
let mut builder = BinaryViewBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
Arc::new(builder.finish()) as ArrayRef | ||
} else { | ||
let mut builder = BinaryBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
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 array = if VIEW { | ||
let mut builder = BinaryViewBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
Arc::new(builder.finish()) as ArrayRef | ||
} else { | ||
let mut builder = BinaryBuilder::new(); | ||
|
||
for value in states { | ||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value.as_bytes()); | ||
} | ||
} | ||
|
||
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() { | ||
invoke_accumulator::<F, VIEW>(self, input_array, opt_filter, group_index, i) | ||
} | ||
|
||
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 Array>::clone(input_array)]); | ||
} | ||
|
||
let filter = opt_filter.unwrap(); | ||
|
||
let array = if VIEW { | ||
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); | ||
} | ||
} | ||
|
||
Arc::new(builder.finish()) as ArrayRef | ||
} else { | ||
let mut builder = BinaryBuilder::new(); | ||
|
||
for i in 0..values.len() { | ||
let value = input_array.as_binary::<i32>().value(i); | ||
|
||
if !filter.value(i) { | ||
builder.append_null(); | ||
continue; | ||
} | ||
|
||
if value.is_empty() { | ||
builder.append_null(); | ||
} else { | ||
builder.append_value(value); | ||
} | ||
} | ||
|
||
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() | ||
} | ||
} | ||
|
||
fn invoke_accumulator<F, const VIEW: bool>( | ||
accumulator: &mut StringGroupsAccumulator<F, VIEW>, | ||
input_array: &ArrayRef, | ||
opt_filter: Option<&BooleanArray>, | ||
group_index: usize, | ||
i: usize, | ||
) where | ||
F: Fn(&[u8], &[u8]) -> bool + Send + Sync, | ||
{ | ||
if let Some(filter) = opt_filter { | ||
if !filter.value(i) { | ||
return; | ||
} | ||
} | ||
if input_array.is_null(i) { | ||
return; | ||
} | ||
|
||
let value: &[u8] = if VIEW { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Optimize downcasts (remove them) |
||
input_array.as_binary_view().value(i) | ||
} else { | ||
input_array.as_binary::<i32>().value(i) | ||
}; | ||
|
||
let value_str = std::str::from_utf8(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Optimize |
||
.map_err(|e| DataFusionError::Execution(format!("could not build utf8 {}", e))) | ||
.expect("failed to build utf8"); | ||
|
||
if accumulator.states[group_index].is_empty() { | ||
accumulator.states[group_index] = value_str.to_string(); | ||
} else { | ||
let curr_value_bytes = accumulator.states[group_index].as_bytes(); | ||
if (accumulator.fun)(value, curr_value_bytes) { | ||
accumulator.states[group_index] = value_str.parse().unwrap(); | ||
} | ||
} | ||
} |
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
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.
TODO: Remove downcasts