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 2022/04/08 18:57:08 UTC

[GitHub] [arrow-datafusion] WinkerDu opened a new pull request, #2183: MINOR: use arrow kernel `take` to avoid value copy in `string_concat`

WinkerDu opened a new pull request, #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   - When `StringConcat` operator runs `evaluate`, it will eliminate `NULL` from output array of `concat` expression. Current `collect` after `Some(array.value(index))` logic will trigger value copying.
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   - Use arrow kernel [take](https://docs.rs/arrow/11.1.0/arrow/compute/kernels/take/fn.take.html) expression to avoid unnecessary value copy.
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   No.


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] WinkerDu commented on a diff in pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846914581


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   @Dandandan Thanks for you information, I' ll learn it later and try some benches on it



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] WinkerDu commented on pull request #2183: MINOR: use arrow kernel `take` to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#issuecomment-1093247465

   cc @alamb @Dandandan @yjshen, please take a review, thank you.


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] WinkerDu commented on pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#issuecomment-1094361246

   cc @alamb @xudong963  PTAL, thank you!


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] WinkerDu commented on a diff in pull request #2183: MINOR: use arrow kernel `take` to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846830459


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   @alamb thanks for the advise.
   
   I think we can optimize the whole string concat process to avoid this value copying, something like:
   
   - original process: build-in `concat` ignoring `NULL` -> generate validity array or array of indexes -> take valid value from `concat` output array according to bitmap or indexes
   - opmized process: concat two input string array well handled with `NULL`, no value copy any more.



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r847772351


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   Filed https://github.com/apache/arrow-rs/issues/1540 to add an optimized implementation to arrow-rs



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846838079


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   AIUC, the suggestion of @alamb might be roughly:
   
   
   * Create the validity array based on `left_bitmap && right_bitmap` (this is a fast operation, as it can be performed on 64 bits at once). A fast path could be implemented for the null count being equal to 0 or being equal to length of the array.
   
   * Compute the concatenation based on left + right side (without needing taking into account null values).
   
   We should add some benches to see the improvements this brings.
   
   In the arrow-rs kernels you could find some examples of this strategy implemented, but usually this brings 2-10x improvements, depending on data type.
   
   In arrow2 this strategy is applied quite a bit more consistently, see for example the `substring` kernel: https://github.com/jorgecarleitao/arrow2/blob/main/src/compute/substring.rs#L66



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2183: MINOR: use arrow kernel `take` to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846768840


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
    `take` still potentially does a copy into a new array, though it is more optimized than the simple loop that was here 



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846838079


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   AIUC, the suggestion of @alamb might be roughly:
   
   
   * Create the validity array based on `left_bitmap && right_bitmap` (this is an fast operation, as it can be performed on 64 bits at once). A fast path could be implemented for the null count being equal to 0 or being equal to length of the array.
   
   * Compute the concatenation based on left + right side (without needing taking into account null values).
   
   We should add some benches to see the improvements this brings.
   
   In the arrow-rs kernels you could find some examples of this strategy implemented, but usually this brings 2-10x improvements, depending on data type.
   
   In arrow2 this strategy is applied quite a bit more consistently, see for example the `substring` kernel: https://github.com/jorgecarleitao/arrow2/blob/main/src/compute/substring.rs#L66



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] WinkerDu commented on a diff in pull request #2183: MINOR: use arrow kernel `take` to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on code in PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183#discussion_r846830459


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -430,17 +431,17 @@ fn string_concat(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
         scalar_value => scalar_value.into_array(left.clone().len()),
     };
     let ignore_null_array = ignore_null.as_any().downcast_ref::<StringArray>().unwrap();
-    let result = (0..ignore_null_array.len())
+    let index_array = (0..ignore_null_array.len())
         .into_iter()
         .map(|index| {
             if left.is_null(index) || right.is_null(index) {
                 None
             } else {
-                Some(ignore_null_array.value(index))
+                Some(index as u32)
             }
         })
-        .collect::<StringArray>();
-
+        .collect::<UInt32Array>();
+    let result = take(ignore_null_array, &index_array, None)?;

Review Comment:
   I think we can optimize the whole string concat process to avoid this value copying, something like:
   
   - original process: build-in `concat` ignoring `NULL` -> generate validity array or array of indexes -> take valid value from `concat` output array according to bitmap or indexes
   - opmized process: concat two input string array well handled with `NULL`, no value copy any more.



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #2183: MINOR: handle `NULL` in advance to avoid value copy in `string_concat`

Posted by GitBox <gi...@apache.org>.
alamb merged PR #2183:
URL: https://github.com/apache/arrow-datafusion/pull/2183


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org