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 04:58:51 UTC

[GitHub] [arrow] jorgecarleitao opened a new pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

jorgecarleitao opened a new pull request #7878:
URL: https://github.com/apache/arrow/pull/7878


   This is on top of #7876 and only the last commit is specific of this PR.


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



[GitHub] [arrow] andygrove closed pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
andygrove closed pull request #7878:
URL: https://github.com/apache/arrow/pull/7878


   


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



[GitHub] [arrow] jorgecarleitao commented on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-674357343


   There are now two distinct commits. The first one is the length for DataFusion, the second one simplifies how we enumerate scalar functions that can be registered on the context.
   
   The main rational for the second commit is that the physical plan should not have to worry about how an `ExecutionContext` registers udfs. Instead, it should just enumerate which udfs are available, and let the `ExecutionContext` register them itself. We can revert this if we would prefer to place it on a second PR / not make the change.


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



[GitHub] [arrow] jorgecarleitao commented on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-673892345


   @andygrove and @nevi-me , the `length` operator was merged to master on arrow; this one adds it to DataFusion.


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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
andygrove commented on a change in pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#discussion_r470889218



##########
File path: rust/datafusion/src/execution/context.rs
##########
@@ -450,6 +451,10 @@ impl ExecutionContext {
                 op.clone(),
                 self.create_physical_expr(right, input_schema)?,
             ))),
+            Expr::Length(expr) => Ok(Arc::new(Length::try_new(

Review comment:
       I wonder if we really want to add a new `Expr` specifically for this function when we already have way to register UDFs. It seems like it might be better to treat it just like the other UDFs?




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



[GitHub] [arrow] jorgecarleitao commented on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-674410326


   @andygrove , let me know if this is what you were thinking about the UDFs.


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



[GitHub] [arrow] github-actions[bot] commented on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-667628261


   https://issues.apache.org/jira/browse/ARROW-9617


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



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

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#discussion_r464083171



##########
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:
       Great suggestions, @nevi-me , thanks a lot. Implemented it leading with +35% or so improvement.




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



[GitHub] [arrow] jorgecarleitao edited a comment on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
jorgecarleitao edited a comment on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-674357343


   There are now two distinct commits. The first one is the length for DataFusion as a UDF, the second one simplifies how we enumerate scalar functions that can be registered on the context.
   
   The main rational for the second commit is that the physical plan should not have to worry about how an `ExecutionContext` registers udfs. Instead, it should just enumerate which udfs are available, and let the `ExecutionContext` register them itself. We can revert this if we would prefer to place it on a second PR / not make the change.


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



[GitHub] [arrow] jorgecarleitao edited a comment on pull request #7878: ARROW-9617: [Rust] [DataFusion] Add length of string array

Posted by GitBox <gi...@apache.org>.
jorgecarleitao edited a comment on pull request #7878:
URL: https://github.com/apache/arrow/pull/7878#issuecomment-674357343


   There are now two distinct commits. The first one is the length for DataFusion as a UDF, the second one simplifies how we enumerate scalar functions that can be registered on the context.
   
   The main rational for the second commit is that the physical plan should not have to worry about how an `ExecutionContext` registers udfs. Instead, it should just enumerate which udfs are available, and let the `ExecutionContext` register them itself. While trying to register the length udf, I ended up needing place some basic physical plan stuff on the `ExecutionContext` module, which drove me to revisit also that. We can revert this if we would prefer to place it on a second PR / not make the change.


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