harshmotw-db commented on code in PR #7884: URL: https://github.com/apache/arrow-rs/pull/7884#discussion_r2193373037
########## parquet-variant-compute/src/to_json.rs: ########## @@ -0,0 +1,178 @@ +// 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 transforming a batch of Variants represented as +//! STRUCT<metadata: BINARY, value: BINARY> into a batch of JSON strings. + +use arrow::array::{Array, ArrayRef, BinaryArray, BooleanBufferBuilder, StringArray, StructArray}; +use arrow::buffer::{Buffer, NullBuffer, OffsetBuffer, ScalarBuffer}; +use arrow::datatypes::DataType; +use arrow_schema::ArrowError; +use parquet_variant::{variant_to_json, Variant}; + +pub fn batch_variant_to_json_string(input: &ArrayRef) -> Result<StringArray, ArrowError> { + let struct_array = input + .as_any() + .downcast_ref::<StructArray>() + .ok_or_else(|| ArrowError::CastError("Expected StructArray as input".into()))?; + + // Validate field types + let data_type = struct_array.data_type(); + match data_type { + DataType::Struct(inner_fields) => { + if inner_fields.len() != 2 + || inner_fields[0].data_type() != &DataType::Binary + || inner_fields[1].data_type() != &DataType::Binary + { + return Err(ArrowError::CastError( + "Expected struct with two binary fields".into(), + )); + } + } + _ => { + return Err(ArrowError::CastError( + "Expected StructArray with known fields".into(), + )) + } + } + + let metadata_array = struct_array + .column(0) + .as_any() + .downcast_ref::<BinaryArray>() + .ok_or_else(|| ArrowError::CastError("Expected BinaryArray for 'metadata'".into()))?; + + let value_array = struct_array + .column(1) + .as_any() + .downcast_ref::<BinaryArray>() + .ok_or_else(|| ArrowError::CastError("Expected BinaryArray for 'value'".into()))?; + + // Zero-copy builder + // The size per JSON string is assumed to be 128 bytes. If this holds true, resizing could be + // minimized to improve performance. Review Comment: @alamb I've tried to minimize copying here. Took help from AI figuring this out. Please see if this looks good to you. -- 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