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/15 11:42:50 UTC
[PR] Improve performance of REPEAT functions [datafusion]
tlm365 opened a new pull request, #12015:
URL: https://github.com/apache/datafusion/pull/12015
## 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 #11990.
## Rationale for this change
Refactor code and improve performance.
<!--
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?
`repeat` function and benchmark.
<!--
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?
Yes
<!--
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?
No
<!--
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] Improve performance of REPEAT functions [datafusion]
Posted by "tlm365 (via GitHub)" <gi...@apache.org>.
tlm365 commented on PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#issuecomment-2291695743
> Thanks @tlm365 this PR looks promising. Would you mind adding bench results to the PR description?
@comphead Thanks for reviewing, I have updated 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] Improve performance of REPEAT functions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#issuecomment-2293245809
Thank you for the reviews @tlm365 @Omega359 and @comphead
--
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] Improve performance of REPEAT functions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #12015:
URL: https://github.com/apache/datafusion/pull/12015
--
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] Improve performance of REPEAT functions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719655976
##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -79,51 +81,53 @@ impl ScalarUDFImpl for RepeatFunc {
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
- match args[0].data_type() {
- DataType::Utf8View => make_scalar_function(repeat_utf8view, vec![])(args),
- DataType::Utf8 => make_scalar_function(repeat::<i32>, vec![])(args),
- DataType::LargeUtf8 => make_scalar_function(repeat::<i64>, vec![])(args),
- other => exec_err!("Unsupported data type {other:?} for function repeat. Expected Utf8, Utf8View or LargeUtf8"),
- }
+ make_scalar_function(repeat, vec![])(args)
}
}
/// Repeats string the specified number of times.
/// repeat('Pg', 4) = 'PgPgPgPg'
-fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
- let string_array = as_generic_string_array::<T>(&args[0])?;
+fn repeat(args: &[ArrayRef]) -> Result<ArrayRef> {
let number_array = as_int64_array(&args[1])?;
-
- let result = string_array
- .iter()
- .zip(number_array.iter())
- .map(|(string, number)| repeat_common(string, number))
- .collect::<GenericStringArray<T>>();
-
- Ok(Arc::new(result) as ArrayRef)
+ match args[0].data_type() {
+ Utf8View => {
+ let string_view_array = args[0].as_string_view();
+ repeat_impl::<i32, &StringViewArray>(string_view_array, number_array)
+ }
+ Utf8 => {
+ let string_array = args[0].as_string::<i32>();
+ repeat_impl::<i32, &GenericStringArray<i32>>(string_array, number_array)
+ }
+ LargeUtf8 => {
+ let string_array = args[0].as_string::<i64>();
+ repeat_impl::<i64, &GenericStringArray<i64>>(string_array, number_array)
+ }
+ other => exec_err!(
+ "Unsupported data type {other:?} for function repeat. \
+ Expected Utf8, Utf8View or LargeUtf8."
+ ),
+ }
}
-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
+fn repeat_impl<'a, T, S>(string_array: S, number_array: &Int64Array) -> Result<ArrayRef>
+where
+ T: OffsetSizeTrait,
+ S: StringArrayType<'a>,
+{
+ let mut builder: GenericStringBuilder<T> = GenericStringBuilder::new();
+ string_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> {
- match (string, number) {
- (Some(string), Some(number)) if number >= 0 => {
- Some(string.repeat(number as usize))
- }
- (Some(_), Some(_)) => Some("".to_string()),
- _ => None,
- }
+ .for_each(|(string, number)| match (string, number) {
Review Comment:
👏 -- very nice
--
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] Improve performance of REPEAT functions [datafusion]
Posted by "Omega359 (via GitHub)" <gi...@apache.org>.
Omega359 commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719199153
##########
datafusion/functions/benches/repeat.rs:
##########
@@ -0,0 +1,129 @@
+// 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.
+
+extern crate criterion;
+
+use arrow::array::{ArrayRef, Int64Array, OffsetSizeTrait};
+use arrow::util::bench_util::{
+ create_string_array_with_len, create_string_view_array_with_len,
+};
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use datafusion_expr::ColumnarValue;
+use datafusion_functions::string;
+use std::sync::Arc;
+
+fn create_args<O: OffsetSizeTrait>(
+ size: usize,
+ str_len: usize,
+ repeat_times: i64,
+ use_string_view: bool,
+) -> Vec<ColumnarValue> {
+ let number_array = Arc::new(Int64Array::from(
+ (0..size).map(|_| repeat_times).collect::<Vec<_>>(),
+ ));
+
+ if use_string_view {
+ let string_array =
+ Arc::new(create_string_view_array_with_len(size, 0.1, str_len, false));
+ vec![
+ ColumnarValue::Array(string_array),
+ ColumnarValue::Array(number_array),
+ ]
+ } else {
+ let string_array =
+ Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));
+
+ vec![
+ ColumnarValue::Array(string_array),
+ ColumnarValue::Array(Arc::clone(&number_array) as ArrayRef),
+ ]
+ }
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let lower = string::lower();
Review Comment:
I think this may be a copy/paste error ... shouldn't this be `let repeat = string::repeat();` ?
##########
datafusion/functions/src/string/repeat.rs:
##########
@@ -79,50 +80,66 @@ impl ScalarUDFImpl for RepeatFunc {
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
- match args[0].data_type() {
- DataType::Utf8View => make_scalar_function(repeat_utf8view, vec![])(args),
- DataType::Utf8 => make_scalar_function(repeat::<i32>, vec![])(args),
- DataType::LargeUtf8 => make_scalar_function(repeat::<i64>, vec![])(args),
- other => exec_err!("Unsupported data type {other:?} for function repeat. Expected Utf8, Utf8View or LargeUtf8"),
- }
+ make_scalar_function(repeat, vec![])(args)
}
}
/// Repeats string the specified number of times.
/// repeat('Pg', 4) = 'PgPgPgPg'
-fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
- let string_array = as_generic_string_array::<T>(&args[0])?;
+fn repeat(args: &[ArrayRef]) -> Result<ArrayRef> {
let number_array = as_int64_array(&args[1])?;
-
- let result = string_array
- .iter()
- .zip(number_array.iter())
- .map(|(string, number)| repeat_common(string, number))
- .collect::<GenericStringArray<T>>();
-
- Ok(Arc::new(result) as ArrayRef)
+ match args[0].data_type() {
+ Utf8View => {
+ let string_view_array = args[0].as_string_view();
+ repeat_impl::<i32, &StringViewArray>(string_view_array, number_array)
+ }
+ Utf8 => {
+ let string_array = args[0].as_string::<i32>();
+ repeat_impl::<i32, &GenericStringArray<i32>>(string_array, number_array)
+ }
+ LargeUtf8 => {
+ let string_array = args[0].as_string::<i64>();
+ repeat_impl::<i64, &GenericStringArray<i64>>(string_array, number_array)
+ }
+ other => exec_err!(
+ "Unsupported data type {other:?} for function repeat. \
+ Expected Utf8, Utf8View or LargeUtf8."
+ ),
+ }
}
-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
+fn repeat_impl<'a, T, S>(string_array: S, number_array: &Int64Array) -> Result<ArrayRef>
+where
+ T: OffsetSizeTrait,
+ S: StringArrayType<'a>,
+{
+ let mut builder: GenericStringBuilder<T> = GenericStringBuilder::new();
+ string_array
.iter()
.zip(number_array.iter())
- .map(|(string, number)| repeat_common(string, number))
- .collect::<StringArray>();
-
- Ok(Arc::new(result) as ArrayRef)
+ .for_each(|(string, number)| match (string, number) {
+ (Some(string), Some(number)) if number >= 0 => {
+ builder.append_value(string.repeat(number as usize))
+ }
+ (Some(_), Some(_)) => builder.append_value(""),
+ _ => builder.append_null(),
+ });
+ let array = builder.finish();
+
+ Ok(Arc::new(array) as ArrayRef)
}
-fn repeat_common(string: Option<&str>, number: Option<i64>) -> Option<String> {
- match (string, number) {
- (Some(string), Some(number)) if number >= 0 => {
- Some(string.repeat(number as usize))
- }
- (Some(_), Some(_)) => Some("".to_string()),
- _ => None,
+trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
Review Comment:
I think @alamb mentioned this somewhere else but we likely should pull this out to a common util somewhere so that we don't have copies of this in multiple places.
--
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] Improve performance of REPEAT functions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719686961
##########
datafusion/functions/src/string/common.rs:
##########
@@ -251,6 +252,22 @@ impl<'a> ColumnarValueRef<'a> {
}
}
+pub trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
Review Comment:
https://github.com/apache/datafusion/pull/12027 is a PR to document this trait and how to use 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] Improve performance of REPEAT functions [datafusion]
Posted by "tlm365 (via GitHub)" <gi...@apache.org>.
tlm365 commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719296602
##########
datafusion/functions/benches/repeat.rs:
##########
@@ -0,0 +1,129 @@
+// 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.
+
+extern crate criterion;
+
+use arrow::array::{ArrayRef, Int64Array, OffsetSizeTrait};
+use arrow::util::bench_util::{
+ create_string_array_with_len, create_string_view_array_with_len,
+};
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use datafusion_expr::ColumnarValue;
+use datafusion_functions::string;
+use std::sync::Arc;
+
+fn create_args<O: OffsetSizeTrait>(
+ size: usize,
+ str_len: usize,
+ repeat_times: i64,
+ use_string_view: bool,
+) -> Vec<ColumnarValue> {
+ let number_array = Arc::new(Int64Array::from(
+ (0..size).map(|_| repeat_times).collect::<Vec<_>>(),
+ ));
+
+ if use_string_view {
+ let string_array =
+ Arc::new(create_string_view_array_with_len(size, 0.1, str_len, false));
+ vec![
+ ColumnarValue::Array(string_array),
+ ColumnarValue::Array(number_array),
+ ]
+ } else {
+ let string_array =
+ Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));
+
+ vec![
+ ColumnarValue::Array(string_array),
+ ColumnarValue::Array(Arc::clone(&number_array) as ArrayRef),
+ ]
+ }
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let lower = string::lower();
Review Comment:
@Omega359 oops, you're right. Code and benchmark are updated now.
--
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] Improve performance of REPEAT functions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #12015:
URL: https://github.com/apache/datafusion/pull/12015#discussion_r1719656343
##########
datafusion/functions/src/string/common.rs:
##########
@@ -251,6 +252,22 @@ impl<'a> ColumnarValueRef<'a> {
}
}
+pub trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
Review Comment:
This makes sense -- thank you @tlm365 and @Omega359 -- I'll make a PR to add some additional comments to this trait as well
--
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