rluvaton commented on code in PR #8653:
URL: https://github.com/apache/arrow-rs/pull/8653#discussion_r2444779762


##########
arrow-buffer/src/buffer/mutable.rs:
##########
@@ -222,6 +222,82 @@ impl MutableBuffer {
         }
     }
 
+    /// Creates a new [`MutableBuffer`] by repeating the contents of 
`slice_to_repeat`
+    /// `repeat_count` times.
+    pub fn new_repeated<T: ArrowNativeType>(repeat_count: usize, 
slice_to_repeat: &[T]) -> Self {
+        if slice_to_repeat.is_empty() || repeat_count == 0 {
+            return Self::new(0);
+        }
+
+        // If we keep extending from ourself we will reach it pretty fast
+        let value_len = slice_to_repeat.len();
+        let final_len = repeat_count * value_len;
+        let mut mutable = Self::with_capacity(final_len);
+
+        mutable.push_slice_repeated(repeat_count, slice_to_repeat);
+
+        mutable
+    }
+
+    /// Adding to this mutable buffer `slice_to_repeat` repeated 
`repeat_count` times.
+    pub fn push_slice_repeated<T: ArrowNativeType>(
+        &mut self,
+        repeat_count: usize,
+        slice_to_repeat: &[T],
+    ) {
+        if repeat_count == 0 || slice_to_repeat.is_empty() {
+            return;
+        }
+
+        // Ensure capacity
+        let additional = repeat_count * mem::size_of_val(slice_to_repeat);
+        self.reserve(additional);
+
+        // No need to special case small repeat counts
+        if repeat_count <= 3 {
+            for _ in 0..repeat_count {
+                self.extend_from_slice(slice_to_repeat);
+            }
+
+            return;
+        }
+
+        // If we keep extending from ourself we will reach it pretty fast

Review Comment:
   Extracted to:
   - #8658 



##########
arrow-select/src/zip.rs:
##########
@@ -166,6 +196,437 @@ pub fn zip(
     Ok(make_array(data))
 }
 
+/// Zipper for 2 scalars
+///
+/// Useful for using in `IF <expr> THEN <scalar> ELSE <scalar> END` expressions
+///
+#[derive(Debug, Clone)]
+pub struct ScalarZipper {
+    zip_impl: Arc<dyn ZipImpl>,
+}
+
+impl ScalarZipper {
+    /// Try to create a new ScalarZipper from two scalar Datum
+    ///
+    /// # Errors
+    /// returns error if:
+    /// - the two Datum have different data types
+    /// - either Datum is not a scalar (or has more than 1 element)
+    ///
+    pub fn try_new(truthy: &dyn Datum, falsy: &dyn Datum) -> Result<Self, 
ArrowError> {
+        let (truthy, truthy_is_scalar) = truthy.get();
+        let (falsy, falsy_is_scalar) = falsy.get();
+
+        if truthy.data_type() != falsy.data_type() {
+            return Err(ArrowError::InvalidArgumentError(
+                "arguments need to have the same data type".into(),
+            ));
+        }
+
+        if !truthy_is_scalar {
+            return Err(ArrowError::InvalidArgumentError(
+                "only scalar arrays are supported".into(),
+            ));
+        }
+
+        if !falsy_is_scalar {
+            return Err(ArrowError::InvalidArgumentError(
+                "only scalar arrays are supported".into(),
+            ));
+        }
+
+        if truthy.len() != 1 {
+            return Err(ArrowError::InvalidArgumentError(
+                "scalar arrays must have 1 element".into(),
+            ));
+        }
+        if falsy.len() != 1 {
+            return Err(ArrowError::InvalidArgumentError(
+                "scalar arrays must have 1 element".into(),
+            ));
+        }
+
+        macro_rules! primitive_size_helper {
+            ($t:ty) => {
+                Arc::new(PrimitiveScalarImpl::<$t>::new(truthy, falsy)) as 
Arc<dyn ZipImpl>
+            };
+        }
+
+        let zip_impl = downcast_primitive! {
+            truthy.data_type() => (primitive_size_helper),
+            DataType::Utf8 => {
+                Arc::new(BytesScalarImpl::<Utf8Type>::new(truthy, falsy)) as 
Arc<dyn ZipImpl>
+            },
+            DataType::LargeUtf8 => {
+                Arc::new(BytesScalarImpl::<LargeUtf8Type>::new(truthy, falsy)) 
as Arc<dyn ZipImpl>
+            },
+            DataType::Binary => {
+                Arc::new(BytesScalarImpl::<BinaryType>::new(truthy, falsy)) as 
Arc<dyn ZipImpl>
+            },
+            DataType::LargeBinary => {
+                Arc::new(BytesScalarImpl::<LargeBinaryType>::new(truthy, 
falsy)) as Arc<dyn ZipImpl>
+            },
+            _ => {
+                Arc::new(FallbackImpl::new(truthy, falsy)) as Arc<dyn ZipImpl>
+            },
+        };
+
+        Ok(Self { zip_impl })
+    }
+}
+
+/// Impl for creating output array based on input boolean array
+trait ZipImpl: Debug {
+    /// Creating output array based on input boolean array
+    fn create_output(&self, input: &BooleanArray) -> Result<ArrayRef, 
ArrowError>;
+}
+
+#[derive(Debug, PartialEq)]
+struct FallbackImpl {
+    truthy: ArrayData,
+    falsy: ArrayData,
+}
+
+impl FallbackImpl {
+    fn new(left: &dyn Array, right: &dyn Array) -> Self {
+        Self {
+            truthy: left.to_data(),
+            falsy: right.to_data(),
+        }
+    }
+}
+
+impl ZipImpl for FallbackImpl {
+    fn create_output(&self, predicate: &BooleanArray) -> Result<ArrayRef, 
ArrowError> {
+        zip_impl(predicate, &self.truthy, false, &self.falsy, false)
+    }
+}
+
+struct PrimitiveScalarImpl<T: ArrowPrimitiveType> {

Review Comment:
   Done



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