Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cpp/src/arrow/array/array_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1076,20 +1076,25 @@ const ArrayVector& StructArray::fields() const {
return boxed_fields_;
}

const std::shared_ptr<Array>& StructArray::field(int i) const {
std::shared_ptr<Array> StructArray::field(int i) const {
std::shared_ptr<Array> result = std::atomic_load(&boxed_fields_[i]);
if (!result) {
std::shared_ptr<ArrayData> field_data;
if (data_->offset != 0 || data_->child_data[i]->length != data_->length) {
field_data = data_->child_data[i]->Slice(data_->offset, data_->length);
} else {
field_data = data_->child_data[i];
}
result = MakeArray(field_data);
std::atomic_store(&boxed_fields_[i], std::move(result));
return boxed_fields_[i];
if (result) {
return result;
}
return boxed_fields_[i];
std::shared_ptr<ArrayData> field_data;
if (data_->offset != 0 || data_->child_data[i]->length != data_->length) {
field_data = data_->child_data[i]->Slice(data_->offset, data_->length);
} else {
field_data = data_->child_data[i];
}
result = MakeArray(field_data);
// Check if some other thread inserted the array in the meantime and return
// that in that case.
std::shared_ptr<Array> expected = nullptr;
if (!std::atomic_compare_exchange_strong(&boxed_fields_[i], &expected, result)) {
result = std::move(expected);
}
return result;
}

std::shared_ptr<Array> StructArray::GetFieldByName(const std::string& name) const {
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/array/array_nested.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ class ARROW_EXPORT StructArray : public Array {
// Return a shared pointer in case the requestor desires to share ownership
// with this array. The returned array has its offset, length and null
// count adjusted.
const std::shared_ptr<Array>& field(int pos) const;
std::shared_ptr<Array> field(int pos) const;

const ArrayVector& fields() const;

Expand Down
Loading