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