@@ -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
0 commit comments