You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/06 08:38:08 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2826: Convert rows to arrays (#2677)

tustvold commented on code in PR #2826:
URL: https://github.com/apache/arrow-rs/pull/2826#discussion_r988722284


##########
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];
+        *row = &row[key_offset + 2..];
+
+        let interned = match options.descending {
+            true => {
+                key_scratch.clear();
+                key_scratch.extend_from_slice(key);
+                key_scratch.iter_mut().for_each(|o| *o = !*o);
+                interner.lookup(&key_scratch).unwrap()
+            }
+            false => interner.lookup(key).unwrap(),
+        };
+
+        let k = *dictionary.entry(interned).or_insert_with(|| {
+            let k = values.len();
+            values.push(interner.value(interned));
+            K::Native::from_usize(k).unwrap()
+        });
+
+        keys.append(k);
+        null_builder.append(true);
+    }
+
+    let child = match &value_type {
+        DataType::Null => NullArray::new(values.len()).into_data(),
+        DataType::Boolean => decode_bool(&values),
+        DataType::Int8 => decode_primitive::<Int8Type>(&values),
+        DataType::Int16 => decode_primitive::<Int16Type>(&values),
+        DataType::Int32 => decode_primitive::<Int32Type>(&values),
+        DataType::Int64 => decode_primitive::<Int64Type>(&values),
+        DataType::UInt8 => decode_primitive::<UInt8Type>(&values),
+        DataType::UInt16 => decode_primitive::<UInt16Type>(&values),
+        DataType::UInt32 => decode_primitive::<UInt32Type>(&values),
+        DataType::UInt64 => decode_primitive::<UInt64Type>(&values),
+        DataType::Float16 => decode_primitive::<Float16Type>(&values),
+        DataType::Float32 => decode_primitive::<Float32Type>(&values),
+        DataType::Float64 => decode_primitive::<Float64Type>(&values),
+        DataType::Timestamp(TimeUnit::Second, _) => {
+            decode_primitive::<TimestampSecondType>(&values)
+        }
+        DataType::Timestamp(TimeUnit::Millisecond, _) => {
+            decode_primitive::<TimestampMillisecondType>(&values)
+        }
+        DataType::Timestamp(TimeUnit::Microsecond, _) => {
+            decode_primitive::<TimestampMicrosecondType>(&values)
+        }
+        DataType::Timestamp(TimeUnit::Nanosecond, _) => {
+            decode_primitive::<TimestampNanosecondType>(&values)
+        }
+        DataType::Date32 => decode_primitive::<Date32Type>(&values),
+        DataType::Date64 => decode_primitive::<Date64Type>(&values),
+        DataType::Time32(t) => match t {
+            TimeUnit::Second => decode_primitive::<Time32SecondType>(&values),
+            TimeUnit::Millisecond => decode_primitive::<Time32MillisecondType>(&values),
+            _ => unreachable!(),
+        },
+        DataType::Time64(t) => match t {
+            TimeUnit::Microsecond => decode_primitive::<Time64MicrosecondType>(&values),
+            TimeUnit::Nanosecond => decode_primitive::<Time64NanosecondType>(&values),
+            _ => unreachable!(),
+        },
+        DataType::Duration(TimeUnit::Second) => {
+            decode_primitive::<DurationSecondType>(&values)
+        }
+        DataType::Duration(TimeUnit::Millisecond) => {
+            decode_primitive::<DurationMillisecondType>(&values)
+        }
+        DataType::Duration(TimeUnit::Microsecond) => {
+            decode_primitive::<DurationMicrosecondType>(&values)
+        }
+        DataType::Duration(TimeUnit::Nanosecond) => {
+            decode_primitive::<DurationNanosecondType>(&values)
+        }
+        DataType::Interval(IntervalUnit::DayTime) => {
+            decode_primitive::<IntervalDayTimeType>(&values)
+        }
+        DataType::Interval(IntervalUnit::MonthDayNano) => {
+            decode_primitive::<IntervalMonthDayNanoType>(&values)
+        }
+        DataType::Interval(IntervalUnit::YearMonth) => {
+            decode_primitive::<IntervalYearMonthType>(&values)
+        }
+        DataType::Decimal128(p, s) => {
+            decode_decimal::<16, Decimal128Type>(&values, *p, *s)
+        }
+        DataType::Decimal256(p, s) => {
+            decode_decimal::<32, Decimal256Type>(&values, *p, *s)
+        }
+        DataType::Utf8 => decode_string::<i32>(&values),
+        DataType::LargeUtf8 => decode_string::<i64>(&values),
+        DataType::Binary => decode_binary::<i32>(&values),
+        DataType::LargeBinary => decode_binary::<i64>(&values),
+        _ => {
+            return Err(ArrowError::NotYetImplemented(format!(
+                "decoding dictionary values of {}",
+                value_type
+            )))
+        }
+    };
+
+    let data_type =
+        DataType::Dictionary(Box::new(K::DATA_TYPE), Box::new(value_type.clone()));
+
+    let builder = ArrayDataBuilder::new(data_type)
+        .len(len)
+        .null_bit_buffer(Some(null_builder.finish()))
+        .null_count(null_count)
+        .add_buffer(keys.finish())
+        .add_child_data(child);
+
+    Ok(DictionaryArray::from(builder.build_unchecked()))
+}
+
+/// Decodes a binary array from dictionary values
+///
+/// # Safety
+///
+/// Values must be valid UTF-8
+fn decode_binary<O: OffsetSizeTrait>(values: &[&[u8]]) -> ArrayData {
+    let capacity = values.iter().map(|x| x.len()).sum();

Review Comment:
   We aren't copying the values, but references to the values, but yes this potentially could be optimised



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org