| 1148 | } |
| 1149 | |
| 1150 | Result<std::shared_ptr<Array>> StructArray::GetFlattenedField(int index, |
| 1151 | MemoryPool* pool) const { |
| 1152 | std::shared_ptr<Buffer> null_bitmap = data_->buffers[0]; |
| 1153 | |
| 1154 | auto child_data = data_->child_data[index]->Copy(); |
| 1155 | |
| 1156 | std::shared_ptr<Buffer> flattened_null_bitmap; |
| 1157 | int64_t flattened_null_count = kUnknownNullCount; |
| 1158 | |
| 1159 | // Push any non-trivial slicing on the parent to the child |
| 1160 | // (including cases with offset = 0) |
| 1161 | if (data_->offset != 0 || data_->length != child_data->length) { |
| 1162 | child_data = child_data->Slice(data_->offset, data_->length); |
| 1163 | } |
| 1164 | std::shared_ptr<Buffer> child_null_bitmap = child_data->buffers[0]; |
| 1165 | const int64_t child_offset = child_data->offset; |
| 1166 | |
| 1167 | // The validity of a flattened datum is the logical AND of the struct |
| 1168 | // element's validity and the individual field element's validity. |
| 1169 | if (null_bitmap && child_null_bitmap) { |
| 1170 | ARROW_ASSIGN_OR_RAISE( |
| 1171 | flattened_null_bitmap, |
| 1172 | BitmapAnd(pool, child_null_bitmap->data(), child_offset, null_bitmap_data_, |
| 1173 | data_->offset, data_->length, child_offset)); |
| 1174 | } else if (child_null_bitmap) { |
| 1175 | flattened_null_bitmap = child_null_bitmap; |
| 1176 | flattened_null_count = child_data->null_count; |
| 1177 | } else if (null_bitmap) { |
| 1178 | if (child_offset == data_->offset) { |
| 1179 | flattened_null_bitmap = null_bitmap; |
| 1180 | } else { |
| 1181 | // If the child has an offset, need to synthesize a validity |
| 1182 | // buffer with an offset too |
| 1183 | ARROW_ASSIGN_OR_RAISE(flattened_null_bitmap, |
| 1184 | AllocateEmptyBitmap(child_offset + data_->length, pool)); |
| 1185 | CopyBitmap(null_bitmap_data_, data_->offset, data_->length, |
| 1186 | flattened_null_bitmap->mutable_data(), child_offset); |
| 1187 | } |
| 1188 | flattened_null_count = data_->null_count; |
| 1189 | } else { |
| 1190 | flattened_null_count = 0; |
| 1191 | } |
| 1192 | |
| 1193 | auto flattened_data = child_data->Copy(); |
| 1194 | flattened_data->buffers[0] = flattened_null_bitmap; |
| 1195 | flattened_data->null_count = flattened_null_count; |
| 1196 | |
| 1197 | return MakeArray(flattened_data); |
| 1198 | } |
| 1199 | |
| 1200 | // ---------------------------------------------------------------------- |
| 1201 | // UnionArray |