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

Function ReadSparseCOOIndex

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

Source from the content-addressed store, hash-verified

2329}
2330
2331Result<std::shared_ptr<SparseIndex>> ReadSparseCOOIndex(
2332 const flatbuf::SparseTensor* sparse_tensor, const std::vector<int64_t>& shape,
2333 int64_t non_zero_length, io::RandomAccessFile* file) {
2334 auto* sparse_index = sparse_tensor->sparseIndex_as_SparseTensorIndexCOO();
2335 const auto ndim = static_cast<int64_t>(shape.size());
2336
2337 std::shared_ptr<DataType> indices_type;
2338 RETURN_NOT_OK(internal::GetSparseCOOIndexMetadata(sparse_index, &indices_type));
2339 const int64_t indices_elsize = indices_type->byte_width();
2340
2341 auto* indices_buffer = sparse_index->indicesBuffer();
2342 ARROW_ASSIGN_OR_RAISE(auto indices_data,
2343 file->ReadAt(indices_buffer->offset(), indices_buffer->length(),
2344 /*allow_short_read=*/false));
2345 std::vector<int64_t> indices_shape({non_zero_length, ndim});
2346 int64_t indices_minimum_bytes;
2347 if (MultiplyWithOverflow(non_zero_length, ndim, &indices_minimum_bytes) ||
2348 MultiplyWithOverflow(indices_minimum_bytes, indices_elsize,
2349 &indices_minimum_bytes) ||
2350 indices_minimum_bytes > indices_buffer->length()) {
2351 return Status::Invalid("shape is inconsistent to the size of indices buffer");
2352 }
2353 auto* indices_strides = sparse_index->indicesStrides();
2354 std::vector<int64_t> strides(2);
2355 if (indices_strides && indices_strides->size() > 0) {
2356 if (indices_strides->size() != 2) {
2357 return Status::Invalid("Wrong size for indicesStrides in SparseCOOIndex");
2358 }
2359 strides[0] = indices_strides->Get(0);
2360 strides[1] = indices_strides->Get(1);
2361 } else {
2362 // Row-major by default
2363 strides[0] = indices_elsize * ndim;
2364 strides[1] = indices_elsize;
2365 }
2366 return SparseCOOIndex::Make(
2367 std::make_shared<Tensor>(indices_type, indices_data, indices_shape, strides),
2368 sparse_index->isCanonical());
2369}
2370
2371Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex(
2372 const flatbuf::SparseTensor* sparse_tensor, const std::vector<int64_t>& shape,

Callers 1

ReadSparseTensorFunction · 0.85

Calls 8

MultiplyWithOverflowFunction · 0.85
InvalidFunction · 0.50
MakeFunction · 0.50
sizeMethod · 0.45
byte_widthMethod · 0.45
lengthMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected