scovich commented on code in PR #9021: URL: https://github.com/apache/arrow-rs/pull/9021#discussion_r2640201636
########## parquet-variant-compute/src/decoder.rs: ########## @@ -0,0 +1,252 @@ +// 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 arrow_array::{Array, StructArray}; +use arrow_json::{DecoderFactory, StructMode}; +use parquet_variant::{ObjectFieldBuilder, Variant, VariantBuilder, VariantBuilderExt}; +use crate::{VariantArrayBuilder, VariantType}; +use arrow_data::ArrayData; +use arrow_schema::{ArrowError, DataType, FieldRef}; + +use arrow_json::reader::ArrayDecoder; +use arrow_json::reader::{Tape, TapeElement}; + +/// An [`ArrayDecoder`] implementation that decodes JSON values into a Variant array. +/// +/// This decoder converts JSON tape elements (parsed JSON tokens) into Parquet Variant +/// format, preserving the full structure of arbitrary JSON including nested objects, +/// arrays, and primitive types. +/// +/// This decoder is typically used indirectly via [`VariantArrayDecoderFactory`] when +/// reading JSON data into Variant columns. +#[derive(Default)] +pub struct VariantArrayDecoder; + +impl ArrayDecoder for VariantArrayDecoder { + fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, ArrowError> { + let mut array_builder = VariantArrayBuilder::new(pos.len()); + for p in pos { + let mut builder = VariantBuilder::new(); + variant_from_tape_element(&mut builder, *p, tape)?; + let (metadata, value) = builder.finish(); + array_builder.append_value(Variant::new(&metadata, &value)); + } + let variant_struct_array: StructArray = array_builder.build().into(); + Ok(variant_struct_array.into_data()) + } +} + +/// A [`DecoderFactory`] that creates [`VariantArrayDecoder`] instances for Variant-typed fields. +/// +/// This factory integrates with the Arrow JSON reader to automatically decode JSON values +/// into Variant arrays when the target field is registered as a [`VariantType`] extension type. +/// +/// # Example +/// +/// ```ignore +/// use arrow_json::reader::ReaderBuilder; +/// use arrow_json::StructMode; +/// use std::sync::Arc; +/// +/// let builder = ReaderBuilder::new(Arc::new(schema)); +/// let reader = builder +/// .with_struct_mode(StructMode::ObjectOnly) +/// .with_decoder_factory(Arc::new(VariantArrayDecoderFactory)) +/// .build(json_input)?; +/// ``` +#[derive(Default, Debug)] +#[allow(unused)] +pub struct VariantArrayDecoderFactory; + +impl DecoderFactory for VariantArrayDecoderFactory { + fn make_default_decoder<'a>(&self, field: Option<FieldRef>, + _data_type: DataType, + _coerce_primitive: bool, + _strict_mode: bool, + _is_nullable: bool, + _struct_mode: StructMode, + ) -> Result<Option<Box<dyn ArrayDecoder>>, ArrowError> { + if let Some(field) = field && field.try_extension_type::<VariantType>().is_ok() { Review Comment: My comment was more about the extension type API, less about your code that's forced to use what we currently have. Sorry if that was unclear. I don't think it's enough to just check the extension name. The extension may involve other metadata and also has opinions about which physical data types are valid. I suppose we could do a hybrid -- only try to instantiate the extension type if the name matches? -- 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]
