MCPcopy Create free account
hub / github.com/apache/arrow-rs / into_zero_offset_run_array

Function into_zero_offset_run_array

arrow-ipc/src/writer.rs:752–800  ·  view source on GitHub ↗

Returns a `RunArray` with zero offset and length matching the last value in run_ends array.

(
    run_array: RunArray<R>,
)

Source from the content-addressed store, hash-verified

750// Returns a `RunArray` with zero offset and length matching the last value
751// in run_ends array.
752fn into_zero_offset_run_array<R: RunEndIndexType>(
753 run_array: RunArray<R>,
754) -> Result<RunArray<R>, ArrowError> {
755 let run_ends = run_array.run_ends();
756 if run_ends.offset() == 0 && run_ends.max_value() == run_ends.len() {
757 return Ok(run_array);
758 }
759
760 // The physical index of original run_ends array from which the `ArrayData`is sliced.
761 let start_physical_index = run_ends.get_start_physical_index();
762
763 // The physical index of original run_ends array until which the `ArrayData`is sliced.
764 let end_physical_index = run_ends.get_end_physical_index();
765
766 let physical_length = end_physical_index - start_physical_index + 1;
767
768 // build new run_ends array by subtracting offset from run ends.
769 let offset = R::Native::usize_as(run_ends.offset());
770 let mut builder = BufferBuilder::<R::Native>::new(physical_length);
771 for run_end_value in &run_ends.values()[start_physical_index..end_physical_index] {
772 builder.append(run_end_value.sub_wrapping(offset));
773 }
774 builder.append(R::Native::from_usize(run_array.len()).unwrap());
775 let new_run_ends = unsafe {
776 // Safety:
777 // The function builds a valid run_ends array and hence need not be validated.
778 ArrayDataBuilder::new(R::DATA_TYPE)
779 .len(physical_length)
780 .add_buffer(builder.finish())
781 .build_unchecked()
782 };
783
784 // build new values by slicing physical indices.
785 let new_values = run_array
786 .values()
787 .slice(start_physical_index, physical_length)
788 .into_data();
789
790 let builder = ArrayDataBuilder::new(run_array.data_type().clone())
791 .len(run_array.len())
792 .add_child_data(new_run_ends)
793 .add_child_data(new_values);
794 let array_data = unsafe {
795 // Safety:
796 // This function builds a valid run array and hence can skip validation.
797 builder.build_unchecked()
798 };
799 Ok(array_data.into())
800}
801
802/// Controls how dictionaries are handled in Arrow IPC messages
803#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]

Callers 2

unslice_run_arrayFunction · 0.85
test_run_array_unsliceFunction · 0.85

Calls 15

run_endsMethod · 0.80
add_bufferMethod · 0.80
add_child_dataMethod · 0.80
offsetMethod · 0.45
max_valueMethod · 0.45
lenMethod · 0.45
valuesMethod · 0.45
appendMethod · 0.45
build_uncheckedMethod · 0.45
finishMethod · 0.45

Tested by 1

test_run_array_unsliceFunction · 0.68