MCPcopy Create free account
hub / github.com/apache/datafusion / case_conversion_ascii_array

Function case_conversion_ascii_array

datafusion/functions/src/string/common.rs:581–614  ·  view source on GitHub ↗

Fast path for case conversion on an all-ASCII string array. ASCII case conversion is byte-length-preserving, so we can convert the entire addressed byte range in one pass over the value buffer and reuse the offsets and nulls buffers — rebasing the offsets when the input is a sliced array.

(
    string_array: &GenericStringArray<O>,
    lower: bool,
)

Source from the content-addressed store, hash-verified

579/// byte range in one pass over the value buffer and reuse the offsets and nulls
580/// buffers — rebasing the offsets when the input is a sliced array.
581fn case_conversion_ascii_array<O: OffsetSizeTrait>(
582 string_array: &GenericStringArray<O>,
583 lower: bool,
584) -> Result<ArrayRef> {
585 let value_offsets = string_array.value_offsets();
586 let start = value_offsets.first().unwrap().as_usize();
587 let end = value_offsets.last().unwrap().as_usize();
588 let relevant = &string_array.value_data()[start..end];
589
590 let converted: Vec<u8> = if lower {
591 relevant.iter().map(u8::to_ascii_lowercase).collect()
592 } else {
593 relevant.iter().map(u8::to_ascii_uppercase).collect()
594 };
595 let values = Buffer::from_vec(converted);
596
597 // Shift offsets from `start`-based to 0-based so they index into `values`.
598 let offsets = if start == 0 {
599 string_array.offsets().clone()
600 } else {
601 let s = O::usize_as(start);
602 let rebased: Vec<O> = value_offsets.iter().map(|&o| o - s).collect();
603 // SAFETY: subtracting a constant from monotonic offsets preserves
604 // monotonicity, and `start` is the minimum offset, so no underflow.
605 unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(rebased)) }
606 };
607
608 let nulls = string_array.nulls().cloned();
609 // SAFETY: offsets are monotonic and in-bounds for `values`; nulls
610 // (if any) match the slice length.
611 Ok(Arc::new(unsafe {
612 GenericStringArray::<O>::new_unchecked(offsets, values, nulls)
613 }))
614}

Callers

nothing calls this directly

Calls 12

newFunction · 0.85
value_offsetsMethod · 0.80
lastMethod · 0.80
collectMethod · 0.80
offsetsMethod · 0.80
as_usizeMethod · 0.45
firstMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45
clonedMethod · 0.45
nullsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…