| 1188 | } |
| 1189 | |
| 1190 | Result<std::shared_ptr<Array>> StructArray::GetFlattenedField(int index, |
| 1191 | MemoryPool* pool) const { |
| 1192 | std::shared_ptr<Buffer> null_bitmap = data_->buffers[0]; |
| 1193 | |
| 1194 | auto child_data = data_->child_data[index]->Copy(); |
| 1195 | |
| 1196 | std::shared_ptr<Buffer> flattened_null_bitmap; |
| 1197 | int64_t flattened_null_count = kUnknownNullCount; |
| 1198 | |
| 1199 | // Push any non-trivial slicing on the parent to the child |
| 1200 | // (including cases with offset = 0) |
| 1201 | if (data_->offset != 0 || data_->length != child_data->length) { |
| 1202 | child_data = child_data->Slice(data_->offset, data_->length); |
| 1203 | } |
| 1204 | std::shared_ptr<Buffer> child_null_bitmap = child_data->buffers[0]; |
| 1205 | const int64_t child_offset = child_data->offset; |
| 1206 | |
| 1207 | // The validity of a flattened datum is the logical AND of the struct |
| 1208 | // element's validity and the individual field element's validity. |
| 1209 | if (null_bitmap && child_null_bitmap) { |
| 1210 | ARROW_ASSIGN_OR_RAISE( |
| 1211 | flattened_null_bitmap, |
| 1212 | BitmapAnd(pool, child_null_bitmap->data(), child_offset, null_bitmap_data_, |
| 1213 | data_->offset, data_->length, child_offset)); |
| 1214 | } else if (child_null_bitmap) { |
| 1215 | flattened_null_bitmap = child_null_bitmap; |
| 1216 | flattened_null_count = child_data->null_count; |
| 1217 | } else if (null_bitmap) { |
| 1218 | if (child_offset == data_->offset) { |
| 1219 | flattened_null_bitmap = null_bitmap; |
| 1220 | } else { |
| 1221 | // If the child has an offset, need to synthesize a validity |
| 1222 | // buffer with an offset too |
| 1223 | ARROW_ASSIGN_OR_RAISE(flattened_null_bitmap, |
| 1224 | AllocateEmptyBitmap(child_offset + data_->length, pool)); |
| 1225 | CopyBitmap(null_bitmap_data_, data_->offset, data_->length, |
| 1226 | flattened_null_bitmap->mutable_data(), child_offset); |
| 1227 | } |
| 1228 | flattened_null_count = data_->null_count; |
| 1229 | } else { |
| 1230 | flattened_null_count = 0; |
| 1231 | } |
| 1232 | |
| 1233 | auto flattened_data = child_data->Copy(); |
| 1234 | flattened_data->buffers[0] = flattened_null_bitmap; |
| 1235 | flattened_data->null_count = flattened_null_count; |
| 1236 | |
| 1237 | return MakeArray(flattened_data); |
| 1238 | } |
| 1239 | |
| 1240 | // ---------------------------------------------------------------------- |
| 1241 | // UnionArray |