Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 6ceb406

Browse files
committed
Implementing review feedback.
1 parent bd0d8c2 commit 6ceb406

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

cpp/src/arrow/sparse_tensor.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,24 @@ class SparseTensorConverter<TYPE, SparseCSFIndex>
442442
using c_index_value_type = typename IndexValueType::c_type;
443443
RETURN_NOT_OK(CheckMaximumValue(std::numeric_limits<c_index_value_type>::max()));
444444

445+
const int64_t ndim = tensor_.ndim();
446+
if (ndim < 2) {
447+
// LCOV_EXCL_START: The following invalid causes program failure.
448+
return Status::Invalid("Invalid tensor dimension");
449+
// LCOV_EXCL_STOP
450+
}
451+
445452
std::shared_ptr<SparseCOOTensor> sparse_coo_tensor;
446453
ARROW_ASSIGN_OR_RAISE(sparse_coo_tensor, SparseCOOTensor::Make(tensor_));
447454
std::shared_ptr<Tensor> coords =
448455
arrow::internal::checked_pointer_cast<SparseCOOIndex>(
449456
sparse_coo_tensor->sparse_index())
450457
->indices();
451458

459+
// TODO(rok): Coords should be sorted with axis_order priority to improve compression.
460+
// ARROW-4221 would help here as well.
461+
452462
// Convert SparseCOOTensor to long CSF buffers
453-
const int64_t ndim = tensor_.ndim();
454463
const int64_t nonzero_count = sparse_coo_tensor->non_zero_length();
455464

456465
std::vector<int64_t> counts(ndim);
@@ -939,18 +948,18 @@ inline Status CheckSparseCSFIndexValidity(const std::shared_ptr<DataType>& indpt
939948
const std::vector<int64_t>& indices_shape,
940949
const int64_t axis_order_size) {
941950
if (!is_integer(indptr_type->id())) {
942-
return Status::Invalid("Type of SparseCSFIndex indptr must be integer");
951+
return Status::TypeError("Type of SparseCSFIndex indptr must be integer");
943952
}
944953
if (!is_integer(indices_type->id())) {
945-
return Status::Invalid("Type of SparseCSFIndex indices must be integer");
954+
return Status::TypeError("Type of SparseCSFIndex indices must be integer");
946955
}
947956
if (num_indptrs + 1 != num_indices) {
948957
return Status::Invalid(
949-
"SparseCSFIndex length indices must be equal to length inptrs plus one.");
958+
"Length of indices must be equal to length of inptrs + 1 for SparseCSFIndex.");
950959
}
951960
if (axis_order_size != num_indices) {
952961
return Status::Invalid(
953-
"SparseCSFIndex length of indices must be equal number of dimensions.");
962+
"Length of indices must be equal number of dimensions for SparseCSFIndex.");
954963
}
955964
return Status::OK();
956965
}
@@ -970,11 +979,15 @@ Result<std::shared_ptr<SparseCSFIndex>> SparseCSFIndex::Make(
970979
for (int64_t i = 0; i < ndim - 1; ++i)
971980
indptr[i] = std::make_shared<Tensor>(indptr_type, indptr_data[i],
972981
std::vector<int64_t>({indices_shapes[i] + 1}));
973-
974982
for (int64_t i = 0; i < ndim; ++i)
975983
indices[i] = std::make_shared<Tensor>(indices_type, indices_data[i],
976984
std::vector<int64_t>({indices_shapes[i]}));
977985

986+
ARROW_CHECK(CheckSparseCSFIndexValidity(indptr_type, indices_type, indptr.size(),
987+
indices.size(), indptr.back()->shape(),
988+
indices.back()->shape(), axis_order.size())
989+
.ok());
990+
978991
return std::make_shared<SparseCSFIndex>(indptr, indices, axis_order);
979992
}
980993

cpp/src/arrow/sparse_tensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class ARROW_EXPORT SparseCSCIndex
347347
class ARROW_EXPORT SparseCSFIndex : public internal::SparseIndexBase<SparseCSFIndex> {
348348
public:
349349
static constexpr SparseTensorFormat::type format_id = SparseTensorFormat::CSF;
350+
static constexpr char const* kTypeName = "SparseCSFIndex";
350351

351352
/// \brief Make SparseCSFIndex from raw properties
352353
static Result<std::shared_ptr<SparseCSFIndex>> Make(

0 commit comments

Comments
 (0)