Coverage Report

Created: 2026-06-29 02:19

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
334k
            : bit_reader_(buffer, buffer_len),
89
334k
              bit_width_(bit_width),
90
334k
              current_value_(0),
91
334k
              repeat_count_(0),
92
334k
              literal_count_(0),
93
334k
              rewind_state_(CANT_REWIND) {
94
334k
        DCHECK_GE(bit_width_, 1);
95
334k
        DCHECK_LE(bit_width_, 64);
96
334k
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
88
8.44k
            : bit_reader_(buffer, buffer_len),
89
8.44k
              bit_width_(bit_width),
90
8.44k
              current_value_(0),
91
8.44k
              repeat_count_(0),
92
8.44k
              literal_count_(0),
93
8.44k
              rewind_state_(CANT_REWIND) {
94
8.44k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
8.44k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
88
656
            : bit_reader_(buffer, buffer_len),
89
656
              bit_width_(bit_width),
90
656
              current_value_(0),
91
656
              repeat_count_(0),
92
656
              literal_count_(0),
93
656
              rewind_state_(CANT_REWIND) {
94
656
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
656
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
88
325k
            : bit_reader_(buffer, buffer_len),
89
325k
              bit_width_(bit_width),
90
325k
              current_value_(0),
91
325k
              repeat_count_(0),
92
325k
              literal_count_(0),
93
325k
              rewind_state_(CANT_REWIND) {
94
325k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
325k
    }
97
98
4.93M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
98
4.76M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
98
553
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
98
172k
    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
50.0k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
50.0k
        DCHECK_GE(bit_width_, 1);
155
50.0k
        DCHECK_LE(bit_width_, 64);
156
50.0k
        Clear();
157
50.0k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
153
223
            : bit_width_(bit_width), bit_writer_(buffer) {
154
223
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
223
        Clear();
157
223
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
153
49.8k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
49.8k
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
49.8k
        Clear();
157
49.8k
    }
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
236
    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
55.7M
bool RleDecoder<T>::ReadHeader() {
232
55.7M
    DCHECK(bit_reader_.is_initialized());
233
55.7M
    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
2.94M
        uint32_t indicator_value = 0;
237
2.94M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
2.94M
        if (!result) [[unlikely]] {
239
376
            return false;
240
376
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
2.94M
        bool is_literal = indicator_value & 1;
244
2.94M
        if (is_literal) {
245
1.40M
            literal_count_ = (indicator_value >> 1) * 8;
246
1.40M
            DCHECK_GT(literal_count_, 0);
247
1.54M
        } else {
248
1.54M
            repeat_count_ = indicator_value >> 1;
249
1.54M
            DCHECK_GT(repeat_count_, 0);
250
1.54M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
1.54M
                                                     reinterpret_cast<T*>(&current_value_));
252
1.54M
            DCHECK(result1);
253
1.54M
        }
254
2.94M
    }
255
55.7M
    return true;
256
55.7M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
231
55.5M
bool RleDecoder<T>::ReadHeader() {
232
55.5M
    DCHECK(bit_reader_.is_initialized());
233
55.5M
    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
2.90M
        uint32_t indicator_value = 0;
237
2.90M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
2.90M
        if (!result) [[unlikely]] {
239
22
            return false;
240
22
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
2.90M
        bool is_literal = indicator_value & 1;
244
2.90M
        if (is_literal) {
245
1.39M
            literal_count_ = (indicator_value >> 1) * 8;
246
1.39M
            DCHECK_GT(literal_count_, 0);
247
1.51M
        } else {
248
1.51M
            repeat_count_ = indicator_value >> 1;
249
1.51M
            DCHECK_GT(repeat_count_, 0);
250
1.51M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
1.51M
                                                     reinterpret_cast<T*>(&current_value_));
252
1.51M
            DCHECK(result1);
253
1.51M
        }
254
2.90M
    }
255
55.5M
    return true;
256
55.5M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
231
116k
bool RleDecoder<T>::ReadHeader() {
232
116k
    DCHECK(bit_reader_.is_initialized());
233
116k
    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
37.9k
        uint32_t indicator_value = 0;
237
37.9k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
37.9k
        if (!result) [[unlikely]] {
239
354
            return false;
240
354
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
37.5k
        bool is_literal = indicator_value & 1;
244
37.5k
        if (is_literal) {
245
5.91k
            literal_count_ = (indicator_value >> 1) * 8;
246
5.91k
            DCHECK_GT(literal_count_, 0);
247
31.6k
        } else {
248
31.6k
            repeat_count_ = indicator_value >> 1;
249
31.6k
            DCHECK_GT(repeat_count_, 0);
250
31.6k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
31.6k
                                                     reinterpret_cast<T*>(&current_value_));
252
31.6k
            DCHECK(result1);
253
31.6k
        }
254
37.5k
    }
255
116k
    return true;
256
116k
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
231
3.06k
bool RleDecoder<T>::ReadHeader() {
232
3.06k
    DCHECK(bit_reader_.is_initialized());
233
3.06k
    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
616
        uint32_t indicator_value = 0;
237
616
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
616
        if (!result) [[unlikely]] {
239
0
            return false;
240
0
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
616
        bool is_literal = indicator_value & 1;
244
616
        if (is_literal) {
245
219
            literal_count_ = (indicator_value >> 1) * 8;
246
219
            DCHECK_GT(literal_count_, 0);
247
397
        } else {
248
397
            repeat_count_ = indicator_value >> 1;
249
397
            DCHECK_GT(repeat_count_, 0);
250
397
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
397
                                                     reinterpret_cast<T*>(&current_value_));
252
397
            DCHECK(result1);
253
397
        }
254
616
    }
255
3.06k
    return true;
256
3.06k
}
257
258
template <typename T>
259
52.4M
bool RleDecoder<T>::Get(T* val) {
260
52.4M
    DCHECK(bit_reader_.is_initialized());
261
52.4M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
52.4M
    if (repeat_count_ > 0) [[likely]] {
266
25.1M
        *val = cast_set<T>(current_value_);
267
25.1M
        --repeat_count_;
268
25.1M
        rewind_state_ = REWIND_RUN;
269
27.3M
    } else {
270
27.3M
        DCHECK(literal_count_ > 0);
271
27.3M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
27.3M
        DCHECK(result);
273
27.3M
        --literal_count_;
274
27.3M
        rewind_state_ = REWIND_LITERAL;
275
27.3M
    }
276
277
52.4M
    return true;
278
52.4M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
259
52.4M
bool RleDecoder<T>::Get(T* val) {
260
52.4M
    DCHECK(bit_reader_.is_initialized());
261
52.4M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
52.4M
    if (repeat_count_ > 0) [[likely]] {
266
25.1M
        *val = cast_set<T>(current_value_);
267
25.1M
        --repeat_count_;
268
25.1M
        rewind_state_ = REWIND_RUN;
269
27.3M
    } else {
270
27.3M
        DCHECK(literal_count_ > 0);
271
27.3M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
27.3M
        DCHECK(result);
273
27.3M
        --literal_count_;
274
27.3M
        rewind_state_ = REWIND_LITERAL;
275
27.3M
    }
276
277
52.4M
    return true;
278
52.4M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
259
2.73k
bool RleDecoder<T>::Get(T* val) {
260
2.73k
    DCHECK(bit_reader_.is_initialized());
261
2.73k
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
2.73k
    if (repeat_count_ > 0) [[likely]] {
266
1.12k
        *val = cast_set<T>(current_value_);
267
1.12k
        --repeat_count_;
268
1.12k
        rewind_state_ = REWIND_RUN;
269
1.61k
    } else {
270
1.61k
        DCHECK(literal_count_ > 0);
271
1.61k
        bool result = bit_reader_.GetValue(bit_width_, val);
272
1.61k
        DCHECK(result);
273
1.61k
        --literal_count_;
274
1.61k
        rewind_state_ = REWIND_LITERAL;
275
1.61k
    }
276
277
2.73k
    return true;
278
2.73k
}
279
280
template <typename T>
281
20.8k
void RleDecoder<T>::RewindOne() {
282
20.8k
    DCHECK(bit_reader_.is_initialized());
283
284
20.8k
    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
7.27k
    case REWIND_RUN:
289
7.27k
        ++repeat_count_;
290
7.27k
        break;
291
13.5k
    case REWIND_LITERAL: {
292
13.5k
        bit_reader_.Rewind(bit_width_);
293
13.5k
        ++literal_count_;
294
13.5k
        break;
295
0
    }
296
20.8k
    }
297
298
20.8k
    rewind_state_ = CANT_REWIND;
299
20.8k
}
300
301
template <typename T>
302
1.50M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
1.50M
    DCHECK(bit_reader_.is_initialized());
304
1.50M
    DCHECK_GT(max_run, 0);
305
1.50M
    size_t ret = 0;
306
1.50M
    size_t rem = max_run;
307
1.92M
    while (ReadHeader()) {
308
1.92M
        if (repeat_count_ > 0) [[likely]] {
309
499k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
45.4k
                return ret;
311
45.4k
            }
312
454k
            *val = cast_set<T>(current_value_);
313
454k
            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
237k
                repeat_count_ -= rem;
317
237k
                ret += rem;
318
237k
                return ret;
319
237k
            }
320
216k
            ret += repeat_count_;
321
216k
            rem -= repeat_count_;
322
216k
            repeat_count_ = 0;
323
1.42M
        } else {
324
1.42M
            DCHECK(literal_count_ > 0);
325
1.42M
            if (ret == 0) {
326
1.22M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
1.22M
                DCHECK(has_more);
328
1.22M
                literal_count_--;
329
1.22M
                ret++;
330
1.22M
                rem--;
331
1.22M
            }
332
333
4.15M
            while (literal_count_ > 0) {
334
3.95M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
3.95M
                DCHECK(result);
336
3.95M
                if (current_value_ != *val || rem == 0) {
337
1.22M
                    bit_reader_.Rewind(bit_width_);
338
1.22M
                    return ret;
339
1.22M
                }
340
2.72M
                ret++;
341
2.72M
                rem--;
342
2.72M
                literal_count_--;
343
2.72M
            }
344
1.42M
        }
345
1.92M
    }
346
385
    return ret;
347
1.50M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
302
1.43M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
1.43M
    DCHECK(bit_reader_.is_initialized());
304
1.43M
    DCHECK_GT(max_run, 0);
305
1.43M
    size_t ret = 0;
306
1.43M
    size_t rem = max_run;
307
1.81M
    while (ReadHeader()) {
308
1.81M
        if (repeat_count_ > 0) [[likely]] {
309
444k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
24.4k
                return ret;
311
24.4k
            }
312
419k
            *val = cast_set<T>(current_value_);
313
419k
            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
227k
                repeat_count_ -= rem;
317
227k
                ret += rem;
318
227k
                return ret;
319
227k
            }
320
192k
            ret += repeat_count_;
321
192k
            rem -= repeat_count_;
322
192k
            repeat_count_ = 0;
323
1.37M
        } else {
324
1.37M
            DCHECK(literal_count_ > 0);
325
1.37M
            if (ret == 0) {
326
1.17M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
1.17M
                DCHECK(has_more);
328
1.17M
                literal_count_--;
329
1.17M
                ret++;
330
1.17M
                rem--;
331
1.17M
            }
332
333
4.01M
            while (literal_count_ > 0) {
334
3.82M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
3.82M
                DCHECK(result);
336
3.82M
                if (current_value_ != *val || rem == 0) {
337
1.17M
                    bit_reader_.Rewind(bit_width_);
338
1.17M
                    return ret;
339
1.17M
                }
340
2.64M
                ret++;
341
2.64M
                rem--;
342
2.64M
                literal_count_--;
343
2.64M
            }
344
1.37M
        }
345
1.81M
    }
346
33
    return ret;
347
1.43M
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
302
76.6k
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
76.6k
    DCHECK(bit_reader_.is_initialized());
304
76.6k
    DCHECK_GT(max_run, 0);
305
76.6k
    size_t ret = 0;
306
76.6k
    size_t rem = max_run;
307
104k
    while (ReadHeader()) {
308
104k
        if (repeat_count_ > 0) [[likely]] {
309
55.6k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
20.9k
                return ret;
311
20.9k
            }
312
34.6k
            *val = cast_set<T>(current_value_);
313
34.6k
            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
10.3k
                repeat_count_ -= rem;
317
10.3k
                ret += rem;
318
10.3k
                return ret;
319
10.3k
            }
320
24.3k
            ret += repeat_count_;
321
24.3k
            rem -= repeat_count_;
322
24.3k
            repeat_count_ = 0;
323
48.6k
        } else {
324
48.6k
            DCHECK(literal_count_ > 0);
325
48.6k
            if (ret == 0) {
326
44.9k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
44.9k
                DCHECK(has_more);
328
44.9k
                literal_count_--;
329
44.9k
                ret++;
330
44.9k
                rem--;
331
44.9k
            }
332
333
136k
            while (literal_count_ > 0) {
334
132k
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
132k
                DCHECK(result);
336
132k
                if (current_value_ != *val || rem == 0) {
337
45.0k
                    bit_reader_.Rewind(bit_width_);
338
45.0k
                    return ret;
339
45.0k
                }
340
87.4k
                ret++;
341
87.4k
                rem--;
342
87.4k
                literal_count_--;
343
87.4k
            }
344
48.6k
        }
345
104k
    }
346
352
    return ret;
347
76.6k
}
348
349
template <typename T>
350
69.6k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
69.6k
    size_t read_num = 0;
352
2.62M
    while (read_num < num_values) {
353
2.55M
        size_t read_this_time = num_values - read_num;
354
355
2.55M
        if (LIKELY(repeat_count_ > 0)) {
356
652k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
652k
            std::fill(values, values + read_this_time, current_value_);
358
652k
            values += read_this_time;
359
652k
            repeat_count_ -= read_this_time;
360
652k
            read_num += read_this_time;
361
1.90M
        } else if (literal_count_ > 0) {
362
628k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
10.0M
            for (int i = 0; i < read_this_time; ++i) {
364
9.43M
                bool result = bit_reader_.GetValue(bit_width_, values);
365
9.43M
                DCHECK(result);
366
9.43M
                values++;
367
9.43M
            }
368
628k
            literal_count_ -= read_this_time;
369
628k
            read_num += read_this_time;
370
1.27M
        } else {
371
1.27M
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
1.27M
        }
375
2.55M
    }
376
69.6k
    return read_num;
377
69.6k
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
350
69.4k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
69.4k
    size_t read_num = 0;
352
2.62M
    while (read_num < num_values) {
353
2.55M
        size_t read_this_time = num_values - read_num;
354
355
2.55M
        if (LIKELY(repeat_count_ > 0)) {
356
652k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
652k
            std::fill(values, values + read_this_time, current_value_);
358
652k
            values += read_this_time;
359
652k
            repeat_count_ -= read_this_time;
360
652k
            read_num += read_this_time;
361
1.90M
        } else if (literal_count_ > 0) {
362
628k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
10.0M
            for (int i = 0; i < read_this_time; ++i) {
364
9.42M
                bool result = bit_reader_.GetValue(bit_width_, values);
365
9.42M
                DCHECK(result);
366
9.42M
                values++;
367
9.42M
            }
368
628k
            literal_count_ -= read_this_time;
369
628k
            read_num += read_this_time;
370
1.27M
        } else {
371
1.27M
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
1.27M
        }
375
2.55M
    }
376
69.4k
    return read_num;
377
69.4k
}
_ZN5doris10RleDecoderIhE10get_valuesEPhm
Line
Count
Source
350
185
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
185
    size_t read_num = 0;
352
554
    while (read_num < num_values) {
353
369
        size_t read_this_time = num_values - read_num;
354
355
369
        if (LIKELY(repeat_count_ > 0)) {
356
20
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
20
            std::fill(values, values + read_this_time, current_value_);
358
20
            values += read_this_time;
359
20
            repeat_count_ -= read_this_time;
360
20
            read_num += read_this_time;
361
349
        } else if (literal_count_ > 0) {
362
165
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
761
            for (int i = 0; i < read_this_time; ++i) {
364
596
                bool result = bit_reader_.GetValue(bit_width_, values);
365
596
                DCHECK(result);
366
596
                values++;
367
596
            }
368
165
            literal_count_ -= read_this_time;
369
165
            read_num += read_this_time;
370
184
        } else {
371
184
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
184
        }
375
369
    }
376
185
    return read_num;
377
185
}
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.65k
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.65k
    DCHECK(bit_reader_.is_initialized());
400
401
1.65k
    size_t set_count = 0;
402
3.62k
    while (to_skip > 0) {
403
1.97k
        bool result = ReadHeader();
404
1.97k
        DCHECK(result);
405
406
1.97k
        if (repeat_count_ > 0) [[likely]] {
407
1.05k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
1.05k
            repeat_count_ -= nskip;
409
1.05k
            to_skip -= nskip;
410
1.05k
            if (current_value_ != 0) {
411
536
                set_count += nskip;
412
536
            }
413
1.05k
        } else {
414
925
            DCHECK(literal_count_ > 0);
415
925
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
925
            literal_count_ -= nskip;
417
925
            to_skip -= nskip;
418
12.3k
            for (; nskip > 0; nskip--) {
419
11.4k
                T value = 0;
420
11.4k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
11.4k
                DCHECK(result1);
422
11.4k
                if (value != 0) {
423
4.82k
                    set_count++;
424
4.82k
                }
425
11.4k
            }
426
925
        }
427
1.97k
    }
428
1.65k
    return set_count;
429
1.65k
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
398
1.25k
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.25k
    DCHECK(bit_reader_.is_initialized());
400
401
1.25k
    size_t set_count = 0;
402
3.08k
    while (to_skip > 0) {
403
1.83k
        bool result = ReadHeader();
404
1.83k
        DCHECK(result);
405
406
1.83k
        if (repeat_count_ > 0) [[likely]] {
407
934
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
934
            repeat_count_ -= nskip;
409
934
            to_skip -= nskip;
410
934
            if (current_value_ != 0) {
411
422
                set_count += nskip;
412
422
            }
413
934
        } else {
414
899
            DCHECK(literal_count_ > 0);
415
899
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
899
            literal_count_ -= nskip;
417
899
            to_skip -= nskip;
418
12.3k
            for (; nskip > 0; nskip--) {
419
11.4k
                T value = 0;
420
11.4k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
11.4k
                DCHECK(result1);
422
11.4k
                if (value != 0) {
423
4.80k
                    set_count++;
424
4.80k
                }
425
11.4k
            }
426
899
        }
427
1.83k
    }
428
1.25k
    return set_count;
429
1.25k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
398
396
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
396
    DCHECK(bit_reader_.is_initialized());
400
401
396
    size_t set_count = 0;
402
538
    while (to_skip > 0) {
403
142
        bool result = ReadHeader();
404
142
        DCHECK(result);
405
406
142
        if (repeat_count_ > 0) [[likely]] {
407
116
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
116
            repeat_count_ -= nskip;
409
116
            to_skip -= nskip;
410
116
            if (current_value_ != 0) {
411
114
                set_count += nskip;
412
114
            }
413
116
        } else {
414
26
            DCHECK(literal_count_ > 0);
415
26
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
26
            literal_count_ -= nskip;
417
26
            to_skip -= nskip;
418
55
            for (; nskip > 0; nskip--) {
419
29
                T value = 0;
420
29
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
29
                DCHECK(result1);
422
29
                if (value != 0) {
423
14
                    set_count++;
424
14
                }
425
29
            }
426
26
        }
427
142
    }
428
396
    return set_count;
429
396
}
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
806k
void RleEncoder<T>::Put(T value, size_t run_length) {
435
806k
    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
806k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
637k
        repeat_count_ += run_length;
441
637k
        return;
442
637k
    }
443
444
    // Handle run_length > 1 more efficiently
445
372k
    while (run_length > 0) {
446
245k
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
143k
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
675k
            for (size_t i = 0; i < to_buffer; ++i) {
450
532k
                buffered_values_[num_buffered_values_++] = value;
451
532k
                ++repeat_count_;
452
532k
            }
453
143k
            run_length -= to_buffer;
454
143k
            if (num_buffered_values_ == 8) {
455
64.0k
                DCHECK_EQ(literal_count_ % 8, 0);
456
64.0k
                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
64.0k
                if (repeat_count_ >= 8 && run_length > 0) {
460
41.7k
                    repeat_count_ += run_length;
461
41.7k
                    return;
462
41.7k
                }
463
64.0k
            }
464
143k
        } else {
465
            // Value changed
466
102k
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
26.6k
                DCHECK_EQ(literal_count_, 0);
470
26.6k
                FlushRepeatedRun();
471
26.6k
            }
472
102k
            repeat_count_ = 1;
473
102k
            current_value_ = value;
474
475
102k
            buffered_values_[num_buffered_values_++] = value;
476
102k
            --run_length;
477
102k
            if (num_buffered_values_ == 8) {
478
7.69k
                DCHECK_EQ(literal_count_ % 8, 0);
479
7.69k
                FlushBufferedValues(false);
480
7.69k
            }
481
102k
        }
482
245k
    }
483
168k
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
434
1.77k
void RleEncoder<T>::Put(T value, size_t run_length) {
435
1.77k
    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
1.77k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
44
        repeat_count_ += run_length;
441
44
        return;
442
44
    }
443
444
    // Handle run_length > 1 more efficiently
445
3.46k
    while (run_length > 0) {
446
1.73k
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
905
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
1.82k
            for (size_t i = 0; i < to_buffer; ++i) {
450
923
                buffered_values_[num_buffered_values_++] = value;
451
923
                ++repeat_count_;
452
923
            }
453
905
            run_length -= to_buffer;
454
905
            if (num_buffered_values_ == 8) {
455
98
                DCHECK_EQ(literal_count_ % 8, 0);
456
98
                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
98
                if (repeat_count_ >= 8 && run_length > 0) {
460
3
                    repeat_count_ += run_length;
461
3
                    return;
462
3
                }
463
98
            }
464
905
        } else {
465
            // Value changed
466
828
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
3
                DCHECK_EQ(literal_count_, 0);
470
3
                FlushRepeatedRun();
471
3
            }
472
828
            repeat_count_ = 1;
473
828
            current_value_ = value;
474
475
828
            buffered_values_[num_buffered_values_++] = value;
476
828
            --run_length;
477
828
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
78
                FlushBufferedValues(false);
480
78
            }
481
828
        }
482
1.73k
    }
483
1.73k
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
434
804k
void RleEncoder<T>::Put(T value, size_t run_length) {
435
804k
    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
804k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
637k
        repeat_count_ += run_length;
441
637k
        return;
442
637k
    }
443
444
    // Handle run_length > 1 more efficiently
445
369k
    while (run_length > 0) {
446
243k
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
142k
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
674k
            for (size_t i = 0; i < to_buffer; ++i) {
450
531k
                buffered_values_[num_buffered_values_++] = value;
451
531k
                ++repeat_count_;
452
531k
            }
453
142k
            run_length -= to_buffer;
454
142k
            if (num_buffered_values_ == 8) {
455
63.9k
                DCHECK_EQ(literal_count_ % 8, 0);
456
63.9k
                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
63.9k
                if (repeat_count_ >= 8 && run_length > 0) {
460
41.7k
                    repeat_count_ += run_length;
461
41.7k
                    return;
462
41.7k
                }
463
63.9k
            }
464
142k
        } else {
465
            // Value changed
466
101k
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
26.6k
                DCHECK_EQ(literal_count_, 0);
470
26.6k
                FlushRepeatedRun();
471
26.6k
            }
472
101k
            repeat_count_ = 1;
473
101k
            current_value_ = value;
474
475
101k
            buffered_values_[num_buffered_values_++] = value;
476
101k
            --run_length;
477
101k
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
7.61k
                FlushBufferedValues(false);
480
7.61k
            }
481
101k
        }
482
243k
    }
483
167k
}
484
485
template <typename T>
486
32.2k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
32.2k
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
6.88k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
6.88k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
6.88k
    }
492
493
    // Write all the buffered values as bit packed literals
494
241k
    for (int i = 0; i < num_buffered_values_; ++i) {
495
209k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
209k
    }
497
32.2k
    num_buffered_values_ = 0;
498
499
32.2k
    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
6.88k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
6.88k
        int32_t indicator_value = (num_groups << 1) | 1;
506
6.88k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
6.88k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
6.88k
                cast_set<uint8_t>(indicator_value);
509
6.88k
        literal_indicator_byte_idx_ = -1;
510
6.88k
        literal_count_ = 0;
511
6.88k
    }
512
32.2k
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
486
176
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
176
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
23
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
23
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
23
    }
492
493
    // Write all the buffered values as bit packed literals
494
1.47k
    for (int i = 0; i < num_buffered_values_; ++i) {
495
1.30k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
1.30k
    }
497
176
    num_buffered_values_ = 0;
498
499
176
    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
23
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
23
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
23
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
23
                cast_set<uint8_t>(indicator_value);
509
23
        literal_indicator_byte_idx_ = -1;
510
23
        literal_count_ = 0;
511
23
    }
512
176
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
486
32.0k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
32.0k
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
6.86k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
6.86k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
6.86k
    }
492
493
    // Write all the buffered values as bit packed literals
494
240k
    for (int i = 0; i < num_buffered_values_; ++i) {
495
208k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
208k
    }
497
32.0k
    num_buffered_values_ = 0;
498
499
32.0k
    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
6.86k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
6.86k
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
6.86k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
6.86k
                cast_set<uint8_t>(indicator_value);
509
6.86k
        literal_indicator_byte_idx_ = -1;
510
6.86k
        literal_count_ = 0;
511
6.86k
    }
512
32.0k
}
513
514
template <typename T>
515
31.6k
void RleEncoder<T>::FlushRepeatedRun() {
516
31.6k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
31.6k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
31.6k
    bit_writer_.PutVlqInt(indicator_value);
520
31.6k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
31.6k
    num_buffered_values_ = 0;
522
31.6k
    repeat_count_ = 0;
523
31.6k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
515
178
void RleEncoder<T>::FlushRepeatedRun() {
516
178
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
178
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
178
    bit_writer_.PutVlqInt(indicator_value);
520
178
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
178
    num_buffered_values_ = 0;
522
178
    repeat_count_ = 0;
523
178
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
515
31.4k
void RleEncoder<T>::FlushRepeatedRun() {
516
31.4k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
31.4k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
31.4k
    bit_writer_.PutVlqInt(indicator_value);
520
31.4k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
31.4k
    num_buffered_values_ = 0;
522
31.4k
    repeat_count_ = 0;
523
31.4k
}
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
71.7k
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
71.7k
    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
46.3k
        num_buffered_values_ = 0;
533
46.3k
        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
5.23k
            DCHECK_EQ(literal_count_ % 8, 0);
537
5.23k
            DCHECK_EQ(repeat_count_, 8);
538
5.23k
            FlushLiteralRun(true);
539
5.23k
        }
540
46.3k
        DCHECK_EQ(literal_count_, 0);
541
46.3k
        return;
542
46.3k
    }
543
544
25.4k
    literal_count_ += num_buffered_values_;
545
25.4k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
25.4k
    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
43
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
43
        FlushLiteralRun(true);
551
25.3k
    } else {
552
25.3k
        FlushLiteralRun(done);
553
25.3k
    }
554
25.4k
    repeat_count_ = 0;
555
25.4k
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
528
176
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
176
    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
22
        num_buffered_values_ = 0;
533
22
        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
1
            DCHECK_EQ(literal_count_ % 8, 0);
537
1
            DCHECK_EQ(repeat_count_, 8);
538
1
            FlushLiteralRun(true);
539
1
        }
540
22
        DCHECK_EQ(literal_count_, 0);
541
22
        return;
542
22
    }
543
544
154
    literal_count_ += num_buffered_values_;
545
154
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
154
    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
1
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
1
        FlushLiteralRun(true);
551
153
    } else {
552
153
        FlushLiteralRun(done);
553
153
    }
554
154
    repeat_count_ = 0;
555
154
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
528
71.5k
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
71.5k
    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
46.2k
        num_buffered_values_ = 0;
533
46.2k
        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
5.23k
            DCHECK_EQ(literal_count_ % 8, 0);
537
5.23k
            DCHECK_EQ(repeat_count_, 8);
538
5.23k
            FlushLiteralRun(true);
539
5.23k
        }
540
46.2k
        DCHECK_EQ(literal_count_, 0);
541
46.2k
        return;
542
46.2k
    }
543
544
25.2k
    literal_count_ += num_buffered_values_;
545
25.2k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
25.2k
    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
42
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
42
        FlushLiteralRun(true);
551
25.2k
    } else {
552
25.2k
        FlushLiteralRun(done);
553
25.2k
    }
554
25.2k
    repeat_count_ = 0;
555
25.2k
}
556
557
template <typename T>
558
440
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
559
2.20k
    for (int i = 0; i < num_bytes; ++i) {
560
1.76k
        bit_writer_.PutValue(val, 8);
561
1.76k
    }
562
440
}
563
564
template <typename T>
565
6.65k
int RleEncoder<T>::Flush() {
566
6.65k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
6.63k
        bool all_repeat = literal_count_ == 0 &&
568
6.63k
                          (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
6.63k
        if (repeat_count_ > 0 && all_repeat) {
571
5.01k
            FlushRepeatedRun();
572
5.01k
        } else {
573
1.61k
            literal_count_ += num_buffered_values_;
574
1.61k
            FlushLiteralRun(true);
575
1.61k
            repeat_count_ = 0;
576
1.61k
        }
577
6.63k
    }
578
6.65k
    bit_writer_.Flush();
579
6.65k
    DCHECK_EQ(num_buffered_values_, 0);
580
6.65k
    DCHECK_EQ(literal_count_, 0);
581
6.65k
    DCHECK_EQ(repeat_count_, 0);
582
6.65k
    return bit_writer_.bytes_written();
583
6.65k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
565
219
int RleEncoder<T>::Flush() {
566
219
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
196
        bool all_repeat = literal_count_ == 0 &&
568
196
                          (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
196
        if (repeat_count_ > 0 && all_repeat) {
571
175
            FlushRepeatedRun();
572
175
        } else {
573
21
            literal_count_ += num_buffered_values_;
574
21
            FlushLiteralRun(true);
575
21
            repeat_count_ = 0;
576
21
        }
577
196
    }
578
219
    bit_writer_.Flush();
579
219
    DCHECK_EQ(num_buffered_values_, 0);
580
219
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
219
    return bit_writer_.bytes_written();
583
219
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
565
6.43k
int RleEncoder<T>::Flush() {
566
6.43k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
6.43k
        bool all_repeat = literal_count_ == 0 &&
568
6.43k
                          (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
6.43k
        if (repeat_count_ > 0 && all_repeat) {
571
4.84k
            FlushRepeatedRun();
572
4.84k
        } else {
573
1.59k
            literal_count_ += num_buffered_values_;
574
1.59k
            FlushLiteralRun(true);
575
1.59k
            repeat_count_ = 0;
576
1.59k
        }
577
6.43k
    }
578
6.43k
    bit_writer_.Flush();
579
6.43k
    DCHECK_EQ(num_buffered_values_, 0);
580
6.43k
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
6.43k
    return bit_writer_.bytes_written();
583
6.43k
}
584
585
template <typename T>
586
103k
void RleEncoder<T>::Clear() {
587
103k
    current_value_ = 0;
588
103k
    repeat_count_ = 0;
589
103k
    num_buffered_values_ = 0;
590
103k
    literal_count_ = 0;
591
103k
    literal_indicator_byte_idx_ = -1;
592
103k
    bit_writer_.Clear();
593
103k
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
586
663
void RleEncoder<T>::Clear() {
587
663
    current_value_ = 0;
588
663
    repeat_count_ = 0;
589
663
    num_buffered_values_ = 0;
590
663
    literal_count_ = 0;
591
663
    literal_indicator_byte_idx_ = -1;
592
663
    bit_writer_.Clear();
593
663
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
586
103k
void RleEncoder<T>::Clear() {
587
103k
    current_value_ = 0;
588
103k
    repeat_count_ = 0;
589
103k
    num_buffered_values_ = 0;
590
103k
    literal_count_ = 0;
591
103k
    literal_indicator_byte_idx_ = -1;
592
103k
    bit_writer_.Clear();
593
103k
}
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
150k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
150k
        Reset(buffer, buffer_len, bit_width);
669
150k
    }
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
1.99M
    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
1.83M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
1.83M
    int32_t num_to_output =
751
1.83M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
1.83M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
1.83M
    literal_buffer_pos_ += num_to_output;
754
1.83M
    literal_count_ -= num_to_output;
755
1.83M
    return num_to_output;
756
1.83M
}
757
758
template <typename T>
759
150k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
150k
    bit_reader_.Reset(buffer, buffer_len);
761
150k
    bit_width_ = bit_width;
762
150k
    repeat_count_ = 0;
763
150k
    literal_count_ = 0;
764
150k
    num_buffered_literals_ = 0;
765
150k
    literal_buffer_pos_ = 0;
766
150k
}
767
768
template <typename T>
769
3.65M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
3.65M
    if (repeat_count_ > 0) return repeat_count_;
771
3.65M
    if (literal_count_ == 0) NextCounts();
772
3.65M
    return repeat_count_;
773
3.65M
}
774
775
template <typename T>
776
3.61M
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
3.61M
    uint32_t indicator_value = 0;
780
3.61M
    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
3.61M
    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
3.61M
    uint32_t run_len = indicator_value >> 1;
790
3.61M
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
1.95M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
1.95M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
1.95M
        literal_count_ = cast_set<int32_t>(literal_count);
795
1.95M
    } else {
796
1.65M
        if (UNLIKELY(run_len == 0)) return;
797
1.65M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
1.65M
        if (UNLIKELY(!result)) return;
799
1.65M
        repeat_count_ = run_len;
800
1.65M
    }
801
3.61M
}
802
803
template <typename T>
804
1.66M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
1.66M
    repeat_count_ -= num_repeats_to_consume;
806
1.66M
    return repeated_value_;
807
1.66M
}
808
809
template <typename T>
810
1.99M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
1.99M
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
1.99M
}
815
816
template <typename T>
817
1.99M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
1.99M
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
1.99M
    if (HaveBufferedLiterals()) {
821
28.4k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
28.4k
    }
823
824
1.99M
    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
1.99M
    int32_t num_to_bypass =
829
1.99M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
1.99M
    if (num_to_bypass > 0) {
831
1.23M
        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
1.23M
        if (num_read < num_to_bypass) return false;
834
1.23M
        literal_count_ -= num_to_bypass;
835
1.23M
        num_consumed += num_to_bypass;
836
1.23M
        num_remaining = num_literals_to_consume - num_consumed;
837
1.23M
    }
838
839
1.99M
    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
1.80M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
1.80M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
1.80M
    }
845
1.99M
    return true;
846
1.99M
}
847
848
template <typename T>
849
1.80M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
1.80M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
1.80M
    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
1.80M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
1.80M
    literal_buffer_pos_ = 0;
855
1.80M
    return true;
856
1.80M
}
857
858
template <typename T>
859
201k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
201k
    uint32_t num_consumed = 0;
861
3.85M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
3.65M
        uint32_t num_repeats = NextNumRepeats();
864
3.65M
        if (num_repeats > 0) {
865
1.66M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
1.66M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
29.4M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
27.8M
                values[num_consumed + i] = repeated_value;
869
27.8M
            }
870
1.66M
            num_consumed += num_repeats_to_set;
871
1.66M
            continue;
872
1.66M
        }
873
874
        // Add remaining literal values, if any.
875
1.99M
        uint32_t num_literals = NextNumLiterals();
876
1.99M
        if (num_literals == 0) {
877
0
            break;
878
0
        }
879
1.99M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
1.99M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
0
            return 0;
882
0
        }
883
1.99M
        num_consumed += num_literals_to_set;
884
1.99M
    }
885
201k
    return num_consumed;
886
201k
}
887
} // namespace doris