viirya commented on code in PR #7911: URL: https://github.com/apache/arrow-rs/pull/7911#discussion_r2223455775
########## parquet-variant-compute/src/variant_array_builder.rs: ########## @@ -168,7 +174,171 @@ impl VariantArrayBuilder { self.value_buffer.extend_from_slice(value); } - // TODO: Return a Variant builder that will write to the underlying buffers (TODO) + /// Return a `VariantArrayVariantBuilder` that writes directly to the + /// buffers of this builder. + /// + /// You must call [`VariantArrayVariantBuilder::finish`] to complete the builder + /// + /// # Example + /// ``` + /// # use parquet_variant::{Variant, VariantBuilder, VariantBuilderExt}; + /// # use parquet_variant_compute::{VariantArray, VariantArrayBuilder}; + /// let mut array_builder = VariantArrayBuilder::new(10); + /// + /// // First row has a string + /// let mut variant_builder = array_builder.variant_builder(); + /// variant_builder.append_value("Hello, World!"); + /// // must call finish to write the variant to the buffers + /// variant_builder.finish(); + /// + /// // Second row is an object + /// let mut variant_builder = array_builder.variant_builder(); + /// variant_builder + /// .new_object() + /// .with_field("my_field", 42i64) + /// .finish() + /// .unwrap(); + /// variant_builder.finish(); + /// + /// // finalize the array + /// let variant_array: VariantArray = array_builder.build(); + /// + /// // verify what we wrote is still there + /// assert_eq!(variant_array.value(0), Variant::from("Hello, World!")); + /// assert!(variant_array.value(1).as_object().is_some()); + /// ``` + pub fn variant_builder(&mut self) -> VariantArrayVariantBuilder { + // append directly into the metadata and value buffers + let metadata_buffer = std::mem::take(&mut self.metadata_buffer); + let value_buffer = std::mem::take(&mut self.value_buffer); + VariantArrayVariantBuilder::new(self, metadata_buffer, value_buffer) + } +} + +/// A `VariantBuilder` that writes directly to the buffers of a `VariantArrayBuilder`. +/// +/// Note this struct implements [`VariantBuilderExt`], so it can be used +/// as a drop-in replacement for [`VariantBuilder`] in most cases. Review Comment: ```suggestion // This struct implements [`VariantBuilderExt`], so in most cases it can be used as a // [`VariantBuilder`] to perform variant-related operations for [`VariantArrayBuilder`]. ``` -- 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