This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new c3f22690a87 [opt](storage) Batch-decode native RLE pages (#71098)
c3f22690a87 is described below
commit c3f22690a87859856bd2f1b3725a319e9dab544c
Author: HappenLee <[email protected]>
AuthorDate: Wed Dec 2 15:19:12 2026 +0800
[opt](storage) Batch-decode native RLE pages (#71098)
### What problem does this PR solve?
Sequential scans of native RLE pages used to call `Get` and append one
value to the column per row. The first revision batched BOOLEAN reads.
This revision shares the page path with integer instantiations and
copies full-width integer literals in bulk.
### Implementation
- The builder and decoder share the page bit width. BOOLEAN uses one bit
and integers use their physical width.
- The page decoder reserves space once and decodes into the destination
column. It publishes the column size and new decoder position together
after a successful read. A short page returns corruption and a caller
can retry from the original position.
- All repeated runs use the existing range fill. For literals, the bit
reader unpacks groups of one-bit BOOLEAN values with the existing bit
unpacker. On little-endian hosts it copies byte-aligned full-width
integers directly. Short BOOLEAN runs and other packed widths use the
scalar reader.
- BOOLEAN is the native segment type currently registered with RLE.
Integer page support is exercised directly in the BE tests. This PR
preserves the existing encoding registrations and wire format.
### Performance
The following microbenchmark compares `RleDecoder::get_values` in the
previous PR revision (`f3e4439`) and this revision. Both decode the same
65,536 encoder-produced values into preallocated output. The numbers are
thread CPU microseconds per traversal on one pinned Intel Xeon Platinum
8457C logical CPU, using Clang 16 and `-O3 -march=native`. Each number
is the median of three runs, each taking the median of seven rounds of
450 traversals. Output was checked against the input before timing. The
benchmark excludes page I/O, column allocation, and SQL execution.
| Distribution | Batch size | Previous PR (µs) | This revision (µs) |
| --- | ---: | ---: | ---: |
| Random INT32 | 127 | 253.50 | 74.31 |
| Random INT32 | 4,096 | 250.06 | 63.72 |
| Repeated INT32 | 127 | 16.26 | 16.55 |
| Repeated INT32 | 4,096 | 7.27 | 6.97 |
| Random BOOLEAN | 127 | 45.32 | 45.73 |
| Random BOOLEAN | 4,096 | 13.05 | 12.72 |
The full-width literal path was 3.41× faster at 127 rows and 3.92×
faster at 4,096 rows for random INT32. The two control distributions
changed by less than 5% at both batch sizes.
<details>
<summary>Earlier BOOLEAN benchmark against per-row decoding</summary>
The initial PR measured the following wall-clock microseconds per 65,536
values in batches of 4,096 on the same CPU model with Clang 16. These
measurements compare the original per-row decoder with the first BOOLEAN
batching implementation.
| Boolean distribution | Before (`Get` per row) | Batch output only |
Batch output and bit unpacking |
| --- | ---: | ---: | ---: |
| All zero | 128.15 | 1.62 | 1.59 |
| 65 scattered ones | 144.52 | 3.35 | 3.33 |
| Alternating 0 and 1 | 286.78 | 101.81 | 5.62 |
| Random, approximately 50% ones | 283.99 | 110.18 | 7.07 |
| Alternating runs of eight | 212.06 | 128.38 | 119.03 |
</details>
### Testing
- 37 BE tests under ASan, including RLE page, bit-stream, bit-packing,
and sparse-row-ID null-map coverage.
- Page tests exercise 8-, 16-, 32-, and 64-bit signed integers, negative
repeated values, full-width literals, and seeks. BOOLEAN tests exercise
partial batches, appending, and mixed sequential and row-ID reads.
Truncated INT32 literal and repeated runs exercise a failed append and
retry.
- Decoder microbenchmark correctness checks cover batch sizes 1, 7, 31,
33, 127, and 4,096.
- An additional ASan/UBSan bit-reader check passed for widths 1 through
64, starting bit offsets 0 through 7, complete and truncated buffers,
and seven batch sizes.
- Clang-format 16 and BE build hygiene checks pass.
### Release note
Speed up sequential reads of native BOOLEAN columns stored in RLE pages.
### Check List (For Author)
- Test: BE unit tests under ASan and standalone decoder checks and
microbenchmarks.
- Behavior changed: No
- Does this need documentation: No
### AI Disclosure
- Tool: OpenAI Codex
- Generated: Code, unit tests, benchmark, and PR description.
- Human review: Pending.
---
be/src/storage/segment/rle_page.h | 77 +++++++++----------------
be/src/util/bit_stream_utils.h | 7 +++
be/src/util/bit_stream_utils.inline.h | 48 ++++++++++++++++
be/src/util/rle_encoding.h | 18 +++---
be/test/storage/segment/rle_page_test.cpp | 96 +++++++++++++++++++++++++++++++
5 files changed, 188 insertions(+), 58 deletions(-)
diff --git a/be/src/storage/segment/rle_page.h
b/be/src/storage/segment/rle_page.h
index 80ed3edda61..544e4adc610 100644
--- a/be/src/storage/segment/rle_page.h
+++ b/be/src/storage/segment/rle_page.h
@@ -18,6 +18,9 @@
#pragma once
#include "common/cast_set.h"
+#include "core/assert_cast.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/storage_field_type.h"
#include "storage/field_type.h" // for FieldType
#include "storage/segment/options.h" // for
PageBuilderOptions/PageDecoderOptions
#include "storage/segment/page_builder.h" // for PageBuilder
@@ -32,6 +35,10 @@ namespace segment_v2 {
static constexpr size_t RLE_PAGE_HEADER_SIZE = 4;
+template <FieldType Type>
+inline constexpr int RLE_PAGE_BIT_WIDTH =
+ Type == FieldType::OLAP_FIELD_TYPE_BOOL ? 1 : TypeTraits<Type>::size *
8;
+
// RLE builder for generic integer and bool types. What is missing is some way
// to enforce that this can only be instantiated for INT and BOOL types.
//
@@ -60,17 +67,7 @@ public:
friend class PageBuilderHelper<Self>;
Status init() override {
- switch (Type) {
- case FieldType::OLAP_FIELD_TYPE_BOOL: {
- _bit_width = 1;
- break;
- }
- default: {
- _bit_width = SIZE_OF_TYPE * 8;
- break;
- }
- }
- _rle_encoder = new RleEncoder<CppType>(&_buf, _bit_width);
+ _rle_encoder = new RleEncoder<CppType>(&_buf,
RLE_PAGE_BIT_WIDTH<Type>);
return reset();
}
@@ -123,11 +120,7 @@ public:
private:
RlePageBuilder(const PageBuilderOptions& options)
- : _options(options),
- _count(0),
- _finished(false),
- _bit_width(0),
- _rle_encoder(nullptr) {}
+ : _options(options), _count(0), _finished(false),
_rle_encoder(nullptr) {}
typedef typename TypeTraits<Type>::CppType CppType;
static constexpr size_t SIZE_OF_TYPE = TypeTraits<Type>::size;
@@ -135,7 +128,6 @@ private:
PageBuilderOptions _options;
size_t _count;
bool _finished;
- int _bit_width;
RleEncoder<CppType>* _rle_encoder = nullptr;
faststring _buf;
uint64_t _raw_data_size = 0;
@@ -145,12 +137,7 @@ template <FieldType Type>
class RlePageDecoder : public PageDecoder {
public:
RlePageDecoder(Slice slice, const PageDecoderOptions& options)
- : _data(slice),
- _options(options),
- _parsed(false),
- _num_elements(0),
- _cur_index(0),
- _bit_width(0) {}
+ : _data(slice), _options(options), _parsed(false),
_num_elements(0), _cur_index(0) {}
Status init() override {
CHECK(!_parsed);
@@ -162,20 +149,9 @@ public:
_parsed = true;
- switch (Type) {
- case FieldType::OLAP_FIELD_TYPE_BOOL: {
- _bit_width = 1;
- break;
- }
- default: {
- _bit_width = SIZE_OF_TYPE * 8;
- break;
- }
- }
-
- _rle_decoder =
- RleDecoder<CppType>((uint8_t*)_data.data +
RLE_PAGE_HEADER_SIZE,
- cast_set<int>(_data.size -
RLE_PAGE_HEADER_SIZE), _bit_width);
+ _rle_decoder = RleDecoder<CppType>((uint8_t*)_data.data +
RLE_PAGE_HEADER_SIZE,
+ cast_set<int>(_data.size -
RLE_PAGE_HEADER_SIZE),
+ RLE_PAGE_BIT_WIDTH<Type>);
RETURN_IF_ERROR(seek_to_position_in_page(0));
return Status::OK();
@@ -204,7 +180,7 @@ public:
} else {
_rle_decoder = RleDecoder<CppType>((uint8_t*)_data.data +
RLE_PAGE_HEADER_SIZE,
cast_set<int>(_data.size -
RLE_PAGE_HEADER_SIZE),
- _bit_width);
+ RLE_PAGE_BIT_WIDTH<Type>);
_rle_decoder.Skip(pos);
}
_cur_index = pos;
@@ -219,16 +195,20 @@ public:
}
size_t to_fetch = std::min(*n, static_cast<size_t>(_num_elements -
_cur_index));
- size_t remaining = to_fetch;
- bool result = false;
- CppType value;
- while (remaining > 0) {
- result = _rle_decoder.Get(&value);
- DCHECK(result);
- dst->insert_data((char*)(&value), SIZE_OF_TYPE);
- remaining--;
- }
-
+ using Column =
ColumnVector<storage_field_type_to_primitive_type(Type)>;
+ auto& data = assert_cast<Column&>(*dst).get_data();
+ const size_t new_size = data.size() + to_fetch;
+ auto decoder = _rle_decoder;
+ RETURN_IF_CATCH_EXCEPTION({
+ data.reserve(new_size);
+ const size_t decoded = decoder.get_values(data.end(), to_fetch);
+ if (UNLIKELY(decoded != to_fetch)) {
+ return Status::Corruption("Truncated RLE page: expected {}
values, got {}",
+ to_fetch, decoded);
+ }
+ });
+ data.resize_assume_reserved(new_size);
+ _rle_decoder = decoder;
_cur_index += to_fetch;
*n = to_fetch;
return Status::OK();
@@ -279,7 +259,6 @@ private:
bool _parsed;
uint32_t _num_elements;
size_t _cur_index;
- int _bit_width;
RleDecoder<CppType> _rle_decoder;
};
diff --git a/be/src/util/bit_stream_utils.h b/be/src/util/bit_stream_utils.h
index 270a0635ed3..11bfb3a2ff2 100644
--- a/be/src/util/bit_stream_utils.h
+++ b/be/src/util/bit_stream_utils.h
@@ -102,6 +102,10 @@ public:
template <typename T>
bool GetValue(int num_bits, T* v);
+ // Reads up to num_values packed integers and returns the number of
complete values.
+ template <typename T>
+ size_t GetValues(int num_bits, T* values, size_t num_values);
+
// Reads a 'num_bytes'-sized value from the buffer and stores it in 'v'. T
needs to be a
// little-endian native type and big enough to store 'num_bytes'. The
value is assumed
// to be byte-aligned so the stream will be advanced to the start of the
next byte
@@ -150,6 +154,9 @@ public:
int max_bytes() const { return max_bytes_; }
private:
+ // Unpacks up to 'num_values' consecutive bits into 0/1 bytes and returns
the count read.
+ size_t GetBitValues(size_t num_values, uint8_t* values);
+
// Used by SeekToBit() and GetValue() to fetch the
// the next word into buffer_.
void BufferValues();
diff --git a/be/src/util/bit_stream_utils.inline.h
b/be/src/util/bit_stream_utils.inline.h
index f24e8ce90f2..6c89fb1096a 100644
--- a/be/src/util/bit_stream_utils.inline.h
+++ b/be/src/util/bit_stream_utils.inline.h
@@ -21,6 +21,8 @@
#pragma once
#include <algorithm>
+#include <bit>
+#include <type_traits>
#include "glog/logging.h"
#include "util/alignment.h"
@@ -137,6 +139,52 @@ bool BitReader::GetValue(int num_bits, T* v) {
return true;
}
+template <typename T>
+size_t BitReader::GetValues(int num_bits, T* values, size_t num_values) {
+ if constexpr (std::is_same_v<T, uint8_t>) {
+ if (num_bits == 1 && num_values >= 32) {
+ return GetBitValues(num_values, values);
+ }
+ }
+ // Full-width literals are consecutive little-endian integers.
+ if (std::endian::native == std::endian::little && !std::is_same_v<T, bool>
&&
+ num_bits == sizeof(T) * 8 && bit_offset_ % 8 == 0) {
+ const int offset = byte_offset_ + bit_offset_ / 8;
+ const size_t count = std::min(num_values, (max_bytes_ - offset) /
sizeof(T));
+ memcpy(values, buffer_ + offset, count * sizeof(T));
+ const bool advanced = Advance(static_cast<int64_t>(count) * num_bits);
+ DCHECK(advanced);
+ return count;
+ }
+ size_t decoded = 0;
+ while (decoded < num_values && GetValue(num_bits, values + decoded)) {
+ ++decoded;
+ }
+ return decoded;
+}
+
+inline size_t BitReader::GetBitValues(size_t num_values, uint8_t* values) {
+ const size_t available = static_cast<size_t>(max_bytes_ - byte_offset_) *
8 - bit_offset_;
+ num_values = std::min(num_values, available);
+ const size_t prefix = std::min(num_values, static_cast<size_t>((8 -
bit_offset_ % 8) % 8));
+ for (size_t i = 0; i < prefix; ++i) {
+ const bool result = GetValue(1, values + i);
+ DCHECK(result);
+ }
+ if (prefix == num_values) {
+ return prefix;
+ }
+
+ const int offset = byte_offset_ + bit_offset_ / 8;
+ const auto decoded = BitPacking::UnpackValues<uint8_t, 1>(buffer_ +
offset, max_bytes_ - offset,
+ num_values -
prefix, values + prefix)
+ .second;
+ // Advance by the consumed bits, since the last output may end inside a
byte.
+ const bool advanced = Advance(decoded);
+ DCHECK(advanced);
+ return prefix + decoded;
+}
+
inline void BitReader::Rewind(int num_bits) {
bit_offset_ -= num_bits;
if (bit_offset_ >= 0) {
diff --git a/be/src/util/rle_encoding.h b/be/src/util/rle_encoding.h
index 18442456c49..a4894774e48 100644
--- a/be/src/util/rle_encoding.h
+++ b/be/src/util/rle_encoding.h
@@ -257,11 +257,11 @@ bool RleDecoder<T>::ReadHeader() {
literal_count_ = (indicator_value >> 1) * 8;
DCHECK_GT(literal_count_, 0);
} else {
+ if (UNLIKELY(!bit_reader_.GetAligned(BitUtil::Ceil(bit_width_, 8),
¤t_value_))) {
+ throw Exception(ErrorCode::CORRUPTION, "Truncated RLE repeated
value");
+ }
repeat_count_ = indicator_value >> 1;
DCHECK_GT(repeat_count_, 0);
- bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_,
8),
-
reinterpret_cast<T*>(¤t_value_));
- DCHECK(result1);
}
}
return true;
@@ -372,13 +372,13 @@ size_t RleDecoder<T>::get_values(T* values, size_t
num_values) {
read_num += read_this_time;
} else if (literal_count_ > 0) {
read_this_time = std::min((size_t)literal_count_, read_this_time);
- for (int i = 0; i < read_this_time; ++i) {
- bool result = bit_reader_.GetValue(bit_width_, values);
- DCHECK(result);
- values++;
+ const size_t decoded = bit_reader_.GetValues(bit_width_, values,
read_this_time);
+ values += decoded;
+ literal_count_ -= decoded;
+ read_num += decoded;
+ if (UNLIKELY(decoded != read_this_time)) {
+ return read_num;
}
- literal_count_ -= read_this_time;
- read_num += read_this_time;
} else {
if (!ReadHeader()) {
return read_num;
diff --git a/be/test/storage/segment/rle_page_test.cpp
b/be/test/storage/segment/rle_page_test.cpp
index 0aa2f9149ff..eb3aecd9372 100644
--- a/be/test/storage/segment/rle_page_test.cpp
+++ b/be/test/storage/segment/rle_page_test.cpp
@@ -19,8 +19,14 @@
#include <gtest/gtest.h>
+#include <algorithm>
+#include <iterator>
+#include <limits>
#include <memory>
+#include <vector>
+#include "core/assert_cast.h"
+#include "core/column/column_vector.h"
#include "core/data_type/data_type_factory.hpp"
#include "storage/segment/options.h"
#include "storage/segment/page_builder.h"
@@ -177,6 +183,96 @@ TEST_F(RlePageTest, TestRleBoolBlockEncoderRandom) {
bools.get(), size);
}
+TEST_F(RlePageTest, BooleanBatchReadAcrossRuns) {
+ constexpr auto type = FieldType::OLAP_FIELD_TYPE_BOOL;
+ std::vector<uint8_t> values(1536);
+ for (size_t i = 128; i < 1392; ++i) {
+ values[i] = (i + 1) % 2;
+ }
+ std::fill(values.begin() + 1392, values.end(), 1);
+
+ segment_v2::PageBuilder* builder = nullptr;
+ ASSERT_TRUE(segment_v2::RlePageBuilder<type>::create(&builder,
PageBuilderOptions {}).ok());
+ std::unique_ptr<segment_v2::PageBuilder> page_builder(builder);
+ size_t count = values.size();
+ ASSERT_TRUE(page_builder->add(values.data(), &count).ok());
+ ASSERT_EQ(values.size(), count);
+ OwnedSlice page;
+ ASSERT_TRUE(page_builder->finish(&page).ok());
+
+ PageDecoderOptions options;
+ segment_v2::RlePageDecoder<type> decoder(page.slice(), options);
+ ASSERT_TRUE(decoder.init().ok());
+ MutableColumnPtr column = ColumnUInt8::create();
+ auto& data = assert_cast<ColumnUInt8&>(*column).get_data();
+ data.push_back(1);
+ size_t offset = 0;
+ for (size_t batch_size : {131, 397, 65, 1000}) {
+ size_t n = batch_size;
+ ASSERT_TRUE(decoder.next_batch(&n, column).ok());
+ ASSERT_EQ(std::min(batch_size, values.size() - offset), n);
+ offset += n;
+ ASSERT_EQ(offset, decoder.current_index());
+ ASSERT_EQ(offset + 1, data.size());
+ }
+ ASSERT_EQ(values.size(), offset);
+ ASSERT_EQ(1, data[0]);
+ EXPECT_TRUE(std::equal(values.begin(), values.end(), data.begin() + 1));
+
+ ASSERT_TRUE(decoder.seek_to_position_in_page(135).ok());
+ const segment_v2::rowid_t rowids[] = {135, 137, 167};
+ size_t n = std::size(rowids);
+ ASSERT_TRUE(decoder.read_by_rowids(rowids, 0, &n, column).ok());
+ ASSERT_EQ(std::size(rowids), n);
+ for (size_t i = 0; i < n; ++i) {
+ EXPECT_EQ(values[rowids[i]], data[values.size() + 1 + i]);
+ }
+ n = 133;
+ ASSERT_TRUE(decoder.next_batch(&n, column).ok());
+ ASSERT_EQ(133, n);
+ EXPECT_TRUE(std::equal(values.begin() + 168, values.begin() + 168 + n,
data.end() - n));
+}
+
+TEST_F(RlePageTest, SignedIntegerBatchReadAcrossRuns) {
+ auto check = [this]<FieldType type>() {
+ using T = typename TypeTraits<type>::CppType;
+ // A full-width literal run followed by a repeated negative value.
+ std::vector<T> values(128, -17);
+ for (int i = 0; i < 64; ++i) {
+ values[i] = std::numeric_limits<T>::min() + i;
+ }
+ test_encode_decode_page_template<type,
segment_v2::RlePageBuilder<type>,
+
segment_v2::RlePageDecoder<type>>(values.data(),
+
values.size());
+ };
+ check.template operator()<FieldType::OLAP_FIELD_TYPE_TINYINT>();
+ check.template operator()<FieldType::OLAP_FIELD_TYPE_SMALLINT>();
+ check.template operator()<FieldType::OLAP_FIELD_TYPE_INT>();
+ check.template operator()<FieldType::OLAP_FIELD_TYPE_BIGINT>();
+}
+
+TEST_F(RlePageTest, TruncatedInt32BatchPreservesColumnAndPosition) {
+ constexpr auto type = FieldType::OLAP_FIELD_TYPE_INT;
+ for (const uint8_t indicator : {3, 16}) {
+ // One complete repeated value, then a run with only three bytes of
its first INT32.
+ const uint8_t bytes[] = {9, 0, 0, 0, 2, 42, 0, 0, 0, indicator, 1, 2,
3};
+ segment_v2::RlePageDecoder<type> decoder(Slice(bytes, sizeof(bytes)),
{});
+ ASSERT_TRUE(decoder.init().ok());
+ MutableColumnPtr column = ColumnInt32::create();
+ auto& data = assert_cast<ColumnInt32&>(*column).get_data();
+ data.push_back(-1);
+ size_t n = 9;
+ EXPECT_FALSE(decoder.next_batch(&n, column).ok());
+ ASSERT_EQ(1, data.size());
+ EXPECT_EQ(-1, data[0]);
+ EXPECT_EQ(0, decoder.current_index());
+ n = 1;
+ ASSERT_TRUE(decoder.next_batch(&n, column).ok());
+ ASSERT_EQ(2, data.size());
+ EXPECT_EQ(42, data[1]);
+ }
+}
+
TEST_F(RlePageTest, TestRleBoolBlockEncoderSize) {
size_t size = 100;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]