Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,39 @@ TEST_F(TestArray, TestBinaryViewAppendArraySlice) {

AssertArraysEqual(*src, *dst);
}

TEST_F(TestArray, GetSpanRespectsOffset) {
auto data_buffer = Buffer::FromString("123456789abcdef0");
auto data = ArrayData::Make(uint8(), 3, {nullptr, data_buffer}, 0, 1);
auto span = data->GetSpan<uint8_t>(1, 3);

EXPECT_EQ(span.size(), 3);
EXPECT_EQ(span[0], '2');
EXPECT_EQ(span[1], '3');
EXPECT_EQ(span[2], '4');
}
TEST_F(TestArray, GetMutableSpanRespectsOffset) {
const int64_t nbytes = 16;
ASSERT_OK_AND_ASSIGN(auto uniq_buffer, AllocateResizableBuffer(nbytes, pool_));
memset(uniq_buffer->mutable_data(), '0', nbytes);
std::shared_ptr<Buffer> buffer(std::move(uniq_buffer));
std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, buffer};
auto data = ArrayData::Make(uint8(), 3, buffers, 0, 1);
auto span = data->template GetMutableSpan<uint8_t>(1, 3);

EXPECT_EQ(span.size(), 3);
EXPECT_EQ(span[0], '0');
EXPECT_EQ(span[1], '0');
EXPECT_EQ(span[2], '0');

span[0] = 'X';
span[1] = 'Y';
span[2] = 'Z';

auto raw = buffer->mutable_data();
EXPECT_EQ(raw[1], 'X');
EXPECT_EQ(raw[2], 'Y');
EXPECT_EQ(raw[3], 'Z');
}
TEST_F(TestArray, ValidateBuffersPrimitive) {
auto empty_buffer = std::make_shared<Buffer>("");
auto null_buffer = Buffer::FromString("\xff");
Expand Down
31 changes: 31 additions & 0 deletions cpp/src/arrow/array/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,37 @@ struct ARROW_EXPORT ArrayData {
return NULLPTR;
}
}
/// \brief Access a buffer's data as a span
///
/// \param i The buffer index
/// \param length The required length (in number of typed values) of the requested span
/// \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
template <typename T>
std::span<const T> GetSpan(int i, int64_t length) const {
const int64_t buffer_length = buffers[i]->size() / static_cast<int64_t>(sizeof(T));
assert(i > 0 && length + offset <= buffer_length);
ARROW_UNUSED(buffer_length);
return std::span<const T>(buffers[i]->data_as<T>() + this->offset, length);
}

/// \brief Access a buffer's data as a span
///
/// \param i The buffer index
/// \param length The required length (in number of typed values) of the requested span
/// \pre i > 0
/// \pre length <= the length of the buffer (in number of values) that's expected for
/// this array type
/// \return A span<T> of the requested length
template <typename T>
std::span<T> GetMutableSpan(int i, int64_t length) {
const int64_t buffer_length = buffers[i]->size() / static_cast<int64_t>(sizeof(T));
assert(i > 0 && length + offset <= buffer_length);
ARROW_UNUSED(buffer_length);
return std::span<T>(buffers[i]->mutable_data_as<T>() + this->offset, length);
}

/// \brief Access a buffer's data as a typed C pointer
///
Expand Down
Loading