andishgar commented on issue #47520:
URL: https://github.com/apache/arrow/issues/47520#issuecomment-3266284243
Another error occurs when creating a `SparseCOOTensor` from a row-major
tensor with a column-major `SparseCOOIndex`.
```c++
TEST(MyTest, SparseCOOTensorColumnMajorTest) {
// clang-format off
std::vector<int64_t> values = {1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12
};
std::vector<int64_t> CooIndices = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,
0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2};
// clang-format on
auto data_buffer = Buffer::FromVector(values);
auto indices_buffer = Buffer::FromVector(CooIndices);
ASSERT_OK_AND_ASSIGN(auto indices_tensor,
Tensor::Make(int64(), indices_buffer, {12, 2}, {8,
96}));
ASSERT_TRUE(indices_tensor->is_column_major());
ASSERT_OK_AND_ASSIGN(auto sparse_coo_index,
SparseCOOIndex::Make(indices_tensor, true));
auto data_pointer = reinterpret_cast<const int64_t*>(data_buffer->data());
auto tensor = Tensor::Make(int64(), data_buffer, {4, 3}).ValueOrDie();
ASSERT_TRUE(tensor->is_row_major());
SparseCOOTensor sparse_tensor(sparse_coo_index, int64(), data_buffer,
{4,3}, {});
// SparseCooIndex is stored as column major tensor
ASSERT_TRUE(internal::checked_pointer_cast<SparseCOOIndex>(sparse_tensor.sparse_index())
->indices()
->is_column_major());
// Check the correctness of indices
for (int64_t i = 0; i < 12; i++) {
auto row =
internal::checked_pointer_cast<SparseCOOIndex>(sparse_tensor.sparse_index())
->indices()
->Value<Int64Type>({i, 0});
auto column =
internal::checked_pointer_cast<SparseCOOIndex>(sparse_tensor.sparse_index())
->indices()
->Value<Int64Type>({i, 1});
ASSERT_TRUE(data_pointer[i] == tensor->Value<Int64Type>({row, column}));
}
auto new_tensor = sparse_tensor.ToTensor().ValueOrDie();
ASSERT_TRUE(new_tensor->is_row_major());
ASSERT_FALSE(new_tensor->Equals(*tensor));
```
`ASSERT_FALSE(new_tensor->Equals(*tensor)); `passed because it is based on
the implementation below, which treats `SparseCOOIndex `as a row-major tensor.
https://github.com/apache/arrow/blob/982d31f35fd2cfe87494698dae9ef67d3333658c/cpp/src/arrow/tensor/coo_converter.cc#L316-L321
Note that, according to the following test, it is acceptable to have a
column-major `SparseCOOIndex`.
https://github.com/apache/arrow/blob/982d31f35fd2cfe87494698dae9ef67d3333658c/cpp/src/arrow/sparse_tensor_test.cc#L109-L123
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]