Skip to content

GH-50816: [C++][IPC] Improve sparse tensor index validation - #50817

Open
rok wants to merge 1 commit into
apache:mainfrom
rok:cpp-ipc-validate-sparse-tensor-indices
Open

rok wants to merge 1 commit into
apache:mainfrom
rok:cpp-ipc-validate-sparse-tensor-indices

Conversation

@rok

@rok rok commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

Invalid COO, CSR, and CSC sparse tensor indices and buffers should be rejected consistently on construction, IPC read, and dense conversion. CSF validation will be handled in a follow-up PR.

What changes are included in this PR?

  • Validate COO, CSR, and CSC index bounds, indptr consistency, and backing buffers.
  • Validate sparse tensor values buffers and dense conversion sizes in Make and ToTensor.
  • Respect COO coordinate strides during dense conversion and use Make constructors in the CSX IPC reader.

Are these changes tested?

Yes. Regression tests were added in sparse_tensor_test.cc and ipc/tensor_test.cc.

Are there any user-facing changes?

Invalid COO, CSR, and CSC sparse tensor inputs that were previously accepted are now rejected with an Invalid status.

@rok
rok force-pushed the cpp-ipc-validate-sparse-tensor-indices branch from 473cec3 to 83eba91 Compare November 2, 2026 18:43
@rok
rok marked this pull request as ready for review November 2, 2026 20:12
@rok
rok requested review from pitrou and a balanced review from Copilot November 2, 2026 20:12
@rok

rok commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@pitrou here's a proposal for several sparse index checkers.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Sparse tensor validation can read past the buffer because the index and indptr helpers assume packed storage without rejecting non-contiguous vectors.

1 open issue
What changed in this PR

This PR strengthens C++ sparse tensor validation for GH-50816, rejecting malformed indices before dense conversion and correcting CSF index buffer sizing in IPC.

Changes:

  • Validate index bounds, buffers, compressed pointers, axis ordering, and conversion sizes.
  • Respect COO coordinate strides and encode CSF IPC buffer lengths in bytes.
  • Add regression tests for malformed inputs and IPC round trips.
File Description
cpp/​src/​arrow/​tensor/​coo_converter.cc Respect coordinate strides during dense conversion.
cpp/​src/​arrow/​tensor.h Declare the shared permutation validator.
cpp/​src/​arrow/​tensor.cc Implement the shared permutation validator.
cpp/​src/​arrow/​tensor_test.cc Test valid and invalid permutations.
cpp/​src/​arrow/​sparse_tensor.h Declare and connect sparse index validation.
cpp/​src/​arrow/​sparse_tensor.cc Validate sparse indices, buffers, and conversion sizes.
cpp/​src/​arrow/​sparse_tensor_test.cc Add sparse validation and conversion regressions.
cpp/​src/​arrow/​ipc/​tensor_test.cc Test CSF buffer sizing and invalid IPC inputs.
cpp/​src/​arrow/​ipc/​metadata_internal.cc Correct CSF buffer lengths and validate layouts.
cpp/​src/​arrow/​extension/​tensor_internal.h Remove the relocated validator declaration.
cpp/​src/​arrow/​extension/​tensor_internal.cc Remove the relocated validator implementation.

Review effort: Balanced

Comment thread cpp/src/arrow/sparse_tensor.cc Resolved
Copilot AI balanced review requested due to automatic review settings November 2, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Maintainer review is needed to clarify compatibility expectations for previously serialized CSF messages and older readers when IPC buffer lengths change from element counts to byte counts.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't want to review all of this.

(did you write it yourself or is it from AI?)


#pragma once

#include <cstdint>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably keep cstdint.

Comment thread cpp/src/arrow/ipc/metadata_internal.cc Outdated
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread cpp/src/arrow/ipc/tensor_test.cc Outdated
ASSERT_RAISES(Invalid, ReadSparseTensor(*message));
ARROW_UNSUPPRESS_DEPRECATION_WARNING
ASSERT_RAISES(Invalid,
internal::ReadSparseTensorPayload(MakeSparseTensorPayload(message, 4)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 4? Would you care to make the parameter name explicit?

Comment thread cpp/src/arrow/ipc/tensor_test.cc Outdated
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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we further validate the result here?

std::is_same_v<IndexType, UInt8Type> ||
std::is_same_v<IndexType, UInt16Type> ||
std::is_same_v<IndexType, UInt32Type>) {
return visit(index_type);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@pitrou

pitrou commented Nov 24, 2026

Copy link
Copy Markdown
Member

@rok I'll let you decide the appropriate way forward. It can involve another reviewer, or enough care and polishing on your part :-)

@rok
rok force-pushed the cpp-ipc-validate-sparse-tensor-indices branch from 460c233 to 0d22042 Compare November 24, 2026 22:58
Copilot AI balanced review requested due to automatic review settings November 24, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@rok
rok force-pushed the cpp-ipc-validate-sparse-tensor-indices branch from 0d22042 to 5fa0ca9 Compare November 24, 2026 23:33
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants