scovich commented on code in PR #8366: URL: https://github.com/apache/arrow-rs/pull/8366#discussion_r2357495226
########## parquet-variant-compute/src/shred_variant.rs: ########## @@ -0,0 +1,717 @@ +// 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. + +//! Module for shredding VariantArray with a given schema. + +use crate::variant_array::{ShreddedVariantFieldArray, StructArrayBuilder}; +use crate::variant_to_arrow::{ + make_primitive_variant_to_arrow_row_builder, PrimitiveVariantToArrowRowBuilder, +}; +use crate::{VariantArray, VariantValueArrayBuilder}; +use arrow::array::{Array as _, ArrayRef, BinaryViewArray, NullBufferBuilder}; +use arrow::buffer::NullBuffer; +use arrow::compute::CastOptions; +use arrow::datatypes::{DataType, Fields}; +use arrow::error::{ArrowError, Result}; +use parquet_variant::{ObjectBuilder, ReadOnlyMetadataBuilder, Variant, EMPTY_VARIANT_METADATA}; + +use indexmap::IndexMap; +use std::sync::Arc; + +/// Shreds the input binary variant using a target shredding schema derived from the requested data type. +/// +/// For example, requesting `DataType::Int64` would produce an output variant array with the schema: +/// +/// ``` +/// { +/// metadata: BINARY, +/// value: BINARY, +/// typed_value: LONG, +/// } +/// ``` +/// +/// Similarly, requesting `DataType::Struct` with two integer fields `a` and `b` would produce an +/// output variant array with the schema: +/// +/// ``` +/// { +/// metadata: BINARY, +/// value: BINARY, +/// typed_value: { +/// a: { +/// value: BINARY, +/// typed_value: INT, +/// }, +/// b: { +/// value: BINARY, +/// typed_value: INT, +/// }, +/// } +/// } +/// ``` +pub fn shred_variant(array: &VariantArray, as_type: &DataType) -> Result<VariantArray> { + if array.typed_value_field().is_some() { + return Err(ArrowError::InvalidArgumentError( + "Input is already shredded".to_string(), + )); + } + + if array.value_field().is_none() { + // all-null case + return Ok(VariantArray::from_parts( + array.metadata_field().clone(), + None, + None, + None, + )); + }; + + let cast_options = CastOptions::default(); + let mut builder = make_variant_to_shredded_variant_arrow_row_builder( + as_type, + &cast_options, + array.len(), + true, + )?; + for i in 0..array.len() { + if array.is_null(i) { + builder.append_null()?; + } else { + builder.append_value(array.value(i))?; + } + } + let (value, typed_value, nulls) = builder.finish()?; + Ok(VariantArray::from_parts( + array.metadata_field().clone(), + Some(value), + Some(typed_value), + nulls, + )) +} + +pub(crate) fn make_variant_to_shredded_variant_arrow_row_builder<'a>( + data_type: &'a DataType, + cast_options: &'a CastOptions, + capacity: usize, + top_level: bool, +) -> Result<VariantToShreddedVariantRowBuilder<'a>> { + let builder = match data_type { + DataType::Struct(fields) => { + let typed_value_builder = VariantToShreddedObjectVariantRowBuilder::try_new( + fields, + cast_options, + capacity, + top_level, + )?; + VariantToShreddedVariantRowBuilder::Object(typed_value_builder) + } + DataType::List(_) + | DataType::LargeList(_) + | DataType::ListView(_) + | DataType::LargeListView(_) + | DataType::FixedSizeList(..) => { + // TODO: Special handling for shredded variant arrays Review Comment: Oh, I think the wording might be confusing, I'll adjust -- 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-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org