Coverage Report

Created: 2026-08-27 11:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/rle_page.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include "common/cast_set.h"
21
#include "storage/field_type.h"           // for FieldType
22
#include "storage/segment/options.h"      // for PageBuilderOptions/PageDecoderOptions
23
#include "storage/segment/page_builder.h" // for PageBuilder
24
#include "storage/segment/page_decoder.h" // for PageDecoder
25
#include "util/coding.h"                  // for encode_fixed32_le/decode_fixed32_le
26
#include "util/rle_encoding.h"            // for RleEncoder/RleDecoder
27
#include "util/slice.h"                   // for OwnedSlice
28
29
namespace doris {
30
namespace segment_v2 {
31
32
enum { RLE_PAGE_HEADER_SIZE = 4 };
33
34
// RLE builder for generic integer and bool types. What is missing is some way
35
// to enforce that this can only be instantiated for INT and BOOL types.
36
//
37
// The page format is as follows:
38
//
39
// 1. Header: (4 bytes total)
40
//
41
//    <num_elements> [32-bit]
42
//      The number of elements encoded in the page.
43
//
44
//    NOTE: all on-disk ints are encoded little-endian
45
//
46
// 2. Element data
47
//
48
//    The header is followed by the rle-encoded element data.
49
//
50
// This Rle encoding algorithm is only effective for repeated INT type and bool type,
51
// It is not good for sequence number or random number. BitshufflePage is recommended
52
// for these case.
53
//
54
// TODO(hkp): optimize rle algorithm
55
template <FieldType Type>
56
class RlePageBuilder : public PageBuilderHelper<RlePageBuilder<Type> > {
57
public:
58
    using Self = RlePageBuilder<Type>;
59
    friend class PageBuilderHelper<Self>;
60
61
14.9k
    Status init() override {
62
14.9k
        switch (Type) {
63
14.9k
        case FieldType::OLAP_FIELD_TYPE_BOOL: {
64
14.9k
            _bit_width = 1;
65
14.9k
            break;
66
0
        }
67
0
        default: {
68
0
            _bit_width = SIZE_OF_TYPE * 8;
69
0
            break;
70
0
        }
71
14.9k
        }
72
14.9k
        _rle_encoder = new RleEncoder<CppType>(&_buf, _bit_width);
73
14.9k
        return reset();
74
14.9k
    }
75
76
14.9k
    ~RlePageBuilder() { delete _rle_encoder; }
77
78
235k
    bool is_page_full() override { return _rle_encoder->len() >= _options.data_page_size; }
79
80
235k
    Status add(const uint8_t* vals, size_t* count) override {
81
235k
        DCHECK(!_finished);
82
235k
        auto new_vals = reinterpret_cast<const CppType*>(vals);
83
4.47M
        for (int i = 0; i < *count; ++i) {
84
            // note: vals is not guaranteed to be aligned for now, thus memcpy here
85
4.23M
            CppType value;
86
4.23M
            memcpy(&value, &new_vals[i], SIZE_OF_TYPE);
87
4.23M
            _rle_encoder->Put(value);
88
4.23M
        }
89
90
235k
        _count += *count;
91
235k
        _raw_data_size += *count * SIZE_OF_TYPE;
92
235k
        return Status::OK();
93
235k
    }
94
95
13.8k
    Status finish(OwnedSlice* slice) override {
96
13.8k
        DCHECK(!_finished);
97
13.8k
        _finished = true;
98
        // here should Flush first and then encode the count header
99
        // or it will lead to a bug if the header is less than 8 byte and the data is small
100
13.8k
        _rle_encoder->Flush();
101
13.8k
        encode_fixed32_le(&_buf[0], cast_set<uint32_t>(_count));
102
13.8k
        *slice = _buf.build();
103
13.8k
        return Status::OK();
104
13.8k
    }
105
106
28.8k
    Status reset() override {
107
28.8k
        RETURN_IF_CATCH_EXCEPTION({
108
28.8k
            _count = 0;
109
28.8k
            _finished = false;
110
28.8k
            _raw_data_size = 0;
111
28.8k
            _rle_encoder->Clear();
112
28.8k
            _rle_encoder->Reserve(RLE_PAGE_HEADER_SIZE, 0);
113
28.8k
        });
114
28.8k
        return Status::OK();
115
28.8k
    }
116
117
0
    size_t count() const override { return _count; }
118
119
532
    uint64_t size() const override { return _rle_encoder->len(); }
120
121
13.8k
    uint64_t get_raw_data_size() const override { return _raw_data_size; }
122
123
private:
124
    RlePageBuilder(const PageBuilderOptions& options)
125
14.9k
            : _options(options),
126
14.9k
              _count(0),
127
14.9k
              _finished(false),
128
14.9k
              _bit_width(0),
129
14.9k
              _rle_encoder(nullptr) {}
130
131
    typedef typename TypeTraits<Type>::CppType CppType;
132
    enum { SIZE_OF_TYPE = TypeTraits<Type>::size };
133
134
    PageBuilderOptions _options;
135
    size_t _count;
136
    bool _finished;
137
    int _bit_width;
138
    RleEncoder<CppType>* _rle_encoder = nullptr;
139
    faststring _buf;
140
    uint64_t _raw_data_size = 0;
141
};
142
143
template <FieldType Type>
144
class RlePageDecoder : public PageDecoder {
145
public:
146
    RlePageDecoder(Slice slice, const PageDecoderOptions& options)
147
27.7k
            : _data(slice),
148
27.7k
              _options(options),
149
27.7k
              _parsed(false),
150
27.7k
              _num_elements(0),
151
27.7k
              _cur_index(0),
152
27.7k
              _bit_width(0) {}
153
154
27.8k
    Status init() override {
155
27.8k
        CHECK(!_parsed);
156
157
27.8k
        if (_data.size < RLE_PAGE_HEADER_SIZE) {
158
0
            return Status::Corruption("not enough bytes for header in RleBitMapBlockDecoder");
159
0
        }
160
27.8k
        _num_elements = decode_fixed32_le((const uint8_t*)&_data[0]);
161
162
27.8k
        _parsed = true;
163
164
27.8k
        switch (Type) {
165
27.8k
        case FieldType::OLAP_FIELD_TYPE_BOOL: {
166
27.8k
            _bit_width = 1;
167
27.8k
            break;
168
0
        }
169
0
        default: {
170
0
            _bit_width = SIZE_OF_TYPE * 8;
171
0
            break;
172
0
        }
173
27.8k
        }
174
175
27.9k
        _rle_decoder =
176
27.9k
                RleDecoder<CppType>((uint8_t*)_data.data + RLE_PAGE_HEADER_SIZE,
177
27.9k
                                    cast_set<int>(_data.size - RLE_PAGE_HEADER_SIZE), _bit_width);
178
179
27.9k
        RETURN_IF_ERROR(seek_to_position_in_page(0));
180
27.9k
        return Status::OK();
181
27.9k
    }
182
183
320k
    Status seek_to_position_in_page(size_t pos) override {
184
18.4E
        DCHECK(_parsed) << "Must call init()";
185
320k
        DCHECK_LE(pos, _num_elements)
186
0
                << "Tried to seek to " << pos << " which is > number of elements (" << _num_elements
187
0
                << ") in the block!";
188
        // If the block is empty (e.g. the column is filled with nulls), there is no data to seek.
189
320k
        if (_num_elements == 0) [[unlikely]] {
190
1.83k
            if (pos != 0) {
191
0
                return Status::Error<ErrorCode::INTERNAL_ERROR, false>(
192
0
                        "seek pos {} is larger than total elements  {}", pos, _num_elements);
193
1.83k
            } else {
194
1.83k
                return Status::OK();
195
1.83k
            }
196
1.83k
        }
197
318k
        if (_cur_index == pos) {
198
            // No need to seek.
199
62.4k
            return Status::OK();
200
256k
        } else if (_cur_index < pos) {
201
254k
            size_t nskip = pos - _cur_index;
202
254k
            _rle_decoder.Skip(nskip);
203
254k
        } else {
204
1.64k
            _rle_decoder = RleDecoder<CppType>((uint8_t*)_data.data + RLE_PAGE_HEADER_SIZE,
205
1.64k
                                               cast_set<int>(_data.size - RLE_PAGE_HEADER_SIZE),
206
1.64k
                                               _bit_width);
207
1.64k
            _rle_decoder.Skip(pos);
208
1.64k
        }
209
256k
        _cur_index = pos;
210
256k
        return Status::OK();
211
318k
    }
212
213
31.6k
    Status next_batch(size_t* n, MutableColumnPtr& dst) override {
214
31.6k
        DCHECK(_parsed);
215
31.6k
        if (*n == 0 || _cur_index >= _num_elements) [[unlikely]] {
216
0
            *n = 0;
217
0
            return Status::OK();
218
0
        }
219
220
31.6k
        size_t to_fetch = std::min(*n, static_cast<size_t>(_num_elements - _cur_index));
221
31.6k
        size_t remaining = to_fetch;
222
31.6k
        bool result = false;
223
31.6k
        CppType value;
224
6.57M
        while (remaining > 0) {
225
6.54M
            result = _rle_decoder.Get(&value);
226
6.54M
            DCHECK(result);
227
6.54M
            dst->insert_data((char*)(&value), SIZE_OF_TYPE);
228
6.54M
            remaining--;
229
6.54M
        }
230
231
31.6k
        _cur_index += to_fetch;
232
31.6k
        *n = to_fetch;
233
31.6k
        return Status::OK();
234
31.6k
    }
235
236
    Status read_by_rowids(const rowid_t* rowids, ordinal_t page_first_ordinal, size_t* n,
237
42.1k
                          MutableColumnPtr& dst) override {
238
42.1k
        DCHECK(_parsed);
239
42.1k
        if (*n == 0 || _cur_index >= _num_elements) [[unlikely]] {
240
0
            *n = 0;
241
0
            return Status::OK();
242
0
        }
243
244
42.1k
        auto total = *n;
245
42.1k
        bool result = false;
246
42.1k
        size_t read_count = 0;
247
42.1k
        CppType value;
248
968k
        for (size_t i = 0; i < total; ++i) {
249
926k
            ordinal_t ord = rowids[i] - page_first_ordinal;
250
926k
            if (UNLIKELY(ord >= _num_elements)) {
251
0
                *n = read_count;
252
0
                return Status::OK();
253
0
            }
254
255
926k
            _rle_decoder.Skip(ord - _cur_index);
256
926k
            _cur_index = ord;
257
258
926k
            result = _rle_decoder.Get(&value);
259
926k
            _cur_index++;
260
926k
            DCHECK(result);
261
926k
            dst->insert_data((char*)(&value), SIZE_OF_TYPE);
262
926k
            read_count++;
263
926k
        }
264
42.1k
        *n = read_count;
265
42.1k
        return Status::OK();
266
42.1k
    }
267
268
0
    size_t count() const override { return _num_elements; }
269
270
314k
    size_t current_index() const override { return _cur_index; }
271
272
private:
273
    typedef typename TypeTraits<Type>::CppType CppType;
274
    enum { SIZE_OF_TYPE = TypeTraits<Type>::size };
275
276
    Slice _data;
277
    PageDecoderOptions _options;
278
    bool _parsed;
279
    uint32_t _num_elements;
280
    size_t _cur_index;
281
    int _bit_width;
282
    RleDecoder<CppType> _rle_decoder;
283
};
284
285
} // namespace segment_v2
286
} // namespace doris