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

Function generic_append_and_prepend

datafusion/functions-nested/src/concat.rs:528–573  ·  view source on GitHub ↗

Appends or prepends elements to a ListArray. This function takes a ListArray, an ArrayRef, a FieldRef, and a boolean flag indicating whether to append or prepend the elements. It returns a `Result ` representing the resulting ListArray after the operation. # Arguments `list_array` - A reference to the ListArray to which elements will be appended/prepended. `element_array` - A reference

(
    list_array: &GenericListArray<O>,
    element_array: &ArrayRef,
    data_type: &DataType,
    is_append: bool,
)

Source from the content-addressed store, hash-verified

526/// 5, [6, 7, 8], prepend => [5, 6, 7, 8]
527/// )
528fn generic_append_and_prepend<O: OffsetSizeTrait>(
529 list_array: &GenericListArray<O>,
530 element_array: &ArrayRef,
531 data_type: &DataType,
532 is_append: bool,
533) -> Result<ArrayRef>
534where
535 i64: TryInto<O>,
536{
537 let mut offsets = vec![O::usize_as(0)];
538 let values = list_array.values();
539 let original_data = values.to_data();
540 let element_data = element_array.to_data();
541 let capacity = Capacities::Array(original_data.len() + element_data.len());
542
543 let mut mutable = MutableArrayData::with_capacities(
544 vec![&original_data, &element_data],
545 false,
546 capacity,
547 );
548
549 let values_index = 0;
550 let element_index = 1;
551
552 for (row_index, offset_window) in list_array.offsets().windows(2).enumerate() {
553 let start = offset_window[0].to_usize().unwrap();
554 let end = offset_window[1].to_usize().unwrap();
555 if is_append {
556 mutable.extend(values_index, start, end);
557 mutable.extend(element_index, row_index, row_index + 1);
558 } else {
559 mutable.extend(element_index, row_index, row_index + 1);
560 mutable.extend(values_index, start, end);
561 }
562 offsets.push(offsets[row_index] + O::usize_as(end - start + 1));
563 }
564
565 let data = mutable.freeze();
566
567 Ok(Arc::new(GenericListArray::<O>::try_new(
568 Arc::new(Field::new_list_field(data_type.to_owned(), true)),
569 OffsetBuffer::new(offsets.into()),
570 arrow::array::make_array(data),
571 None,
572 )?))
573}

Callers

nothing calls this directly

Calls 8

newFunction · 0.85
make_arrayFunction · 0.85
offsetsMethod · 0.80
valuesMethod · 0.45
lenMethod · 0.45
extendMethod · 0.45
pushMethod · 0.45
intoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…