lidavidm commented on a change in pull request #11466: URL: https://github.com/apache/arrow/pull/11466#discussion_r741077193
########## File path: cpp/src/arrow/array/array_nested.cc ########## @@ -541,56 +541,62 @@ std::shared_ptr<Array> StructArray::GetFieldByName(const std::string& name) cons Result<ArrayVector> StructArray::Flatten(MemoryPool* pool) const { ArrayVector flattened; - flattened.reserve(data_->child_data.size()); + flattened.resize(data_->child_data.size()); std::shared_ptr<Buffer> null_bitmap = data_->buffers[0]; - for (const auto& child_data_ptr : data_->child_data) { - auto child_data = child_data_ptr->Copy(); + for (int i = 0; static_cast<size_t>(i) < data_->child_data.size(); i++) { + ARROW_ASSIGN_OR_RAISE(flattened[i], Flatten(i, pool)); + } - std::shared_ptr<Buffer> flattened_null_bitmap; - int64_t flattened_null_count = kUnknownNullCount; + return flattened; +} - // Need to adjust for parent offset - if (data_->offset != 0 || data_->length != child_data->length) { - child_data = child_data->Slice(data_->offset, data_->length); - } - std::shared_ptr<Buffer> child_null_bitmap = child_data->buffers[0]; - const int64_t child_offset = child_data->offset; - - // The validity of a flattened datum is the logical AND of the struct - // element's validity and the individual field element's validity. - if (null_bitmap && child_null_bitmap) { - ARROW_ASSIGN_OR_RAISE( - flattened_null_bitmap, - BitmapAnd(pool, child_null_bitmap->data(), child_offset, null_bitmap_data_, - data_->offset, data_->length, child_offset)); - } else if (child_null_bitmap) { - flattened_null_bitmap = child_null_bitmap; - flattened_null_count = child_data->null_count; - } else if (null_bitmap) { - if (child_offset == data_->offset) { - flattened_null_bitmap = null_bitmap; - } else { - // If the child has an offset, need to synthesize a validity - // buffer with an offset too - ARROW_ASSIGN_OR_RAISE(flattened_null_bitmap, - AllocateEmptyBitmap(child_offset + data_->length, pool)); - CopyBitmap(null_bitmap_data_, data_->offset, data_->length, - flattened_null_bitmap->mutable_data(), child_offset); - } - flattened_null_count = data_->null_count; - } else { - flattened_null_count = 0; - } +Result<std::shared_ptr<Array>> StructArray::Flatten(int index, MemoryPool* pool) const { + std::shared_ptr<Buffer> null_bitmap = data_->buffers[0]; + + auto child_data = data_->child_data[index]->Copy(); Review comment: Shallow copy. https://github.com/apache/arrow/blob/a0c650415bc28920512077faecdfa9d07d3c4efe/cpp/src/arrow/array/data.h#L164 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org