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


##########
arrow-array/src/builder/primitive_ree_array_builder.rs:
##########
@@ -0,0 +1,218 @@
+// 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, ArrowPrimitiveType, 
RunEndEncodedArray};
+
+use super::PrimitiveBuilder;
+
+use arrow_buffer::ArrowNativeType;
+use arrow_schema::ArrowError;
+
+/// Array builder for [`RunEndEncodedArray`] that encodes primitive values.
+///
+/// # Example:
+///
+/// ```
+///
+/// # use arrow_array::builder::PrimitiveREEArrayBuilder;
+/// # use arrow_array::types::{UInt32Type, Int16Type};
+/// # use arrow_array::{Array, UInt32Array, Int16Array};
+///
+/// let mut builder =
+/// PrimitiveREEArrayBuilder::<Int16Type, UInt32Type>::new();
+/// builder.append_value(1234).unwrap();
+/// builder.append_value(1234).unwrap();
+/// builder.append_value(1234).unwrap();
+/// builder.append_null().unwrap();
+/// builder.append_value(5678).unwrap();
+/// builder.append_value(5678).unwrap();
+/// let array = builder.finish();
+///
+/// assert_eq!(
+///     array.run_ends(),
+///     &Int16Array::from(vec![Some(3), Some(4), Some(6)])
+/// );
+///
+/// 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: &UInt32Array = av.as_any().downcast_ref::<UInt32Array>().unwrap();
+///
+/// assert_eq!(ava, &UInt32Array::from(vec![Some(1234), None, Some(5678)]));
+/// ```
+#[derive(Debug)]
+pub struct PrimitiveREEArrayBuilder<R, V>
+where
+    R: ArrowRunEndIndexType,
+    V: ArrowPrimitiveType,
+{
+    run_ends_builder: PrimitiveBuilder<R>,
+    values_builder: PrimitiveBuilder<V>,
+    current_value: Option<V::Native>,
+    current_run_end_index: usize,
+}
+
+impl<R, V> Default for PrimitiveREEArrayBuilder<R, V>
+where
+    R: ArrowRunEndIndexType,
+    V: ArrowPrimitiveType,
+{
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl<R, V> PrimitiveREEArrayBuilder<R, V>
+where
+    R: ArrowRunEndIndexType,
+    V: ArrowPrimitiveType,
+{
+    /// Creates a new `PrimitiveREEArrayBuilder`
+    pub fn new() -> Self {
+        Self {
+            run_ends_builder: PrimitiveBuilder::new(),
+            values_builder: PrimitiveBuilder::new(),
+            current_value: None,
+            current_run_end_index: 0,
+        }
+    }
+
+    /// Creates a new `PrimitiveREEArrayBuilder` with the provided capacity
+    ///
+    /// `capacity`: the expected number of run-end encoded values.
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self {
+            run_ends_builder: PrimitiveBuilder::with_capacity(capacity),
+            values_builder: PrimitiveBuilder::with_capacity(capacity),
+            current_value: None,
+            current_run_end_index: 0,
+        }
+    }
+}
+
+impl<R, V> PrimitiveREEArrayBuilder<R, V>
+where
+    R: ArrowRunEndIndexType,
+    V: ArrowPrimitiveType,
+{
+    /// Appends optional value to the logical array encoded by the 
RunEndEncodedArray.
+    pub fn append_option(&mut self, value: Option<V::Native>) -> Result<(), 
ArrowError> {
+        if self.current_run_end_index == 0 {
+            self.current_run_end_index = 1;
+            self.current_value = value;
+            return Ok(());
+        }
+        if self.current_value != value {
+            self.append_run_end()?;
+            self.current_value = value;
+        }
+
+        self.current_run_end_index = self
+            .current_run_end_index
+            .checked_add(1)
+            .ok_or(ArrowError::RunEndIndexOverflowError)?;
+
+        Ok(())
+    }
+    /// Appends value to the logical array encoded by the run-ends array.
+    pub fn append_value(&mut self, value: V::Native) -> Result<(), ArrowError> 
{
+        self.append_option(Some(value))
+    }
+    /// Appends null to the logical array encoded by the run-ends array.
+    pub fn append_null(&mut self) -> Result<(), ArrowError> {
+        self.append_option(None)
+    }
+    /// Creates the RunEndEncodedArray and resets the builder.
+    /// Panics if RunEndEncodedArray cannot be built.
+    pub fn finish(&mut self) -> RunEndEncodedArray<R> {
+        //write the last run end to the array.
+        self.append_run_end().unwrap();
+
+        //reset the run index to zero.
+        self.current_value = None;
+        self.current_run_end_index = 0;
+
+        //build the run encoded array by adding run_ends and values array as 
its children.
+        let run_ends_array = self.run_ends_builder.finish();
+        let values_array = self.values_builder.finish();
+        RunEndEncodedArray::<R>::try_new(&run_ends_array, 
&values_array).unwrap()
+    }
+    /// Creates the RunEndEncodedArray and without resetting the builder.

Review Comment:
   Please leave one blank line between functions. There are more such style 
issues that I don't comment all of them.



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