| 2667 | namespace { |
| 2668 | |
| 2669 | Result<std::shared_ptr<SparseTensor>> ReadSparseTensor(const Buffer& metadata, |
| 2670 | io::RandomAccessFile* file) { |
| 2671 | std::shared_ptr<DataType> type; |
| 2672 | std::vector<int64_t> shape; |
| 2673 | std::vector<std::string> dim_names; |
| 2674 | int64_t non_zero_length; |
| 2675 | SparseTensorFormat::type sparse_tensor_format_id; |
| 2676 | const flatbuf::SparseTensor* sparse_tensor; |
| 2677 | const flatbuf::Buffer* buffer; |
| 2678 | |
| 2679 | RETURN_NOT_OK(ReadSparseTensorMetadata(metadata, &type, &shape, &dim_names, |
| 2680 | &non_zero_length, &sparse_tensor_format_id, |
| 2681 | &sparse_tensor, &buffer)); |
| 2682 | |
| 2683 | ARROW_ASSIGN_OR_RAISE(auto data, file->ReadAt(buffer->offset(), buffer->length(), |
| 2684 | /*allow_short_read=*/false)); |
| 2685 | |
| 2686 | std::shared_ptr<SparseIndex> sparse_index; |
| 2687 | switch (sparse_tensor_format_id) { |
| 2688 | case SparseTensorFormat::COO: { |
| 2689 | ARROW_ASSIGN_OR_RAISE( |
| 2690 | sparse_index, ReadSparseCOOIndex(sparse_tensor, shape, non_zero_length, file)); |
| 2691 | return MakeSparseTensorWithSparseCOOIndex( |
| 2692 | type, shape, dim_names, checked_pointer_cast<SparseCOOIndex>(sparse_index), |
| 2693 | non_zero_length, data); |
| 2694 | } |
| 2695 | case SparseTensorFormat::CSR: { |
| 2696 | ARROW_ASSIGN_OR_RAISE( |
| 2697 | sparse_index, ReadSparseCSXIndex(sparse_tensor, shape, non_zero_length, file)); |
| 2698 | return MakeSparseTensorWithSparseCSRIndex( |
| 2699 | type, shape, dim_names, checked_pointer_cast<SparseCSRIndex>(sparse_index), |
| 2700 | non_zero_length, data); |
| 2701 | } |
| 2702 | case SparseTensorFormat::CSC: { |
| 2703 | ARROW_ASSIGN_OR_RAISE( |
| 2704 | sparse_index, ReadSparseCSXIndex(sparse_tensor, shape, non_zero_length, file)); |
| 2705 | return MakeSparseTensorWithSparseCSCIndex( |
| 2706 | type, shape, dim_names, checked_pointer_cast<SparseCSCIndex>(sparse_index), |
| 2707 | non_zero_length, data); |
| 2708 | } |
| 2709 | case SparseTensorFormat::CSF: { |
| 2710 | ARROW_ASSIGN_OR_RAISE(sparse_index, ReadSparseCSFIndex(sparse_tensor, shape, file)); |
| 2711 | return MakeSparseTensorWithSparseCSFIndex( |
| 2712 | type, shape, dim_names, checked_pointer_cast<SparseCSFIndex>(sparse_index), |
| 2713 | data); |
| 2714 | } |
| 2715 | default: |
| 2716 | return Status::Invalid("Unsupported sparse index format"); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | } // namespace |
| 2721 | |