Coverage Report

Created: 2026-03-14 21:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/rle_encoding.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
#pragma once
18
19
#include <glog/logging.h>
20
21
#include <limits> // IWYU pragma: keep
22
23
#include "common/cast_set.h"
24
#include "util/bit_stream_utils.inline.h"
25
#include "util/bit_util.h"
26
27
namespace doris {
28
#include "common/compile_check_begin.h"
29
30
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
31
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
32
// (literal encoding).
33
// For both types of runs, there is a byte-aligned indicator which encodes the length
34
// of the run and the type of the run.
35
// This encoding has the benefit that when there aren't any long enough runs, values
36
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
37
// the run length are byte aligned. This allows for very efficient decoding
38
// implementations.
39
// The encoding is:
40
//    encoded-block := run*
41
//    run := literal-run | repeated-run
42
//    literal-run := literal-indicator < literal bytes >
43
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
44
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
45
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
46
//
47
// Each run is preceded by a varint. The varint's least significant bit is
48
// used to indicate whether the run is a literal run or a repeated run. The rest
49
// of the varint is used to determine the length of the run (eg how many times the
50
// value repeats).
51
//
52
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
53
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
54
// on a byte boundary without padding.
55
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
56
// the actual number of encoded ints. (This means that the total number of encoded values
57
// can not be determined from the encoded data, since the number of values in the last
58
// group may not be a multiple of 8).
59
// There is a break-even point when it is more storage efficient to do run length
60
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
61
// for both the repeated encoding or the literal encoding.  This value can always
62
// be computed based on the bit-width.
63
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
64
//
65
// Examples with bit-width 1 (eg encoding booleans):
66
// ----------------------------------------
67
// 100 1s followed by 100 0s:
68
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
69
//  - (total 4 bytes)
70
//
71
// alternating 1s and 0s (200 total):
72
// 200 ints = 25 groups of 8
73
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
74
// (total 26 bytes, 1 byte overhead)
75
//
76
77
// Decoder class for RLE encoded data.
78
//
79
// NOTE: the encoded format does not have any length prefix or any other way of
80
// indicating that the encoded sequence ends at a certain point, so the Decoder
81
// methods may return some extra bits at the end before the read methods start
82
// to return 0/false.
83
template <typename T>
84
class RleDecoder {
85
public:
86
    // Create a decoder object. buffer/buffer_len is the decoded data.
87
    // bit_width is the width of each value (before encoding).
88
    RleDecoder(const uint8_t* buffer, int buffer_len, int bit_width)
89
472k
            : bit_reader_(buffer, buffer_len),
90
472k
              bit_width_(bit_width),
91
472k
              current_value_(0),
92
472k
              repeat_count_(0),
93
472k
              literal_count_(0),
94
472k
              rewind_state_(CANT_REWIND) {
95
472k
        DCHECK_GE(bit_width_, 1);
96
472k
        DCHECK_LE(bit_width_, 64);
97
472k
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
89
450k
            : bit_reader_(buffer, buffer_len),
90
450k
              bit_width_(bit_width),
91
450k
              current_value_(0),
92
450k
              repeat_count_(0),
93
450k
              literal_count_(0),
94
450k
              rewind_state_(CANT_REWIND) {
95
450k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
450k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
89
20.5k
            : bit_reader_(buffer, buffer_len),
90
20.5k
              bit_width_(bit_width),
91
20.5k
              current_value_(0),
92
20.5k
              repeat_count_(0),
93
20.5k
              literal_count_(0),
94
20.5k
              rewind_state_(CANT_REWIND) {
95
20.5k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
20.5k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
89
1.51k
            : bit_reader_(buffer, buffer_len),
90
1.51k
              bit_width_(bit_width),
91
1.51k
              current_value_(0),
92
1.51k
              repeat_count_(0),
93
1.51k
              literal_count_(0),
94
1.51k
              rewind_state_(CANT_REWIND) {
95
1.51k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
1.51k
    }
98
99
30.0M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
99
30.0M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
99
18.3k
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
99
2.34k
    RleDecoder() {}
100
101
    // Skip n values, and returns the number of non-zero entries skipped.
102
    size_t Skip(size_t to_skip);
103
104
    // Gets the next value.  Returns false if there are no more.
105
    bool Get(T* val);
106
107
    // Seek to the previous value.
108
    void RewindOne();
109
110
    // Gets the next run of the same 'val'. Returns 0 if there is no
111
    // more data to be decoded. Will return a run of at most 'max_run'
112
    // values. If there are more values than this, the next call to
113
    // GetNextRun will return more from the same run.
114
    size_t GetNextRun(T* val, size_t max_run);
115
116
    size_t get_values(T* values, size_t num_values);
117
118
    // Get the count of current repeated value
119
    size_t repeated_count();
120
121
    // Get current repeated value, make sure that count equals repeated_count()
122
    T get_repeated_value(size_t count);
123
124
0
    const BitReader& bit_reader() const { return bit_reader_; }
125
126
private:
127
    bool ReadHeader();
128
129
    enum RewindState { REWIND_LITERAL, REWIND_RUN, CANT_REWIND };
130
131
    BitReader bit_reader_;
132
    int bit_width_;
133
    uint64_t current_value_;
134
    uint32_t repeat_count_;
135
    uint32_t literal_count_;
136
    RewindState rewind_state_;
137
};
138
139
// Class to incrementally build the rle data.
140
// The encoding has two modes: encoding repeated runs and literal runs.
141
// If the run is sufficiently short, it is more efficient to encode as a literal run.
142
// This class does so by buffering 8 values at a time.  If they are not all the same
143
// they are added to the literal run.  If they are the same, they are added to the
144
// repeated run.  When we switch modes, the previous run is flushed out.
145
template <typename T>
146
class RleEncoder {
147
public:
148
    // buffer: buffer to write bits to.
149
    // bit_width: max number of bits for value.
150
    // TODO: consider adding a min_repeated_run_length so the caller can control
151
    // when values should be encoded as repeated runs.  Currently this is derived
152
    // based on the bit_width, which can determine a storage optimal choice.
153
    explicit RleEncoder(faststring* buffer, int bit_width)
154
543k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
543k
        DCHECK_GE(bit_width_, 1);
156
543k
        DCHECK_LE(bit_width_, 64);
157
543k
        Clear();
158
543k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
154
12.9k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
12.9k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
12.9k
        Clear();
158
12.9k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
154
530k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
530k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
530k
        Clear();
158
530k
    }
159
160
    // Reserve 'num_bytes' bytes for a plain encoded header, set each
161
    // byte with 'val': this is used for the RLE-encoded data blocks in
162
    // order to be able to able to store the initial ordinal position
163
    // and number of elements. This is a part of RleEncoder in order to
164
    // maintain the correct offset in 'buffer'.
165
    void Reserve(int num_bytes, uint8_t val);
166
167
    // Encode value. This value must be representable with bit_width_ bits.
168
    void Put(T value, size_t run_length = 1);
169
170
    // Flushes any pending values to the underlying buffer.
171
    // Returns the total number of bytes written
172
    int Flush();
173
174
    // Resets all the state in the encoder.
175
    void Clear();
176
177
236k
    int32_t len() const { return bit_writer_.bytes_written(); }
178
179
private:
180
    // Flushes any buffered values.  If this is part of a repeated run, this is largely
181
    // a no-op.
182
    // If it is part of a literal run, this will call FlushLiteralRun, which writes
183
    // out the buffered literal values.
184
    // If 'done' is true, the current run would be written even if it would normally
185
    // have been buffered more.  This should only be called at the end, when the
186
    // encoder has received all values even if it would normally continue to be
187
    // buffered.
188
    void FlushBufferedValues(bool done);
189
190
    // Flushes literal values to the underlying buffer.  If update_indicator_byte,
191
    // then the current literal run is complete and the indicator byte is updated.
192
    void FlushLiteralRun(bool update_indicator_byte);
193
194
    // Flushes a repeated run to the underlying buffer.
195
    void FlushRepeatedRun();
196
197
    // Number of bits needed to encode the value.
198
    const int bit_width_;
199
200
    // Underlying buffer.
201
    BitWriter bit_writer_;
202
203
    // We need to buffer at most 8 values for literals.  This happens when the
204
    // bit_width is 1 (so 8 values fit in one byte).
205
    // TODO: generalize this to other bit widths
206
    uint64_t buffered_values_[8];
207
208
    // Number of values in buffered_values_
209
    int num_buffered_values_;
210
211
    // The current (also last) value that was written and the count of how
212
    // many times in a row that value has been seen.  This is maintained even
213
    // if we are in a literal run.  If the repeat_count_ get high enough, we switch
214
    // to encoding repeated runs.
215
    uint64_t current_value_;
216
    int repeat_count_;
217
218
    // Number of literals in the current run.  This does not include the literals
219
    // that might be in buffered_values_.  Only after we've got a group big enough
220
    // can we decide if they should part of the literal_count_ or repeat_count_
221
    int literal_count_;
222
223
    // Index of a byte in the underlying buffer that stores the indicator byte.
224
    // This is reserved as soon as we need a literal run but the value is written
225
    // when the literal run is complete. We maintain an index rather than a pointer
226
    // into the underlying buffer because the pointer value may become invalid if
227
    // the underlying buffer is resized.
228
    int literal_indicator_byte_idx_;
229
};
230
231
template <typename T>
232
21.5M
bool RleDecoder<T>::ReadHeader() {
233
21.5M
    DCHECK(bit_reader_.is_initialized());
234
21.5M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
2.02M
        uint32_t indicator_value = 0;
238
2.02M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
2.02M
        if (!result) [[unlikely]] {
240
2.65k
            return false;
241
2.65k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
2.02M
        bool is_literal = indicator_value & 1;
245
2.02M
        if (is_literal) {
246
1.08M
            literal_count_ = (indicator_value >> 1) * 8;
247
1.08M
            DCHECK_GT(literal_count_, 0);
248
1.08M
        } else {
249
938k
            repeat_count_ = indicator_value >> 1;
250
938k
            DCHECK_GT(repeat_count_, 0);
251
938k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
938k
                                                     reinterpret_cast<T*>(&current_value_));
253
938k
            DCHECK(result1);
254
938k
        }
255
2.02M
    }
256
21.5M
    return true;
257
21.5M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
232
8.43M
bool RleDecoder<T>::ReadHeader() {
233
8.43M
    DCHECK(bit_reader_.is_initialized());
234
8.43M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
172k
        uint32_t indicator_value = 0;
238
172k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
172k
        if (!result) [[unlikely]] {
240
8
            return false;
241
8
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
172k
        bool is_literal = indicator_value & 1;
245
172k
        if (is_literal) {
246
85.9k
            literal_count_ = (indicator_value >> 1) * 8;
247
85.9k
            DCHECK_GT(literal_count_, 0);
248
86.6k
        } else {
249
86.6k
            repeat_count_ = indicator_value >> 1;
250
86.6k
            DCHECK_GT(repeat_count_, 0);
251
86.6k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
86.6k
                                                     reinterpret_cast<T*>(&current_value_));
253
86.6k
            DCHECK(result1);
254
86.6k
        }
255
172k
    }
256
8.43M
    return true;
257
8.43M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
232
5.09M
bool RleDecoder<T>::ReadHeader() {
233
5.09M
    DCHECK(bit_reader_.is_initialized());
234
5.09M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
1.71M
        uint32_t indicator_value = 0;
238
1.71M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
1.71M
        if (!result) [[unlikely]] {
240
2.64k
            return false;
241
2.64k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
1.70M
        bool is_literal = indicator_value & 1;
245
1.70M
        if (is_literal) {
246
913k
            literal_count_ = (indicator_value >> 1) * 8;
247
913k
            DCHECK_GT(literal_count_, 0);
248
913k
        } else {
249
793k
            repeat_count_ = indicator_value >> 1;
250
793k
            DCHECK_GT(repeat_count_, 0);
251
793k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
793k
                                                     reinterpret_cast<T*>(&current_value_));
253
793k
            DCHECK(result1);
254
793k
        }
255
1.70M
    }
256
5.08M
    return true;
257
5.09M
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
232
8.02M
bool RleDecoder<T>::ReadHeader() {
233
8.02M
    DCHECK(bit_reader_.is_initialized());
234
8.02M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
145k
        uint32_t indicator_value = 0;
238
145k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
145k
        if (!result) [[unlikely]] {
240
0
            return false;
241
0
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
145k
        bool is_literal = indicator_value & 1;
245
145k
        if (is_literal) {
246
87.4k
            literal_count_ = (indicator_value >> 1) * 8;
247
87.4k
            DCHECK_GT(literal_count_, 0);
248
87.4k
        } else {
249
57.7k
            repeat_count_ = indicator_value >> 1;
250
57.7k
            DCHECK_GT(repeat_count_, 0);
251
57.7k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
57.7k
                                                     reinterpret_cast<T*>(&current_value_));
253
57.7k
            DCHECK(result1);
254
57.7k
        }
255
145k
    }
256
8.02M
    return true;
257
8.02M
}
258
259
template <typename T>
260
16.2M
bool RleDecoder<T>::Get(T* val) {
261
16.2M
    DCHECK(bit_reader_.is_initialized());
262
16.2M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
16.2M
    if (repeat_count_ > 0) [[likely]] {
267
7.87M
        *val = cast_set<T>(current_value_);
268
7.87M
        --repeat_count_;
269
7.87M
        rewind_state_ = REWIND_RUN;
270
8.40M
    } else {
271
8.40M
        DCHECK(literal_count_ > 0);
272
8.40M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
8.40M
        DCHECK(result);
274
8.40M
        --literal_count_;
275
8.40M
        rewind_state_ = REWIND_LITERAL;
276
8.40M
    }
277
278
16.2M
    return true;
279
16.2M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
260
8.42M
bool RleDecoder<T>::Get(T* val) {
261
8.42M
    DCHECK(bit_reader_.is_initialized());
262
8.42M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
8.42M
    if (repeat_count_ > 0) [[likely]] {
267
7.61M
        *val = cast_set<T>(current_value_);
268
7.61M
        --repeat_count_;
269
7.61M
        rewind_state_ = REWIND_RUN;
270
7.61M
    } else {
271
814k
        DCHECK(literal_count_ > 0);
272
814k
        bool result = bit_reader_.GetValue(bit_width_, val);
273
814k
        DCHECK(result);
274
814k
        --literal_count_;
275
814k
        rewind_state_ = REWIND_LITERAL;
276
814k
    }
277
278
8.42M
    return true;
279
8.42M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
260
7.84M
bool RleDecoder<T>::Get(T* val) {
261
7.84M
    DCHECK(bit_reader_.is_initialized());
262
7.84M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
7.84M
    if (repeat_count_ > 0) [[likely]] {
267
260k
        *val = cast_set<T>(current_value_);
268
260k
        --repeat_count_;
269
260k
        rewind_state_ = REWIND_RUN;
270
7.58M
    } else {
271
7.58M
        DCHECK(literal_count_ > 0);
272
7.58M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
7.58M
        DCHECK(result);
274
7.58M
        --literal_count_;
275
7.58M
        rewind_state_ = REWIND_LITERAL;
276
7.58M
    }
277
278
7.84M
    return true;
279
7.84M
}
280
281
template <typename T>
282
625
void RleDecoder<T>::RewindOne() {
283
625
    DCHECK(bit_reader_.is_initialized());
284
285
625
    switch (rewind_state_) {
286
0
    case CANT_REWIND:
287
0
        throw Exception(Status::FatalError("Can't rewind more than once after each read!"));
288
0
        break;
289
2
    case REWIND_RUN:
290
2
        ++repeat_count_;
291
2
        break;
292
623
    case REWIND_LITERAL: {
293
623
        bit_reader_.Rewind(bit_width_);
294
623
        ++literal_count_;
295
623
        break;
296
0
    }
297
625
    }
298
299
625
    rewind_state_ = CANT_REWIND;
300
625
}
301
302
template <typename T>
303
3.50M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
3.50M
    DCHECK(bit_reader_.is_initialized());
305
3.50M
    DCHECK_GT(max_run, 0);
306
3.50M
    size_t ret = 0;
307
3.50M
    size_t rem = max_run;
308
4.48M
    while (ReadHeader()) {
309
4.48M
        if (repeat_count_ > 0) [[likely]] {
310
988k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
64.2k
                return ret;
312
64.2k
            }
313
924k
            *val = cast_set<T>(current_value_);
314
924k
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
419k
                repeat_count_ -= rem;
318
419k
                ret += rem;
319
419k
                return ret;
320
419k
            }
321
505k
            ret += repeat_count_;
322
505k
            rem -= repeat_count_;
323
505k
            repeat_count_ = 0;
324
3.49M
        } else {
325
3.49M
            DCHECK(literal_count_ > 0);
326
3.49M
            if (ret == 0) {
327
3.01M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
3.01M
                DCHECK(has_more);
329
3.01M
                literal_count_--;
330
3.01M
                ret++;
331
3.01M
                rem--;
332
3.01M
            }
333
334
8.79M
            while (literal_count_ > 0) {
335
8.31M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
8.31M
                DCHECK(result);
337
8.31M
                if (current_value_ != *val || rem == 0) {
338
3.02M
                    bit_reader_.Rewind(bit_width_);
339
3.02M
                    return ret;
340
3.02M
                }
341
5.29M
                ret++;
342
5.29M
                rem--;
343
5.29M
                literal_count_--;
344
5.29M
            }
345
3.49M
        }
346
4.48M
    }
347
18.4E
    return ret;
348
3.50M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
303
1.11k
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
1.11k
    DCHECK(bit_reader_.is_initialized());
305
1.11k
    DCHECK_GT(max_run, 0);
306
1.11k
    size_t ret = 0;
307
1.11k
    size_t rem = max_run;
308
1.21k
    while (ReadHeader()) {
309
1.20k
        if (repeat_count_ > 0) [[likely]] {
310
984
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
41
                return ret;
312
41
            }
313
943
            *val = cast_set<T>(current_value_);
314
943
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
878
                repeat_count_ -= rem;
318
878
                ret += rem;
319
878
                return ret;
320
878
            }
321
65
            ret += repeat_count_;
322
65
            rem -= repeat_count_;
323
65
            repeat_count_ = 0;
324
223
        } else {
325
223
            DCHECK(literal_count_ > 0);
326
223
            if (ret == 0) {
327
199
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
199
                DCHECK(has_more);
329
199
                literal_count_--;
330
199
                ret++;
331
199
                rem--;
332
199
            }
333
334
597
            while (literal_count_ > 0) {
335
565
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
565
                DCHECK(result);
337
565
                if (current_value_ != *val || rem == 0) {
338
191
                    bit_reader_.Rewind(bit_width_);
339
191
                    return ret;
340
191
                }
341
374
                ret++;
342
374
                rem--;
343
374
                literal_count_--;
344
374
            }
345
223
        }
346
1.20k
    }
347
8
    return ret;
348
1.11k
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
303
3.49M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
3.49M
    DCHECK(bit_reader_.is_initialized());
305
3.49M
    DCHECK_GT(max_run, 0);
306
3.49M
    size_t ret = 0;
307
3.49M
    size_t rem = max_run;
308
4.48M
    while (ReadHeader()) {
309
4.48M
        if (repeat_count_ > 0) [[likely]] {
310
987k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
64.1k
                return ret;
312
64.1k
            }
313
923k
            *val = cast_set<T>(current_value_);
314
923k
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
418k
                repeat_count_ -= rem;
318
418k
                ret += rem;
319
418k
                return ret;
320
418k
            }
321
505k
            ret += repeat_count_;
322
505k
            rem -= repeat_count_;
323
505k
            repeat_count_ = 0;
324
3.49M
        } else {
325
3.49M
            DCHECK(literal_count_ > 0);
326
3.49M
            if (ret == 0) {
327
3.01M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
3.01M
                DCHECK(has_more);
329
3.01M
                literal_count_--;
330
3.01M
                ret++;
331
3.01M
                rem--;
332
3.01M
            }
333
334
8.79M
            while (literal_count_ > 0) {
335
8.31M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
8.31M
                DCHECK(result);
337
8.31M
                if (current_value_ != *val || rem == 0) {
338
3.02M
                    bit_reader_.Rewind(bit_width_);
339
3.02M
                    return ret;
340
3.02M
                }
341
5.29M
                ret++;
342
5.29M
                rem--;
343
5.29M
                literal_count_--;
344
5.29M
            }
345
3.49M
        }
346
4.48M
    }
347
18.4E
    return ret;
348
3.49M
}
349
350
template <typename T>
351
919
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
919
    size_t read_num = 0;
353
5.95k
    while (read_num < num_values) {
354
5.04k
        size_t read_this_time = num_values - read_num;
355
356
5.04k
        if (LIKELY(repeat_count_ > 0)) {
357
1.53k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.53k
            std::fill(values, values + read_this_time, current_value_);
359
1.53k
            values += read_this_time;
360
1.53k
            repeat_count_ -= read_this_time;
361
1.53k
            read_num += read_this_time;
362
3.51k
        } else if (literal_count_ > 0) {
363
1.05k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
11.1k
            for (int i = 0; i < read_this_time; ++i) {
365
10.0k
                bool result = bit_reader_.GetValue(bit_width_, values);
366
10.0k
                DCHECK(result);
367
10.0k
                values++;
368
10.0k
            }
369
1.05k
            literal_count_ -= read_this_time;
370
1.05k
            read_num += read_this_time;
371
2.45k
        } else {
372
2.45k
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.45k
        }
376
5.04k
    }
377
919
    return read_num;
378
919
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
351
914
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
914
    size_t read_num = 0;
353
5.94k
    while (read_num < num_values) {
354
5.03k
        size_t read_this_time = num_values - read_num;
355
356
5.03k
        if (LIKELY(repeat_count_ > 0)) {
357
1.53k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.53k
            std::fill(values, values + read_this_time, current_value_);
359
1.53k
            values += read_this_time;
360
1.53k
            repeat_count_ -= read_this_time;
361
1.53k
            read_num += read_this_time;
362
3.50k
        } else if (literal_count_ > 0) {
363
1.05k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
11.1k
            for (int i = 0; i < read_this_time; ++i) {
365
10.0k
                bool result = bit_reader_.GetValue(bit_width_, values);
366
10.0k
                DCHECK(result);
367
10.0k
                values++;
368
10.0k
            }
369
1.05k
            literal_count_ -= read_this_time;
370
1.05k
            read_num += read_this_time;
371
2.44k
        } else {
372
2.44k
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.44k
        }
376
5.03k
    }
377
914
    return read_num;
378
914
}
_ZN5doris10RleDecoderIhE10get_valuesEPhm
Line
Count
Source
351
5
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
5
    size_t read_num = 0;
353
14
    while (read_num < num_values) {
354
9
        size_t read_this_time = num_values - read_num;
355
356
9
        if (LIKELY(repeat_count_ > 0)) {
357
0
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
0
            std::fill(values, values + read_this_time, current_value_);
359
0
            values += read_this_time;
360
0
            repeat_count_ -= read_this_time;
361
0
            read_num += read_this_time;
362
9
        } else if (literal_count_ > 0) {
363
5
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
40
            for (int i = 0; i < read_this_time; ++i) {
365
35
                bool result = bit_reader_.GetValue(bit_width_, values);
366
35
                DCHECK(result);
367
35
                values++;
368
35
            }
369
5
            literal_count_ -= read_this_time;
370
5
            read_num += read_this_time;
371
5
        } else {
372
4
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
4
        }
376
9
    }
377
5
    return read_num;
378
5
}
379
380
template <typename T>
381
size_t RleDecoder<T>::repeated_count() {
382
    if (repeat_count_ > 0) {
383
        return repeat_count_;
384
    }
385
    if (literal_count_ == 0) {
386
        ReadHeader();
387
    }
388
    return repeat_count_;
389
}
390
391
template <typename T>
392
T RleDecoder<T>::get_repeated_value(size_t count) {
393
    DCHECK_GE(repeat_count_, count);
394
    repeat_count_ -= count;
395
    return current_value_;
396
}
397
398
template <typename T>
399
1.72M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.72M
    DCHECK(bit_reader_.is_initialized());
401
402
1.72M
    size_t set_count = 0;
403
2.47M
    while (to_skip > 0) {
404
743k
        bool result = ReadHeader();
405
743k
        DCHECK(result);
406
407
743k
        if (repeat_count_ > 0) [[likely]] {
408
274k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
274k
            repeat_count_ -= nskip;
410
274k
            to_skip -= nskip;
411
274k
            if (current_value_ != 0) {
412
223k
                set_count += nskip;
413
223k
            }
414
469k
        } else {
415
469k
            DCHECK(literal_count_ > 0);
416
469k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
469k
            literal_count_ -= nskip;
418
469k
            to_skip -= nskip;
419
29.8M
            for (; nskip > 0; nskip--) {
420
29.3M
                T value = 0;
421
29.3M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
29.3M
                DCHECK(result1);
423
29.3M
                if (value != 0) {
424
17.1M
                    set_count++;
425
17.1M
                }
426
29.3M
            }
427
469k
        }
428
743k
    }
429
1.72M
    return set_count;
430
1.72M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
399
345k
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
345k
    DCHECK(bit_reader_.is_initialized());
401
402
345k
    size_t set_count = 0;
403
941k
    while (to_skip > 0) {
404
595k
        bool result = ReadHeader();
405
595k
        DCHECK(result);
406
407
595k
        if (repeat_count_ > 0) [[likely]] {
408
214k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
214k
            repeat_count_ -= nskip;
410
214k
            to_skip -= nskip;
411
214k
            if (current_value_ != 0) {
412
197k
                set_count += nskip;
413
197k
            }
414
381k
        } else {
415
381k
            DCHECK(literal_count_ > 0);
416
381k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
381k
            literal_count_ -= nskip;
418
381k
            to_skip -= nskip;
419
7.38M
            for (; nskip > 0; nskip--) {
420
7.00M
                T value = 0;
421
7.00M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
7.00M
                DCHECK(result1);
423
7.00M
                if (value != 0) {
424
5.57M
                    set_count++;
425
5.57M
                }
426
7.00M
            }
427
381k
        }
428
595k
    }
429
345k
    return set_count;
430
345k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
399
1.38M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.38M
    DCHECK(bit_reader_.is_initialized());
401
402
1.38M
    size_t set_count = 0;
403
1.53M
    while (to_skip > 0) {
404
147k
        bool result = ReadHeader();
405
147k
        DCHECK(result);
406
407
147k
        if (repeat_count_ > 0) [[likely]] {
408
60.0k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
60.0k
            repeat_count_ -= nskip;
410
60.0k
            to_skip -= nskip;
411
60.0k
            if (current_value_ != 0) {
412
26.1k
                set_count += nskip;
413
26.1k
            }
414
87.5k
        } else {
415
87.5k
            DCHECK(literal_count_ > 0);
416
87.5k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
87.5k
            literal_count_ -= nskip;
418
87.5k
            to_skip -= nskip;
419
22.4M
            for (; nskip > 0; nskip--) {
420
22.3M
                T value = 0;
421
22.3M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
22.3M
                DCHECK(result1);
423
22.3M
                if (value != 0) {
424
11.5M
                    set_count++;
425
11.5M
                }
426
22.3M
            }
427
87.5k
        }
428
147k
    }
429
1.38M
    return set_count;
430
1.38M
}
431
432
// This function buffers input values 8 at a time.  After seeing all 8 values,
433
// it decides whether they should be encoded as a literal or repeated run.
434
template <typename T>
435
12.8M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
12.8M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
12.8M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
1.15M
        repeat_count_ += run_length;
442
1.15M
        return;
443
1.15M
    }
444
445
    // Handle run_length > 1 more efficiently
446
27.7M
    while (run_length > 0) {
447
17.5M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
8.13M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
38.3M
            for (size_t i = 0; i < to_buffer; ++i) {
451
30.1M
                buffered_values_[num_buffered_values_++] = value;
452
30.1M
                ++repeat_count_;
453
30.1M
            }
454
8.13M
            run_length -= to_buffer;
455
8.13M
            if (num_buffered_values_ == 8) {
456
4.01M
                DCHECK_EQ(literal_count_ % 8, 0);
457
4.01M
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
4.01M
                if (repeat_count_ >= 8 && run_length > 0) {
461
1.51M
                    repeat_count_ += run_length;
462
1.51M
                    return;
463
1.51M
                }
464
4.01M
            }
465
9.44M
        } else {
466
            // Value changed
467
9.44M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
1.48M
                DCHECK_EQ(literal_count_, 0);
471
1.48M
                FlushRepeatedRun();
472
1.48M
            }
473
9.44M
            repeat_count_ = 1;
474
9.44M
            current_value_ = value;
475
476
9.44M
            buffered_values_[num_buffered_values_++] = value;
477
9.44M
            --run_length;
478
9.44M
            if (num_buffered_values_ == 8) {
479
834k
                DCHECK_EQ(literal_count_ % 8, 0);
480
834k
                FlushBufferedValues(false);
481
834k
            }
482
9.44M
        }
483
17.5M
    }
484
11.7M
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
435
4.15M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
4.15M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
4.15M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
508k
        repeat_count_ += run_length;
442
508k
        return;
443
508k
    }
444
445
    // Handle run_length > 1 more efficiently
446
7.29M
    while (run_length > 0) {
447
3.65M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
1.82M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
3.65M
            for (size_t i = 0; i < to_buffer; ++i) {
451
1.82M
                buffered_values_[num_buffered_values_++] = value;
452
1.82M
                ++repeat_count_;
453
1.82M
            }
454
1.82M
            run_length -= to_buffer;
455
1.82M
            if (num_buffered_values_ == 8) {
456
229k
                DCHECK_EQ(literal_count_ % 8, 0);
457
229k
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
229k
                if (repeat_count_ >= 8 && run_length > 0) {
461
3
                    repeat_count_ += run_length;
462
3
                    return;
463
3
                }
464
229k
            }
465
1.82M
        } else {
466
            // Value changed
467
1.82M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
6.30k
                DCHECK_EQ(literal_count_, 0);
471
6.30k
                FlushRepeatedRun();
472
6.30k
            }
473
1.82M
            repeat_count_ = 1;
474
1.82M
            current_value_ = value;
475
476
1.82M
            buffered_values_[num_buffered_values_++] = value;
477
1.82M
            --run_length;
478
1.82M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
224k
                FlushBufferedValues(false);
481
224k
            }
482
1.82M
        }
483
3.65M
    }
484
3.64M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
435
8.70M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
8.70M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
8.70M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
650k
        repeat_count_ += run_length;
442
650k
        return;
443
650k
    }
444
445
    // Handle run_length > 1 more efficiently
446
20.4M
    while (run_length > 0) {
447
13.9M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
6.30M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
34.6M
            for (size_t i = 0; i < to_buffer; ++i) {
451
28.3M
                buffered_values_[num_buffered_values_++] = value;
452
28.3M
                ++repeat_count_;
453
28.3M
            }
454
6.30M
            run_length -= to_buffer;
455
6.30M
            if (num_buffered_values_ == 8) {
456
3.78M
                DCHECK_EQ(literal_count_ % 8, 0);
457
3.78M
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
3.78M
                if (repeat_count_ >= 8 && run_length > 0) {
461
1.51M
                    repeat_count_ += run_length;
462
1.51M
                    return;
463
1.51M
                }
464
3.78M
            }
465
7.62M
        } else {
466
            // Value changed
467
7.62M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
1.47M
                DCHECK_EQ(literal_count_, 0);
471
1.47M
                FlushRepeatedRun();
472
1.47M
            }
473
7.62M
            repeat_count_ = 1;
474
7.62M
            current_value_ = value;
475
476
7.62M
            buffered_values_[num_buffered_values_++] = value;
477
7.62M
            --run_length;
478
7.62M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
610k
                FlushBufferedValues(false);
481
610k
            }
482
7.62M
        }
483
13.9M
    }
484
8.05M
}
485
486
template <typename T>
487
4.71M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
4.71M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
1.50M
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
1.50M
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
1.50M
    }
493
494
    // Write all the buffered values as bit packed literals
495
30.6M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
25.8M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
25.8M
    }
498
4.71M
    num_buffered_values_ = 0;
499
500
4.71M
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
1.50M
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
1.50M
        int32_t indicator_value = (num_groups << 1) | 1;
507
1.50M
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
1.50M
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
1.50M
                cast_set<uint8_t>(indicator_value);
510
1.50M
        literal_indicator_byte_idx_ = -1;
511
1.50M
        literal_count_ = 0;
512
1.50M
    }
513
4.71M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
487
454k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
454k
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
13.1k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
13.1k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
13.1k
    }
493
494
    // Write all the buffered values as bit packed literals
495
4.03M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
3.57M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
3.57M
    }
498
454k
    num_buffered_values_ = 0;
499
500
454k
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
13.1k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
13.1k
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
13.1k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
13.1k
                cast_set<uint8_t>(indicator_value);
510
13.1k
        literal_indicator_byte_idx_ = -1;
511
13.1k
        literal_count_ = 0;
512
13.1k
    }
513
454k
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
487
4.26M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
4.26M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
1.49M
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
1.49M
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
1.49M
    }
493
494
    // Write all the buffered values as bit packed literals
495
26.5M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
22.3M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
22.3M
    }
498
4.26M
    num_buffered_values_ = 0;
499
500
4.26M
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
1.49M
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
1.49M
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
1.49M
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
1.49M
                cast_set<uint8_t>(indicator_value);
510
1.49M
        literal_indicator_byte_idx_ = -1;
511
1.49M
        literal_count_ = 0;
512
1.49M
    }
513
4.26M
}
514
515
template <typename T>
516
1.59M
void RleEncoder<T>::FlushRepeatedRun() {
517
1.59M
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
1.59M
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
1.59M
    bit_writer_.PutVlqInt(indicator_value);
521
1.59M
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
1.59M
    num_buffered_values_ = 0;
523
1.59M
    repeat_count_ = 0;
524
1.59M
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
516
14.8k
void RleEncoder<T>::FlushRepeatedRun() {
517
14.8k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
14.8k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
14.8k
    bit_writer_.PutVlqInt(indicator_value);
521
14.8k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
14.8k
    num_buffered_values_ = 0;
523
14.8k
    repeat_count_ = 0;
524
14.8k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
516
1.58M
void RleEncoder<T>::FlushRepeatedRun() {
517
1.58M
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
1.58M
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
1.58M
    bit_writer_.PutVlqInt(indicator_value);
521
1.58M
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
1.58M
    num_buffered_values_ = 0;
523
1.58M
    repeat_count_ = 0;
524
1.58M
}
525
526
// Flush the values that have been buffered.  At this point we decide whether
527
// we need to switch between the run types or continue the current one.
528
template <typename T>
529
4.84M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
4.84M
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
1.62M
        num_buffered_values_ = 0;
534
1.62M
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
1.44M
            DCHECK_EQ(literal_count_ % 8, 0);
538
1.44M
            DCHECK_EQ(repeat_count_, 8);
539
1.44M
            FlushLiteralRun(true);
540
1.44M
        }
541
1.62M
        DCHECK_EQ(literal_count_, 0);
542
1.62M
        return;
543
1.62M
    }
544
545
3.21M
    literal_count_ += num_buffered_values_;
546
3.21M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
3.21M
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
7.85k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
7.85k
        FlushLiteralRun(true);
552
3.21M
    } else {
553
3.21M
        FlushLiteralRun(done);
554
3.21M
    }
555
3.21M
    repeat_count_ = 0;
556
3.21M
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
529
453k
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
453k
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
7.72k
        num_buffered_values_ = 0;
534
7.72k
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
5.06k
            DCHECK_EQ(literal_count_ % 8, 0);
538
5.06k
            DCHECK_EQ(repeat_count_, 8);
539
5.06k
            FlushLiteralRun(true);
540
5.06k
        }
541
7.72k
        DCHECK_EQ(literal_count_, 0);
542
7.72k
        return;
543
7.72k
    }
544
545
446k
    literal_count_ += num_buffered_values_;
546
446k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
446k
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
5.25k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
5.25k
        FlushLiteralRun(true);
552
440k
    } else {
553
440k
        FlushLiteralRun(done);
554
440k
    }
555
446k
    repeat_count_ = 0;
556
446k
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
529
4.38M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
4.38M
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
1.61M
        num_buffered_values_ = 0;
534
1.61M
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
1.44M
            DCHECK_EQ(literal_count_ % 8, 0);
538
1.44M
            DCHECK_EQ(repeat_count_, 8);
539
1.44M
            FlushLiteralRun(true);
540
1.44M
        }
541
1.61M
        DCHECK_EQ(literal_count_, 0);
542
1.61M
        return;
543
1.61M
    }
544
545
2.77M
    literal_count_ += num_buffered_values_;
546
2.77M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
2.77M
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
2.60k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
2.60k
        FlushLiteralRun(true);
552
2.77M
    } else {
553
2.77M
        FlushLiteralRun(done);
554
2.77M
    }
555
2.77M
    repeat_count_ = 0;
556
2.77M
}
557
558
template <typename T>
559
24.9k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
560
124k
    for (int i = 0; i < num_bytes; ++i) {
561
99.7k
        bit_writer_.PutValue(val, 8);
562
99.7k
    }
563
24.9k
}
564
565
template <typename T>
566
165k
int RleEncoder<T>::Flush() {
567
165k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
164k
        bool all_repeat = literal_count_ == 0 &&
569
164k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
164k
        if (repeat_count_ > 0 && all_repeat) {
572
119k
            FlushRepeatedRun();
573
119k
        } else {
574
45.5k
            literal_count_ += num_buffered_values_;
575
45.5k
            FlushLiteralRun(true);
576
45.5k
            repeat_count_ = 0;
577
45.5k
        }
578
164k
    }
579
165k
    bit_writer_.Flush();
580
165k
    DCHECK_EQ(num_buffered_values_, 0);
581
165k
    DCHECK_EQ(literal_count_, 0);
582
165k
    DCHECK_EQ(repeat_count_, 0);
583
165k
    return bit_writer_.bytes_written();
584
165k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
566
11.9k
int RleEncoder<T>::Flush() {
567
11.9k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
11.3k
        bool all_repeat = literal_count_ == 0 &&
569
11.3k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
11.3k
        if (repeat_count_ > 0 && all_repeat) {
572
8.50k
            FlushRepeatedRun();
573
8.50k
        } else {
574
2.87k
            literal_count_ += num_buffered_values_;
575
2.87k
            FlushLiteralRun(true);
576
2.87k
            repeat_count_ = 0;
577
2.87k
        }
578
11.3k
    }
579
11.9k
    bit_writer_.Flush();
580
11.9k
    DCHECK_EQ(num_buffered_values_, 0);
581
11.9k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
11.9k
    return bit_writer_.bytes_written();
584
11.9k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
566
153k
int RleEncoder<T>::Flush() {
567
153k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
153k
        bool all_repeat = literal_count_ == 0 &&
569
153k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
153k
        if (repeat_count_ > 0 && all_repeat) {
572
110k
            FlushRepeatedRun();
573
110k
        } else {
574
42.6k
            literal_count_ += num_buffered_values_;
575
42.6k
            FlushLiteralRun(true);
576
42.6k
            repeat_count_ = 0;
577
42.6k
        }
578
153k
    }
579
153k
    bit_writer_.Flush();
580
153k
    DCHECK_EQ(num_buffered_values_, 0);
581
153k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
153k
    return bit_writer_.bytes_written();
584
153k
}
585
586
template <typename T>
587
1.09M
void RleEncoder<T>::Clear() {
588
1.09M
    current_value_ = 0;
589
1.09M
    repeat_count_ = 0;
590
1.09M
    num_buffered_values_ = 0;
591
1.09M
    literal_count_ = 0;
592
1.09M
    literal_indicator_byte_idx_ = -1;
593
1.09M
    bit_writer_.Clear();
594
1.09M
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
587
37.9k
void RleEncoder<T>::Clear() {
588
37.9k
    current_value_ = 0;
589
37.9k
    repeat_count_ = 0;
590
37.9k
    num_buffered_values_ = 0;
591
37.9k
    literal_count_ = 0;
592
37.9k
    literal_indicator_byte_idx_ = -1;
593
37.9k
    bit_writer_.Clear();
594
37.9k
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
587
1.05M
void RleEncoder<T>::Clear() {
588
1.05M
    current_value_ = 0;
589
1.05M
    repeat_count_ = 0;
590
1.05M
    num_buffered_values_ = 0;
591
1.05M
    literal_count_ = 0;
592
1.05M
    literal_indicator_byte_idx_ = -1;
593
1.05M
    bit_writer_.Clear();
594
1.05M
}
595
596
// Copy from https://github.com/apache/impala/blob/master/be/src/util/rle-encoding.h
597
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
598
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
599
// (literal encoding).
600
//
601
// For both types of runs, there is a byte-aligned indicator which encodes the length
602
// of the run and the type of the run.
603
//
604
// This encoding has the benefit that when there aren't any long enough runs, values
605
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
606
// the run length are byte aligned. This allows for very efficient decoding
607
// implementations.
608
// The encoding is:
609
//    encoded-block := run*
610
//    run := literal-run | repeated-run
611
//    literal-run := literal-indicator < literal bytes >
612
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
613
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
614
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
615
//
616
// Each run is preceded by a varint. The varint's least significant bit is
617
// used to indicate whether the run is a literal run or a repeated run. The rest
618
// of the varint is used to determine the length of the run (eg how many times the
619
// value repeats).
620
//
621
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
622
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
623
// on a byte boundary without padding.
624
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
625
// the actual number of encoded ints. (This means that the total number of encoded values
626
// can not be determined from the encoded data, since the number of values in the last
627
// group may not be a multiple of 8). For the last group of literal runs, we pad
628
// the group to 8 with zeros. This allows for 8 at a time decoding on the read side
629
// without the need for additional checks.
630
//
631
// There is a break-even point when it is more storage efficient to do run length
632
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
633
// for both the repeated encoding or the literal encoding.  This value can always
634
// be computed based on the bit-width.
635
// TODO: For 1 bit-width values it can be optimal to use 16 or 24 values, but more
636
// investigation is needed to do this efficiently, see the reverted IMPALA-6658.
637
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
638
//
639
// Examples with bit-width 1 (eg encoding booleans):
640
// ----------------------------------------
641
// 100 1s followed by 100 0s:
642
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
643
//  - (total 4 bytes)
644
//
645
// alternating 1s and 0s (200 total):
646
// 200 ints = 25 groups of 8
647
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
648
// (total 26 bytes, 1 byte overhead)
649
650
// RLE decoder with a batch-oriented interface that enables fast decoding.
651
// Users of this class must first initialize the class to point to a buffer of
652
// RLE-encoded data, passed into the constructor or Reset(). The provided
653
// bit_width must be at most min(sizeof(T) * 8, BatchedBitReader::MAX_BITWIDTH).
654
// Then they can decode data by checking NextNumRepeats()/NextNumLiterals() to
655
// see if the next run is a repeated or literal run, then calling
656
// GetRepeatedValue() or GetLiteralValues() respectively to read the values.
657
//
658
// End-of-input is signalled by NextNumRepeats() == NextNumLiterals() == 0.
659
// Other decoding errors are signalled by functions returning false. If an
660
// error is encountered then it is not valid to read any more data until
661
// Reset() is called.
662
663
//bit-packed-run-len and rle-run-len must be in the range [1, 2^31 - 1].
664
// This means that a Parquet implementation can always store the run length in a signed 32-bit integer.
665
template <typename T>
666
class RleBatchDecoder {
667
public:
668
974
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
669
974
        Reset(buffer, buffer_len, bit_width);
670
974
    }
671
672
    RleBatchDecoder() = default;
673
674
    // Reset the decoder to read from a new buffer.
675
    void Reset(uint8_t* buffer, int buffer_len, int bit_width);
676
677
    // Return the size of the current repeated run. Returns zero if the current run is
678
    // a literal run or if no more runs can be read from the input.
679
    int32_t NextNumRepeats();
680
681
    // Get the value of the current repeated run and consume the given number of repeats.
682
    // Only valid to call when NextNumRepeats() > 0. The given number of repeats cannot
683
    // be greater than the remaining number of repeats in the run. 'num_repeats_to_consume'
684
    // can be set to 0 to peek at the value without consuming repeats.
685
    T GetRepeatedValue(int32_t num_repeats_to_consume);
686
687
    // Return the size of the current literal run. Returns zero if the current run is
688
    // a repeated run or if no more runs can be read from the input.
689
    int32_t NextNumLiterals();
690
691
    // Consume 'num_literals_to_consume' literals from the current literal run,
692
    // copying the values to 'values'. 'num_literals_to_consume' must be <=
693
    // NextNumLiterals(). Returns true if the requested number of literals were
694
    // successfully read or false if an error was encountered, e.g. the input was
695
    // truncated.
696
    bool GetLiteralValues(int32_t num_literals_to_consume, T* values) WARN_UNUSED_RESULT;
697
698
    // Consume 'num_values_to_consume' values and copy them to 'values'.
699
    // Returns the number of consumed values or 0 if an error occurred.
700
    uint32_t GetBatch(T* values, uint32_t batch_num);
701
702
private:
703
    // Called when both 'literal_count_' and 'repeat_count_' have been exhausted.
704
    // Sets either 'literal_count_' or 'repeat_count_' to the size of the next literal
705
    // or repeated run, or leaves both at 0 if no more values can be read (either because
706
    // the end of the input was reached or an error was encountered decoding).
707
    void NextCounts();
708
709
    /// Fill the literal buffer. Invalid to call if there are already buffered literals.
710
    /// Return false if the input was truncated. This does not advance 'literal_count_'.
711
    bool FillLiteralBuffer() WARN_UNUSED_RESULT;
712
713
11.6k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
714
715
    /// Output buffered literals, advancing 'literal_buffer_pos_' and decrementing
716
    /// 'literal_count_'. Returns the number of literals outputted.
717
    int32_t OutputBufferedLiterals(int32_t max_to_output, T* values);
718
719
    BatchedBitReader bit_reader_;
720
721
    // Number of bits needed to encode the value. Must be between 0 and 64 after
722
    // the decoder is initialized with a buffer. -1 indicates the decoder was not
723
    // initialized.
724
    int bit_width_ = -1;
725
726
    // If a repeated run, the number of repeats remaining in the current run to be read.
727
    // If the current run is a literal run, this is 0.
728
    int32_t repeat_count_ = 0;
729
730
    // If a literal run, the number of literals remaining in the current run to be read.
731
    // If the current run is a repeated run, this is 0.
732
    int32_t literal_count_ = 0;
733
734
    // If a repeated run, the current repeated value.
735
    T repeated_value_;
736
737
    // Size of buffer for literal values. Large enough to decode a full batch of 32
738
    // literals. The buffer is needed to allow clients to read in batches that are not
739
    // multiples of 32.
740
    static constexpr int LITERAL_BUFFER_LEN = 32;
741
742
    // Buffer containing 'num_buffered_literals_' values. 'literal_buffer_pos_' is the
743
    // position of the next literal to be read from the buffer.
744
    T literal_buffer_[LITERAL_BUFFER_LEN];
745
    int num_buffered_literals_ = 0;
746
    int literal_buffer_pos_ = 0;
747
};
748
749
template <typename T>
750
11.7k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
751
11.7k
    int32_t num_to_output =
752
11.7k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
753
11.7k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
754
11.7k
    literal_buffer_pos_ += num_to_output;
755
11.7k
    literal_count_ -= num_to_output;
756
11.7k
    return num_to_output;
757
11.7k
}
758
759
template <typename T>
760
974
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
761
974
    bit_reader_.Reset(buffer, buffer_len);
762
974
    bit_width_ = bit_width;
763
974
    repeat_count_ = 0;
764
974
    literal_count_ = 0;
765
974
    num_buffered_literals_ = 0;
766
974
    literal_buffer_pos_ = 0;
767
974
}
768
769
template <typename T>
770
12.1k
int32_t RleBatchDecoder<T>::NextNumRepeats() {
771
12.1k
    if (repeat_count_ > 0) return repeat_count_;
772
12.1k
    if (literal_count_ == 0) NextCounts();
773
12.1k
    return repeat_count_;
774
12.1k
}
775
776
template <typename T>
777
11.9k
void RleBatchDecoder<T>::NextCounts() {
778
    // Read the next run's indicator int, it could be a literal or repeated run.
779
    // The int is encoded as a ULEB128-encoded value.
780
11.9k
    uint32_t indicator_value = 0;
781
11.9k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
782
0
        return;
783
0
    }
784
785
    // lsb indicates if it is a literal run or repeated run
786
11.9k
    bool is_literal = indicator_value & 1;
787
788
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
789
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
790
11.9k
    uint32_t run_len = indicator_value >> 1;
791
11.9k
    if (is_literal) {
792
        // Use int64_t to avoid overflowing multiplication.
793
11.4k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
794
11.4k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
795
11.4k
        literal_count_ = cast_set<int32_t>(literal_count);
796
11.4k
    } else {
797
506
        if (UNLIKELY(run_len == 0)) return;
798
506
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
799
506
        if (UNLIKELY(!result)) return;
800
506
        repeat_count_ = run_len;
801
506
    }
802
11.9k
}
803
804
template <typename T>
805
520
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
806
520
    repeat_count_ -= num_repeats_to_consume;
807
520
    return repeated_value_;
808
520
}
809
810
template <typename T>
811
11.6k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
812
11.6k
    if (literal_count_ > 0) return literal_count_;
813
0
    if (repeat_count_ == 0) NextCounts();
814
0
    return literal_count_;
815
11.6k
}
816
817
template <typename T>
818
11.6k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
819
11.6k
    int32_t num_consumed = 0;
820
    // Copy any buffered literals left over from previous calls.
821
11.6k
    if (HaveBufferedLiterals()) {
822
163
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
823
163
    }
824
825
11.6k
    int32_t num_remaining = num_literals_to_consume - num_consumed;
826
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
827
    // Need to round to a batch of 32 if the caller is consuming only part of the current
828
    // run avoid ending on a non-byte boundary.
829
11.6k
    int32_t num_to_bypass =
830
11.6k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
831
11.6k
    if (num_to_bypass > 0) {
832
11.0k
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
833
        // If we couldn't read the expected number, that means the input was truncated.
834
11.0k
        if (num_read < num_to_bypass) return false;
835
11.0k
        literal_count_ -= num_to_bypass;
836
11.0k
        num_consumed += num_to_bypass;
837
11.0k
        num_remaining = num_literals_to_consume - num_consumed;
838
11.0k
    }
839
840
11.6k
    if (num_remaining > 0) {
841
        // We weren't able to copy all the literals requested directly from the input.
842
        // Buffer literals and copy over the requested number.
843
11.5k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
844
11.5k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
845
11.5k
    }
846
11.6k
    return true;
847
11.6k
}
848
849
template <typename T>
850
11.5k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
851
11.5k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
852
11.5k
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
853
    // If we couldn't read the expected number, that means the input was truncated.
854
11.5k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
855
11.5k
    literal_buffer_pos_ = 0;
856
11.5k
    return true;
857
11.5k
}
858
859
template <typename T>
860
1.39k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
861
1.39k
    uint32_t num_consumed = 0;
862
13.5k
    while (num_consumed < batch_num) {
863
        // Add RLE encoded values by repeating the current value this number of times.
864
12.1k
        uint32_t num_repeats = NextNumRepeats();
865
12.1k
        if (num_repeats > 0) {
866
520
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
867
520
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
868
1.34M
            for (int i = 0; i < num_repeats_to_set; ++i) {
869
1.34M
                values[num_consumed + i] = repeated_value;
870
1.34M
            }
871
520
            num_consumed += num_repeats_to_set;
872
520
            continue;
873
520
        }
874
875
        // Add remaining literal values, if any.
876
11.6k
        uint32_t num_literals = NextNumLiterals();
877
11.6k
        if (num_literals == 0) {
878
0
            break;
879
0
        }
880
11.6k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
881
11.6k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
882
0
            return 0;
883
0
        }
884
11.6k
        num_consumed += num_literals_to_set;
885
11.6k
    }
886
1.39k
    return num_consumed;
887
1.39k
}
888
#include "common/compile_check_end.h"
889
} // namespace doris