Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datafusion/functions-aggregate-common/src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

pub mod count_distinct;
pub mod groups_accumulator;
pub mod min_max;
17 changes: 17 additions & 0 deletions datafusion/functions-aggregate-common/src/aggregate/min_max.rs
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;
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() {
Copy link
Contributor

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

accumulate_indices(
group_indices,
values.logical_nulls().as_ref(),
opt_filter,
|group_index| {
self.counts[group_index] += 1;
},
);

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

Copy link
Contributor Author

@devanbenz devanbenz Oct 3, 2024

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?

accumulate_indices(group_indices, input_array.logical_nulls().as_ref(), opt_filter, |group_index| {
        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
            ))
        }).unwrap();
    
        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();
            }
        } 
});

And then make this generic. I.E. I can pass a generic function in instead of:

        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();
            }
        } 

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);

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()
}
}
Loading