Coverage Report

Created: 2026-05-28 13:45

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