Skip to content

Commit

Permalink
ARROW-9116: [C++] Add BaseBinaryArray::total_values_length
Browse files Browse the repository at this point in the history
Closes #7426 from wesm/ARROW-9116

Authored-by: Wes McKinney <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
wesm authored and kou committed Jun 14, 2020
1 parent 2e977a2 commit 657d9c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cpp/src/arrow/array/array_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
12 changes: 12 additions & 0 deletions cpp/src/arrow/array/array_binary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ class TestStringArray : public ::testing::Test {
AssertZeroPadded(*strings_);
}

void TestTotalValuesLength() {
auto ty = TypeTraits<T>::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<offset_type>(13));

offset_type sliced_values_length =
checked_cast<const ArrayType&>(*arr.Slice(3)).total_values_length();
ASSERT_EQ(sliced_values_length, static_cast<offset_type>(9));
}

void TestType() {
std::shared_ptr<DataType> type = this->strings_->type();

Expand Down

0 comments on commit 657d9c6

Please sign in to comment.