alamb commented on code in PR #8827: URL: https://github.com/apache/arrow-datafusion/pull/8827#discussion_r1450869492
########## datafusion/physical-plan/src/aggregates/group_values/binary.rs: ########## @@ -0,0 +1,258 @@ +// 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 ahash::RandomState; +use arrow_array::{Array, ArrayRef, RecordBatch, StringArray}; +use arrow_buffer::{ + BooleanBufferBuilder, BufferBuilder, NullBuffer, OffsetBuffer, ScalarBuffer, +}; +use arrow_schema::DataType; +use datafusion_common::cast::as_string_array; +use datafusion_common::hash_utils::create_hashes; +use datafusion_execution::memory_pool::proxy::{RawTableAllocExt, VecAllocExt}; +use datafusion_physical_expr::EmitTo; +use hashbrown::raw::RawTable; +use std::sync::Arc; + +/// A [`GroupValues`] storing single column of Utf8/LargeUtf8/Binary/LargeBinary values +/// +/// This specialization is significantly faster than using the more general +/// purpose `Row`s format +pub struct GroupValuesBinary { + /// The data type of the output array + data_type: DataType, + /// Stores the group index based on the hash of its value + map: RawTable<GroupEntry>, + /// The group index of the null value if any + null_group: Option<usize>, + /// The total number of groups so far (used to assign group_index) + num_groups: usize, + /// Total size of the map in bytes + map_size: usize, + /// The raw string value for each group (becomes the variable length data in + /// the output array) + buffer: BufferBuilder<u8>, + /// The random state used to generate hashes + random_state: RandomState, + // buffer to be reused to store hashes + hashes_buffer: Vec<u64>, +} + +// TODO we could move the string set to a separate struct to reduce the size and +// cognative load of GroupValuesBinary. Would have to make it generic on payload +// type (in this case group_idx) + +/// Stores information about string value. +/// +/// Is also used to create the offsets when creating for the output array. +#[derive(Debug)] +struct GroupEntry { + /// hash of the string value (used when resizing table) + hash: u64, Review Comment: I believe it is needed when emitting only a portion of the hash table. Note this code is based largely on https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-plan/src/aggregates/group_values/row.rs so that might offer some insight -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
