Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion datafusion/functions/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,17 @@ impl StringViewArrayBuilder {
let offset: u32 = i32::try_from(self.in_progress.len())
.expect("offset exceeds i32::MAX") as u32;
self.in_progress.extend_from_slice(v);
self.views.push(make_view(v, buffer_index, offset));

// Build the ByteView inline rather than going through `make_view`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This might be worth a mention upstream in the docs -- I reacall this isn't inlined because for some really tight inner loops is messed up vectorization or something

But in general inlining probably helps (like maybe we should provide a version of make_view that is inlineable 🤔 )

// which is marked as `[inline(never)]`.
let view = ByteView {
length,
// SAFETY: length > 12 here, so v has at least 4 bytes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FWIW there is no safety implication here (as the code below uses unwrap() rather than unsafe)

prefix: u32::from_le_bytes(v[0..4].try_into().unwrap()),
buffer_index,
offset,
};
self.views.push(view.into());
}

/// Append an empty placeholder row. The corresponding slot must be
Expand Down
60 changes: 43 additions & 17 deletions datafusion/functions/src/unicode/initcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

use std::sync::Arc;

use arrow::array::{
Array, ArrayRef, GenericStringArray, GenericStringBuilder, OffsetSizeTrait,
StringViewBuilder,
};
use arrow::array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait};
use arrow::buffer::{Buffer, OffsetBuffer};
use arrow::datatypes::DataType;

use crate::strings::{GenericStringArrayBuilder, StringViewArrayBuilder};
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;
Expand Down Expand Up @@ -157,21 +155,35 @@ fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
return Ok(initcap_ascii_array(string_array));
}

let mut builder = GenericStringBuilder::<T>::with_capacity(
string_array.len(),
let len = string_array.len();
let mut builder = GenericStringArrayBuilder::<T>::with_capacity(
len,
string_array.value_data().len(),
);

let mut container = String::new();
string_array.iter().for_each(|str| match str {
Some(s) => {
let nulls = string_array.nulls().cloned();
if let Some(ref n) = nulls {
for i in 0..len {
if n.is_null(i) {
builder.append_placeholder();
} else {
// SAFETY: not null per check above.
let s = unsafe { string_array.value_unchecked(i) };
initcap_string(s, &mut container);
builder.append_value(&container);
}
}
} else {
for i in 0..len {
// SAFETY: no null buffer means every index is valid.
let s = unsafe { string_array.value_unchecked(i) };
initcap_string(s, &mut container);
builder.append_value(&container);
}
None => builder.append_null(),
});
}

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

/// Fast path for `Utf8` or `LargeUtf8` arrays that are ASCII-only. We can use a
Expand Down Expand Up @@ -232,18 +244,32 @@ fn initcap_ascii_array<T: OffsetSizeTrait>(

fn initcap_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_view_array = as_string_view_array(&args[0])?;
let mut builder = StringViewBuilder::with_capacity(string_view_array.len());
let len = string_view_array.len();
let mut builder = StringViewArrayBuilder::with_capacity(len);
let mut container = String::new();

string_view_array.iter().for_each(|str| match str {
Some(s) => {
let nulls = string_view_array.nulls().cloned();
if let Some(ref n) = nulls {
for i in 0..len {
if n.is_null(i) {
builder.append_placeholder();
} else {
// SAFETY: not null per check above.
let s = unsafe { string_view_array.value_unchecked(i) };
initcap_string(s, &mut container);
builder.append_value(&container);
}
}
} else {
for i in 0..len {
// SAFETY: no null buffer means every index is valid.
let s = unsafe { string_view_array.value_unchecked(i) };
initcap_string(s, &mut container);
builder.append_value(&container);
}
None => builder.append_null(),
});
}

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

fn initcap_string(input: &str, container: &mut String) {
Expand Down
Loading