askoa commented on code in PR #3553: URL: https://github.com/apache/arrow-rs/pull/3553#discussion_r1083955929
########## arrow-array/src/builder/generic_byte_ree_array_builder.rs: ########## @@ -0,0 +1,423 @@ +// 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 crate::{ + types::{ + ArrowRunEndIndexType, BinaryType, ByteArrayType, LargeBinaryType, LargeUtf8Type, + Utf8Type, + }, + ArrowPrimitiveType, RunEndEncodedArray, +}; + +use super::{GenericByteBuilder, PrimitiveBuilder}; + +use arrow_buffer::ArrowNativeType; +use arrow_schema::ArrowError; + +/// Array builder for [`RunEndEncodedArray`] for String and Binary types. +/// +/// # Example: +/// +/// ``` +/// +/// # use arrow_array::builder::GenericByteREEArrayBuilder; +/// # use arrow_array::{GenericByteArray, BinaryArray}; +/// # use arrow_array::types::{BinaryType, Int16Type}; +/// # use arrow_array::{Array, Int16Array}; +/// +/// let mut builder = +/// GenericByteREEArrayBuilder::<Int16Type, BinaryType>::new(); +/// builder.append_value(b"abc").unwrap(); +/// builder.append_value(b"abc").unwrap(); +/// builder.append_null().unwrap(); +/// builder.append_value(b"def").unwrap(); +/// let array = builder.finish(); +/// +/// assert_eq!( +/// array.run_ends(), +/// &Int16Array::from(vec![Some(2), Some(3), Some(4)]) +/// ); +/// +/// let av = array.values(); +/// +/// assert!(!av.is_null(0)); +/// assert!(av.is_null(1)); +/// assert!(!av.is_null(2)); +/// +/// // Values are polymorphic and so require a downcast. +/// let ava: &BinaryArray = av.as_any().downcast_ref::<BinaryArray>().unwrap(); +/// +/// assert_eq!(ava.value(0), b"abc"); +/// assert_eq!(ava.value(2), b"def"); +/// ``` +#[derive(Debug)] +pub struct GenericByteREEArrayBuilder<R, V> +where + R: ArrowPrimitiveType, + V: ByteArrayType, +{ + run_ends_builder: PrimitiveBuilder<R>, + values_builder: GenericByteBuilder<V>, + current_value: Option<Vec<u8>>, + current_run_end_index: usize, +} + +impl<R, V> Default for GenericByteREEArrayBuilder<R, V> +where + R: ArrowPrimitiveType, + V: ByteArrayType, +{ + fn default() -> Self { + Self::new() + } +} + +impl<R, V> GenericByteREEArrayBuilder<R, V> +where + R: ArrowPrimitiveType, + V: ByteArrayType, +{ + /// Creates a new `GenericByteREEArrayBuilder` + pub fn new() -> Self { + Self { + run_ends_builder: PrimitiveBuilder::new(), + values_builder: GenericByteBuilder::<V>::new(), + current_value: None, + current_run_end_index: 0, + } + } + + /// Creates a new `GenericByteREEArrayBuilder` with the provided capacity + /// + /// `capacity`: the expected number of run-end encoded values. + /// `data_capacity`: the expected number of bytes of run end encoded values + pub fn with_capacity(capacity: usize, data_capacity: usize) -> Self { + Self { + run_ends_builder: PrimitiveBuilder::with_capacity(capacity), + values_builder: GenericByteBuilder::<V>::with_capacity( + capacity, + data_capacity, + ), + current_value: None, + current_run_end_index: 0, + } + } +} + +impl<R, V> GenericByteREEArrayBuilder<R, V> +where + R: ArrowRunEndIndexType, + V: ByteArrayType, +{ + /// Appends optional value to the logical array encoded by the RunEndEncodedArray. + pub fn append_option( + &mut self, + input_value: Option<impl AsRef<V::Native>>, + ) -> Result<(), ArrowError> { + match input_value { + Some(value) => self.append_value(value)?, + None => self.append_null()?, + } + Ok(()) + } + + /// Appends value to the logical array encoded by the RunEndEncodedArray. + pub fn append_value( + &mut self, + input_value: impl AsRef<V::Native>, + ) -> Result<(), ArrowError> { + let value: &[u8] = input_value.as_ref().as_ref(); + match self.current_value.as_deref() { + None if self.current_run_end_index > 0 => { + self.append_run_end()?; + self.current_value = Some(value.to_owned()); Review Comment: There is nothing wrong to indulge in some premature optimization. I have updated this based on your suggestion. -- 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]
