@@ -1058,6 +1058,35 @@ Result<std::shared_ptr<SparseIndex>> ReadSparseCSXIndex(
10581058 }
10591059}
10601060
1061+ Result<std::shared_ptr<SparseIndex>> ReadSparseCSFIndex (
1062+ const flatbuf::SparseTensor* sparse_tensor, const std::vector<int64_t >& shape,
1063+ io::RandomAccessFile* file) {
1064+ auto * sparse_index = sparse_tensor->sparseIndex_as_SparseTensorIndexCSF ();
1065+ const auto ndim = static_cast <int64_t >(shape.size ());
1066+ auto * indptr_buffers = sparse_index->indptrBuffers ();
1067+ auto * indices_buffers = sparse_index->indicesBuffers ();
1068+ std::vector<std::shared_ptr<Buffer>> indptr_data (ndim - 1 );
1069+ std::vector<std::shared_ptr<Buffer>> indices_data (ndim);
1070+
1071+ std::shared_ptr<DataType> indptr_type, indices_type;
1072+ std::vector<int64_t > axis_order, indices_size;
1073+
1074+ RETURN_NOT_OK (internal::GetSparseCSFIndexMetadata (
1075+ sparse_index, &axis_order, &indices_size, &indptr_type, &indices_type));
1076+ for (int i = 0 ; i < static_cast <int >(indptr_buffers->Length ()); ++i) {
1077+ ARROW_ASSIGN_OR_RAISE (indptr_data[i], file->ReadAt (indptr_buffers->Get (i)->offset (),
1078+ indptr_buffers->Get (i)->length ()));
1079+ }
1080+ for (int i = 0 ; i < static_cast <int >(indices_buffers->Length ()); ++i) {
1081+ ARROW_ASSIGN_OR_RAISE (indices_data[i],
1082+ file->ReadAt (indices_buffers->Get (i)->offset (),
1083+ indices_buffers->Get (i)->length ()));
1084+ }
1085+
1086+ return SparseCSFIndex::Make (indptr_type, indices_type, indices_size, axis_order,
1087+ indptr_data, indices_data);
1088+ }
1089+
10611090Result<std::shared_ptr<SparseTensor>> MakeSparseTensorWithSparseCOOIndex (
10621091 const std::shared_ptr<DataType>& type, const std::vector<int64_t >& shape,
10631092 const std::vector<std::string>& dim_names,
@@ -1082,6 +1111,14 @@ Result<std::shared_ptr<SparseTensor>> MakeSparseTensorWithSparseCSCIndex(
10821111 return SparseCSCMatrix::Make (sparse_index, type, data, shape, dim_names);
10831112}
10841113
1114+ Result<std::shared_ptr<SparseTensor>> MakeSparseTensorWithSparseCSFIndex (
1115+ const std::shared_ptr<DataType>& type, const std::vector<int64_t >& shape,
1116+ const std::vector<std::string>& dim_names,
1117+ const std::shared_ptr<SparseCSFIndex>& sparse_index,
1118+ const std::shared_ptr<Buffer>& data) {
1119+ return SparseCSFTensor::Make (sparse_index, type, data, shape, dim_names);
1120+ }
1121+
10851122Status ReadSparseTensorMetadata (const Buffer& metadata,
10861123 std::shared_ptr<DataType>* out_type,
10871124 std::vector<int64_t >* out_shape,
@@ -1131,6 +1168,9 @@ Result<size_t> GetSparseTensorBodyBufferCount(SparseTensorFormat::type format_id
11311168 case SparseTensorFormat::CSC :
11321169 return 3 ;
11331170
1171+ case SparseTensorFormat::CSF :
1172+ return 3 ;
1173+
11341174 default :
11351175 return Status::Invalid (" Unrecognized sparse tensor format" );
11361176 }
@@ -1215,6 +1255,33 @@ Result<std::shared_ptr<SparseTensor>> ReadSparseTensorPayload(const IpcPayload&
12151255 return MakeSparseTensorWithSparseCSCIndex (type, shape, dim_names, sparse_index,
12161256 non_zero_length, payload.body_buffers [2 ]);
12171257 }
1258+ case SparseTensorFormat::CSF : {
1259+ std::shared_ptr<SparseCSFIndex> sparse_index;
1260+ std::shared_ptr<DataType> indptr_type, indices_type;
1261+ std::vector<int64_t > axis_order, indices_size;
1262+
1263+ RETURN_NOT_OK (internal::GetSparseCSFIndexMetadata (
1264+ sparse_tensor->sparseIndex_as_SparseTensorIndexCSF (), &axis_order,
1265+ &indices_size, &indptr_type, &indices_type));
1266+ ARROW_CHECK_EQ (indptr_type, indices_type);
1267+
1268+ const int64_t ndim = shape.size ();
1269+ std::vector<std::shared_ptr<Buffer>> indptr_data (ndim - 1 );
1270+ std::vector<std::shared_ptr<Buffer>> indices_data (ndim);
1271+
1272+ for (int64_t i = 0 ; i < ndim - 1 ; ++i) {
1273+ indptr_data[i] = payload.body_buffers [i];
1274+ }
1275+ for (int64_t i = 0 ; i < ndim; ++i) {
1276+ indices_data[i] = payload.body_buffers [i + ndim - 1 ];
1277+ }
1278+
1279+ ARROW_ASSIGN_OR_RAISE (sparse_index,
1280+ SparseCSFIndex::Make (indptr_type, indices_type, indices_size,
1281+ axis_order, indptr_data, indices_data));
1282+ return MakeSparseTensorWithSparseCSFIndex (type, shape, dim_names, sparse_index,
1283+ payload.body_buffers [2 * ndim - 1 ]);
1284+ }
12181285 default :
12191286 return Status::Invalid (" Unsupported sparse index format" );
12201287 }
@@ -1261,6 +1328,12 @@ Result<std::shared_ptr<SparseTensor>> ReadSparseTensor(const Buffer& metadata,
12611328 type, shape, dim_names, checked_pointer_cast<SparseCSCIndex>(sparse_index),
12621329 non_zero_length, data);
12631330 }
1331+ case SparseTensorFormat::CSF : {
1332+ ARROW_ASSIGN_OR_RAISE (sparse_index, ReadSparseCSFIndex (sparse_tensor, shape, file));
1333+ return MakeSparseTensorWithSparseCSFIndex (
1334+ type, shape, dim_names, checked_pointer_cast<SparseCSFIndex>(sparse_index),
1335+ data);
1336+ }
12641337 default :
12651338 return Status::Invalid (" Unsupported sparse index format" );
12661339 }
0 commit comments