Skip to content

GH-50251: Add GetSpan() convenience method to ArrayData#50310

Closed
VedantRalekar wants to merge 2 commits into
apache:mainfrom
VedantRalekar:issue-50251
Closed

GH-50251: Add GetSpan() convenience method to ArrayData#50310
VedantRalekar wants to merge 2 commits into
apache:mainfrom
VedantRalekar:issue-50251

Conversation

@VedantRalekar

@VedantRalekar VedantRalekar commented Jul 1, 2026

Copy link
Copy Markdown

What changes were proposed in this pull request?

This PR adds a GetSpan() convenience method to ArrayData, similar to the existing GetSpan() implementation in ArraySpan.

Why are the changes needed?

ArraySpan already provides a convenient way to access buffer data as std::span, while ArrayData does not. This change adds a similar API to improve consistency and make span-based buffer access easier.

Fixes #50251.

@VedantRalekar VedantRalekar requested a review from pitrou as a code owner July 1, 2026 03:47
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

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

Comment thread cpp/src/arrow/array/data.h Outdated
Comment on lines +292 to +294
return std::span<const T>(
reinterpret_cast<const T*>(buffers[i]->data()) + offset, length);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for pr, @VedantRalekar!
These lines need clang-format fix per Dev / Lint job logs.
See also Styling and Dev Guidelines, you can run
pre-commit run --show-diff-on-failure --color=always --all-files cpp

One optional point to consider - perhaps add a test in array_test.cc to cover + offset and also the new null-buffer case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I'll apply the clang-format fixes and add the suggested tests for the offset and null-buffer cases. I'll update the PR once the changes are ready.

Comment thread cpp/src/arrow/array/data.h Outdated
/// \pre i > 0
/// \pre length <= the length of the buffer (in number of values) that's expected for
/// this array type
/// \return A span<const T> of the requested length

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe add a mention of empty span to \return

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i have added a mention of empty span to \return, please review it.

@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.

Thanks for your contribution @VedantRalekar , please find some comments below.

/// this array type
/// \return A span<const T> of the requested length, or empty if buffers[i] is null
template <typename T>
std::span<const T> GetSpan(int i, int64_t length) const {

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.

We have ArrayData::GetMutableValues in addition to ArrayData::GetValues, why not add GetMutableSpan as well?


// Buffer with values 0..9
std::vector<T> values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto value_buffer = Buffer::FromVector(values).ValueOrDie();

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 use ASSERT_OK_AND_ASSIGN instead of dying on error

ASSERT_EQ(span.size(), length);
ASSERT_EQ(span[0], 3); // values[offset]
ASSERT_EQ(span[length - 1], 6); // values[offset + length - 1]
ASSERT_EQ(span.data(), value_buffer->data_as<T>() + offset);

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 you also exercise calling GetSpan with a smaller length?

Comment on lines +138 to +142
auto data = ArrayData::Make(int64(), length, {nullptr, nullptr},
/*null_count=*/0, offset);
auto span = data->GetSpan<T>(/*i=*/1, length);
ASSERT_TRUE(span.empty());
ASSERT_EQ(span.data(), nullptr);

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.

This is invalid. All null buffers in an integer array is only supported for length=0, can you fix the test accordingly?

Comment on lines +145 to +153
// --- Case B: Null bitmap is nullptr, values buffer valid → span works ---
{
auto data = ArrayData::Make(int64(), length, {nullptr, value_buffer},
/*null_count=*/0, offset);
auto span = data->GetSpan<T>(/*i=*/1, length);
ASSERT_EQ(span.size(), length);
ASSERT_EQ(span[0], 10);
ASSERT_EQ(span[3], 40);
}

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're already testing this in GetSpanWithOffset, aren't you?

Comment thread cpp/src/arrow/array/array_test.cc
@pitrou

pitrou commented Jul 7, 2026

Copy link
Copy Markdown
Member

Closing as superseded by #50366

@pitrou pitrou closed this Jul 7, 2026
@VedantRalekar VedantRalekar deleted the issue-50251 branch July 7, 2026 17:11
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.

[C++] Add GetSpan to ArrayData

3 participants