Skip to content

GH-50906: [C++] Support 1D tensors in SparseCSR/CSC matrix conversion - #50907

Open
pratyushadk wants to merge 1 commit into
apache:mainfrom
pratyushadk:GH-50906-csx-1d-tensor-support
Open

GH-50906: [C++] Support 1D tensors in SparseCSR/CSC matrix conversion#50907
pratyushadk wants to merge 1 commit into
apache:mainfrom
pratyushadk:GH-50906-csx-1d-tensor-support

Conversation

@pratyushadk

@pratyushadk pratyushadk commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

Calling SparseCSRMatrix::Make or SparseCSCMatrix::Make on a 1D dense tensor currently returns a NotImplemented error ("TODO for ndim <= 1"). The CSR/CSC representation for a 1D vector is well-defined though: it is equivalent to a single-row sparse matrix where indptr = [0, nnz], indices holds the positions of non-zero values, and values holds the corresponding values. The COO format already handles 1D tensors, so this fills a straightforward gap.

What changes are included in this PR?

cpp/src/arrow/tensor/csx_converter.cc

The Convert() method in SparseCSXMatrixConverter now handles three cases cleanly. ndim == 0 returns Status::Invalid since scalars have no meaningful sparse representation. ndim == 1 takes a new path that does a single linear scan of the vector, writes indptr = [0, nnz], and records the positions and values of non-zeros. ndim == 2 is the existing path and is completely unchanged.

The reverse path (MakeTensorFromSparseCSXMatrix) had a hardcoded shape[1] access which would be out of bounds for a 1D tensor. That is fixed to use shape.size() > 1 ? shape[1] : shape[0].

cpp/src/arrow/sparse_tensor_test.cc

Three new tests added to the existing TestSparseCSRMatrix fixture covering the 1D case.

Are these changes tested?

Yes. Three new tests cover the main conversion path, the all-zero edge case, and a full round-trip from dense to sparse and back. All 153 tests in the sparse tensor test suite passed locally with no regressions.

Are there any user-facing changes?

SparseCSRMatrix::Make and SparseCSCMatrix::Make now accept 1D tensors instead of returning NotImplemented. There are no breaking changes.

@pratyushadk
pratyushadk requested a review from pitrou as a code owner August 18, 2026 19:07
Copilot AI lite review requested due to automatic review settings August 18, 2026 19:07

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 reached their quota limit.

@github-actions github-actions Bot added the awaiting review Awaiting review label Aug 18, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #50906 has been automatically assigned in GitHub to PR creator.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants