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



##########
File path: rust/arrow/src/compute/kernels/length.rs
##########
@@ -0,0 +1,185 @@
+// 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;
+
+/// 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 is in number of bytes
+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();
+            // this is a 30% improvement over iterating over u8s and building 
u32

Review comment:
       I kept this here as a justification for using `unsafe`. By making it 
explicit in a comment, an auditor of this code will have an easier time 
understand why the unsafe is being considered in this particular case, instead 
of having to spend the time trying to refactor out the `unsafe` only to find a 
performance drop.




----------------------------------------------------------------
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:
[email protected]


Reply via email to