| 328 | |
| 329 | struct CastStruct { |
| 330 | static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { |
| 331 | static constexpr int kFillNullSentinel = -2; |
| 332 | |
| 333 | const CastOptions& options = CastState::Get(ctx); |
| 334 | const auto& in_type = checked_cast<const StructType&>(*batch[0].type()); |
| 335 | const auto& out_type = checked_cast<const StructType&>(*out->type()); |
| 336 | const int in_field_count = in_type.num_fields(); |
| 337 | const int out_field_count = out_type.num_fields(); |
| 338 | |
| 339 | std::vector<int> fields_to_select(out_field_count, -1); |
| 340 | |
| 341 | std::multimap<std::string, int> in_fields; |
| 342 | for (int in_field_index = 0; in_field_index < in_field_count; ++in_field_index) { |
| 343 | in_fields.insert({in_type.field(in_field_index)->name(), in_field_index}); |
| 344 | } |
| 345 | |
| 346 | for (int out_field_index = 0; out_field_index < out_field_count; ++out_field_index) { |
| 347 | const auto& out_field = out_type.field(out_field_index); |
| 348 | |
| 349 | // Take the first field with matching name, if any. Erase it from the map so it |
| 350 | // can't be reused. Use lower_bound (which guarantees first-match) instead of |
| 351 | // find/extract (which do not guarantee first for duplicate keys). |
| 352 | auto it = in_fields.lower_bound(out_field->name()); |
| 353 | if (it != in_fields.end() && it->first == out_field->name()) { |
| 354 | fields_to_select[out_field_index] = it->second; |
| 355 | in_fields.erase(it); |
| 356 | } else if (out_field->nullable()) { |
| 357 | fields_to_select[out_field_index] = kFillNullSentinel; |
| 358 | } else { |
| 359 | return Status::TypeError("struct fields don't match: non-nullable out field `", |
| 360 | out_field->name(), "` not found in in fields ", |
| 361 | in_type.ToString()); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | const ArraySpan& in_array = batch[0].array; |
| 366 | ArrayData* out_array = out->array_data().get(); |
| 367 | |
| 368 | if (in_array.buffers[0].data != nullptr) { |
| 369 | ARROW_ASSIGN_OR_RAISE(out_array->buffers[0], |
| 370 | CopyBitmap(ctx->memory_pool(), in_array.buffers[0].data, |
| 371 | in_array.offset, in_array.length)); |
| 372 | } |
| 373 | |
| 374 | int out_field_index = 0; |
| 375 | for (int in_field_index : fields_to_select) { |
| 376 | const auto& out_field = out_type.field(out_field_index++); |
| 377 | const auto& out_field_type = out_field->type(); |
| 378 | if (in_field_index == kFillNullSentinel) { |
| 379 | ARROW_ASSIGN_OR_RAISE( |
| 380 | auto nulls, MakeArrayOfNull(out_field_type->GetSharedPtr(), batch.length, |
| 381 | ctx->memory_pool())); |
| 382 | out_array->child_data.push_back(nulls->data()); |
| 383 | } else { |
| 384 | const auto& in_field = in_type.field(in_field_index); |
| 385 | const auto& in_values = (in_array.child_data[in_field_index].ToArrayData()->Slice( |
| 386 | in_array.offset, in_array.length)); |
| 387 | if (in_field->nullable() && !out_field->nullable() && |
nothing calls this directly
no test coverage detected