You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/09/20 15:34:57 UTC

[GitHub] [arrow] nevi-me commented on a change in pull request #8199: ARROW-10019: [Rust] Add substring kernel

nevi-me commented on a change in pull request #8199:
URL: https://github.com/apache/arrow/pull/8199#discussion_r491702713



##########
File path: rust/arrow/src/compute/kernels/substring.rs
##########
@@ -0,0 +1,226 @@
+// 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.
+
+//! Defines kernel to extract a substring of a StringArray
+
+use crate::{array::*, buffer::Buffer, datatypes::ToByteSlice};
+use crate::{
+    datatypes::DataType,
+    error::{ArrowError, Result},
+};
+use std::sync::Arc;
+
+/// Returns an ArrayRef with a substring starting from `start` and with optional length `length` of each of the elements in `array`.
+/// `start` can be negative, in which case the start counts from the end of the string.
+pub fn substring(array: &Array, start: i32, length: &Option<u32>) -> Result<ArrayRef> {
+    match array.data_type() {
+        DataType::Utf8 => {
+            // compute current offsets
+            let offsets = array.data_ref().clone().buffers()[0].clone();
+            let offsets: &[u32] = unsafe { offsets.typed_data::<u32>() };
+
+            // compute null bitmap (copy)
+            let null_bit_buffer = array.data_ref().null_buffer().map(|e| e.clone());
+
+            // compute values
+            let values = &array.data_ref().buffers()[1];
+            let data = values.data();
+
+            let mut new_values = Vec::new(); // we have no way to estimate how much this will be.
+            let mut new_offsets = Vec::with_capacity(array.len() + 1);
+
+            let mut length_so_far = 0i32;
+            new_offsets.push(length_so_far);
+            (0..array.len()).for_each(|i| {
+                // the length of this entry
+                let lenght_i = offsets[i + 1] - offsets[i];
+                // compute where we should start slicing this entry
+                let start = offsets[i] as i32
+                    + if start >= 0 {
+                        start
+                    } else {
+                        lenght_i as i32 + start
+                    };
+
+                let start =
+                    start.max(offsets[i] as i32).min(offsets[i + 1] as i32) as usize;
+                // compute the lenght of the slice
+                let length = length
+                    .unwrap_or(lenght_i)
+                    // .max(0) is not needed as it is guaranteed
+                    .min(offsets[i + 1] - start as u32) // so we do not go beyond this entry
+                    as i32;
+
+                length_so_far += length;
+
+                new_offsets.push(length_so_far);
+                new_values.extend_from_slice(&data[start..start + length as usize]);
+            });
+
+            let data = ArrayData::new(
+                DataType::Utf8,
+                array.len(),
+                None,
+                null_bit_buffer,
+                0,
+                vec![
+                    Buffer::from(new_offsets.to_byte_slice()),
+                    Buffer::from(&new_values[..]),
+                ],
+                vec![],
+            );
+            Ok(Arc::new(StringArray::from(Arc::new(data))))
+        }
+        _ => Err(ArrowError::ComputeError(format!(

Review comment:
       We should also support `DataType::LargeUtf8`. We're likely to get a scenario where Arrow users who deal with large data would want to use the `LargeList` and `LargeString` arrays by default (something I would also do), so it's ideal to support both string arrays from the onset going forward.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org