Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize performance of character_length function #13696

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions datafusion/functions/src/unicode/character_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::strings::StringArrayType;
use crate::utils::{make_scalar_function, utf8_to_int_type};
use arrow::array::{
Array, ArrayRef, ArrowPrimitiveType, AsArray, OffsetSizeTrait, PrimitiveArray,
Array, ArrayRef, ArrowPrimitiveType, AsArray, OffsetSizeTrait, PrimitiveBuilder,
};
use arrow::datatypes::{ArrowNativeType, DataType, Int32Type, Int64Type};
use datafusion_common::Result;
Expand Down Expand Up @@ -136,31 +136,52 @@ fn character_length(args: &[ArrayRef]) -> Result<ArrayRef> {
}
}

fn character_length_general<'a, T: ArrowPrimitiveType, V: StringArrayType<'a>>(
array: V,
) -> Result<ArrayRef>
fn character_length_general<'a, T, V>(array: V) -> Result<ArrayRef>
where
T: ArrowPrimitiveType,
T::Native: OffsetSizeTrait,
V: StringArrayType<'a>,
{
let mut builder = PrimitiveBuilder::<T>::with_capacity(array.len());

// String characters are variable length encoded in UTF-8, counting the
// number of chars requires expensive decoding, however checking if the
// string is ASCII only is relatively cheap.
// If strings are ASCII only, count bytes instead.
let is_array_ascii_only = array.is_ascii();
let iter = array.iter();
let result = iter
.map(|string| {
string.map(|string: &str| {
if is_array_ascii_only {
T::Native::usize_as(string.len())
} else {
T::Native::usize_as(string.chars().count())
}
})
})
.collect::<PrimitiveArray<T>>();

Ok(Arc::new(result) as ArrayRef)
if array.null_count() == 0 {
if is_array_ascii_only {
for i in 0..array.len() {
let value = array.value(i);
builder.append_value(T::Native::usize_as(value.len()));
}
} else {
for i in 0..array.len() {
let value = array.value(i);
builder.append_value(T::Native::usize_as(value.chars().count()));
}
}
} else if is_array_ascii_only {
for i in 0..array.len() {
if array.is_null(i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can specialize the code for non-null data here (removing array.is_null(i) if array.null_count() == 0)
which can give another gain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dandandan Thank you so much for the review. Yeah, nice catch 👍

builder.append_null();
} else {
let value = array.value(i);
builder.append_value(T::Native::usize_as(value.len()));
}
}
} else {
for i in 0..array.len() {
if array.is_null(i) {
builder.append_null();
} else {
let value = array.value(i);
builder.append_value(T::Native::usize_as(value.chars().count()));
}
}
}

Ok(Arc::new(builder.finish()) as ArrayRef)
}

#[cfg(test)]
Expand Down
Loading