tustvold commented on code in PR #3558: URL: https://github.com/apache/arrow-rs/pull/3558#discussion_r1313543657
########## arrow-select/src/dictionary.rs: ########## @@ -0,0 +1,333 @@ +// 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::interleave::interleave; +use ahash::RandomState; +use arrow_array::builder::BooleanBufferBuilder; +use arrow_array::cast::AsArray; +use arrow_array::types::{ + ArrowDictionaryKeyType, BinaryType, ByteArrayType, LargeBinaryType, LargeUtf8Type, + Utf8Type, +}; +use arrow_array::{Array, ArrayRef, DictionaryArray, GenericByteArray}; +use arrow_buffer::{ArrowNativeType, BooleanBuffer, ScalarBuffer}; +use arrow_schema::{ArrowError, DataType}; + +/// A best effort interner that maintains a fixed number of buckets +/// and interns keys based on their hash value +/// +/// Hash collisions will result in replacement +struct Interner<'a, V> { + state: RandomState, + buckets: Vec<Option<(&'a [u8], V)>>, + shift: u32, +} + +impl<'a, V> Interner<'a, V> { + /// Capacity controls the number of unique buckets allocated within the Interner + /// + /// A larger capacity reduces the probability of hash collisions, and should be set + /// based on an approximation of the upper bound of unique values + fn new(capacity: usize) -> Self { + // Add additional buckets to help reduce collisions + let shift = (capacity as u64 + 128).leading_zeros(); + let num_buckets = (u64::MAX >> shift) as usize; + let buckets = (0..num_buckets.saturating_add(1)).map(|_| None).collect(); + Self { + // A fixed seed to ensure deterministic behaviour + state: RandomState::with_seeds(0, 0, 0, 0), + buckets, + shift, + } + } + + fn intern<F: FnOnce() -> Result<V, E>, E>( + &mut self, + new: &'a [u8], + f: F, + ) -> Result<&V, E> { + let hash = self.state.hash_one(new); + let bucket_idx = hash >> self.shift; + Ok(match &mut self.buckets[bucket_idx as usize] { + Some((current, v)) => { + if *current != new { + *v = f()?; + *current = new; + } + v + } + slot => &slot.insert((new, f()?)).1, + }) + } +} + +pub struct MergedDictionaries<K: ArrowDictionaryKeyType> { + /// Provides `key_mappings[`array_idx`][`old_key`] -> new_key` + pub key_mappings: Vec<Vec<K::Native>>, + /// The new values + pub values: ArrayRef, +} + +/// Performs a cheap, pointer-based comparison of two byte array +/// +/// See [`ScalarBuffer::ptr_eq`] +fn bytes_ptr_eq<T: ByteArrayType>(a: &dyn Array, b: &dyn Array) -> bool { Review Comment: I could see us eventually promoting this to a first-class API on array -- 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]
