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

Method new

arrow-data/src/ffi.rs:131–220  ·  view source on GitHub ↗

creates a new `FFI_ArrowArray` from existing data.

(data: &ArrayData)

Source from the content-addressed store, hash-verified

129impl FFI_ArrowArray {
130 /// creates a new `FFI_ArrowArray` from existing data.
131 pub fn new(data: &ArrayData) -> Self {
132 let data_layout = layout(data.data_type());
133
134 let mut buffers = if data_layout.can_contain_null_mask {
135 // * insert the null buffer at the start
136 // * make all others `Option<Buffer>`.
137 std::iter::once(align_nulls(data.offset(), data.nulls()))
138 .chain(data.buffers().iter().map(|b| Some(b.clone())))
139 .collect::<Vec<_>>()
140 } else {
141 data.buffers().iter().map(|b| Some(b.clone())).collect()
142 };
143
144 // `n_buffers` is the number of buffers by the spec.
145 let mut n_buffers = {
146 data_layout.buffers.len() + {
147 // If the layout has a null buffer by Arrow spec.
148 // Note that even the array doesn't have a null buffer because it has
149 // no null value, we still need to count 1 here to follow the spec.
150 usize::from(data_layout.can_contain_null_mask)
151 }
152 } as i64;
153
154 if data_layout.variadic {
155 // Save the lengths of all variadic buffers into a new buffer.
156 // The first buffer is `views`, and the rest are variadic.
157 let mut data_buffers_lengths = Vec::new();
158 for buffer in data.buffers().iter().skip(1) {
159 data_buffers_lengths.push(buffer.len() as i64);
160 n_buffers += 1;
161 }
162
163 buffers.push(Some(ScalarBuffer::from(data_buffers_lengths).into_inner()));
164 n_buffers += 1;
165 }
166
167 let buffers_ptr = buffers
168 .iter()
169 .flat_map(|maybe_buffer| match maybe_buffer {
170 Some(b) => Some(b.as_ptr() as *const c_void),
171 // This is for null buffer. We only put a null pointer for
172 // null buffer if by spec it can contain null mask.
173 None if data_layout.can_contain_null_mask => Some(std::ptr::null()),
174 None => None,
175 })
176 .collect::<Box<[_]>>();
177
178 let empty = vec![];
179 let (child_data, dictionary) = match data.data_type() {
180 DataType::Dictionary(_, _) => (
181 empty.as_slice(),
182 Box::into_raw(Box::new(FFI_ArrowArray::new(&data.child_data()[0]))),
183 ),
184 _ => (data.child_data(), std::ptr::null_mut()),
185 };
186
187 let children = child_data
188 .iter()

Callers

nothing calls this directly

Calls 15

layoutFunction · 0.85
align_nullsFunction · 0.85
collectMethod · 0.80
child_dataMethod · 0.80
as_mut_ptrMethod · 0.80
data_typeMethod · 0.45
offsetMethod · 0.45
nullsMethod · 0.45
iterMethod · 0.45
buffersMethod · 0.45
cloneMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected