alamb commented on code in PR #2944:
URL: https://github.com/apache/arrow-rs/pull/2944#discussion_r1007164086


##########
arrow-array/src/array/string_array.rs:
##########
@@ -103,10 +103,8 @@ impl<OffsetSize: OffsetSizeTrait> 
GenericStringArray<OffsetSize> {
         // OffsetSizeTrait. Currently, only i32 and i64 implement 
OffsetSizeTrait,
         // both of which should cleanly cast to isize on an architecture that 
supports
         // 32/64-bit offsets
-        let slice = std::slice::from_raw_parts(
-            self.value_data.as_ptr().offset(start.to_isize().unwrap()),
-            (*end - *start).to_usize().unwrap(),
-        );
+        let slice =
+            std::slice::from_raw_parts(self.value_data.as_ptr().add(start), 
end - start);

Review Comment:
   this is a drive by cleanup right?



##########
arrow-select/src/interleave.rs:
##########
@@ -85,51 +85,105 @@ pub fn interleave(
 
     downcast_primitive! {
         data_type => (primitive_helper, values, indices, data_type),
+        DataType::Utf8 => interleave_string::<i32>(values, indices, data_type),
+        DataType::LargeUtf8 => interleave_string::<i64>(values, indices, 
data_type),
         _ => interleave_fallback(values, indices)
     }
 }
 
+/// Common functionality for interleaving arrays
+struct Interleave<'a, T> {

Review Comment:
   some comments might help here specifically what `null_count` and `nulls` 
represent and what the generic `T` is used for 



##########
arrow-select/src/interleave.rs:
##########
@@ -85,51 +85,105 @@ pub fn interleave(
 
     downcast_primitive! {
         data_type => (primitive_helper, values, indices, data_type),
+        DataType::Utf8 => interleave_string::<i32>(values, indices, data_type),
+        DataType::LargeUtf8 => interleave_string::<i64>(values, indices, 
data_type),
         _ => interleave_fallback(values, indices)
     }
 }
 
+/// Common functionality for interleaving arrays
+struct Interleave<'a, T> {
+    arrays: Vec<&'a T>,
+    null_count: usize,
+    nulls: Option<Buffer>,
+}
+
+impl<'a, T: Array + 'static> Interleave<'a, T> {
+    fn new(values: &[&'a dyn Array], indices: &'a [(usize, usize)]) -> Self {
+        let mut has_nulls = false;
+        let arrays: Vec<&T> = values
+            .iter()
+            .map(|x| {
+                has_nulls = has_nulls || x.null_count() != 0;
+                x.as_any().downcast_ref().unwrap()
+            })
+            .collect();
+
+        let mut null_count = 0;
+        let nulls = has_nulls.then(|| {
+            let mut builder = BooleanBufferBuilder::new(indices.len());
+            for (a, b) in indices {
+                let v = arrays[*a].is_valid(*b);
+                null_count += !v as usize;
+                builder.append(v)
+            }
+            builder.finish()
+        });
+
+        Self {
+            arrays,
+            null_count,
+            nulls,
+        }
+    }
+}
+
 fn interleave_primitive<T: ArrowPrimitiveType>(
     values: &[&dyn Array],
     indices: &[(usize, usize)],
     data_type: &DataType,
 ) -> Result<ArrayRef, ArrowError> {
-    let mut has_nulls = false;
-    let cast: Vec<_> = values
-        .iter()
-        .map(|x| {
-            has_nulls = has_nulls || x.null_count() != 0;
-            as_primitive_array::<T>(*x)
-        })
-        .collect();
+    let interleaved = Interleave::<'_, PrimitiveArray<T>>::new(values, 
indices);

Review Comment:
   👍 



##########
arrow-select/src/interleave.rs:
##########
@@ -85,51 +85,105 @@ pub fn interleave(
 
     downcast_primitive! {
         data_type => (primitive_helper, values, indices, data_type),
+        DataType::Utf8 => interleave_string::<i32>(values, indices, data_type),
+        DataType::LargeUtf8 => interleave_string::<i64>(values, indices, 
data_type),
         _ => interleave_fallback(values, indices)
     }
 }
 
+/// Common functionality for interleaving arrays
+struct Interleave<'a, T> {
+    arrays: Vec<&'a T>,
+    null_count: usize,
+    nulls: Option<Buffer>,
+}
+
+impl<'a, T: Array + 'static> Interleave<'a, T> {
+    fn new(values: &[&'a dyn Array], indices: &'a [(usize, usize)]) -> Self {
+        let mut has_nulls = false;
+        let arrays: Vec<&T> = values
+            .iter()
+            .map(|x| {
+                has_nulls = has_nulls || x.null_count() != 0;
+                x.as_any().downcast_ref().unwrap()
+            })
+            .collect();
+
+        let mut null_count = 0;
+        let nulls = has_nulls.then(|| {
+            let mut builder = BooleanBufferBuilder::new(indices.len());
+            for (a, b) in indices {
+                let v = arrays[*a].is_valid(*b);
+                null_count += !v as usize;
+                builder.append(v)
+            }
+            builder.finish()
+        });
+
+        Self {
+            arrays,
+            null_count,
+            nulls,
+        }
+    }
+}
+
 fn interleave_primitive<T: ArrowPrimitiveType>(
     values: &[&dyn Array],
     indices: &[(usize, usize)],
     data_type: &DataType,
 ) -> Result<ArrayRef, ArrowError> {
-    let mut has_nulls = false;
-    let cast: Vec<_> = values
-        .iter()
-        .map(|x| {
-            has_nulls = has_nulls || x.null_count() != 0;
-            as_primitive_array::<T>(*x)
-        })
-        .collect();
+    let interleaved = Interleave::<'_, PrimitiveArray<T>>::new(values, 
indices);
 
     let mut values = BufferBuilder::<T::Native>::new(indices.len());
     for (a, b) in indices {
-        let v = cast[*a].value(*b);
+        let v = interleaved.arrays[*a].value(*b);
         values.append(v)
     }
 
-    let mut null_count = 0;
-    let nulls = has_nulls.then(|| {
-        let mut builder = BooleanBufferBuilder::new(indices.len());
-        for (a, b) in indices {
-            let v = cast[*a].is_valid(*b);
-            null_count += !v as usize;
-            builder.append(v)
-        }
-        builder.finish()
-    });
-
     let builder = ArrayDataBuilder::new(data_type.clone())
         .len(indices.len())
         .add_buffer(values.finish())
-        .null_bit_buffer(nulls)
-        .null_count(null_count);
+        .null_bit_buffer(interleaved.nulls)
+        .null_count(interleaved.null_count);
 
     let data = unsafe { builder.build_unchecked() };
     Ok(Arc::new(PrimitiveArray::<T>::from(data)))
 }
 
+fn interleave_string<O: OffsetSizeTrait>(
+    values: &[&dyn Array],
+    indices: &[(usize, usize)],
+    data_type: &DataType,
+) -> Result<ArrayRef, ArrowError> {
+    let interleaved = Interleave::<'_, GenericStringArray<O>>::new(values, 
indices);
+
+    let mut capacity = 0;
+    let mut offsets = BufferBuilder::<O>::new(indices.len() + 1);
+    offsets.append(O::from_usize(0).unwrap());
+    for (a, b) in indices {

Review Comment:
   this is clever -- do the offsets in one pass and the strings in another



##########
arrow-select/src/interleave.rs:
##########
@@ -85,51 +85,105 @@ pub fn interleave(
 
     downcast_primitive! {
         data_type => (primitive_helper, values, indices, data_type),
+        DataType::Utf8 => interleave_string::<i32>(values, indices, data_type),
+        DataType::LargeUtf8 => interleave_string::<i64>(values, indices, 
data_type),
         _ => interleave_fallback(values, indices)
     }
 }
 
+/// Common functionality for interleaving arrays
+struct Interleave<'a, T> {
+    arrays: Vec<&'a T>,
+    null_count: usize,
+    nulls: Option<Buffer>,
+}
+
+impl<'a, T: Array + 'static> Interleave<'a, T> {
+    fn new(values: &[&'a dyn Array], indices: &'a [(usize, usize)]) -> Self {
+        let mut has_nulls = false;
+        let arrays: Vec<&T> = values
+            .iter()
+            .map(|x| {
+                has_nulls = has_nulls || x.null_count() != 0;
+                x.as_any().downcast_ref().unwrap()
+            })
+            .collect();
+
+        let mut null_count = 0;
+        let nulls = has_nulls.then(|| {
+            let mut builder = BooleanBufferBuilder::new(indices.len());
+            for (a, b) in indices {
+                let v = arrays[*a].is_valid(*b);
+                null_count += !v as usize;
+                builder.append(v)
+            }
+            builder.finish()
+        });
+
+        Self {
+            arrays,
+            null_count,
+            nulls,
+        }
+    }
+}
+
 fn interleave_primitive<T: ArrowPrimitiveType>(
     values: &[&dyn Array],
     indices: &[(usize, usize)],
     data_type: &DataType,
 ) -> Result<ArrayRef, ArrowError> {
-    let mut has_nulls = false;
-    let cast: Vec<_> = values
-        .iter()
-        .map(|x| {
-            has_nulls = has_nulls || x.null_count() != 0;
-            as_primitive_array::<T>(*x)
-        })
-        .collect();
+    let interleaved = Interleave::<'_, PrimitiveArray<T>>::new(values, 
indices);
 
     let mut values = BufferBuilder::<T::Native>::new(indices.len());
     for (a, b) in indices {
-        let v = cast[*a].value(*b);
+        let v = interleaved.arrays[*a].value(*b);
         values.append(v)
     }
 
-    let mut null_count = 0;
-    let nulls = has_nulls.then(|| {
-        let mut builder = BooleanBufferBuilder::new(indices.len());
-        for (a, b) in indices {
-            let v = cast[*a].is_valid(*b);
-            null_count += !v as usize;
-            builder.append(v)
-        }
-        builder.finish()
-    });
-
     let builder = ArrayDataBuilder::new(data_type.clone())
         .len(indices.len())
         .add_buffer(values.finish())
-        .null_bit_buffer(nulls)
-        .null_count(null_count);
+        .null_bit_buffer(interleaved.nulls)
+        .null_count(interleaved.null_count);
 
     let data = unsafe { builder.build_unchecked() };
     Ok(Arc::new(PrimitiveArray::<T>::from(data)))
 }
 
+fn interleave_string<O: OffsetSizeTrait>(
+    values: &[&dyn Array],
+    indices: &[(usize, usize)],
+    data_type: &DataType,
+) -> Result<ArrayRef, ArrowError> {
+    let interleaved = Interleave::<'_, GenericStringArray<O>>::new(values, 
indices);
+
+    let mut capacity = 0;
+    let mut offsets = BufferBuilder::<O>::new(indices.len() + 1);
+    offsets.append(O::from_usize(0).unwrap());
+    for (a, b) in indices {
+        let o = interleaved.arrays[*a].value_offsets();
+        let len = o[*b + 1].as_usize() - o[*b].as_usize();

Review Comment:
   ```suggestion
           // element length
           let len = o[*b + 1].as_usize() - o[*b].as_usize();
   ```



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