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/08/02 10:17:19 UTC

[GitHub] [arrow] nevi-me commented on a change in pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

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



##########
File path: rust/arrow/src/compute/kernels/length.rs
##########
@@ -0,0 +1,197 @@
+// 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 for length of a string array
+
+use crate::array::*;
+use crate::{
+    datatypes::DataType,
+    datatypes::UInt32Type,
+    error::{ArrowError, Result},
+};
+use std::sync::Arc;
+
+fn as_u32_le(array: &[u8]) -> u32 {
+    // le: little endian
+    ((array[0] as u32) << 0)
+        + ((array[1] as u32) << 8)
+        + ((array[2] as u32) << 16)
+        + ((array[3] as u32) << 24)
+}
+
+/// Returns an array of UInt32 denoting the number of characters in each string in the array.
+///
+/// * this only accepts StringArray
+/// * lenght of null is null.
+/// * length of utf8 with more than one code point is the number of code points
+pub fn length(array: &Array) -> Result<UInt32Array> {
+    match array.data_type() {
+        DataType::Utf8 => {
+            // note: offsets are stored as u8, but they can be interpreted as u32
+            let offsets = array.data_ref().clone().buffers()[0].clone();
+            let offsets = offsets.data();
+
+            let mut builder = UInt32BufferBuilder::new(array.len());
+
+            let mut previous_offset = 0;
+            // the first u32 offset is always 0 and is not needed to compute lengths
+            for i in (4..offsets.len()).step_by(4) {
+                // interpret 4 u8 as 1 u32:
+                let offset = as_u32_le(&offsets[i..(i + 4)]);

Review comment:
       You can use `typed_data` to do the byte conversion for you (https://github.com/apache/arrow/blob/e771b94e63e1287bf61ee905f39799aa433f4326/rust/arrow/src/buffer.rs#L247). Then you could maybe iterate through a window (https://github.com/apache/arrow/blob/e771b94e63e1287bf61ee905f39799aa433f4326/rust/arrow/src/array/builder.rs#L756)




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