alamb commented on code in PR #8133: URL: https://github.com/apache/arrow-rs/pull/8133#discussion_r2277470277
########## parquet/src/variant.rs: ########## @@ -0,0 +1,101 @@ +// 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. + +//! ⚠️ Experimental Support for reading and writing [`Variant`]s to / from Parquet files ⚠️ +//! +//! This is a 🚧 Work In Progress +//! +//! Note: Requires the `variant` feature of the `parquet` crate to be enabled. +//! +//! # Features +//! * [`Variant`] represents variant value, which can be an object, list, or primitive. +//! * [`VariantBuilder`] for building `Variant` values. +//! * [`VariantArray`] for representing a column of Variant values. +//! * [`compute`] module with functions for manipulating Variants, such as +//! [`variant_get`] to extracting a value by path and functions to convert +//! between `Variant` and JSON. +//! +//! [Variant Logical Type]: Variant +//! [`VariantArray`]: compute::VariantArray +//! [`variant_get`]: compute::variant_get +//! +//! # Example: Writing a Parquet file with Variant column +//! ```rust +//! # use parquet::variant::compute::{VariantArray, VariantArrayBuilder}; +//! # use parquet::variant::VariantBuilderExt; +//! # use std::sync::Arc; +//! # use arrow_array::{ArrayRef, RecordBatch}; +//! # use parquet::arrow::ArrowWriter; +//! # fn main() -> Result<(), parquet::errors::ParquetError> { +//! // Use the VariantArrayBuilder to build a VariantArray +//! let mut builder = VariantArrayBuilder::new(3); +//! // row 1: {"name": "Alice"} +//! let mut variant_builder = builder.variant_builder(); +//! variant_builder.new_object().with_field("name", "Alice").finish()?; +//! variant_builder.finish(); +//! let array = builder.build(); +//! +//! // TODO support writing VariantArray directly Review Comment: I will file a follow on ticket for this problem ########## parquet/src/variant.rs: ########## @@ -0,0 +1,101 @@ +// 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. + +//! ⚠️ Experimental Support for reading and writing [`Variant`]s to / from Parquet files ⚠️ +//! +//! This is a 🚧 Work In Progress +//! +//! Note: Requires the `variant` feature of the `parquet` crate to be enabled. +//! +//! # Features +//! * [`Variant`] represents variant value, which can be an object, list, or primitive. +//! * [`VariantBuilder`] for building `Variant` values. +//! * [`VariantArray`] for representing a column of Variant values. +//! * [`compute`] module with functions for manipulating Variants, such as +//! [`variant_get`] to extracting a value by path and functions to convert +//! between `Variant` and JSON. +//! +//! [Variant Logical Type]: Variant +//! [`VariantArray`]: compute::VariantArray +//! [`variant_get`]: compute::variant_get +//! +//! # Example: Writing a Parquet file with Variant column Review Comment: Being able to write this example is the largest rationale in my mind for the feature flag in the parquet crate ########## parquet/src/variant.rs: ########## @@ -0,0 +1,111 @@ +// 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. + +//! ⚠️ Experimental Support for reading and writing [`Variant`]s to / from Parquet files ⚠️ +//! +//! This is a 🚧 Work In Progress +//! +//! Note: Requires the `variant` feature of the `parquet` crate to be enabled. +//! +//! # Features +//! * [`Variant`] represents variant value, which can be an object, list, or primitive. +//! * [`VariantBuilder`] for building `Variant` values. +//! * [`VariantArray`] for representing a column of Variant values. +//! * [`compute`] module with functions for manipulating Variants, such as +//! [`variant_get`] to extracting a value by path and functions to convert +//! between `Variant` and JSON. +//! +//! [Variant Logical Type]: Variant +//! [`VariantArray`]: compute::VariantArray +//! [`variant_get`]: compute::variant_get +//! +//! # Example: Writing a Parquet file with Variant column +//! ```rust +//! # use parquet::variant::compute::{VariantArray, VariantArrayBuilder}; +//! # use parquet::variant::VariantBuilderExt; +//! # use std::sync::Arc; +//! # use arrow_array::{ArrayRef, RecordBatch}; +//! # use parquet::arrow::ArrowWriter; +//! # fn main() -> Result<(), parquet::errors::ParquetError> { +//! // Use the VariantArrayBuilder to build a VariantArray +//! let mut builder = VariantArrayBuilder::new(3); +//! // row 1: {"name": "Alice"} +//! let mut variant_builder = builder.variant_builder(); +//! variant_builder.new_object().with_field("name", "Alice").finish()?; +//! variant_builder.finish(); +//! let array = builder.build(); +//! +//! // TODO support writing VariantArray directly +//! // at the moment it panics when trying to downcast to a struct array +//! // let array: ArrayRef = Arc::new(array); +//! let array: ArrayRef = Arc::new(array.into_inner()); +//! +//! // create a RecordBatch with the VariantArray +//! let batch = RecordBatch::try_from_iter(vec![("data", array)])?; +//! +//! // write the RecordBatch to a Parquet file +//! let file = std::fs::File::create("variant.parquet")?; +//! let mut writer = ArrowWriter::try_new(file, batch.schema(), None)?; +//! writer.write(&batch)?; +//! writer.close()?; +//! +//! # Ok(()) +//! # } +//! ``` +//! +//! # Example: Writing JSON with a Parquet file with Variant column +//! ```rust +//! # use std::sync::Arc; +//! # use arrow_array::{ArrayRef, RecordBatch, StringArray}; +//! # use parquet::variant::compute::batch_json_string_to_variant; +//! # use parquet::variant::compute::VariantArray; +//! # use parquet::arrow::ArrowWriter; +//! # fn main() -> Result<(), parquet::errors::ParquetError> { +//! // Create an array of JSON strings, simulating a column of JSON data +//! // TODO use StringViewArray when available +//! let input_array = StringArray::from(vec![ +//! Some(r#"{"name": "Alice", "age": 30}"#), +//! Some(r#"{"name": "Bob", "age": 25, "address": {"city": "New York"}}"#), +//! None, +//! Some("{}"), +//! ]); +//! let input_array: ArrayRef = Arc::new(input_array); +//! +//! // Convert the JSON strings to a VariantArray +//! let array: VariantArray = batch_json_string_to_variant(&input_array)?; +//! +//! // TODO support writing VariantArray directly +//! // at the moment it panics when trying to downcast to a struct array +//! // let array: ArrayRef = Arc::new(array); +//! let array: ArrayRef = Arc::new(array.into_inner()); +//! +//! // create a RecordBatch with the VariantArray +//! let batch = RecordBatch::try_from_iter(vec![("data", array)])?; +//! +//! // write the RecordBatch to a Parquet file +//! let file = std::fs::File::create("variant-json.parquet")?; Review Comment: This is kind of neat to see the data written. I looked at the resulting file using `datafusion-cli` and it looks like like this (note it does not have the parquet logical annotation on it) ```sql > select * from 'parquet/variant-json.parquet'; +-----------------------------------------------------------------------------------------------------------------------------------+ | data | +-----------------------------------------------------------------------------------------------------------------------------------+ | {metadata: 11020003076167656e616d65, value: 020200010002080c1e15416c696365} | | {metadata: 010400040b0e1263697479616464726573736167656e616d65, value: 0203010203000e10140201000009214e657720596f726b0c190d426f62} | | {metadata: 010000, value: 020000} | | {metadata: 010000, value: 020000} | +-----------------------------------------------------------------------------------------------------------------------------------+ 4 row(s) fetched. Elapsed 0.003 seconds. ``` -- 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]
