| 2588 | } |
| 2589 | |
| 2590 | Result<std::shared_ptr<SparseTensor>> ReadSparseTensorPayload(const IpcPayload& payload) { |
| 2591 | std::shared_ptr<DataType> type; |
| 2592 | std::vector<int64_t> shape; |
| 2593 | std::vector<std::string> dim_names; |
| 2594 | int64_t non_zero_length; |
| 2595 | SparseTensorFormat::type sparse_tensor_format_id; |
| 2596 | const flatbuf::SparseTensor* sparse_tensor; |
| 2597 | const flatbuf::Buffer* buffer; |
| 2598 | |
| 2599 | RETURN_NOT_OK(ReadSparseTensorMetadata(*payload.metadata, &type, &shape, &dim_names, |
| 2600 | &non_zero_length, &sparse_tensor_format_id, |
| 2601 | &sparse_tensor, &buffer)); |
| 2602 | |
| 2603 | RETURN_NOT_OK(CheckSparseTensorBodyBufferCount(payload, sparse_tensor_format_id, |
| 2604 | static_cast<size_t>(shape.size()))); |
| 2605 | |
| 2606 | switch (sparse_tensor_format_id) { |
| 2607 | case SparseTensorFormat::COO: { |
| 2608 | std::shared_ptr<SparseCOOIndex> sparse_index; |
| 2609 | std::shared_ptr<DataType> indices_type; |
| 2610 | RETURN_NOT_OK(internal::GetSparseCOOIndexMetadata( |
| 2611 | sparse_tensor->sparseIndex_as_SparseTensorIndexCOO(), &indices_type)); |
| 2612 | ARROW_ASSIGN_OR_RAISE(sparse_index, |
| 2613 | SparseCOOIndex::Make(indices_type, shape, non_zero_length, |
| 2614 | payload.body_buffers[0])); |
| 2615 | return MakeSparseTensorWithSparseCOOIndex(type, shape, dim_names, sparse_index, |
| 2616 | non_zero_length, payload.body_buffers[1]); |
| 2617 | } |
| 2618 | case SparseTensorFormat::CSR: { |
| 2619 | std::shared_ptr<SparseCSRIndex> sparse_index; |
| 2620 | std::shared_ptr<DataType> indptr_type; |
| 2621 | std::shared_ptr<DataType> indices_type; |
| 2622 | RETURN_NOT_OK(internal::GetSparseCSXIndexMetadata( |
| 2623 | sparse_tensor->sparseIndex_as_SparseMatrixIndexCSX(), &indptr_type, |
| 2624 | &indices_type)); |
| 2625 | ARROW_CHECK_EQ(indptr_type, indices_type); |
| 2626 | ARROW_ASSIGN_OR_RAISE( |
| 2627 | sparse_index, |
| 2628 | SparseCSRIndex::Make(indices_type, shape, non_zero_length, |
| 2629 | payload.body_buffers[0], payload.body_buffers[1])); |
| 2630 | return MakeSparseTensorWithSparseCSRIndex(type, shape, dim_names, sparse_index, |
| 2631 | non_zero_length, payload.body_buffers[2]); |
| 2632 | } |
| 2633 | case SparseTensorFormat::CSC: { |
| 2634 | std::shared_ptr<SparseCSCIndex> sparse_index; |
| 2635 | std::shared_ptr<DataType> indptr_type; |
| 2636 | std::shared_ptr<DataType> indices_type; |
| 2637 | RETURN_NOT_OK(internal::GetSparseCSXIndexMetadata( |
| 2638 | sparse_tensor->sparseIndex_as_SparseMatrixIndexCSX(), &indptr_type, |
| 2639 | &indices_type)); |
| 2640 | ARROW_CHECK_EQ(indptr_type, indices_type); |
| 2641 | ARROW_ASSIGN_OR_RAISE( |
| 2642 | sparse_index, |
| 2643 | SparseCSCIndex::Make(indices_type, shape, non_zero_length, |
| 2644 | payload.body_buffers[0], payload.body_buffers[1])); |
| 2645 | return MakeSparseTensorWithSparseCSCIndex(type, shape, dim_names, sparse_index, |
| 2646 | non_zero_length, payload.body_buffers[2]); |
| 2647 | } |