Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions cpp/src/arrow/sparse_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,10 @@ SparseCSFIndex::SparseCSFIndex(const std::vector<std::shared_ptr<Tensor>>& indpt
std::string SparseCSFIndex::ToString() const { return std::string("SparseCSFIndex"); }

bool SparseCSFIndex::Equals(const SparseCSFIndex& other) const {
for (int64_t i = 0; i < static_cast<int64_t>(indices().size()); ++i) {
if (!indices()[i]->Equals(*other.indices()[i])) return false;
}
for (int64_t i = 0; i < static_cast<int64_t>(indptr().size()); ++i) {
if (!indptr()[i]->Equals(*other.indptr()[i])) return false;
}
return axis_order() == other.axis_order();
auto eq = [](const auto& a, const auto& b) { return a->Equals(*b); };
return axis_order() == other.axis_order() &&
std::ranges::equal(indices(), other.indices(), eq) &&
std::ranges::equal(indptr(), other.indptr(), eq);
}

// ----------------------------------------------------------------------
Expand Down
24 changes: 23 additions & 1 deletion cpp/src/arrow/sparse_tensor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1641,10 +1641,32 @@ TYPED_TEST_P(TestSparseCSFTensorForIndexValueType, TestNonAscendingShape) {
ASSERT_TRUE(st->Equals(*sparse_tensor));
}

TYPED_TEST_P(TestSparseCSFTensorForIndexValueType, TestEqualityMismatchedDimensions) {
using IndexValueType = TypeParam;
using c_index_value_type = typename IndexValueType::c_type;

// 2D vs 3D - comparing indices with different dimensionality
// 2D CSF: ndim=2, so indptr.size()=1, indices.size()=2
std::vector<int64_t> axis_order_2D = {0, 1};
std::vector<std::vector<c_index_value_type>> indptr_2D = {{0, 1}};
std::vector<std::vector<c_index_value_type>> indices_2D = {{0}, {0}};
auto si_2D = this->MakeSparseCSFIndex(axis_order_2D, indptr_2D, indices_2D);

// 3D CSF: ndim=3, so indptr.size()=2, indices.size()=3
std::vector<int64_t> axis_order_3D = {0, 1, 2};
std::vector<std::vector<c_index_value_type>> indptr_3D = {{0, 1}, {0, 1}};
std::vector<std::vector<c_index_value_type>> indices_3D = {{0}, {0}, {0}};
auto si_3D = this->MakeSparseCSFIndex(axis_order_3D, indptr_3D, indices_3D);

ASSERT_FALSE(si_2D->Equals(*si_3D));
ASSERT_FALSE(si_3D->Equals(*si_2D));
ASSERT_TRUE(si_2D->Equals(*si_2D));
}

REGISTER_TYPED_TEST_SUITE_P(TestSparseCSFTensorForIndexValueType, TestCreateSparseTensor,
TestTensorToSparseTensor, TestSparseTensorToTensor,
TestAlternativeAxisOrder, TestNonAscendingShape,
TestRoundTrip);
TestRoundTrip, TestEqualityMismatchedDimensions);

INSTANTIATE_TYPED_TEST_SUITE_P(TestInt8, TestSparseCSFTensorForIndexValueType, Int8Type);
INSTANTIATE_TYPED_TEST_SUITE_P(TestUInt8, TestSparseCSFTensorForIndexValueType,
Expand Down
Loading