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 16:05:08 UTC

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

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



##########
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 that this all depends on what assumptions we make about a typical query. Specifically:
   
   * what is the average start
   * what is the average length
   * what is the average string size
   
   from these 3, we could compute an initial estimation. For example: is this going to be used to treat texts, on which "string size" can go to Mbs, or is this going to be used on 10-12 character arrays?
   
   I considered this discussion beyond the scope of this PR and just went with the least memory-intensive/highest CPU-intensive approach, just because 100% memory usage usually has more nefarious implications than 100% CPU.
   
   The semantics is so that it does not error: it just goes to the maximum/minimum available for each entry. The rational here is that otherwise the user would have to perform some calculation on the side to ensure no errors at runtime, which are never fun. :)




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