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/16 12:43:53 UTC

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

jhorstmann commented on a change in pull request #8199:
URL: https://github.com/apache/arrow/pull/8199#discussion_r489406369



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

Review comment:
       I think we can estimate the capacity as `length * array.len()` if the length is specified and `values.len() - (start*array.len())` otherwise.
   
   Hm, actually depends on the semantics of the `length` parameter, is it supposed to be a maximum length or should it be an error if `start+length` goes out of bounds of one entry?




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