harshmotw-db commented on code in PR #7884: URL: https://github.com/apache/arrow-rs/pull/7884#discussion_r2193605459
########## parquet-variant-compute/src/from_json.rs: ########## @@ -0,0 +1,136 @@ +// 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::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, BinaryBuilder, BooleanBufferBuilder, StringArray, StructArray, +}; +use arrow::buffer::NullBuffer; +use arrow::datatypes::{DataType, Field}; +use arrow_schema::ArrowError; +use parquet_variant::{json_to_variant, VariantBuilder}; + +fn variant_arrow_repr() -> DataType { + let metadata_field = Field::new("metadata", DataType::Binary, true); + let value_field = Field::new("value", DataType::Binary, true); + let fields = vec![metadata_field, value_field]; + DataType::Struct(fields.into()) +} + +pub fn batch_json_string_to_variant(input: &ArrayRef) -> Result<StructArray, ArrowError> { + let input_string_array = match input.as_any().downcast_ref::<StringArray>() { + Some(string_array) => Ok(string_array), + None => Err(ArrowError::CastError( + "Expected reference to StringArray as input".into(), + )), + }?; + + let mut metadata_builder = BinaryBuilder::new(); + let mut value_builder = BinaryBuilder::new(); + let mut validity = BooleanBufferBuilder::new(input.len()); + for i in 0..input.len() { + if input.is_null(i) { + metadata_builder.append_null(); + value_builder.append_null(); + validity.append(false); + } else { + let mut vb = VariantBuilder::new(); Review Comment: Well, would this resolve the issue you're talking about: ``` pub fn try_new(metadata: &'m [u8], value: &'v [u8]) -> Result<Self, ArrowError> { // let metadata = VariantMetadata::try_new(metadata)?; Self::try_new_with_metadata(VariantMetadata::try_new(metadata)?, value) } ``` **Edit:** Oh no never mind. THere are more copies downstream. I suppose that is more of a library issue that can potentially be fixed separately ########## parquet-variant-compute/src/from_json.rs: ########## @@ -0,0 +1,136 @@ +// 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::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, BinaryBuilder, BooleanBufferBuilder, StringArray, StructArray, +}; +use arrow::buffer::NullBuffer; +use arrow::datatypes::{DataType, Field}; +use arrow_schema::ArrowError; +use parquet_variant::{json_to_variant, VariantBuilder}; + +fn variant_arrow_repr() -> DataType { + let metadata_field = Field::new("metadata", DataType::Binary, true); + let value_field = Field::new("value", DataType::Binary, true); + let fields = vec![metadata_field, value_field]; + DataType::Struct(fields.into()) +} + +pub fn batch_json_string_to_variant(input: &ArrayRef) -> Result<StructArray, ArrowError> { + let input_string_array = match input.as_any().downcast_ref::<StringArray>() { + Some(string_array) => Ok(string_array), + None => Err(ArrowError::CastError( + "Expected reference to StringArray as input".into(), + )), + }?; + + let mut metadata_builder = BinaryBuilder::new(); + let mut value_builder = BinaryBuilder::new(); + let mut validity = BooleanBufferBuilder::new(input.len()); + for i in 0..input.len() { + if input.is_null(i) { + metadata_builder.append_null(); + value_builder.append_null(); + validity.append(false); + } else { + let mut vb = VariantBuilder::new(); Review Comment: Well, would this resolve the issue you're talking about: ``` pub fn try_new(metadata: &'m [u8], value: &'v [u8]) -> Result<Self, ArrowError> { // let metadata = VariantMetadata::try_new(metadata)?; Self::try_new_with_metadata(VariantMetadata::try_new(metadata)?, value) } ``` **Edit:** Oh no never mind. There are more copies downstream. I suppose that is more of a library issue that can potentially be fixed separately -- 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