Coverage Report

Created: 2026-10-09 07:21

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 "storage/types.h"                // for TypeTraits
26
#include "util/coding.h"                  // for encode_fixed32_le/decode_fixed32_le
27
#include "util/rle_encoding.h"            // for RleEncoder/RleDecoder
28
#include "util/slice.h"                   // for OwnedSlice
29
30
namespace doris {
31
namespace segment_v2 {
32
33
enum { RLE_PAGE_HEADER_SIZE = 4 };
34
35
// RLE builder for generic integer and bool types. What is missing is some way
36
// to enforce that this can only be instantiated for INT and BOOL types.
37
//
38
// The page format is as follows:
39
//
40
// 1. Header: (4 bytes total)
41
//
42
//    <num_elements> [32-bit]
43
//      The number of elements encoded in the page.
44
//
45
//    NOTE: all on-disk ints are encoded little-endian
46
//
47
// 2. Element data
48
//
49
//    The header is followed by the rle-encoded element data.
50
//
51
// This Rle encoding algorithm is only effective for repeated INT type and bool type,
52
// It is not good for sequence number or random number. BitshufflePage is recommended
53
// for these case.
54
//
55
// TODO(hkp): optimize rle algorithm
56
template <FieldType Type>
57
class RlePageBuilder : public PageBuilderHelper<RlePageBuilder<Type> > {
58
public:
59
    using Self = RlePageBuilder<Type>;
60
    friend class PageBuilderHelper<Self>;
61
62
13.7k
    Status init() override {
63
13.7k
        switch (Type) {
64
13.7k
        case FieldType::OLAP_FIELD_TYPE_BOOL: {
65
13.7k
            _bit_width = 1;
66
13.7k
            break;
67
0
        }
68
0
        default: {
69
0
            _bit_width = SIZE_OF_TYPE * 8;
70
0
            break;
71
0
        }
72
13.7k
        }
73
13.7k
        _rle_encoder = new RleEncoder<CppType>(&_buf, _bit_width);
74
13.7k
        return reset();
75
13.7k
    }
76
77
13.7k
    ~RlePageBuilder() { delete _rle_encoder; }
78
79
223k
    bool is_page_full() override { return _rle_encoder->len() >= _options.data_page_size; }
80
81
223k
    Status add(const uint8_t* vals, size_t* count) override {
82
223k
        DCHECK(!_finished);
83
223k
        auto new_vals = reinterpret_cast<const CppType*>(vals);
84
4.41M
        for (int i = 0; i < *count; ++i) {
85
            // note: vals is not guaranteed to be aligned for now, thus memcpy here
86
4.18M
            CppType value;
87
4.18M
            memcpy(&value, &new_vals[i], SIZE_OF_TYPE);
88
4.18M
            _rle_encoder->Put(value);
89
4.18M
        }
90
91
223k
        _count += *count;
92
223k
        _raw_data_size += *count * SIZE_OF_TYPE;
93
223k
        return Status::OK();
94
223k
    }
95
96
12.6k
    Status finish(OwnedSlice* slice) override {
97
12.6k
        DCHECK(!_finished);
98
12.6k
        _finished = true;
99
        // here should Flush first and then encode the count header
100
        // or it will lead to a bug if the header is less than 8 byte and the data is small
101
12.6k
        _rle_encoder->Flush();
102
12.6k
        encode_fixed32_le(&_buf[0], cast_set<uint32_t>(_count));
103
12.6k
        *slice = _buf.build();
104
12.6k
        return Status::OK();
105
12.6k
    }
106
107
26.4k
    Status reset() override {
108
26.4k
        RETURN_IF_CATCH_EXCEPTION({
109
26.4k
            _count = 0;
110
26.4k
            _finished = false;
111
26.4k
            _raw_data_size = 0;
112
26.4k
            _rle_encoder->Clear();
113
26.4k
            _rle_encoder->Reserve(RLE_PAGE_HEADER_SIZE, 0);
114
26.4k
        });
115
26.4k
        return Status::OK();
116
26.4k
    }
117
118
1
    size_t count() const override { return _count; }
119
120
586
    uint64_t size() const override { return _rle_encoder->len(); }
121
122
12.6k
    uint64_t get_raw_data_size() const override { return _raw_data_size; }
123
124
private:
125
    RlePageBuilder(const PageBuilderOptions& options)
126
13.7k
            : _options(options),
127
13.7k
              _count(0),
128
13.7k
              _finished(false),
129
13.7k
              _bit_width(0),
130
13.7k
              _rle_encoder(nullptr) {}
131
132
    typedef typename TypeTraits<Type>::CppType CppType;
133
    enum { SIZE_OF_TYPE = TypeTraits<Type>::size };
134
135
    PageBuilderOptions _options;
136
    size_t _count;
137
    bool _finished;
138
    int _bit_width;
139
    RleEncoder<CppType>* _rle_encoder = nullptr;
140
    faststring _buf;
141
    uint64_t _raw_data_size = 0;
142
};
143
144
template <FieldType Type>
145
class RlePageDecoder : public PageDecoder {
146
public:
147
    RlePageDecoder(Slice slice, const PageDecoderOptions& options)
148
27.3k
            : _data(slice),
149
27.3k
              _options(options),
150
27.3k
              _parsed(false),
151
27.3k
              _num_elements(0),
152
27.3k
              _cur_index(0),
153
27.3k
              _bit_width(0) {}
154
155
27.4k
    Status init() override {
156
27.4k
        CHECK(!_parsed);
157
158
27.4k
        if (_data.size < RLE_PAGE_HEADER_SIZE) {
159
0
            return Status::Corruption("not enough bytes for header in RleBitMapBlockDecoder");
160
0
        }
161
27.4k
        _num_elements = decode_fixed32_le((const uint8_t*)&_data[0]);
162
163
27.4k
        _parsed = true;
164
165
27.4k
        switch (Type) {
166
27.4k
        case FieldType::OLAP_FIELD_TYPE_BOOL: {
167
27.4k
            _bit_width = 1;
168
27.4k
            break;
169
0
        }
170
0
        default: {
171
0
            _bit_width = SIZE_OF_TYPE * 8;
172
0
            break;
173
0
        }
174
27.4k
        }
175
176
27.4k
        _rle_decoder =
177
27.4k
                RleDecoder<CppType>((uint8_t*)_data.data + RLE_PAGE_HEADER_SIZE,
178
27.4k
                                    cast_set<int>(_data.size - RLE_PAGE_HEADER_SIZE), _bit_width);
179
180
27.4k
        RETURN_IF_ERROR(seek_to_position_in_page(0));
181
27.4k
        return Status::OK();
182
27.4k
    }
183
184
293k
    Status seek_to_position_in_page(size_t pos) override {
185
18.4E
        DCHECK(_parsed) << "Must call init()";
186
293k
        DCHECK_LE(pos, _num_elements)
187
0
                << "Tried to seek to " << pos << " which is > number of elements (" << _num_elements
188
0
                << ") in the block!";
189
        // If the block is empty (e.g. the column is filled with nulls), there is no data to seek.
190
293k
        if (_num_elements == 0) [[unlikely]] {
191
1.85k
            if (pos != 0) {
192
0
                return Status::Error<ErrorCode::INTERNAL_ERROR, false>(
193
0
                        "seek pos {} is larger than total elements  {}", pos, _num_elements);
194
1.85k
            } else {
195
1.85k
                return Status::OK();
196
1.85k
            }
197
1.85k
        }
198
292k
        if (_cur_index == pos) {
199
            // No need to seek.
200
61.9k
            return Status::OK();
201
230k
        } else if (_cur_index < pos) {
202
228k
            size_t nskip = pos - _cur_index;
203
228k
            _rle_decoder.Skip(nskip);
204
228k
        } else {
205
1.63k
            _rle_decoder = RleDecoder<CppType>((uint8_t*)_data.data + RLE_PAGE_HEADER_SIZE,
206
1.63k
                                               cast_set<int>(_data.size - RLE_PAGE_HEADER_SIZE),
207
1.63k
                                               _bit_width);
208
1.63k
            _rle_decoder.Skip(pos);
209
1.63k
        }
210
230k
        _cur_index = pos;
211
230k
        return Status::OK();
212
292k
    }
213
214
30.2k
    Status next_batch(size_t* n, MutableColumnPtr& dst) override {
215
30.2k
        DCHECK(_parsed);
216
30.2k
        if (*n == 0 || _cur_index >= _num_elements) [[unlikely]] {
217
0
            *n = 0;
218
0
            return Status::OK();
219
0
        }
220
221
30.2k
        size_t to_fetch = std::min(*n, static_cast<size_t>(_num_elements - _cur_index));
222
30.2k
        size_t remaining = to_fetch;
223
30.2k
        bool result = false;
224
30.2k
        CppType value;
225
6.61M
        while (remaining > 0) {
226
6.58M
            result = _rle_decoder.Get(&value);
227
6.58M
            DCHECK(result);
228
6.58M
            dst->insert_data((char*)(&value), SIZE_OF_TYPE);
229
6.58M
            remaining--;
230
6.58M
        }
231
232
30.2k
        _cur_index += to_fetch;
233
30.2k
        *n = to_fetch;
234
30.2k
        return Status::OK();
235
30.2k
    }
236
237
    Status read_by_rowids(const rowid_t* rowids, ordinal_t page_first_ordinal, size_t* n,
238
42.1k
                          MutableColumnPtr& dst) override {
239
42.1k
        DCHECK(_parsed);
240
42.1k
        if (*n == 0 || _cur_index >= _num_elements) [[unlikely]] {
241
0
            *n = 0;
242
0
            return Status::OK();
243
0
        }
244
245
42.1k
        auto total = *n;
246
42.1k
        bool result = false;
247
42.1k
        size_t read_count = 0;
248
42.1k
        CppType value;
249
959k
        for (size_t i = 0; i < total; ++i) {
250
917k
            ordinal_t ord = rowids[i] - page_first_ordinal;
251
917k
            if (UNLIKELY(ord >= _num_elements)) {
252
0
                *n = read_count;
253
0
                return Status::OK();
254
0
            }
255
256
917k
            _rle_decoder.Skip(ord - _cur_index);
257
917k
            _cur_index = ord;
258
259
917k
            result = _rle_decoder.Get(&value);
260
917k
            _cur_index++;
261
917k
            DCHECK(result);
262
917k
            dst->insert_data((char*)(&value), SIZE_OF_TYPE);
263
917k
            read_count++;
264
917k
        }
265
42.1k
        *n = read_count;
266
42.1k
        return Status::OK();
267
42.1k
    }
268
269
1
    size_t count() const override { return _num_elements; }
270
271
289k
    size_t current_index() const override { return _cur_index; }
272
273
private:
274
    typedef typename TypeTraits<Type>::CppType CppType;
275
    enum { SIZE_OF_TYPE = TypeTraits<Type>::size };
276
277
    Slice _data;
278
    PageDecoderOptions _options;
279
    bool _parsed;
280
    uint32_t _num_elements;
281
    size_t _cur_index;
282
    int _bit_width;
283
    RleDecoder<CppType> _rle_decoder;
284
};
285
286
} // namespace segment_v2
287
} // namespace doris