tustvold commented on code in PR #2826: URL: https://github.com/apache/arrow-rs/pull/2826#discussion_r988752645
########## arrow/src/row/dictionary.rs: ########## @@ -0,0 +1,296 @@ +// 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::compute::SortOptions; +use crate::row::fixed::{FixedLengthEncoding, FromSlice, RawDecimal}; +use crate::row::interner::{Interned, OrderPreservingInterner}; +use arrow_array::builder::*; +use arrow_array::cast::*; +use arrow_array::types::*; +use arrow_array::*; +use arrow_buffer::{ArrowNativeType, MutableBuffer, ToByteSlice}; +use arrow_data::{ArrayData, ArrayDataBuilder}; +use arrow_schema::{ArrowError, DataType, IntervalUnit, TimeUnit}; +use std::collections::HashMap; + +/// Computes the dictionary mapping for the given dictionary values +pub fn compute_dictionary_mapping( + interner: &mut OrderPreservingInterner, + values: &ArrayRef, +) -> Result<Vec<Option<Interned>>, ArrowError> { + Ok(downcast_primitive_array! { + values => interner + .intern(values.iter().map(|x| x.map(|x| x.encode()))), + DataType::Binary => { + let iter = as_generic_binary_array::<i64>(values).iter(); + interner.intern(iter) + } + DataType::LargeBinary => { + let iter = as_generic_binary_array::<i64>(values).iter(); + interner.intern(iter) + } + DataType::Utf8 => { + let iter = as_string_array(values).iter().map(|x| x.map(|x| x.as_bytes())); + interner.intern(iter) + } + DataType::LargeUtf8 => { + let iter = as_largestring_array(values).iter().map(|x| x.map(|x| x.as_bytes())); + interner.intern(iter) + } + t => return Err(ArrowError::NotYetImplemented(format!("dictionary value {} is not supported", t))), + }) +} + +/// Decodes a string array from `rows` with the provided `options` +/// +/// # Safety +/// +/// `interner` must contain valid data for the provided `value_type` +pub unsafe fn decode_dictionary<K: ArrowDictionaryKeyType>( + interner: &OrderPreservingInterner, + value_type: &DataType, + options: SortOptions, + rows: &mut [&[u8]], +) -> Result<DictionaryArray<K>, ArrowError> { + let len = rows.len(); + let mut dictionary: HashMap<Interned, K::Native> = HashMap::with_capacity(len); + + let null_sentinel = match options.nulls_first { + true => 0_u8, + false => 0xFF, + }; + + let null_terminator = match options.descending { + true => 0xFF, + false => 0_u8, + }; + + let mut null_builder = BooleanBufferBuilder::new(len); + let mut keys = BufferBuilder::<K::Native>::new(len); + let mut values = Vec::with_capacity(len); + let mut null_count = 0; + let mut key_scratch = Vec::new(); + + for row in rows { + if row[0] == null_sentinel { + null_builder.append(false); + null_count += 1; + *row = &row[1..]; + keys.append(K::Native::default()); + continue; + } + + let key_offset = row + .iter() + .skip(1) + .position(|x| *x == null_terminator) + .unwrap(); + let key = &row[1..key_offset + 2]; Review Comment: This isn't copying values, it is just copying a fat pointer (address + slice length). The interner expects the same key it spat out, which includes the null terminator -- 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]
