duongcongtoai commented on code in PR #8652: URL: https://github.com/apache/arrow-rs/pull/8652#discussion_r2604389541
########## arrow-data/src/transform/dictionary.rs: ########## @@ -0,0 +1,140 @@ +// 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 std::collections::HashMap; + +use arrow_buffer::ArrowNativeType; +use arrow_schema::{ArrowError, DataType}; + +use crate::{ + ArrayData, + transform::{_MutableArrayData, Extend, MutableArrayData, utils::iter_in_bytes}, +}; + +pub(crate) fn merge_dictionaries<'a>( + key_data_type: &DataType, + value_data_type: &DataType, + dicts: &[&'a ArrayData], +) -> Result<(Vec<Extend<'a>>, ArrayData), ArrowError> { + match key_data_type { + DataType::UInt8 => merge_dictionaries_casted::<u8>(value_data_type, dicts), + DataType::UInt16 => merge_dictionaries_casted::<u16>(value_data_type, dicts), + DataType::UInt32 => merge_dictionaries_casted::<u32>(value_data_type, dicts), + DataType::UInt64 => merge_dictionaries_casted::<u64>(value_data_type, dicts), + DataType::Int8 => merge_dictionaries_casted::<i8>(value_data_type, dicts), + DataType::Int16 => merge_dictionaries_casted::<i16>(value_data_type, dicts), + DataType::Int32 => merge_dictionaries_casted::<i32>(value_data_type, dicts), + DataType::Int64 => merge_dictionaries_casted::<i64>(value_data_type, dicts), + _ => unreachable!(), + } +} + +fn merge_dictionaries_casted<'a, K: ArrowNativeType>( + data_type: &DataType, + dicts: &[&'a ArrayData], +) -> Result<(Vec<Extend<'a>>, ArrayData), ArrowError> { + let mut dedup = HashMap::new(); + let mut indices = vec![]; + let mut data_refs = vec![]; + let new_dict_keys = dicts + .iter() + .enumerate() + .map(|(dict_idx, dict)| { + let value_data = dict.child_data().first().unwrap(); + let old_keys = dict.buffer::<K>(0); + data_refs.push(value_data); + let mut new_keys = vec![K::usize_as(0); old_keys.len()]; + let values = iter_in_bytes(data_type, value_data); + for (key_index, old_key) in old_keys.iter().enumerate() { + if dict.is_valid(key_index) { + let value = values[old_key.as_usize()]; + match K::from_usize(dedup.len()) { + Some(idx) => { + let idx_for_value = dedup.entry(value).or_insert(idx); + // a new entry + if *idx_for_value == idx { + indices.push((dict_idx, old_key.as_usize())); + } + + new_keys[key_index] = *idx_for_value; + } + // the built dictionary has reach the cap of the key type + None => match dedup.get(value) { + // as long as this value has already been indexed + // the merge dictionary is still valid + Some(previous_key) => { + new_keys[key_index] = *previous_key; + } + None => return Err(ArrowError::DictionaryKeyOverflowError), Review Comment: let me add more coverage on this -- 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]
