datafusion-functions 54.0.0

Function packages for the DataFusion query engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Common utilities for implementing string functions

use std::sync::Arc;

use crate::strings::{
    GenericStringArrayBuilder, STRING_VIEW_INIT_BLOCK_SIZE, STRING_VIEW_MAX_BLOCK_SIZE,
    StringViewArrayBuilder, append_view,
};
use arrow::array::{
    Array, ArrayRef, GenericStringArray, NullBufferBuilder, OffsetSizeTrait,
    StringViewArray, new_null_array,
};
use arrow::buffer::{Buffer, OffsetBuffer, ScalarBuffer};
use arrow::datatypes::DataType;
use datafusion_common::Result;
use datafusion_common::cast::{as_generic_string_array, as_string_view_array};
use datafusion_common::{ScalarValue, exec_err};
use datafusion_expr::ColumnarValue;

/// Trait for trim operations, allowing compile-time dispatch instead of runtime matching.
///
/// Each implementation performs its specific trim operation and returns
/// (trimmed_str, start_offset) where start_offset is the byte offset
/// from the beginning of the input string where the trimmed result starts.
pub(crate) trait Trimmer {
    fn trim<'a>(input: &'a str, pattern: &[char]) -> (&'a str, u32);

    /// Optimized trim for a single ASCII byte.
    /// Uses byte-level scanning instead of char-level iteration.
    fn trim_ascii_char(input: &str, byte: u8) -> (&str, u32);
}

/// Returns the number of leading bytes matching `byte`
#[inline]
fn leading_bytes(bytes: &[u8], byte: u8) -> usize {
    bytes.iter().take_while(|&&b| b == byte).count()
}

/// Returns the number of trailing bytes matching `byte`
#[inline]
fn trailing_bytes(bytes: &[u8], byte: u8) -> usize {
    bytes.iter().rev().take_while(|&&b| b == byte).count()
}

/// Left trim - removes leading characters
pub(crate) struct TrimLeft;

impl Trimmer for TrimLeft {
    #[inline]
    fn trim<'a>(input: &'a str, pattern: &[char]) -> (&'a str, u32) {
        if pattern.len() == 1 && pattern[0].is_ascii() {
            return Self::trim_ascii_char(input, pattern[0] as u8);
        }
        let trimmed = input.trim_start_matches(pattern);
        let offset = (input.len() - trimmed.len()) as u32;
        (trimmed, offset)
    }

    #[inline]
    fn trim_ascii_char(input: &str, byte: u8) -> (&str, u32) {
        let start = leading_bytes(input.as_bytes(), byte);
        (&input[start..], start as u32)
    }
}

/// Right trim - removes trailing characters
pub(crate) struct TrimRight;

impl Trimmer for TrimRight {
    #[inline]
    fn trim<'a>(input: &'a str, pattern: &[char]) -> (&'a str, u32) {
        if pattern.len() == 1 && pattern[0].is_ascii() {
            return Self::trim_ascii_char(input, pattern[0] as u8);
        }
        let trimmed = input.trim_end_matches(pattern);
        (trimmed, 0)
    }

    #[inline]
    fn trim_ascii_char(input: &str, byte: u8) -> (&str, u32) {
        let bytes = input.as_bytes();
        let end = bytes.len() - trailing_bytes(bytes, byte);
        (&input[..end], 0)
    }
}

/// Both trim - removes both leading and trailing characters
pub(crate) struct TrimBoth;

impl Trimmer for TrimBoth {
    #[inline]
    fn trim<'a>(input: &'a str, pattern: &[char]) -> (&'a str, u32) {
        if pattern.len() == 1 && pattern[0].is_ascii() {
            return Self::trim_ascii_char(input, pattern[0] as u8);
        }
        let left_trimmed = input.trim_start_matches(pattern);
        let offset = (input.len() - left_trimmed.len()) as u32;
        let trimmed = left_trimmed.trim_end_matches(pattern);
        (trimmed, offset)
    }

    #[inline]
    fn trim_ascii_char(input: &str, byte: u8) -> (&str, u32) {
        let bytes = input.as_bytes();
        let start = leading_bytes(bytes, byte);
        let end = bytes.len() - trailing_bytes(&bytes[start..], byte);
        (&input[start..end], start as u32)
    }
}

pub(crate) fn general_trim<T: OffsetSizeTrait, Tr: Trimmer>(
    args: &[ArrayRef],
    use_string_view: bool,
) -> Result<ArrayRef> {
    if use_string_view {
        string_view_trim::<Tr>(args)
    } else {
        string_trim::<T, Tr>(args)
    }
}

/// Applies the trim function to the given string view array(s)
/// and returns a new string view array with the trimmed values.
///
/// Pre-computes the pattern characters once for scalar patterns to avoid
/// repeated allocations per row.
fn string_view_trim<Tr: Trimmer>(args: &[ArrayRef]) -> Result<ArrayRef> {
    let string_view_array = as_string_view_array(&args[0])?;
    let mut views_buf = Vec::with_capacity(string_view_array.len());
    let mut null_builder = NullBufferBuilder::new(string_view_array.len());

    match args.len() {
        1 => {
            // Trim spaces by default
            for (src_str_opt, raw_view) in string_view_array
                .iter()
                .zip(string_view_array.views().iter())
            {
                if let Some(src_str) = src_str_opt {
                    let (trimmed, offset) = Tr::trim_ascii_char(src_str, b' ');
                    append_view(&mut views_buf, raw_view, trimmed, offset);
                    null_builder.append_non_null();
                } else {
                    null_builder.append_null();
                    views_buf.push(0);
                }
            }
        }
        2 => {
            let characters_array = as_string_view_array(&args[1])?;

            if characters_array.len() == 1 {
                // Scalar pattern - pre-compute pattern chars once
                if characters_array.is_null(0) {
                    return Ok(new_null_array(
                        &DataType::Utf8View,
                        string_view_array.len(),
                    ));
                }

                let pattern: Vec<char> = characters_array.value(0).chars().collect();
                for (src_str_opt, raw_view) in string_view_array
                    .iter()
                    .zip(string_view_array.views().iter())
                {
                    trim_and_append_view::<Tr>(
                        src_str_opt,
                        &pattern,
                        &mut views_buf,
                        &mut null_builder,
                        raw_view,
                    );
                }
            } else {
                // Per-row pattern - must compute pattern chars for each row
                let mut pattern: Vec<char> = Vec::new();
                for ((src_str_opt, raw_view), characters_opt) in string_view_array
                    .iter()
                    .zip(string_view_array.views().iter())
                    .zip(characters_array.iter())
                {
                    if let (Some(src_str), Some(characters)) =
                        (src_str_opt, characters_opt)
                    {
                        pattern.clear();
                        pattern.extend(characters.chars());
                        let (trimmed, offset) = Tr::trim(src_str, &pattern);
                        append_view(&mut views_buf, raw_view, trimmed, offset);
                        null_builder.append_non_null();
                    } else {
                        null_builder.append_null();
                        views_buf.push(0);
                    }
                }
            }
        }
        other => {
            return exec_err!(
                "Function TRIM was called with {other} arguments. It requires at least 1 and at most 2."
            );
        }
    }

    let views_buf = ScalarBuffer::from(views_buf);
    let nulls_buf = null_builder.finish();

    // Safety:
    // (1) The blocks of the given views are all provided
    // (2) Each of the range `view.offset+start..end` of view in views_buf is within
    // the bounds of each of the blocks
    unsafe {
        let array = StringViewArray::new_unchecked(
            views_buf,
            string_view_array.data_buffers().to_vec(),
            nulls_buf,
        );
        Ok(Arc::new(array) as ArrayRef)
    }
}

/// Trims the given string and appends the trimmed string to the views buffer
/// and the null buffer.
///
/// Arguments
/// - `src_str_opt`: The original string value (represented by the view)
/// - `pattern`: Pre-computed character pattern to trim
/// - `views_buf`: The buffer to append the updated views to
/// - `null_builder`: The buffer to append the null values to
/// - `original_view`: The original view value (that contains src_str_opt)
#[inline]
fn trim_and_append_view<Tr: Trimmer>(
    src_str_opt: Option<&str>,
    pattern: &[char],
    views_buf: &mut Vec<u128>,
    null_builder: &mut NullBufferBuilder,
    original_view: &u128,
) {
    if let Some(src_str) = src_str_opt {
        let (trimmed, offset) = Tr::trim(src_str, pattern);
        append_view(views_buf, original_view, trimmed, offset);
        null_builder.append_non_null();
    } else {
        null_builder.append_null();
        views_buf.push(0);
    }
}

/// Applies the trim function to the given string array(s)
/// and returns a new string array with the trimmed values.
///
/// Pre-computes the pattern characters once for scalar patterns to avoid
/// repeated allocations per row.
fn string_trim<T: OffsetSizeTrait, Tr: Trimmer>(args: &[ArrayRef]) -> Result<ArrayRef> {
    let string_array = as_generic_string_array::<T>(&args[0])?;

    match args.len() {
        1 => {
            // Trim spaces by default
            let result = string_array
                .iter()
                .map(|string| string.map(|s| Tr::trim_ascii_char(s, b' ').0))
                .collect::<GenericStringArray<T>>();

            Ok(Arc::new(result) as ArrayRef)
        }
        2 => {
            let characters_array = as_generic_string_array::<T>(&args[1])?;

            if characters_array.len() == 1 {
                // Scalar pattern - pre-compute pattern chars once
                if characters_array.is_null(0) {
                    return Ok(new_null_array(
                        string_array.data_type(),
                        string_array.len(),
                    ));
                }

                let pattern: Vec<char> = characters_array.value(0).chars().collect();
                let result = string_array
                    .iter()
                    .map(|item| item.map(|s| Tr::trim(s, &pattern).0))
                    .collect::<GenericStringArray<T>>();
                return Ok(Arc::new(result) as ArrayRef);
            }

            // Per-row pattern - must compute pattern chars for each row
            let mut pattern: Vec<char> = Vec::new();
            let result = string_array
                .iter()
                .zip(characters_array.iter())
                .map(|(string, characters)| match (string, characters) {
                    (Some(s), Some(c)) => {
                        pattern.clear();
                        pattern.extend(c.chars());
                        Some(Tr::trim(s, &pattern).0)
                    }
                    _ => None,
                })
                .collect::<GenericStringArray<T>>();

            Ok(Arc::new(result) as ArrayRef)
        }
        other => {
            exec_err!(
                "Function TRIM was called with {other} arguments. It requires at least 1 and at most 2."
            )
        }
    }
}

pub(crate) fn to_lower(args: &[ColumnarValue], name: &str) -> Result<ColumnarValue> {
    case_conversion(args, true, name)
}

pub(crate) fn to_upper(args: &[ColumnarValue], name: &str) -> Result<ColumnarValue> {
    case_conversion(args, false, name)
}

#[inline]
fn unicode_case(s: &str, lower: bool) -> String {
    if lower {
        s.to_lowercase()
    } else {
        s.to_uppercase()
    }
}

fn case_conversion(
    args: &[ColumnarValue],
    lower: bool,
    name: &str,
) -> Result<ColumnarValue> {
    match &args[0] {
        ColumnarValue::Array(array) => match array.data_type() {
            DataType::Utf8 => Ok(ColumnarValue::Array(case_conversion_array::<i32>(
                array, lower,
            )?)),
            DataType::LargeUtf8 => Ok(ColumnarValue::Array(
                case_conversion_array::<i64>(array, lower)?,
            )),
            DataType::Utf8View => {
                let string_array = as_string_view_array(array)?;
                if string_array.is_ascii() {
                    return Ok(ColumnarValue::Array(Arc::new(
                        case_conversion_utf8view_ascii(string_array, lower),
                    )));
                }
                let item_len = string_array.len();
                // Null-preserving: reuse the input null buffer as the output null buffer.
                let nulls = string_array.nulls().cloned();
                let mut builder = StringViewArrayBuilder::with_capacity(item_len);

                if let Some(ref n) = nulls {
                    for i in 0..item_len {
                        if n.is_null(i) {
                            builder.append_placeholder();
                        } else {
                            // SAFETY: `n.is_null(i)` was false in the branch above.
                            let s = unsafe { string_array.value_unchecked(i) };
                            builder.append_value(&unicode_case(s, lower));
                        }
                    }
                } else {
                    for i in 0..item_len {
                        // SAFETY: no null buffer means every index is valid.
                        let s = unsafe { string_array.value_unchecked(i) };
                        builder.append_value(&unicode_case(s, lower));
                    }
                }

                Ok(ColumnarValue::Array(Arc::new(builder.finish(nulls)?)))
            }
            other => exec_err!("Unsupported data type {other:?} for function {name}"),
        },
        ColumnarValue::Scalar(scalar) => match scalar {
            ScalarValue::Utf8(a) => {
                let result = a.as_ref().map(|x| unicode_case(x, lower));
                Ok(ColumnarValue::Scalar(ScalarValue::Utf8(result)))
            }
            ScalarValue::LargeUtf8(a) => {
                let result = a.as_ref().map(|x| unicode_case(x, lower));
                Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(result)))
            }
            ScalarValue::Utf8View(a) => {
                let result = a.as_ref().map(|x| unicode_case(x, lower));
                Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(result)))
            }
            other => exec_err!("Unsupported data type {other:?} for function {name}"),
        },
    }
}

fn case_conversion_array<O: OffsetSizeTrait>(
    array: &ArrayRef,
    lower: bool,
) -> Result<ArrayRef> {
    const PRE_ALLOC_BYTES: usize = 8;

    let string_array = as_generic_string_array::<O>(array)?;
    if string_array.is_ascii() {
        return case_conversion_ascii_array::<O>(string_array, lower);
    }

    // Values contain non-ASCII.
    let item_len = string_array.len();
    let offsets = string_array.value_offsets();
    let start = offsets.first().unwrap().as_usize();
    let end = offsets.last().unwrap().as_usize();
    let capacity = (end - start) + PRE_ALLOC_BYTES;
    // Null-preserving: reuse the input null buffer as the output null buffer.
    let nulls = string_array.nulls().cloned();
    let mut builder = GenericStringArrayBuilder::<O>::with_capacity(item_len, capacity);

    if let Some(ref n) = nulls {
        for i in 0..item_len {
            if n.is_null(i) {
                builder.append_placeholder();
            } else {
                // SAFETY: `n.is_null(i)` was false in the branch above.
                let s = unsafe { string_array.value_unchecked(i) };
                builder.append_value(&unicode_case(s, lower));
            }
        }
    } else {
        for i in 0..item_len {
            // SAFETY: no null buffer means every index is valid.
            let s = unsafe { string_array.value_unchecked(i) };
            builder.append_value(&unicode_case(s, lower));
        }
    }
    Ok(Arc::new(builder.finish(nulls)?))
}

/// Fast path for case conversion on an all-ASCII `StringViewArray`.
fn case_conversion_utf8view_ascii(
    array: &StringViewArray,
    lower: bool,
) -> StringViewArray {
    // Specialize per conversion so the byte call inlines in the hot loops below.
    if lower {
        case_conversion_utf8view_ascii_inner(array, u8::to_ascii_lowercase)
    } else {
        case_conversion_utf8view_ascii_inner(array, u8::to_ascii_uppercase)
    }
}

/// Walks the views once and produces a new `StringViewArray` with
/// case-converted bytes. Inline strings (<= 12 bytes) are converted in-place;
/// long strings copy-and-convert into output buffers and have their view fields
/// rewritten to address the new bytes. ASCII case conversion preserves is byte
/// length, so no row migrates between the inline and long layouts.
fn case_conversion_utf8view_ascii_inner<F: Fn(&u8) -> u8>(
    array: &StringViewArray,
    convert: F,
) -> StringViewArray {
    let item_len = array.len();
    let views = array.views();
    let data_buffers = array.data_buffers();
    let nulls = array.nulls();

    let mut new_views: Vec<u128> = Vec::with_capacity(item_len);
    // Long values are packed into `in_progress`; when full it is sealed into
    // `completed` and a new, larger block is started — same block-doubling
    // scheme as Arrow's `GenericByteViewBuilder`.
    let mut in_progress: Vec<u8> = Vec::new();
    let mut completed: Vec<Buffer> = Vec::new();
    let mut block_size: u32 = STRING_VIEW_INIT_BLOCK_SIZE;

    for i in 0..item_len {
        if nulls.is_some_and(|n| n.is_null(i)) {
            // Zero view = empty, no buffer reference; the null buffer is what
            // marks the row null, so the view's value is irrelevant.
            new_views.push(0);
            continue;
        }
        let view = views[i];
        // Length is the low 32 bits; `as u32` discards the rest of the view.
        let len = view as u32 as usize;
        if len == 0 {
            new_views.push(0);
            continue;
        }
        let mut bytes = view.to_le_bytes();
        if len <= 12 {
            // Inline: value is in bytes[4..4+len], no buffer reference. Convert
            // in place; nothing else in the view needs to change.
            for b in &mut bytes[4..4 + len] {
                *b = convert(b);
            }
            new_views.push(u128::from_le_bytes(bytes));
        } else {
            // Long: input view points into shared `data_buffers` we can't
            // mutate, so copy-convert into our own buffer and rewrite the
            // view's prefix/buffer_index/offset (length is preserved).

            // Ensure the current block has room; otherwise flush and grow.
            let required_cap = in_progress.len() + len;
            if in_progress.capacity() < required_cap {
                if !in_progress.is_empty() {
                    completed.push(Buffer::from_vec(std::mem::take(&mut in_progress)));
                }
                if block_size < STRING_VIEW_MAX_BLOCK_SIZE {
                    block_size = block_size.saturating_mul(2);
                }
                let to_reserve = len.max(block_size as usize);
                in_progress.reserve(to_reserve);
            }

            // The in-progress block will be sealed at index `completed.len()`,
            // and our value starts at the current write position within it.
            let buffer_index: u32 = i32::try_from(completed.len())
                .expect("buffer count exceeds i32::MAX")
                as u32;
            let new_offset: u32 =
                i32::try_from(in_progress.len()).expect("offset exceeds i32::MAX") as u32;

            // Source location from the input view: bytes 8..12 are buffer
            // index, bytes 12..16 are the offset within it.
            let src_buffer_index =
                u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as usize;
            let src_offset =
                u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
            let src =
                &data_buffers[src_buffer_index].as_slice()[src_offset..src_offset + len];

            let prefix_start = in_progress.len();
            in_progress.extend(src.iter().map(&convert));

            // Rewrite the three long-view fields; bytes[0..4] (length) is
            // left untouched. The prefix is read back from the bytes we just
            // wrote so the converted value has a single source of truth.
            let prefix: [u8; 4] = in_progress[prefix_start..prefix_start + 4]
                .try_into()
                .unwrap();
            bytes[4..8].copy_from_slice(&prefix);
            bytes[8..12].copy_from_slice(&buffer_index.to_le_bytes());
            bytes[12..16].copy_from_slice(&new_offset.to_le_bytes());
            new_views.push(u128::from_le_bytes(bytes));
        }
    }

    if !in_progress.is_empty() {
        completed.push(Buffer::from_vec(in_progress));
    }

    // SAFETY: each long view's buffer_index addresses a buffer we wrote, and
    // its offset addresses bytes within that buffer; prefixes were copied from
    // those same bytes; inline views were rewritten from valid inline bytes;
    // null/empty rows are zero views with no buffer reference; row count is
    // unchanged.
    unsafe {
        StringViewArray::new_unchecked(
            ScalarBuffer::from(new_views),
            completed,
            array.nulls().cloned(),
        )
    }
}

/// 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.
fn case_conversion_ascii_array<O: OffsetSizeTrait>(
    string_array: &GenericStringArray<O>,
    lower: bool,
) -> Result<ArrayRef> {
    let value_offsets = string_array.value_offsets();
    let start = value_offsets.first().unwrap().as_usize();
    let end = value_offsets.last().unwrap().as_usize();
    let relevant = &string_array.value_data()[start..end];

    let converted: Vec<u8> = if lower {
        relevant.iter().map(u8::to_ascii_lowercase).collect()
    } else {
        relevant.iter().map(u8::to_ascii_uppercase).collect()
    };
    let values = Buffer::from_vec(converted);

    // Shift offsets from `start`-based to 0-based so they index into `values`.
    let offsets = if start == 0 {
        string_array.offsets().clone()
    } else {
        let s = O::usize_as(start);
        let rebased: Vec<O> = value_offsets.iter().map(|&o| o - s).collect();
        // SAFETY: subtracting a constant from monotonic offsets preserves
        // monotonicity, and `start` is the minimum offset, so no underflow.
        unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(rebased)) }
    };

    let nulls = string_array.nulls().cloned();
    // SAFETY: offsets are monotonic and in-bounds for `values`; nulls
    // (if any) match the slice length.
    Ok(Arc::new(unsafe {
        GenericStringArray::<O>::new_unchecked(offsets, values, nulls)
    }))
}