You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "tlm365 (via GitHub)" <gi...@apache.org> on 2024/08/13 10:55:05 UTC

[PR] Implement native support StringView for `REPEAT` [datafusion]

tlm365 opened a new pull request, #11962:
URL: https://github.com/apache/datafusion/pull/11962

   ## Which issue does this PR close?
   Closes #11914 and part of #11790.
   <!--
   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.
   -->
   
   ## 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.  
   -->
   
   ## 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.
   -->
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## 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.
   -->
   


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Implement native support StringView for `REPEAT` [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #11962:
URL: https://github.com/apache/datafusion/pull/11962#issuecomment-2288488049

   I took the liberty of merging up from main and running `cargo fmt` on 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.

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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Implement native support StringView for `REPEAT` [datafusion]

Posted by "tlm365 (via GitHub)" <gi...@apache.org>.
tlm365 commented on code in PR #11962:
URL: https://github.com/apache/datafusion/pull/11962#discussion_r1716788840


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -87,18 +95,35 @@ fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
     let result = string_array
         .iter()
         .zip(number_array.iter())
-        .map(|(string, number)| match (string, number) {
-            (Some(string), Some(number)) if number >= 0 => {
-                Some(string.repeat(number as usize))
-            }
-            (Some(_), Some(_)) => Some("".to_string()),
-            _ => None,
-        })
+        .map(|(string, number)| repeat_common(string, number))
         .collect::<GenericStringArray<T>>();
 
     Ok(Arc::new(result) as ArrayRef)
 }
 
+fn repeat_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
+    let string_view_array = as_string_view_array(&args[0])?;
+    let number_array = as_int64_array(&args[1])?;
+
+    let result = string_view_array
+        .iter()
+        .zip(number_array.iter())
+        .map(|(string, number)| repeat_common(string, number))
+        .collect::<StringArray>();
+
+    Ok(Arc::new(result) as ArrayRef)
+}
+
+fn repeat_common(string: Option<&str>, number: Option<i64>) -> Option<String> {

Review Comment:
   @alamb thanks so much for reviewing. I will take a look!



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Implement native support StringView for `REPEAT` [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11962:
URL: https://github.com/apache/datafusion/pull/11962#discussion_r1715778889


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -87,18 +95,35 @@ fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
     let result = string_array
         .iter()
         .zip(number_array.iter())
-        .map(|(string, number)| match (string, number) {
-            (Some(string), Some(number)) if number >= 0 => {
-                Some(string.repeat(number as usize))
-            }
-            (Some(_), Some(_)) => Some("".to_string()),
-            _ => None,
-        })
+        .map(|(string, number)| repeat_common(string, number))
         .collect::<GenericStringArray<T>>();
 
     Ok(Arc::new(result) as ArrayRef)
 }
 
+fn repeat_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
+    let string_view_array = as_string_view_array(&args[0])?;
+    let number_array = as_int64_array(&args[1])?;
+
+    let result = string_view_array
+        .iter()
+        .zip(number_array.iter())
+        .map(|(string, number)| repeat_common(string, number))
+        .collect::<StringArray>();
+
+    Ok(Arc::new(result) as ArrayRef)
+}
+
+fn repeat_common(string: Option<&str>, number: Option<i64>) -> Option<String> {

Review Comment:
   I think you could make this perform much better by avoiding the `String` and instead building the output directly with `StringViewBuilder` 
   
   https://docs.rs/arrow/latest/arrow/array/type.StringViewBuilder.html
   
   here is an example of how to use them: https://github.com/apache/arrow-rs/pull/6240
   
   I realize this just follows the same model as was here. However, if we are messing with the code it might be nice to make it faster while we are at 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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Implement native support StringView for `REPEAT` [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11962:
URL: https://github.com/apache/datafusion/pull/11962#discussion_r1717211609


##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -87,18 +95,35 @@ fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
     let result = string_array
         .iter()
         .zip(number_array.iter())
-        .map(|(string, number)| match (string, number) {
-            (Some(string), Some(number)) if number >= 0 => {
-                Some(string.repeat(number as usize))
-            }
-            (Some(_), Some(_)) => Some("".to_string()),
-            _ => None,
-        })
+        .map(|(string, number)| repeat_common(string, number))
         .collect::<GenericStringArray<T>>();
 
     Ok(Arc::new(result) as ArrayRef)
 }
 
+fn repeat_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
+    let string_view_array = as_string_view_array(&args[0])?;
+    let number_array = as_int64_array(&args[1])?;
+
+    let result = string_view_array
+        .iter()
+        .zip(number_array.iter())
+        .map(|(string, number)| repeat_common(string, number))
+        .collect::<StringArray>();
+
+    Ok(Arc::new(result) as ArrayRef)
+}
+
+fn repeat_common(string: Option<&str>, number: Option<i64>) -> Option<String> {

Review Comment:
   Thank you -- I filed https://github.com/apache/datafusion/issues/11990 to track the idea



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Implement native support StringView for `REPEAT` [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #11962:
URL: https://github.com/apache/datafusion/pull/11962


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org