| 592 | |
| 593 | template <typename IndexType> |
| 594 | class ListReader : public ColumnReaderImpl { |
| 595 | public: |
| 596 | ListReader(std::shared_ptr<ReaderContext> ctx, std::shared_ptr<Field> field, |
| 597 | ::parquet::internal::LevelInfo level_info, |
| 598 | std::unique_ptr<ColumnReaderImpl> child_reader) |
| 599 | : ctx_(std::move(ctx)), |
| 600 | field_(std::move(field)), |
| 601 | level_info_(level_info), |
| 602 | item_reader_(std::move(child_reader)) {} |
| 603 | |
| 604 | Status GetDefLevels(const int16_t** data, int64_t* length) override { |
| 605 | return item_reader_->GetDefLevels(data, length); |
| 606 | } |
| 607 | |
| 608 | Status GetRepLevels(const int16_t** data, int64_t* length) override { |
| 609 | return item_reader_->GetRepLevels(data, length); |
| 610 | } |
| 611 | |
| 612 | bool IsOrHasRepeatedChild() const final { return true; } |
| 613 | |
| 614 | Status LoadBatch(int64_t number_of_records) final { |
| 615 | return item_reader_->LoadBatch(number_of_records); |
| 616 | } |
| 617 | |
| 618 | virtual ::arrow::Result<std::shared_ptr<ChunkedArray>> AssembleArray( |
| 619 | std::shared_ptr<ArrayData> data) { |
| 620 | if (field_->type()->id() == ::arrow::Type::MAP) { |
| 621 | // Error out if data is not map-compliant instead of aborting in MakeArray below |
| 622 | RETURN_NOT_OK(::arrow::MapArray::ValidateChildData(data->child_data)); |
| 623 | } |
| 624 | std::shared_ptr<Array> result = ::arrow::MakeArray(data); |
| 625 | return std::make_shared<ChunkedArray>(result); |
| 626 | } |
| 627 | |
| 628 | Status BuildArray(int64_t length_upper_bound, |
| 629 | std::shared_ptr<ChunkedArray>* out) override { |
| 630 | const int16_t* def_levels; |
| 631 | const int16_t* rep_levels; |
| 632 | int64_t num_levels; |
| 633 | RETURN_NOT_OK(item_reader_->GetDefLevels(&def_levels, &num_levels)); |
| 634 | RETURN_NOT_OK(item_reader_->GetRepLevels(&rep_levels, &num_levels)); |
| 635 | |
| 636 | std::shared_ptr<ResizableBuffer> validity_buffer; |
| 637 | ::parquet::internal::ValidityBitmapInputOutput validity_io; |
| 638 | validity_io.values_read_upper_bound = length_upper_bound; |
| 639 | if (field_->nullable()) { |
| 640 | ARROW_ASSIGN_OR_RAISE(validity_buffer, |
| 641 | AllocateResizableBuffer( |
| 642 | bit_util::BytesForBits(length_upper_bound), ctx_->pool)); |
| 643 | validity_io.valid_bits = validity_buffer->mutable_data(); |
| 644 | } |
| 645 | ARROW_ASSIGN_OR_RAISE( |
| 646 | std::shared_ptr<ResizableBuffer> offsets_buffer, |
| 647 | AllocateResizableBuffer( |
| 648 | sizeof(IndexType) * std::max(int64_t{1}, length_upper_bound + 1), |
| 649 | ctx_->pool)); |
| 650 | // Ensure zero initialization in case we have reached a zero length list (and |
| 651 | // because first entry is always zero). |