Dandandan commented on code in PR #7043: URL: https://github.com/apache/arrow-datafusion/pull/7043#discussion_r1269874826
########## datafusion/core/src/physical_plan/aggregates/group_values/primitive.rs: ########## @@ -0,0 +1,191 @@ +// 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::physical_plan::aggregates::group_values::GroupValues; +use ahash::RandomState; +use arrow::array::BooleanBufferBuilder; +use arrow::buffer::NullBuffer; +use arrow::datatypes::i256; +use arrow_array::cast::AsArray; +use arrow_array::{ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType, PrimitiveArray}; +use arrow_schema::DataType; +use datafusion_common::Result; +use datafusion_execution::memory_pool::proxy::VecAllocExt; +use datafusion_physical_expr::EmitTo; +use half::f16; +use hashbrown::raw::RawTable; +use std::sync::Arc; + +/// A trait to allow hashing of floating point numbers +trait HashValue { + fn hash(self, state: &RandomState) -> u64; +} + +macro_rules! hash_integer { + ($($t:ty),+) => { + $(impl HashValue for $t { + fn hash(self, state: &RandomState) -> u64 { + state.hash_one(self) + } + })+ + }; +} +hash_integer!(i8, i16, i32, i64, i128, i256); +hash_integer!(u8, u16, u32, u64); + +macro_rules! hash_float { + ($($t:ty),+) => { + $(impl HashValue for $t { + fn hash(self, state: &RandomState) -> u64 { + state.hash_one(self.to_bits()) + } + })+ + }; +} + +hash_float!(f16, f32, f64); + +/// A [`GroupValues`] storing raw primitive values +pub struct GroupValuesPrimitive<T: ArrowPrimitiveType> { + /// The data type of the output array + data_type: DataType, + /// Stores the group index based on the hash of its value + map: RawTable<usize>, + /// The group index of the null value if any + null_group: Option<usize>, + /// The values for each group index + values: Vec<T::Native>, + /// The random state used to generate hashes + random_state: RandomState, +} + +impl<T: ArrowPrimitiveType> GroupValuesPrimitive<T> { + pub fn new(data_type: DataType) -> Self { + assert!(PrimitiveArray::<T>::is_compatible(&data_type)); + Self { + data_type, + map: RawTable::with_capacity(128), + values: Vec::with_capacity(128), + null_group: None, + random_state: Default::default(), + } + } +} + +impl<T: ArrowPrimitiveType> GroupValues for GroupValuesPrimitive<T> +where + T::Native: HashValue, +{ + fn intern(&mut self, cols: &[ArrayRef], groups: &mut Vec<usize>) -> Result<()> { + assert_eq!(cols.len(), 1); + groups.clear(); + + for v in cols[0].as_primitive::<T>() { + let group_id = match v { + None => *self.null_group.get_or_insert_with(|| { + let group_id = self.values.len(); + self.values.push(Default::default()); + group_id + }), + Some(key) => { + let state = &self.random_state; + let hash = key.hash(state); + let insert = self.map.find_or_find_insert_slot( + hash, + |g| unsafe { self.values.get_unchecked(*g).is_eq(key) }, Review Comment: this is awesome 🚀 ########## datafusion/core/src/physical_plan/aggregates/group_values/primitive.rs: ########## @@ -0,0 +1,191 @@ +// 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::physical_plan::aggregates::group_values::GroupValues; +use ahash::RandomState; +use arrow::array::BooleanBufferBuilder; +use arrow::buffer::NullBuffer; +use arrow::datatypes::i256; +use arrow_array::cast::AsArray; +use arrow_array::{ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType, PrimitiveArray}; +use arrow_schema::DataType; +use datafusion_common::Result; +use datafusion_execution::memory_pool::proxy::VecAllocExt; +use datafusion_physical_expr::EmitTo; +use half::f16; +use hashbrown::raw::RawTable; +use std::sync::Arc; + +/// A trait to allow hashing of floating point numbers +trait HashValue { + fn hash(self, state: &RandomState) -> u64; +} + +macro_rules! hash_integer { + ($($t:ty),+) => { + $(impl HashValue for $t { + fn hash(self, state: &RandomState) -> u64 { + state.hash_one(self) + } + })+ + }; +} +hash_integer!(i8, i16, i32, i64, i128, i256); +hash_integer!(u8, u16, u32, u64); + +macro_rules! hash_float { + ($($t:ty),+) => { + $(impl HashValue for $t { + fn hash(self, state: &RandomState) -> u64 { + state.hash_one(self.to_bits()) + } + })+ + }; +} + +hash_float!(f16, f32, f64); + +/// A [`GroupValues`] storing raw primitive values +pub struct GroupValuesPrimitive<T: ArrowPrimitiveType> { + /// The data type of the output array + data_type: DataType, + /// Stores the group index based on the hash of its value + map: RawTable<usize>, + /// The group index of the null value if any + null_group: Option<usize>, + /// The values for each group index + values: Vec<T::Native>, + /// The random state used to generate hashes + random_state: RandomState, +} + +impl<T: ArrowPrimitiveType> GroupValuesPrimitive<T> { + pub fn new(data_type: DataType) -> Self { + assert!(PrimitiveArray::<T>::is_compatible(&data_type)); + Self { + data_type, + map: RawTable::with_capacity(128), + values: Vec::with_capacity(128), + null_group: None, + random_state: Default::default(), + } + } +} + +impl<T: ArrowPrimitiveType> GroupValues for GroupValuesPrimitive<T> +where + T::Native: HashValue, +{ + fn intern(&mut self, cols: &[ArrayRef], groups: &mut Vec<usize>) -> Result<()> { + assert_eq!(cols.len(), 1); + groups.clear(); + + for v in cols[0].as_primitive::<T>() { + let group_id = match v { + None => *self.null_group.get_or_insert_with(|| { + let group_id = self.values.len(); + self.values.push(Default::default()); + group_id + }), + Some(key) => { + let state = &self.random_state; + let hash = key.hash(state); + let insert = self.map.find_or_find_insert_slot( + hash, + |g| unsafe { self.values.get_unchecked(*g).is_eq(key) }, Review Comment: this is awesome 🚀 -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org