| 437 | }; |
| 438 | |
| 439 | Status MakeChunkedArrayBuilder(const std::shared_ptr<TaskGroup>& task_group, |
| 440 | MemoryPool* pool, const PromotionGraph* promotion_graph, |
| 441 | const std::shared_ptr<DataType>& type, |
| 442 | bool allow_promotion, |
| 443 | std::shared_ptr<ChunkedArrayBuilder>* out) { |
| 444 | // If a promotion graph is provided, unexpected fields will be allowed - using the graph |
| 445 | // recursively for itself and any child fields (via the `allow_promotion` parameter). |
| 446 | // Fields provided in the schema will adhere to their corresponding type. However, |
| 447 | // structs defined in the schema may obtain unexpected child fields, which will use the |
| 448 | // promotion graph as well. |
| 449 | // |
| 450 | // If a promotion graph is not provided, unexpected fields are always ignored and |
| 451 | // type inference never occurs. |
| 452 | if (type->id() == Type::STRUCT) { |
| 453 | std::vector<std::pair<std::string, std::shared_ptr<ChunkedArrayBuilder>>> |
| 454 | child_builders; |
| 455 | for (const auto& f : type->fields()) { |
| 456 | std::shared_ptr<ChunkedArrayBuilder> child_builder; |
| 457 | RETURN_NOT_OK(MakeChunkedArrayBuilder(task_group, pool, promotion_graph, f->type(), |
| 458 | allow_promotion, &child_builder)); |
| 459 | child_builders.emplace_back(f->name(), std::move(child_builder)); |
| 460 | } |
| 461 | *out = std::make_shared<ChunkedStructArrayBuilder>(task_group, pool, promotion_graph, |
| 462 | std::move(child_builders)); |
| 463 | return Status::OK(); |
| 464 | } |
| 465 | if (type->id() == Type::LIST) { |
| 466 | const auto& list_type = checked_cast<const ListType&>(*type); |
| 467 | std::shared_ptr<ChunkedArrayBuilder> value_builder; |
| 468 | RETURN_NOT_OK(MakeChunkedArrayBuilder(task_group, pool, promotion_graph, |
| 469 | list_type.value_type(), allow_promotion, |
| 470 | &value_builder)); |
| 471 | *out = std::make_shared<ChunkedListArrayBuilder>( |
| 472 | task_group, pool, std::move(value_builder), list_type.value_field()); |
| 473 | return Status::OK(); |
| 474 | } |
| 475 | |
| 476 | // Construct the "leaf" builder |
| 477 | std::shared_ptr<Converter> converter; |
| 478 | RETURN_NOT_OK(MakeConverter(type, pool, &converter)); |
| 479 | if (allow_promotion && promotion_graph) { |
| 480 | *out = std::make_shared<InferringChunkedArrayBuilder>(task_group, promotion_graph, |
| 481 | std::move(converter)); |
| 482 | } else { |
| 483 | *out = std::make_shared<TypedChunkedArrayBuilder>(task_group, std::move(converter)); |
| 484 | } |
| 485 | return Status::OK(); |
| 486 | } |
| 487 | |
| 488 | } // namespace |
| 489 | |