This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new eadbed51b5 perf: Optimize initcap scalar performance (#19776)
eadbed51b5 is described below
commit eadbed51b5c711423bc5f1a4d72fbf1e5be5d975
Author: Kumar Ujjawal <[email protected]>
AuthorDate: Thu Jan 15 10:17:21 2026 +0530
perf: Optimize initcap scalar performance (#19776)
## 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.
-->
- Part of https://github.com/apache/datafusion-comet/issues/2986.
## Rationale for this change
- `initcap` uses `make_scalar_function` which converts scalar inputs to
arrays.
<!--
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?
- Add scalar fast path for Utf8/LargeUtf8/Utf8View inputs
- Reuse existing `initcap_string` helper for direct scalar processing
<!--
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. Unit tests and sqllogictest pass.
## Benchmark Results
| Type | Before | After | Speedup |
|------|--------|-------|---------|
| scalar_utf8 | 698 ns | 250 ns | **2.8x** |
| scalar_utf8view | 729 ns | 248 ns | **2.9x** |
Measured using:
```bash
cargo bench -p datafusion-functions --bench initcap -- "scalar"
```
<!--
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.
-->
---
datafusion/functions/benches/initcap.rs | 125 +++++++++++++++++-----------
datafusion/functions/src/unicode/initcap.rs | 35 +++++++-
2 files changed, 111 insertions(+), 49 deletions(-)
diff --git a/datafusion/functions/benches/initcap.rs
b/datafusion/functions/benches/initcap.rs
index ba055d58f5..a92c2cc2d5 100644
--- a/datafusion/functions/benches/initcap.rs
+++ b/datafusion/functions/benches/initcap.rs
@@ -22,12 +22,14 @@ use arrow::datatypes::{DataType, Field};
use arrow::util::bench_util::{
create_string_array_with_len, create_string_view_array_with_len,
};
-use criterion::{Criterion, criterion_group, criterion_main};
+use criterion::{Criterion, SamplingMode, criterion_group, criterion_main};
+use datafusion_common::ScalarValue;
use datafusion_common::config::ConfigOptions;
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
use datafusion_functions::unicode;
use std::hint::black_box;
use std::sync::Arc;
+use std::time::Duration;
fn create_args<O: OffsetSizeTrait>(
size: usize,
@@ -49,60 +51,87 @@ fn create_args<O: OffsetSizeTrait>(
fn criterion_benchmark(c: &mut Criterion) {
let initcap = unicode::initcap();
- for size in [1024, 4096] {
- let args = create_args::<i32>(size, 8, true);
- let arg_fields = args
- .iter()
- .enumerate()
- .map(|(idx, arg)| {
- Field::new(format!("arg_{idx}"), arg.data_type(), true).into()
- })
- .collect::<Vec<_>>();
- let config_options = Arc::new(ConfigOptions::default());
-
- c.bench_function(
- format!("initcap string view shorter than 12
[size={size}]").as_str(),
- |b| {
- b.iter(|| {
- black_box(initcap.invoke_with_args(ScalarFunctionArgs {
- args: args.clone(),
- arg_fields: arg_fields.clone(),
- number_rows: size,
- return_field: Field::new("f", DataType::Utf8View,
true).into(),
- config_options: Arc::clone(&config_options),
- }))
- })
- },
- );
-
- let args = create_args::<i32>(size, 16, true);
- c.bench_function(
- format!("initcap string view longer than 12
[size={size}]").as_str(),
- |b| {
- b.iter(|| {
- black_box(initcap.invoke_with_args(ScalarFunctionArgs {
- args: args.clone(),
- arg_fields: arg_fields.clone(),
- number_rows: size,
- return_field: Field::new("f", DataType::Utf8View,
true).into(),
- config_options: Arc::clone(&config_options),
- }))
- })
- },
- );
-
- let args = create_args::<i32>(size, 16, false);
- c.bench_function(format!("initcap string [size={size}]").as_str(), |b|
{
+ let config_options = Arc::new(ConfigOptions::default());
+
+ // Grouped benchmarks for array sizes - to compare with scalar performance
+ for size in [1024, 4096, 8192] {
+ let mut group = c.benchmark_group(format!("initcap size={size}"));
+ group.sampling_mode(SamplingMode::Flat);
+ group.sample_size(10);
+ group.measurement_time(Duration::from_secs(10));
+
+ // Array benchmark - Utf8
+ let array_args = create_args::<i32>(size, 16, false);
+ let array_arg_fields = vec![Field::new("arg_0", DataType::Utf8,
true).into()];
+ let batch_len = size;
+
+ group.bench_function("array_utf8", |b| {
b.iter(|| {
black_box(initcap.invoke_with_args(ScalarFunctionArgs {
- args: args.clone(),
- arg_fields: arg_fields.clone(),
- number_rows: size,
+ args: array_args.clone(),
+ arg_fields: array_arg_fields.clone(),
+ number_rows: batch_len,
return_field: Field::new("f", DataType::Utf8, true).into(),
config_options: Arc::clone(&config_options),
}))
})
});
+
+ // Array benchmark - Utf8View
+ let array_view_args = create_args::<i32>(size, 16, true);
+ let array_view_arg_fields =
+ vec![Field::new("arg_0", DataType::Utf8View, true).into()];
+
+ group.bench_function("array_utf8view", |b| {
+ b.iter(|| {
+ black_box(initcap.invoke_with_args(ScalarFunctionArgs {
+ args: array_view_args.clone(),
+ arg_fields: array_view_arg_fields.clone(),
+ number_rows: batch_len,
+ return_field: Field::new("f", DataType::Utf8View,
true).into(),
+ config_options: Arc::clone(&config_options),
+ }))
+ })
+ });
+
+ // Scalar benchmark - Utf8 (the optimization we added)
+ let scalar_args = vec![ColumnarValue::Scalar(ScalarValue::Utf8(Some(
+ "hello world test string".to_string(),
+ )))];
+ let scalar_arg_fields = vec![Field::new("arg_0", DataType::Utf8,
false).into()];
+
+ group.bench_function("scalar_utf8", |b| {
+ b.iter(|| {
+ black_box(initcap.invoke_with_args(ScalarFunctionArgs {
+ args: scalar_args.clone(),
+ arg_fields: scalar_arg_fields.clone(),
+ number_rows: 1,
+ return_field: Field::new("f", DataType::Utf8,
false).into(),
+ config_options: Arc::clone(&config_options),
+ }))
+ })
+ });
+
+ // Scalar benchmark - Utf8View
+ let scalar_view_args =
vec![ColumnarValue::Scalar(ScalarValue::Utf8View(Some(
+ "hello world test string".to_string(),
+ )))];
+ let scalar_view_arg_fields =
+ vec![Field::new("arg_0", DataType::Utf8View, false).into()];
+
+ group.bench_function("scalar_utf8view", |b| {
+ b.iter(|| {
+ black_box(initcap.invoke_with_args(ScalarFunctionArgs {
+ args: scalar_view_args.clone(),
+ arg_fields: scalar_view_arg_fields.clone(),
+ number_rows: 1,
+ return_field: Field::new("f", DataType::Utf8View,
false).into(),
+ config_options: Arc::clone(&config_options),
+ }))
+ })
+ });
+
+ group.finish();
}
}
diff --git a/datafusion/functions/src/unicode/initcap.rs
b/datafusion/functions/src/unicode/initcap.rs
index 929b0c3169..e2fc913099 100644
--- a/datafusion/functions/src/unicode/initcap.rs
+++ b/datafusion/functions/src/unicode/initcap.rs
@@ -26,7 +26,7 @@ use arrow::datatypes::DataType;
use crate::utils::{make_scalar_function, utf8_to_str_type};
use datafusion_common::cast::{as_generic_string_array, as_string_view_array};
use datafusion_common::types::logical_string;
-use datafusion_common::{Result, exec_err};
+use datafusion_common::{Result, ScalarValue, exec_err};
use datafusion_expr::{
Coercion, ColumnarValue, Documentation, ScalarUDFImpl, Signature,
TypeSignatureClass,
Volatility,
@@ -99,6 +99,39 @@ impl ScalarUDFImpl for InitcapFunc {
&self,
args: datafusion_expr::ScalarFunctionArgs,
) -> Result<ColumnarValue> {
+ let arg = &args.args[0];
+
+ // Scalar fast path - handle directly without array conversion
+ if let ColumnarValue::Scalar(scalar) = arg {
+ return match scalar {
+ ScalarValue::Utf8(None)
+ | ScalarValue::LargeUtf8(None)
+ | ScalarValue::Utf8View(None) => Ok(arg.clone()),
+ ScalarValue::Utf8(Some(s)) => {
+ let mut result = String::new();
+ initcap_string(s, &mut result);
+ Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(result))))
+ }
+ ScalarValue::LargeUtf8(Some(s)) => {
+ let mut result = String::new();
+ initcap_string(s, &mut result);
+
Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(result))))
+ }
+ ScalarValue::Utf8View(Some(s)) => {
+ let mut result = String::new();
+ initcap_string(s, &mut result);
+
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(result))))
+ }
+ other => {
+ exec_err!(
+ "Unsupported data type {:?} for function `initcap`",
+ other.data_type()
+ )
+ }
+ };
+ }
+
+ // Array path
let args = &args.args;
match args[0].data_type() {
DataType::Utf8 => make_scalar_function(initcap::<i32>,
vec![])(args),
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]