MCPcopy Create free account
hub / github.com/apache/arrow / FlattenListViewArray

Function FlattenListViewArray

cpp/src/arrow/array/array_nested.cc:270–355  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

268
269template <typename ListViewArrayT, bool HasNulls>
270Result<std::shared_ptr<Array>> FlattenListViewArray(const ListViewArrayT& list_view_array,
271 MemoryPool* memory_pool) {
272 using offset_type = typename ListViewArrayT::offset_type;
273 const int64_t list_view_array_offset = list_view_array.offset();
274 const int64_t list_view_array_length = list_view_array.length();
275 std::shared_ptr<arrow::Array> value_array = list_view_array.values();
276
277 if (list_view_array_length == 0) {
278 return SliceArrayWithOffsets(*value_array, 0, 0);
279 }
280
281 // If the list array is *all* nulls, then just return an empty array.
282 if constexpr (HasNulls) {
283 if (list_view_array.null_count() == list_view_array.length()) {
284 return MakeEmptyArray(value_array->type(), memory_pool);
285 }
286 }
287
288 const auto* validity = list_view_array.data()->template GetValues<uint8_t>(0, 0);
289 const auto* offsets = list_view_array.data()->template GetValues<offset_type>(1);
290 const auto* sizes = list_view_array.data()->template GetValues<offset_type>(2);
291
292 auto is_null_or_empty = [&](int64_t i) {
293 if (HasNulls && !bit_util::GetBit(validity, list_view_array_offset + i)) {
294 return true;
295 }
296 return sizes[i] == 0;
297 };
298
299 // Index of the first valid, non-empty list-view.
300 int64_t first_i = 0;
301 for (; first_i < list_view_array_length; first_i++) {
302 if (!is_null_or_empty(first_i)) {
303 break;
304 }
305 }
306 // If all list-views are empty, return an empty array.
307 if (first_i == list_view_array_length) {
308 return MakeEmptyArray(value_array->type(), memory_pool);
309 }
310
311 std::vector<std::shared_ptr<Array>> slices;
312 {
313 int64_t i = first_i;
314 auto begin_offset = offsets[i];
315 auto end_offset = offsets[i] + sizes[i];
316 i += 1;
317 // Inductive invariant: slices and the always non-empty values slice
318 // [begin_offset, end_offset) contains all the maximally contiguous slices of the
319 // values array that are covered by all the list-views before list-view i.
320 for (; i < list_view_array_length; i++) {
321 if (is_null_or_empty(i)) {
322 // The invariant is preserved by simply preserving the current set of slices.
323 } else {
324 if (offsets[i] == end_offset) {
325 end_offset += sizes[i];
326 // The invariant is preserved because since the non-empty list-view i
327 // starts at end_offset, the current range can be extended to end at

Callers

nothing calls this directly

Calls 12

SliceArrayWithOffsetsFunction · 0.85
ConcatenateFunction · 0.85
push_backMethod · 0.80
MakeEmptyArrayFunction · 0.70
GetBitFunction · 0.50
offsetMethod · 0.45
lengthMethod · 0.45
valuesMethod · 0.45
null_countMethod · 0.45
typeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected