MCPcopy Create free account
hub / github.com/apache/arrow / ReadSparseCSXIndex

Function ReadSparseCSXIndex

cpp/src/arrow/ipc/reader.cc:2371–2439  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2369}
2370
2371Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex(
2372 const flatbuf::SparseTensor* sparse_tensor, const std::vector<int64_t>& shape,
2373 int64_t non_zero_length, io::RandomAccessFile* file) {
2374 if (shape.size() != 2) {
2375 return Status::Invalid("Invalid shape length for a sparse matrix");
2376 }
2377
2378 auto* sparse_index = sparse_tensor->sparseIndex_as_SparseMatrixIndexCSX();
2379
2380 std::shared_ptr<DataType> indptr_type, indices_type;
2381 RETURN_NOT_OK(
2382 internal::GetSparseCSXIndexMetadata(sparse_index, &indptr_type, &indices_type));
2383 const int indptr_byte_width = indptr_type->byte_width();
2384
2385 auto* indptr_buffer = sparse_index->indptrBuffer();
2386 ARROW_ASSIGN_OR_RAISE(auto indptr_data,
2387 file->ReadAt(indptr_buffer->offset(), indptr_buffer->length(),
2388 /*allow_short_read=*/false));
2389
2390 auto* indices_buffer = sparse_index->indicesBuffer();
2391 ARROW_ASSIGN_OR_RAISE(auto indices_data,
2392 file->ReadAt(indices_buffer->offset(), indices_buffer->length(),
2393 /*allow_short_read=*/false));
2394
2395 std::vector<int64_t> indices_shape({non_zero_length});
2396 const auto indices_minimum_bytes =
2397 MultiplyWithOverflow<int64_t>({indices_shape[0], indices_type->byte_width()});
2398 if (!indices_minimum_bytes.has_value() ||
2399 indices_minimum_bytes.value() > indices_buffer->length()) {
2400 return Status::Invalid("shape is inconsistent to the size of indices buffer");
2401 }
2402
2403 switch (sparse_index->compressedAxis()) {
2404 case flatbuf::SparseMatrixCompressedAxis::SparseMatrixCompressedAxis_Row: {
2405 const auto indptr_length = AddWithOverflow<int64_t>({shape[0], 1});
2406 if (!indptr_length.has_value()) {
2407 return Status::Invalid("shape is inconsistent to the size of indptr buffer");
2408 }
2409 std::vector<int64_t> indptr_shape({indptr_length.value()});
2410 const auto indptr_minimum_bytes =
2411 MultiplyWithOverflow<int64_t>({indptr_shape[0], indptr_byte_width});
2412 if (!indptr_minimum_bytes.has_value() ||
2413 indptr_minimum_bytes.value() > indptr_buffer->length()) {
2414 return Status::Invalid("shape is inconsistent to the size of indptr buffer");
2415 }
2416 return std::make_shared<SparseCSRIndex>(
2417 std::make_shared<Tensor>(indptr_type, indptr_data, indptr_shape),
2418 std::make_shared<Tensor>(indices_type, indices_data, indices_shape));
2419 }
2420 case flatbuf::SparseMatrixCompressedAxis::SparseMatrixCompressedAxis_Column: {
2421 const auto indptr_length = AddWithOverflow<int64_t>({shape[1], 1});
2422 if (!indptr_length.has_value()) {
2423 return Status::Invalid("shape is inconsistent to the size of indptr buffer");
2424 }
2425 std::vector<int64_t> indptr_shape({indptr_length.value()});
2426 const auto indptr_minimum_bytes =
2427 MultiplyWithOverflow<int64_t>({indptr_shape[0], indptr_byte_width});
2428 if (!indptr_minimum_bytes.has_value() ||

Callers 1

ReadSparseTensorFunction · 0.85

Calls 6

InvalidFunction · 0.50
sizeMethod · 0.45
byte_widthMethod · 0.45
valueMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected