diff --git a/cpp/src/arrow/array/array_binary.h b/cpp/src/arrow/array/array_binary.h index c68489b3834..e27a2bfcc5e 100644 --- a/cpp/src/arrow/array/array_binary.h +++ b/cpp/src/arrow/array/array_binary.h @@ -85,15 +85,30 @@ class BaseBinaryArray : public FlatArray { return raw_value_offsets_ + data_->offset; } - // Neither of these functions will perform boundschecking + /// \brief Return the data buffer absolute offset of the data for the value + /// at the passed index. + /// + /// Does not perform boundschecking offset_type value_offset(int64_t i) const { return raw_value_offsets_[i + data_->offset]; } + + /// \brief Return the length of the data for the value at the passed index. + /// + /// Does not perform boundschecking offset_type value_length(int64_t i) const { i += data_->offset; return raw_value_offsets_[i + 1] - raw_value_offsets_[i]; } + /// \brief Return the total length of the memory in the data buffer + /// referenced by this array. If the array has been sliced then this may be + /// less than the size of the data buffer (data_->buffers[2]). + offset_type total_values_length() const { + return raw_value_offsets_[data_->length + data_->offset] - + raw_value_offsets_[data_->offset]; + } + protected: // For subclasses BaseBinaryArray() : raw_value_offsets_(NULLPTR), raw_data_(NULLPTR) {} diff --git a/cpp/src/arrow/array/array_binary_test.cc b/cpp/src/arrow/array/array_binary_test.cc index d044bafb23f..41945e552ff 100644 --- a/cpp/src/arrow/array/array_binary_test.cc +++ b/cpp/src/arrow/array/array_binary_test.cc @@ -109,6 +109,18 @@ class TestStringArray : public ::testing::Test { AssertZeroPadded(*strings_); } + void TestTotalValuesLength() { + auto ty = TypeTraits::type_singleton(); + auto arr = ArrayFromJSON(ty, R"(["a", null, "bbb", "cccc", "ddddd"])"); + + offset_type values_length = arr.total_values_length(); + ASSERT_EQ(values_length, static_cast(13)); + + offset_type sliced_values_length = + checked_cast(*arr.Slice(3)).total_values_length(); + ASSERT_EQ(sliced_values_length, static_cast(9)); + } + void TestType() { std::shared_ptr type = this->strings_->type();