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

Function FlattenListArray

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

Source from the content-addressed store, hash-verified

221
222template <typename ListArrayT>
223Result<std::shared_ptr<Array>> FlattenListArray(const ListArrayT& list_array,
224 MemoryPool* memory_pool) {
225 const int64_t list_array_length = list_array.length();
226 std::shared_ptr<arrow::Array> value_array = list_array.values();
227
228 // Shortcut: if a ListArray does not contain nulls, then simply slice its
229 // value array with the first and the last offsets.
230 if (list_array.null_count() == 0) {
231 return SliceArrayWithOffsets(*value_array, list_array.value_offset(0),
232 list_array.value_offset(list_array_length));
233 }
234
235 // Second shortcut: if the list array is *all* nulls, then just return
236 // an empty array.
237 if (list_array.null_count() == list_array.length()) {
238 return MakeEmptyArray(value_array->type(), memory_pool);
239 }
240
241 // The ListArray contains nulls: there may be a non-empty sub-list behind
242 // a null and it must not be contained in the result.
243 std::vector<std::shared_ptr<Array>> non_null_fragments;
244 int64_t valid_begin = 0;
245 while (valid_begin < list_array_length) {
246 int64_t valid_end = valid_begin;
247 while (valid_end < list_array_length &&
248 (list_array.IsValid(valid_end) || list_array.value_length(valid_end) == 0)) {
249 ++valid_end;
250 }
251 if (valid_begin < valid_end) {
252 non_null_fragments.push_back(
253 SliceArrayWithOffsets(*value_array, list_array.value_offset(valid_begin),
254 list_array.value_offset(valid_end)));
255 }
256 valid_begin = valid_end + 1; // skip null entry
257 }
258
259 // Final attempt to avoid invoking Concatenate().
260 if (non_null_fragments.size() == 1) {
261 return non_null_fragments[0];
262 } else if (non_null_fragments.size() == 0) {
263 return MakeEmptyArray(value_array->type(), memory_pool);
264 }
265
266 return Concatenate(non_null_fragments, memory_pool);
267}
268
269template <typename ListViewArrayT, bool HasNulls>
270Result<std::shared_ptr<Array>> FlattenListViewArray(const ListViewArrayT& list_view_array,

Callers 1

FlattenMethod · 0.85

Calls 12

SliceArrayWithOffsetsFunction · 0.85
ConcatenateFunction · 0.85
push_backMethod · 0.80
MakeEmptyArrayFunction · 0.70
lengthMethod · 0.45
valuesMethod · 0.45
null_countMethod · 0.45
value_offsetMethod · 0.45
typeMethod · 0.45
IsValidMethod · 0.45
value_lengthMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected