@@ -24,7 +24,7 @@ use arrow::array::{
2424 Array , ArrayRef , GenericStringArray , GenericStringBuilder , NullBufferBuilder ,
2525 OffsetSizeTrait , StringViewArray , StringViewBuilder , new_null_array,
2626} ;
27- use arrow:: buffer:: { Buffer , ScalarBuffer } ;
27+ use arrow:: buffer:: { Buffer , OffsetBuffer , ScalarBuffer } ;
2828use arrow:: datatypes:: DataType ;
2929use datafusion_common:: Result ;
3030use datafusion_common:: cast:: { as_generic_string_array, as_string_view_array} ;
@@ -390,16 +390,16 @@ where
390390 const PRE_ALLOC_BYTES : usize = 8 ;
391391
392392 let string_array = as_generic_string_array :: < O > ( array) ?;
393- let value_data = string_array. value_data ( ) ;
394-
395- // All values are ASCII.
396- if value_data. is_ascii ( ) {
393+ if string_array. is_ascii ( ) {
397394 return case_conversion_ascii_array :: < O , _ > ( string_array, op) ;
398395 }
399396
400397 // Values contain non-ASCII.
401398 let item_len = string_array. len ( ) ;
402- let capacity = string_array. value_data ( ) . len ( ) + PRE_ALLOC_BYTES ;
399+ let offsets = string_array. value_offsets ( ) ;
400+ let start = offsets. first ( ) . unwrap ( ) . as_usize ( ) ;
401+ let end = offsets. last ( ) . unwrap ( ) . as_usize ( ) ;
402+ let capacity = ( end - start) + PRE_ALLOC_BYTES ;
403403 let mut builder = GenericStringBuilder :: < O > :: with_capacity ( item_len, capacity) ;
404404
405405 if string_array. null_count ( ) == 0 {
@@ -413,9 +413,10 @@ where
413413 Ok ( Arc :: new ( builder. finish ( ) ) )
414414}
415415
416- /// All values of string_array are ASCII, and when converting case, there is no changes in the byte
417- /// array length. Therefore, the StringArray can be treated as a complete ASCII string for
418- /// case conversion, and we can reuse the offsets buffer and the nulls buffer.
416+ /// Fast path for case conversion on an all-ASCII string array. ASCII case
417+ /// conversion is byte-length-preserving, so we can convert the entire addressed
418+ /// range in one call and reuse the offsets and nulls buffers — rebasing the
419+ /// offsets when the input is a sliced array.
419420fn case_conversion_ascii_array < ' a , O , F > (
420421 string_array : & ' a GenericStringArray < O > ,
421422 op : F ,
@@ -424,21 +425,33 @@ where
424425 O : OffsetSizeTrait ,
425426 F : Fn ( & ' a str ) -> String ,
426427{
427- let value_data = string_array. value_data ( ) ;
428- // SAFETY: all items stored in value_data satisfy UTF8.
429- // ref: impl ByteArrayNativeType for str {...}
430- let str_values = unsafe { std:: str:: from_utf8_unchecked ( value_data) } ;
428+ let value_offsets = string_array. value_offsets ( ) ;
429+ let start = value_offsets. first ( ) . unwrap ( ) . as_usize ( ) ;
430+ let end = value_offsets. last ( ) . unwrap ( ) . as_usize ( ) ;
431+ let relevant = & string_array. value_data ( ) [ start..end] ;
432+
433+ // SAFETY: `relevant` is a subslice of the string array's value buffer,
434+ // which is valid UTF-8.
435+ let str_values = unsafe { std:: str:: from_utf8_unchecked ( relevant) } ;
431436
432- // conversion
433437 let converted_values = op ( str_values) ;
434- assert_eq ! ( converted_values. len( ) , str_values. len( ) ) ;
435- let bytes = converted_values. into_bytes ( ) ;
438+ debug_assert_eq ! ( converted_values. len( ) , str_values. len( ) ) ;
439+ let values = Buffer :: from_vec ( converted_values. into_bytes ( ) ) ;
440+
441+ // Shift offsets from `start`-based to 0-based so they index into `values`.
442+ let offsets = if start == 0 {
443+ string_array. offsets ( ) . clone ( )
444+ } else {
445+ let s = O :: usize_as ( start) ;
446+ let rebased: Vec < O > = value_offsets. iter ( ) . map ( |& o| o - s) . collect ( ) ;
447+ // SAFETY: subtracting a constant from monotonic offsets preserves
448+ // monotonicity, and `start` is the minimum offset so no underflow.
449+ unsafe { OffsetBuffer :: new_unchecked ( ScalarBuffer :: from ( rebased) ) }
450+ } ;
436451
437- // build result
438- let values = Buffer :: from_vec ( bytes) ;
439- let offsets = string_array. offsets ( ) . clone ( ) ;
440452 let nulls = string_array. nulls ( ) . cloned ( ) ;
441- // SAFETY: offsets and nulls are consistent with the input array.
453+ // SAFETY: offsets are monotonic and in-bounds for `values`; nulls
454+ // (if any) match the slice length.
442455 Ok ( Arc :: new ( unsafe {
443456 GenericStringArray :: < O > :: new_unchecked ( offsets, values, nulls)
444457 } ) )
0 commit comments