pitrou commented on code in PR #50817:
URL: https://github.com/apache/arrow/pull/50817#discussion_r4681085204


##########
cpp/src/arrow/ipc/tensor_test.cc:
##########
@@ -609,6 +688,20 @@ TEST(TestSparseCSFIndex, RejectInconsistentBufferCounts) {
   ARROW_UNSUPPRESS_DEPRECATION_WARNING
 }
 
+TEST(TestSparseCSFIndex, RejectInvalidAxisOrder) {
+  // MakeCSFSparseTensorMessage initializes axisOrder to all zeroes, which is 
not a
+  // permutation for a two-dimensional tensor.
+  ASSERT_OK_AND_ASSIGN(auto message, MakeCSFSparseTensorMessage(/*shape=*/{4, 
4},
+                                                                
/*num_indptr_buffers=*/1,
+                                                                
/*num_indices_buffers=*/2,
+                                                                
/*axis_order_size=*/2));
+  ARROW_SUPPRESS_DEPRECATION_WARNING
+  ASSERT_RAISES(Invalid, ReadSparseTensor(*message));
+  ARROW_UNSUPPRESS_DEPRECATION_WARNING
+  ASSERT_RAISES(Invalid,
+                
internal::ReadSparseTensorPayload(MakeSparseTensorPayload(message, 4)));

Review Comment:
   What is `4`? Would you care to make the parameter name explicit?



##########
cpp/src/arrow/extension/tensor_internal.h:
##########
@@ -17,7 +17,6 @@
 
 #pragma once
 
-#include <cstdint>

Review Comment:
   You can probably keep `cstdint`.



##########
cpp/src/arrow/sparse_tensor.cc:
##########
@@ -123,6 +125,74 @@ Status MakeSparseTensorFromTensor(const Tensor& tensor,
 
 namespace {
 
+template <typename Visit>
+Status VisitSparseIndexType(const DataType& type, Visit&& visit) {
+  return VisitType(type, [&](const auto& index_type) -> Status {
+    using IndexType = std::decay_t<decltype(index_type)>;
+    if constexpr (is_signed_integer(IndexType::type_id) ||
+                  std::is_same_v<IndexType, UInt8Type> ||
+                  std::is_same_v<IndexType, UInt16Type> ||
+                  std::is_same_v<IndexType, UInt32Type>) {
+      return visit(index_type);

Review Comment:
   How about `is_integer(IndexType::type_id) && IndexType::type_id != 
Type::UINT64`?



##########
cpp/src/arrow/ipc/metadata_internal.cc:
##########
@@ -1163,26 +1163,32 @@ Status MakeSparseTensorIndexCSF(FBB& fbb, const 
SparseCSFIndex& sparse_index,
   auto indices_type_offset = flatbuf::CreateInt(fbb, 
indices_value_type.bit_width(),
                                                 
indices_value_type.is_signed());
 
-  const int64_t indptr_elem_size = indptr_value_type.byte_width();
-  const int64_t indices_elem_size = indices_value_type.byte_width();
-
-  int64_t offset = 0;
-  std::vector<flatbuf::Buffer> indptr, indices;
-
-  for (const std::shared_ptr<arrow::Tensor>& tensor : sparse_index.indptr()) {
-    const int64_t size = tensor->data()->size() / indptr_elem_size;
-    const int64_t padded_size = PaddedLength(tensor->data()->size(), 
kArrowIpcAlignment);
-
-    indptr.push_back({offset, size});
-    offset += padded_size;
-  }
-  for (const std::shared_ptr<arrow::Tensor>& tensor : sparse_index.indices()) {
-    const int64_t size = tensor->data()->size() / indices_elem_size;
-    const int64_t padded_size = PaddedLength(tensor->data()->size(), 
kArrowIpcAlignment);
-
-    indices.push_back({offset, size});
-    offset += padded_size;
-  }
+  size_t buffer_index = 0;
+  auto make_buffers =
+      [&](const std::vector<std::shared_ptr<Tensor>>& tensors,
+          const DataType& value_type) -> Result<std::vector<flatbuf::Buffer>> {
+    std::vector<flatbuf::Buffer> result;
+    result.reserve(tensors.size());
+    for (const auto& tensor : tensors) {
+      RETURN_NOT_OK(tensor->Validate());
+      if (tensor->ndim() != 1 || !tensor->is_contiguous() ||
+          !tensor->type()->Equals(value_type)) {
+        return Status::Invalid("CSF index buffers must be contiguous vectors 
of ",
+                               value_type.ToString());
+      }
+      // Backing buffers can contain trailing bytes. Describe only the logical
+      // elements, but use the serializer's offsets to account for those bytes.
+      ARROW_ASSIGN_OR_RAISE(const int64_t size, 
::arrow::internal::ComputeTensorSize(
+                                                    tensor->shape(), 
tensor->strides(),
+                                                    value_type.byte_width()));
+      result.emplace_back(buffers[buffer_index++].offset, size);

Review Comment:
   I honestly don't understand this part, so I'm going to trust you that it is 
right (is it properly tested?).



##########
cpp/src/arrow/ipc/tensor_test.cc:
##########
@@ -523,6 +527,81 @@ INSTANTIATE_TYPED_TEST_SUITE_P(TestInt32, 
TestSparseTensorRoundTrip, Int32Type);
 INSTANTIATE_TYPED_TEST_SUITE_P(TestUInt32, TestSparseTensorRoundTrip, 
UInt32Type);
 INSTANTIATE_TYPED_TEST_SUITE_P(TestInt64, TestSparseTensorRoundTrip, 
Int64Type);
 
+TEST(TestSparseCSFIndex, RejectInvalidIndexBuffers) {
+  const std::vector<int64_t> indptr_values = {0, 1};
+  const std::vector<int64_t> indices_values = {0, 0, 1};
+  const std::vector<int32_t> narrow_indices_values = {0};
+  const std::vector<int64_t> values = {42};
+  auto indptr = std::make_shared<Tensor>(int64(), Buffer::Wrap(indptr_values),
+                                         std::vector<int64_t>{2});
+  auto first_indices = std::make_shared<Tensor>(int64(), 
Buffer::Wrap(indices_values),
+                                                std::vector<int64_t>{1});
+  for (const auto& invalid_indices : std::vector<std::shared_ptr<Tensor>>{
+           std::make_shared<Tensor>(int64(), Buffer::FromString(""),
+                                    std::vector<int64_t>{1}),
+           std::make_shared<Tensor>(int64(), Buffer::Wrap(indices_values),
+                                    std::vector<int64_t>{}),
+           std::make_shared<Tensor>(int64(), Buffer::Wrap(indices_values),
+                                    std::vector<int64_t>{2}, 
std::vector<int64_t>{16}),
+           std::make_shared<Tensor>(int32(), 
Buffer::Wrap(narrow_indices_values),
+                                    std::vector<int64_t>{1})}) {
+    auto index = std::make_shared<SparseCSFIndex>(
+        std::vector<std::shared_ptr<Tensor>>{indptr},
+        std::vector<std::shared_ptr<Tensor>>{first_indices, invalid_indices},
+        std::vector<int64_t>{0, 1});
+    SparseCSFTensor tensor(index, int64(), Buffer::Wrap(values), {1, 2}, {});
+    IpcPayload payload;
+    ARROW_SUPPRESS_DEPRECATION_WARNING
+    ASSERT_RAISES(Invalid,
+                  GetSparseTensorPayload(tensor, default_memory_pool(), 
&payload));
+    ARROW_UNSUPPRESS_DEPRECATION_WARNING
+  }
+}
+
+TEST(TestSparseCSFIndex, RoundTripWithTrailingBufferBytes) {
+  const std::vector<int64_t> indptr_values = {0, 1};
+  const std::vector<int64_t> indices_values = {0};
+  const std::vector<int64_t> values = {42};
+
+  for (const int trailing_size : {1, 8, 9}) {
+    SCOPED_TRACE(trailing_size);
+    const std::string trailing_bytes(trailing_size, '\xff');
+    auto indptr_buffer =
+        Buffer::FromString(Buffer::Wrap(indptr_values)->ToString() + 
trailing_bytes);
+    auto indices_buffer =
+        Buffer::FromString(Buffer::Wrap(indices_values)->ToString() + 
trailing_bytes);
+    ASSERT_OK_AND_ASSIGN(auto index,
+                         SparseCSFIndex::Make(int64(), {1, 1}, {0, 1}, 
{indptr_buffer},
+                                              {indices_buffer, 
indices_buffer}));
+    ASSERT_OK_AND_ASSIGN(
+        auto tensor,
+        SparseCSFTensor::Make(index, int64(), Buffer::Wrap(values), {1, 1}, 
{}));
+
+    ARROW_SUPPRESS_DEPRECATION_WARNING
+    IpcPayload payload;
+    ASSERT_OK(GetSparseTensorPayload(*tensor, default_memory_pool(), 
&payload));
+    ASSERT_OK_AND_ASSIGN(auto from_payload, 
internal::ReadSparseTensorPayload(payload));
+    ASSERT_TRUE(from_payload->Equals(*tensor));
+
+    ASSERT_OK_AND_ASSIGN(auto sink, io::BufferOutputStream::Create());
+    int32_t metadata_length;
+    int64_t body_length;
+    ASSERT_OK(WriteSparseTensor(*tensor, sink.get(), &metadata_length, 
&body_length));
+    ASSERT_OK_AND_ASSIGN(auto serialized, sink->Finish());
+    io::BufferReader reader(serialized);
+    ASSERT_OK_AND_ASSIGN(auto result, ReadSparseTensor(&reader));

Review Comment:
   Can we further validate the result here?



-- 
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]

Reply via email to