tustvold commented on code in PR #3553:
URL: https://github.com/apache/arrow-rs/pull/3553#discussion_r1084190817


##########
arrow-array/src/builder/generic_byte_run_builder.rs:
##########
@@ -0,0 +1,499 @@
+// 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::bytes::ByteArrayNativeType;
+use std::{any::Any, sync::Arc};
+
+use crate::{
+    types::{
+        BinaryType, ByteArrayType, LargeBinaryType, LargeUtf8Type, 
RunEndIndexType,
+        Utf8Type,
+    },
+    ArrayRef, ArrowPrimitiveType, RunArray,
+};
+
+use super::{ArrayBuilder, GenericByteBuilder, PrimitiveBuilder};
+
+use arrow_buffer::ArrowNativeType;
+
+/// Array builder for [`RunArray`] for String and Binary types.
+///
+/// # Example:
+///
+/// ```
+///
+/// # use arrow_array::builder::GenericByteRunBuilder;
+/// # use arrow_array::{GenericByteArray, BinaryArray};
+/// # use arrow_array::types::{BinaryType, Int16Type};
+/// # use arrow_array::{Array, Int16Array};
+///
+/// let mut builder =
+/// GenericByteRunBuilder::<Int16Type, BinaryType>::new();
+/// builder.append_value(b"abc");
+/// builder.append_value(b"abc");
+/// builder.append_null();
+/// builder.append_value(b"def");
+/// 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 GenericByteRunBuilder<R, V>
+where
+    R: ArrowPrimitiveType,
+    V: ByteArrayType,
+{
+    run_ends_builder: PrimitiveBuilder<R>,
+    values_builder: GenericByteBuilder<V>,
+    current_value: Vec<u8>,
+    has_current_value: bool,
+    current_run_end_index: usize,
+    prev_run_end_index: usize,
+}
+
+impl<R, V> Default for GenericByteRunBuilder<R, V>
+where
+    R: ArrowPrimitiveType,
+    V: ByteArrayType,
+{
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl<R, V> GenericByteRunBuilder<R, V>
+where
+    R: ArrowPrimitiveType,
+    V: ByteArrayType,
+{
+    /// Creates a new `GenericByteRunBuilder`
+    pub fn new() -> Self {
+        Self {
+            run_ends_builder: PrimitiveBuilder::new(),
+            values_builder: GenericByteBuilder::<V>::new(),
+            current_value: Vec::new(),
+            has_current_value: false,
+            current_run_end_index: 0,
+            prev_run_end_index: 0,
+        }
+    }
+
+    /// Creates a new `GenericByteRunBuilder` 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: Vec::new(),
+            has_current_value: false,
+            current_run_end_index: 0,
+            prev_run_end_index: 0,
+        }
+    }
+}
+
+impl<R, V> ArrayBuilder for GenericByteRunBuilder<R, V>
+where
+    R: RunEndIndexType,
+    V: ByteArrayType,
+{
+    /// Returns the builder as a non-mutable `Any` reference.
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    /// Returns the builder as a mutable `Any` reference.
+    fn as_any_mut(&mut self) -> &mut dyn Any {
+        self
+    }
+
+    /// Returns the boxed builder as a box of `Any`.
+    fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
+        self
+    }
+
+    /// Returns the number of array slots in the builder
+    fn len(&self) -> usize {
+        let mut len = self.run_ends_builder.len();
+        // If there is an ongoing run yet to be added, include it in the len
+        if self.prev_run_end_index != self.current_run_end_index {
+            len += 1;
+        }
+        len

Review Comment:
   ```suggestion
           // If there is an ongoing run yet to be added, include it in the len
           self.current_run_end_index
   ```
   I think this should be the length of the output array, not the number of rows



-- 
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]

Reply via email to