tustvold commented on code in PR #2593: URL: https://github.com/apache/arrow-rs/pull/2593#discussion_r966987721
########## arrow/src/row/interner.rs: ########## @@ -0,0 +1,326 @@ +// 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 hashbrown::hash_map::RawEntryMut; +use hashbrown::HashMap; +use std::cmp::Ordering; +use std::num::NonZeroU32; +use std::ops::Index; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct Interned(NonZeroU32); + +/// A byte array interner that generates normalized keys that are sorted with respect +/// to the interned values, e.g. `inter(a) < intern(b) => a < b` +#[derive(Debug, Default)] +pub struct OrderPreservingInterner { + keys: InternBuffer, + values: InternBuffer, + bucket: Box<Bucket>, + + hasher: ahash::RandomState, + lookup: HashMap<Interned, (), ()>, +} + +impl OrderPreservingInterner { + /// Interns an iterator of values returning a list of [`Interned`] which can be + /// used with [`Self::normalized_key`] to retrieve the normalized keys with a + /// lifetime not tied to the mutable borrow passed to this method + pub fn intern<I, V>(&mut self, input: I) -> Vec<Option<Interned>> + where + I: IntoIterator<Item = Option<V>>, + V: AsRef<[u8]>, + { + let iter = input.into_iter(); + let capacity = iter.size_hint().0; + let mut out = Vec::with_capacity(capacity); + let mut to_intern: Vec<(usize, u64, V)> = Vec::with_capacity(capacity); + let mut to_intern_len = 0; + + for (idx, item) in iter.enumerate() { + let value: V = match item { + Some(value) => value, + None => { + out.push(None); + continue; + } + }; + + let v = value.as_ref(); + let hash = self.hasher.hash_one(v); + let entry = self + .lookup + .raw_entry_mut() + .from_hash(hash, |a| &self.values[*a] == v); + + match entry { + RawEntryMut::Occupied(o) => out.push(Some(*o.key())), + RawEntryMut::Vacant(_) => { + // Push placeholder + out.push(None); + to_intern_len += v.len(); + to_intern.push((idx, hash, value)); + } + }; + } + + to_intern.sort_unstable_by(|(_, _, a), (_, _, b)| a.as_ref().cmp(b.as_ref())); + + self.keys.offsets.reserve(to_intern.len()); Review Comment: Yes -- 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]
