Coverage Report

Created: 2026-09-01 18:48

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
223k
            : bit_reader_(buffer, buffer_len),
89
223k
              bit_width_(bit_width),
90
223k
              current_value_(0),
91
223k
              repeat_count_(0),
92
223k
              literal_count_(0),
93
223k
              rewind_state_(CANT_REWIND) {
94
223k
        DCHECK_GE(bit_width_, 1);
95
223k
        DCHECK_LE(bit_width_, 64);
96
223k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
88
2.44k
            : bit_reader_(buffer, buffer_len),
89
2.44k
              bit_width_(bit_width),
90
2.44k
              current_value_(0),
91
2.44k
              repeat_count_(0),
92
2.44k
              literal_count_(0),
93
2.44k
              rewind_state_(CANT_REWIND) {
94
2.44k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
2.44k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
88
28.1k
            : bit_reader_(buffer, buffer_len),
89
28.1k
              bit_width_(bit_width),
90
28.1k
              current_value_(0),
91
28.1k
              repeat_count_(0),
92
28.1k
              literal_count_(0),
93
28.1k
              rewind_state_(CANT_REWIND) {
94
28.1k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
28.1k
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
88
193k
            : bit_reader_(buffer, buffer_len),
89
193k
              bit_width_(bit_width),
90
193k
              current_value_(0),
91
193k
              repeat_count_(0),
92
193k
              literal_count_(0),
93
193k
              rewind_state_(CANT_REWIND) {
94
193k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
193k
    }
97
98
27.8M
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
98
4.73k
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
98
26.3k
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
98
27.7M
    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
467k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
467k
        DCHECK_GE(bit_width_, 1);
155
467k
        DCHECK_LE(bit_width_, 64);
156
467k
        Clear();
157
467k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
153
454k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
454k
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
454k
        Clear();
157
454k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
153
12.9k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
12.9k
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
12.9k
        Clear();
157
12.9k
    }
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
61.1k
    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
12.3M
bool RleDecoder<T>::ReadHeader() {
232
12.3M
    DCHECK(bit_reader_.is_initialized());
233
12.3M
    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
477k
        uint32_t indicator_value = 0;
237
477k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
477k
        if (!result) [[unlikely]] {
239
2.30k
            return false;
240
2.30k
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
475k
        bool is_literal = indicator_value & 1;
244
475k
        if (is_literal) {
245
214k
            literal_count_ = (indicator_value >> 1) * 8;
246
214k
            DCHECK_GT(literal_count_, 0);
247
260k
        } else {
248
260k
            repeat_count_ = indicator_value >> 1;
249
260k
            DCHECK_GT(repeat_count_, 0);
250
260k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
260k
                                                     reinterpret_cast<T*>(&current_value_));
252
260k
            DCHECK(result1);
253
260k
        }
254
475k
    }
255
12.3M
    return true;
256
12.3M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
231
147k
bool RleDecoder<T>::ReadHeader() {
232
147k
    DCHECK(bit_reader_.is_initialized());
233
147k
    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
5.79k
        uint32_t indicator_value = 0;
237
5.79k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
5.79k
        if (!result) [[unlikely]] {
239
0
            return false;
240
0
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
5.79k
        bool is_literal = indicator_value & 1;
244
5.79k
        if (is_literal) {
245
1.91k
            literal_count_ = (indicator_value >> 1) * 8;
246
1.91k
            DCHECK_GT(literal_count_, 0);
247
3.88k
        } else {
248
3.88k
            repeat_count_ = indicator_value >> 1;
249
3.88k
            DCHECK_GT(repeat_count_, 0);
250
3.88k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
3.88k
                                                     reinterpret_cast<T*>(&current_value_));
252
3.88k
            DCHECK(result1);
253
3.88k
        }
254
5.79k
    }
255
147k
    return true;
256
147k
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
231
7.90M
bool RleDecoder<T>::ReadHeader() {
232
7.90M
    DCHECK(bit_reader_.is_initialized());
233
7.90M
    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
107k
        uint32_t indicator_value = 0;
237
107k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
107k
        if (!result) [[unlikely]] {
239
0
            return false;
240
0
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
107k
        bool is_literal = indicator_value & 1;
244
107k
        if (is_literal) {
245
69.1k
            literal_count_ = (indicator_value >> 1) * 8;
246
69.1k
            DCHECK_GT(literal_count_, 0);
247
69.1k
        } else {
248
38.7k
            repeat_count_ = indicator_value >> 1;
249
38.7k
            DCHECK_GT(repeat_count_, 0);
250
38.7k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
38.7k
                                                     reinterpret_cast<T*>(&current_value_));
252
38.7k
            DCHECK(result1);
253
38.7k
        }
254
107k
    }
255
7.90M
    return true;
256
7.90M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
231
4.28M
bool RleDecoder<T>::ReadHeader() {
232
4.28M
    DCHECK(bit_reader_.is_initialized());
233
4.28M
    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
363k
        uint32_t indicator_value = 0;
237
363k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
363k
        if (!result) [[unlikely]] {
239
2.30k
            return false;
240
2.30k
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
361k
        bool is_literal = indicator_value & 1;
244
361k
        if (is_literal) {
245
143k
            literal_count_ = (indicator_value >> 1) * 8;
246
143k
            DCHECK_GT(literal_count_, 0);
247
217k
        } else {
248
217k
            repeat_count_ = indicator_value >> 1;
249
217k
            DCHECK_GT(repeat_count_, 0);
250
217k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
217k
                                                     reinterpret_cast<T*>(&current_value_));
252
217k
            DCHECK(result1);
253
217k
        }
254
361k
    }
255
4.28M
    return true;
256
4.28M
}
257
258
template <typename T>
259
7.69M
bool RleDecoder<T>::Get(T* val) {
260
7.69M
    DCHECK(bit_reader_.is_initialized());
261
7.69M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
7.69M
    if (repeat_count_ > 0) [[likely]] {
266
341k
        *val = cast_set<T>(current_value_);
267
341k
        --repeat_count_;
268
341k
        rewind_state_ = REWIND_RUN;
269
7.35M
    } else {
270
7.35M
        DCHECK(literal_count_ > 0);
271
7.35M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
7.35M
        DCHECK(result);
273
7.35M
        --literal_count_;
274
7.35M
        rewind_state_ = REWIND_LITERAL;
275
7.35M
    }
276
277
7.69M
    return true;
278
7.69M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
259
144k
bool RleDecoder<T>::Get(T* val) {
260
144k
    DCHECK(bit_reader_.is_initialized());
261
144k
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
144k
    if (repeat_count_ > 0) [[likely]] {
266
116k
        *val = cast_set<T>(current_value_);
267
116k
        --repeat_count_;
268
116k
        rewind_state_ = REWIND_RUN;
269
116k
    } else {
270
27.6k
        DCHECK(literal_count_ > 0);
271
27.6k
        bool result = bit_reader_.GetValue(bit_width_, val);
272
27.6k
        DCHECK(result);
273
27.6k
        --literal_count_;
274
27.6k
        rewind_state_ = REWIND_LITERAL;
275
27.6k
    }
276
277
144k
    return true;
278
144k
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
259
7.55M
bool RleDecoder<T>::Get(T* val) {
260
7.55M
    DCHECK(bit_reader_.is_initialized());
261
7.55M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
7.55M
    if (repeat_count_ > 0) [[likely]] {
266
224k
        *val = cast_set<T>(current_value_);
267
224k
        --repeat_count_;
268
224k
        rewind_state_ = REWIND_RUN;
269
7.32M
    } else {
270
7.32M
        DCHECK(literal_count_ > 0);
271
7.32M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
7.32M
        DCHECK(result);
273
7.32M
        --literal_count_;
274
7.32M
        rewind_state_ = REWIND_LITERAL;
275
7.32M
    }
276
277
7.55M
    return true;
278
7.55M
}
279
280
template <typename T>
281
503
void RleDecoder<T>::RewindOne() {
282
503
    DCHECK(bit_reader_.is_initialized());
283
284
503
    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
501
    case REWIND_LITERAL: {
292
501
        bit_reader_.Rewind(bit_width_);
293
501
        ++literal_count_;
294
501
        break;
295
0
    }
296
503
    }
297
298
503
    rewind_state_ = CANT_REWIND;
299
503
}
300
301
template <typename T>
302
4.01M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
4.01M
    DCHECK(bit_reader_.is_initialized());
304
4.01M
    DCHECK_GT(max_run, 0);
305
4.01M
    size_t ret = 0;
306
4.01M
    size_t rem = max_run;
307
4.16M
    while (ReadHeader()) {
308
4.16M
        if (repeat_count_ > 0) [[likely]] {
309
3.33M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
28.8k
                return ret;
311
28.8k
            }
312
3.30M
            *val = cast_set<T>(current_value_);
313
3.30M
            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.22M
                repeat_count_ -= rem;
317
3.22M
                ret += rem;
318
3.22M
                return ret;
319
3.22M
            }
320
86.3k
            ret += repeat_count_;
321
86.3k
            rem -= repeat_count_;
322
86.3k
            repeat_count_ = 0;
323
824k
        } else {
324
824k
            DCHECK(literal_count_ > 0);
325
824k
            if (ret == 0) {
326
763k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
763k
                DCHECK(has_more);
328
763k
                literal_count_--;
329
763k
                ret++;
330
763k
                rem--;
331
763k
            }
332
333
1.82M
            while (literal_count_ > 0) {
334
1.76M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
1.76M
                DCHECK(result);
336
1.76M
                if (current_value_ != *val || rem == 0) {
337
758k
                    bit_reader_.Rewind(bit_width_);
338
758k
                    return ret;
339
758k
                }
340
1.00M
                ret++;
341
1.00M
                rem--;
342
1.00M
                literal_count_--;
343
1.00M
            }
344
824k
        }
345
4.16M
    }
346
2.40k
    return ret;
347
4.01M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
302
1.17k
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
1.17k
    DCHECK(bit_reader_.is_initialized());
304
1.17k
    DCHECK_GT(max_run, 0);
305
1.17k
    size_t ret = 0;
306
1.17k
    size_t rem = max_run;
307
1.21k
    while (ReadHeader()) {
308
1.21k
        if (repeat_count_ > 0) [[likely]] {
309
1.09k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
23
                return ret;
311
23
            }
312
1.07k
            *val = cast_set<T>(current_value_);
313
1.07k
            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.04k
                repeat_count_ -= rem;
317
1.04k
                ret += rem;
318
1.04k
                return ret;
319
1.04k
            }
320
34
            ret += repeat_count_;
321
34
            rem -= repeat_count_;
322
34
            repeat_count_ = 0;
323
117
        } else {
324
117
            DCHECK(literal_count_ > 0);
325
117
            if (ret == 0) {
326
106
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
106
                DCHECK(has_more);
328
106
                literal_count_--;
329
106
                ret++;
330
106
                rem--;
331
106
            }
332
333
254
            while (literal_count_ > 0) {
334
243
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
243
                DCHECK(result);
336
243
                if (current_value_ != *val || rem == 0) {
337
106
                    bit_reader_.Rewind(bit_width_);
338
106
                    return ret;
339
106
                }
340
137
                ret++;
341
137
                rem--;
342
137
                literal_count_--;
343
137
            }
344
117
        }
345
1.21k
    }
346
0
    return ret;
347
1.17k
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
302
4.01M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
4.01M
    DCHECK(bit_reader_.is_initialized());
304
4.01M
    DCHECK_GT(max_run, 0);
305
4.01M
    size_t ret = 0;
306
4.01M
    size_t rem = max_run;
307
4.16M
    while (ReadHeader()) {
308
4.15M
        if (repeat_count_ > 0) [[likely]] {
309
3.33M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
28.8k
                return ret;
311
28.8k
            }
312
3.30M
            *val = cast_set<T>(current_value_);
313
3.30M
            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.22M
                repeat_count_ -= rem;
317
3.22M
                ret += rem;
318
3.22M
                return ret;
319
3.22M
            }
320
86.3k
            ret += repeat_count_;
321
86.3k
            rem -= repeat_count_;
322
86.3k
            repeat_count_ = 0;
323
824k
        } else {
324
824k
            DCHECK(literal_count_ > 0);
325
824k
            if (ret == 0) {
326
763k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
763k
                DCHECK(has_more);
328
763k
                literal_count_--;
329
763k
                ret++;
330
763k
                rem--;
331
763k
            }
332
333
1.82M
            while (literal_count_ > 0) {
334
1.76M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
1.76M
                DCHECK(result);
336
1.76M
                if (current_value_ != *val || rem == 0) {
337
758k
                    bit_reader_.Rewind(bit_width_);
338
758k
                    return ret;
339
758k
                }
340
1.00M
                ret++;
341
1.00M
                rem--;
342
1.00M
                literal_count_--;
343
1.00M
            }
344
824k
        }
345
4.15M
    }
346
2.40k
    return ret;
347
4.01M
}
348
349
template <typename T>
350
4.62k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
4.62k
    size_t read_num = 0;
352
11.3k
    while (read_num < num_values) {
353
6.71k
        size_t read_this_time = num_values - read_num;
354
355
6.71k
        if (LIKELY(repeat_count_ > 0)) {
356
4.46k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
4.46k
            std::fill(values, values + read_this_time, current_value_);
358
4.46k
            values += read_this_time;
359
4.46k
            repeat_count_ -= read_this_time;
360
4.46k
            read_num += read_this_time;
361
4.46k
        } else if (literal_count_ > 0) {
362
261
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
1.90k
            for (int i = 0; i < read_this_time; ++i) {
364
1.64k
                bool result = bit_reader_.GetValue(bit_width_, values);
365
1.64k
                DCHECK(result);
366
1.64k
                values++;
367
1.64k
            }
368
261
            literal_count_ -= read_this_time;
369
261
            read_num += read_this_time;
370
1.99k
        } else {
371
1.99k
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
1.99k
        }
375
6.71k
    }
376
4.62k
    return read_num;
377
4.62k
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
350
4.62k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
4.62k
    size_t read_num = 0;
352
11.3k
    while (read_num < num_values) {
353
6.71k
        size_t read_this_time = num_values - read_num;
354
355
6.71k
        if (LIKELY(repeat_count_ > 0)) {
356
4.46k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
4.46k
            std::fill(values, values + read_this_time, current_value_);
358
4.46k
            values += read_this_time;
359
4.46k
            repeat_count_ -= read_this_time;
360
4.46k
            read_num += read_this_time;
361
4.46k
        } else if (literal_count_ > 0) {
362
256
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
1.86k
            for (int i = 0; i < read_this_time; ++i) {
364
1.60k
                bool result = bit_reader_.GetValue(bit_width_, values);
365
1.60k
                DCHECK(result);
366
1.60k
                values++;
367
1.60k
            }
368
256
            literal_count_ -= read_this_time;
369
256
            read_num += read_this_time;
370
1.98k
        } else {
371
1.98k
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
1.98k
        }
375
6.71k
    }
376
4.62k
    return read_num;
377
4.62k
}
_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.17M
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.17M
    DCHECK(bit_reader_.is_initialized());
400
401
1.17M
    size_t set_count = 0;
402
1.58M
    while (to_skip > 0) {
403
410k
        bool result = ReadHeader();
404
410k
        DCHECK(result);
405
406
410k
        if (repeat_count_ > 0) [[likely]] {
407
99.0k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
99.0k
            repeat_count_ -= nskip;
409
99.0k
            to_skip -= nskip;
410
99.0k
            if (current_value_ != 0) {
411
32.2k
                set_count += nskip;
412
32.2k
            }
413
311k
        } else {
414
311k
            DCHECK(literal_count_ > 0);
415
311k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
311k
            literal_count_ -= nskip;
417
311k
            to_skip -= nskip;
418
15.6M
            for (; nskip > 0; nskip--) {
419
15.3M
                T value = 0;
420
15.3M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
15.3M
                DCHECK(result1);
422
15.3M
                if (value != 0) {
423
7.84M
                    set_count++;
424
7.84M
                }
425
15.3M
            }
426
311k
        }
427
410k
    }
428
1.17M
    return set_count;
429
1.17M
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
398
1.06M
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.06M
    DCHECK(bit_reader_.is_initialized());
400
401
1.06M
    size_t set_count = 0;
402
1.36M
    while (to_skip > 0) {
403
299k
        bool result = ReadHeader();
404
299k
        DCHECK(result);
405
406
299k
        if (repeat_count_ > 0) [[likely]] {
407
21.6k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
21.6k
            repeat_count_ -= nskip;
409
21.6k
            to_skip -= nskip;
410
21.6k
            if (current_value_ != 0) {
411
10.2k
                set_count += nskip;
412
10.2k
            }
413
277k
        } else {
414
277k
            DCHECK(literal_count_ > 0);
415
277k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
277k
            literal_count_ -= nskip;
417
277k
            to_skip -= nskip;
418
15.4M
            for (; nskip > 0; nskip--) {
419
15.2M
                T value = 0;
420
15.2M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
15.2M
                DCHECK(result1);
422
15.2M
                if (value != 0) {
423
7.80M
                    set_count++;
424
7.80M
                }
425
15.2M
            }
426
277k
        }
427
299k
    }
428
1.06M
    return set_count;
429
1.06M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
398
107k
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
107k
    DCHECK(bit_reader_.is_initialized());
400
401
107k
    size_t set_count = 0;
402
219k
    while (to_skip > 0) {
403
111k
        bool result = ReadHeader();
404
111k
        DCHECK(result);
405
406
111k
        if (repeat_count_ > 0) [[likely]] {
407
77.3k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
77.3k
            repeat_count_ -= nskip;
409
77.3k
            to_skip -= nskip;
410
77.3k
            if (current_value_ != 0) {
411
22.0k
                set_count += nskip;
412
22.0k
            }
413
77.3k
        } else {
414
34.3k
            DCHECK(literal_count_ > 0);
415
34.3k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
34.3k
            literal_count_ -= nskip;
417
34.3k
            to_skip -= nskip;
418
158k
            for (; nskip > 0; nskip--) {
419
123k
                T value = 0;
420
123k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
123k
                DCHECK(result1);
422
123k
                if (value != 0) {
423
40.6k
                    set_count++;
424
40.6k
                }
425
123k
            }
426
34.3k
        }
427
111k
    }
428
107k
    return set_count;
429
107k
}
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
7.02M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
7.02M
    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
7.02M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
972k
        repeat_count_ += run_length;
441
972k
        return;
442
972k
    }
443
444
    // Handle run_length > 1 more efficiently
445
13.1M
    while (run_length > 0) {
446
7.64M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
3.78M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
14.3M
            for (size_t i = 0; i < to_buffer; ++i) {
450
10.5M
                buffered_values_[num_buffered_values_++] = value;
451
10.5M
                ++repeat_count_;
452
10.5M
            }
453
3.78M
            run_length -= to_buffer;
454
3.78M
            if (num_buffered_values_ == 8) {
455
1.34M
                DCHECK_EQ(literal_count_ % 8, 0);
456
1.34M
                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.34M
                if (repeat_count_ >= 8 && run_length > 0) {
460
499k
                    repeat_count_ += run_length;
461
499k
                    return;
462
499k
                }
463
1.34M
            }
464
3.86M
        } else {
465
            // Value changed
466
3.86M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
427k
                DCHECK_EQ(literal_count_, 0);
470
427k
                FlushRepeatedRun();
471
427k
            }
472
3.86M
            repeat_count_ = 1;
473
3.86M
            current_value_ = value;
474
475
3.86M
            buffered_values_[num_buffered_values_++] = value;
476
3.86M
            --run_length;
477
3.86M
            if (num_buffered_values_ == 8) {
478
370k
                DCHECK_EQ(literal_count_ % 8, 0);
479
370k
                FlushBufferedValues(false);
480
370k
            }
481
3.86M
        }
482
7.64M
    }
483
6.05M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
434
3.06M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
3.06M
    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.06M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
645k
        repeat_count_ += run_length;
441
645k
        return;
442
645k
    }
443
444
    // Handle run_length > 1 more efficiently
445
5.93M
    while (run_length > 0) {
446
4.00M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
1.96M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
10.7M
            for (size_t i = 0; i < to_buffer; ++i) {
450
8.74M
                buffered_values_[num_buffered_values_++] = value;
451
8.74M
                ++repeat_count_;
452
8.74M
            }
453
1.96M
            run_length -= to_buffer;
454
1.96M
            if (num_buffered_values_ == 8) {
455
1.11M
                DCHECK_EQ(literal_count_ % 8, 0);
456
1.11M
                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.11M
                if (repeat_count_ >= 8 && run_length > 0) {
460
499k
                    repeat_count_ += run_length;
461
499k
                    return;
462
499k
                }
463
1.11M
            }
464
2.03M
        } else {
465
            // Value changed
466
2.03M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
423k
                DCHECK_EQ(literal_count_, 0);
470
423k
                FlushRepeatedRun();
471
423k
            }
472
2.03M
            repeat_count_ = 1;
473
2.03M
            current_value_ = value;
474
475
2.03M
            buffered_values_[num_buffered_values_++] = value;
476
2.03M
            --run_length;
477
2.03M
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
144k
                FlushBufferedValues(false);
480
144k
            }
481
2.03M
        }
482
4.00M
    }
483
2.42M
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
434
3.96M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
3.96M
    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.96M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
327k
        repeat_count_ += run_length;
441
327k
        return;
442
327k
    }
443
444
    // Handle run_length > 1 more efficiently
445
7.26M
    while (run_length > 0) {
446
3.63M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
1.81M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
3.62M
            for (size_t i = 0; i < to_buffer; ++i) {
450
1.81M
                buffered_values_[num_buffered_values_++] = value;
451
1.81M
                ++repeat_count_;
452
1.81M
            }
453
1.81M
            run_length -= to_buffer;
454
1.81M
            if (num_buffered_values_ == 8) {
455
227k
                DCHECK_EQ(literal_count_ % 8, 0);
456
227k
                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
227k
                if (repeat_count_ >= 8 && run_length > 0) {
460
3
                    repeat_count_ += run_length;
461
3
                    return;
462
3
                }
463
227k
            }
464
1.82M
        } else {
465
            // Value changed
466
1.82M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
4.86k
                DCHECK_EQ(literal_count_, 0);
470
4.86k
                FlushRepeatedRun();
471
4.86k
            }
472
1.82M
            repeat_count_ = 1;
473
1.82M
            current_value_ = value;
474
475
1.82M
            buffered_values_[num_buffered_values_++] = value;
476
1.82M
            --run_length;
477
1.82M
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
225k
                FlushBufferedValues(false);
480
225k
            }
481
1.82M
        }
482
3.63M
    }
483
3.63M
}
484
485
template <typename T>
486
1.59M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
1.59M
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
440k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
440k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
440k
    }
492
493
    // Write all the buffered values as bit packed literals
494
10.9M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
9.32M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
9.32M
    }
497
1.59M
    num_buffered_values_ = 0;
498
499
1.59M
    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
440k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
440k
        int32_t indicator_value = (num_groups << 1) | 1;
506
440k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
440k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
440k
                cast_set<uint8_t>(indicator_value);
509
440k
        literal_indicator_byte_idx_ = -1;
510
440k
        literal_count_ = 0;
511
440k
    }
512
1.59M
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
486
1.14M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
1.14M
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
428k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
428k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
428k
    }
492
493
    // Write all the buffered values as bit packed literals
494
6.90M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
5.75M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
5.75M
    }
497
1.14M
    num_buffered_values_ = 0;
498
499
1.14M
    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
428k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
428k
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
428k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
428k
                cast_set<uint8_t>(indicator_value);
509
428k
        literal_indicator_byte_idx_ = -1;
510
428k
        literal_count_ = 0;
511
428k
    }
512
1.14M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
486
453k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
453k
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
12.2k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
12.2k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
12.2k
    }
492
493
    // Write all the buffered values as bit packed literals
494
4.01M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
3.56M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
3.56M
    }
497
453k
    num_buffered_values_ = 0;
498
499
453k
    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.2k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
12.2k
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
12.2k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
12.2k
                cast_set<uint8_t>(indicator_value);
509
12.2k
        literal_indicator_byte_idx_ = -1;
510
12.2k
        literal_count_ = 0;
511
12.2k
    }
512
453k
}
513
514
template <typename T>
515
512k
void RleEncoder<T>::FlushRepeatedRun() {
516
512k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
512k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
512k
    bit_writer_.PutVlqInt(indicator_value);
520
512k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
512k
    num_buffered_values_ = 0;
522
512k
    repeat_count_ = 0;
523
512k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
515
499k
void RleEncoder<T>::FlushRepeatedRun() {
516
499k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
499k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
499k
    bit_writer_.PutVlqInt(indicator_value);
520
499k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
499k
    num_buffered_values_ = 0;
522
499k
    repeat_count_ = 0;
523
499k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
515
12.8k
void RleEncoder<T>::FlushRepeatedRun() {
516
12.8k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
12.8k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
12.8k
    bit_writer_.PutVlqInt(indicator_value);
520
12.8k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
12.8k
    num_buffered_values_ = 0;
522
12.8k
    repeat_count_ = 0;
523
12.8k
}
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
1.70M
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
1.70M
    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
541k
        num_buffered_values_ = 0;
533
541k
        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
399k
            DCHECK_EQ(literal_count_ % 8, 0);
537
399k
            DCHECK_EQ(repeat_count_, 8);
538
399k
            FlushLiteralRun(true);
539
399k
        }
540
541k
        DCHECK_EQ(literal_count_, 0);
541
541k
        return;
542
541k
    }
543
544
1.16M
    literal_count_ += num_buffered_values_;
545
1.16M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
1.16M
    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.85k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
5.85k
        FlushLiteralRun(true);
551
1.15M
    } else {
552
1.15M
        FlushLiteralRun(done);
553
1.15M
    }
554
1.16M
    repeat_count_ = 0;
555
1.16M
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
528
1.24M
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
1.24M
    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
535k
        num_buffered_values_ = 0;
533
535k
        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
396k
            DCHECK_EQ(literal_count_ % 8, 0);
537
396k
            DCHECK_EQ(repeat_count_, 8);
538
396k
            FlushLiteralRun(true);
539
396k
        }
540
535k
        DCHECK_EQ(literal_count_, 0);
541
535k
        return;
542
535k
    }
543
544
713k
    literal_count_ += num_buffered_values_;
545
713k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
713k
    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
537
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
537
        FlushLiteralRun(true);
551
712k
    } else {
552
712k
        FlushLiteralRun(done);
553
712k
    }
554
713k
    repeat_count_ = 0;
555
713k
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
528
452k
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
452k
    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
5.93k
        num_buffered_values_ = 0;
533
5.93k
        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.63k
            DCHECK_EQ(literal_count_ % 8, 0);
537
3.63k
            DCHECK_EQ(repeat_count_, 8);
538
3.63k
            FlushLiteralRun(true);
539
3.63k
        }
540
5.93k
        DCHECK_EQ(literal_count_, 0);
541
5.93k
        return;
542
5.93k
    }
543
544
446k
    literal_count_ += num_buffered_values_;
545
446k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
446k
    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.31k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
5.31k
        FlushLiteralRun(true);
551
441k
    } else {
552
441k
        FlushLiteralRun(done);
553
441k
    }
554
446k
    repeat_count_ = 0;
555
446k
}
556
557
template <typename T>
558
24.8k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
559
124k
    for (int i = 0; i < num_bytes; ++i) {
560
99.2k
        bit_writer_.PutValue(val, 8);
561
99.2k
    }
562
24.8k
}
563
564
template <typename T>
565
119k
int RleEncoder<T>::Flush() {
566
119k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
119k
        bool all_repeat = literal_count_ == 0 &&
568
119k
                          (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
119k
        if (repeat_count_ > 0 && all_repeat) {
571
85.0k
            FlushRepeatedRun();
572
85.0k
        } else {
573
34.3k
            literal_count_ += num_buffered_values_;
574
34.3k
            FlushLiteralRun(true);
575
34.3k
            repeat_count_ = 0;
576
34.3k
        }
577
119k
    }
578
119k
    bit_writer_.Flush();
579
119k
    DCHECK_EQ(num_buffered_values_, 0);
580
119k
    DCHECK_EQ(literal_count_, 0);
581
119k
    DCHECK_EQ(repeat_count_, 0);
582
119k
    return bit_writer_.bytes_written();
583
119k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
565
108k
int RleEncoder<T>::Flush() {
566
108k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
108k
        bool all_repeat = literal_count_ == 0 &&
568
108k
                          (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
108k
        if (repeat_count_ > 0 && all_repeat) {
571
76.9k
            FlushRepeatedRun();
572
76.9k
        } else {
573
31.0k
            literal_count_ += num_buffered_values_;
574
31.0k
            FlushLiteralRun(true);
575
31.0k
            repeat_count_ = 0;
576
31.0k
        }
577
108k
    }
578
108k
    bit_writer_.Flush();
579
108k
    DCHECK_EQ(num_buffered_values_, 0);
580
108k
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
108k
    return bit_writer_.bytes_written();
583
108k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
565
11.8k
int RleEncoder<T>::Flush() {
566
11.8k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
11.3k
        bool all_repeat = literal_count_ == 0 &&
568
11.3k
                          (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.3k
        if (repeat_count_ > 0 && all_repeat) {
571
8.04k
            FlushRepeatedRun();
572
8.04k
        } else {
573
3.25k
            literal_count_ += num_buffered_values_;
574
3.25k
            FlushLiteralRun(true);
575
3.25k
            repeat_count_ = 0;
576
3.25k
        }
577
11.3k
    }
578
11.8k
    bit_writer_.Flush();
579
11.8k
    DCHECK_EQ(num_buffered_values_, 0);
580
11.8k
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
11.8k
    return bit_writer_.bytes_written();
583
11.8k
}
584
585
template <typename T>
586
945k
void RleEncoder<T>::Clear() {
587
945k
    current_value_ = 0;
588
945k
    repeat_count_ = 0;
589
945k
    num_buffered_values_ = 0;
590
945k
    literal_count_ = 0;
591
945k
    literal_indicator_byte_idx_ = -1;
592
945k
    bit_writer_.Clear();
593
945k
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
586
907k
void RleEncoder<T>::Clear() {
587
907k
    current_value_ = 0;
588
907k
    repeat_count_ = 0;
589
907k
    num_buffered_values_ = 0;
590
907k
    literal_count_ = 0;
591
907k
    literal_indicator_byte_idx_ = -1;
592
907k
    bit_writer_.Clear();
593
907k
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
586
37.7k
void RleEncoder<T>::Clear() {
587
37.7k
    current_value_ = 0;
588
37.7k
    repeat_count_ = 0;
589
37.7k
    num_buffered_values_ = 0;
590
37.7k
    literal_count_ = 0;
591
37.7k
    literal_indicator_byte_idx_ = -1;
592
37.7k
    bit_writer_.Clear();
593
37.7k
}
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
8.90k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
8.90k
        Reset(buffer, buffer_len, bit_width);
669
8.90k
    }
_ZN5doris15RleBatchDecoderIjEC2EPhii
Line
Count
Source
667
5.17k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
5.17k
        Reset(buffer, buffer_len, bit_width);
669
5.17k
    }
_ZN5doris15RleBatchDecoderItEC2EPhii
Line
Count
Source
667
3.72k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
3.72k
        Reset(buffer, buffer_len, bit_width);
669
3.72k
    }
_ZN5doris15RleBatchDecoderIhEC2EPhii
Line
Count
Source
667
6
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
6
        Reset(buffer, buffer_len, bit_width);
669
6
    }
670
671
6.97k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderItEC2Ev
Line
Count
Source
671
6.96k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderIhEC2Ev
Line
Count
Source
671
8
    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
878k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIjE20HaveBufferedLiteralsEv
Line
Count
Source
712
20.7k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderItE20HaveBufferedLiteralsEv
Line
Count
Source
712
857k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIhE20HaveBufferedLiteralsEv
Line
Count
Source
712
9
    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
878k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
878k
    int32_t num_to_output =
751
878k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
878k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
878k
    literal_buffer_pos_ += num_to_output;
754
878k
    literal_count_ -= num_to_output;
755
878k
    return num_to_output;
756
878k
}
_ZN5doris15RleBatchDecoderIjE22OutputBufferedLiteralsEiPj
Line
Count
Source
749
20.8k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
20.8k
    int32_t num_to_output =
751
20.8k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
20.8k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
20.8k
    literal_buffer_pos_ += num_to_output;
754
20.8k
    literal_count_ -= num_to_output;
755
20.8k
    return num_to_output;
756
20.8k
}
_ZN5doris15RleBatchDecoderItE22OutputBufferedLiteralsEiPt
Line
Count
Source
749
857k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
857k
    int32_t num_to_output =
751
857k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
857k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
857k
    literal_buffer_pos_ += num_to_output;
754
857k
    literal_count_ -= num_to_output;
755
857k
    return num_to_output;
756
857k
}
_ZN5doris15RleBatchDecoderIhE22OutputBufferedLiteralsEiPh
Line
Count
Source
749
6
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
6
    int32_t num_to_output =
751
6
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
6
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
6
    literal_buffer_pos_ += num_to_output;
754
6
    literal_count_ -= num_to_output;
755
6
    return num_to_output;
756
6
}
757
758
template <typename T>
759
8.90k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
8.90k
    bit_reader_.Reset(buffer, buffer_len);
761
8.90k
    bit_width_ = bit_width;
762
8.90k
    repeat_count_ = 0;
763
8.90k
    literal_count_ = 0;
764
8.90k
    num_buffered_literals_ = 0;
765
8.90k
    literal_buffer_pos_ = 0;
766
8.90k
}
_ZN5doris15RleBatchDecoderIjE5ResetEPhii
Line
Count
Source
759
5.17k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
5.17k
    bit_reader_.Reset(buffer, buffer_len);
761
5.17k
    bit_width_ = bit_width;
762
5.17k
    repeat_count_ = 0;
763
5.17k
    literal_count_ = 0;
764
5.17k
    num_buffered_literals_ = 0;
765
5.17k
    literal_buffer_pos_ = 0;
766
5.17k
}
_ZN5doris15RleBatchDecoderItE5ResetEPhii
Line
Count
Source
759
3.72k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
3.72k
    bit_reader_.Reset(buffer, buffer_len);
761
3.72k
    bit_width_ = bit_width;
762
3.72k
    repeat_count_ = 0;
763
3.72k
    literal_count_ = 0;
764
3.72k
    num_buffered_literals_ = 0;
765
3.72k
    literal_buffer_pos_ = 0;
766
3.72k
}
_ZN5doris15RleBatchDecoderIhE5ResetEPhii
Line
Count
Source
759
6
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
6
    bit_reader_.Reset(buffer, buffer_len);
761
6
    bit_width_ = bit_width;
762
6
    repeat_count_ = 0;
763
6
    literal_count_ = 0;
764
6
    num_buffered_literals_ = 0;
765
6
    literal_buffer_pos_ = 0;
766
6
}
767
768
template <typename T>
769
9.15M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
9.15M
    if (repeat_count_ > 0) return repeat_count_;
771
971k
    if (literal_count_ == 0) NextCounts();
772
971k
    return repeat_count_;
773
9.15M
}
_ZN5doris15RleBatchDecoderIjE14NextNumRepeatsEv
Line
Count
Source
769
21.9k
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
21.9k
    if (repeat_count_ > 0) return repeat_count_;
771
21.4k
    if (literal_count_ == 0) NextCounts();
772
21.4k
    return repeat_count_;
773
21.9k
}
_ZN5doris15RleBatchDecoderItE14NextNumRepeatsEv
Line
Count
Source
769
9.12M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
9.12M
    if (repeat_count_ > 0) return repeat_count_;
771
949k
    if (literal_count_ == 0) NextCounts();
772
949k
    return repeat_count_;
773
9.12M
}
_ZN5doris15RleBatchDecoderIhE14NextNumRepeatsEv
Line
Count
Source
769
265
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
265
    if (repeat_count_ > 0) return repeat_count_;
771
10
    if (literal_count_ == 0) NextCounts();
772
10
    return repeat_count_;
773
265
}
774
775
template <typename T>
776
205k
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
205k
    uint32_t indicator_value = 0;
780
205k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
781
2
        return;
782
2
    }
783
784
    // lsb indicates if it is a literal run or repeated run
785
205k
    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
205k
    uint32_t run_len = indicator_value >> 1;
790
205k
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
110k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
110k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
110k
        literal_count_ = cast_set<int32_t>(literal_count);
795
110k
    } else {
796
95.9k
        if (UNLIKELY(run_len == 0)) return;
797
95.9k
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
95.9k
        if (UNLIKELY(!result)) return;
799
95.9k
        repeat_count_ = run_len;
800
95.9k
    }
801
205k
}
_ZN5doris15RleBatchDecoderIjE10NextCountsEv
Line
Count
Source
776
17.8k
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
17.8k
    uint32_t indicator_value = 0;
780
17.8k
    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
17.8k
    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
17.8k
    uint32_t run_len = indicator_value >> 1;
790
17.8k
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
17.1k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
17.1k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
17.1k
        literal_count_ = cast_set<int32_t>(literal_count);
795
17.1k
    } else {
796
712
        if (UNLIKELY(run_len == 0)) return;
797
712
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
712
        if (UNLIKELY(!result)) return;
799
712
        repeat_count_ = run_len;
800
712
    }
801
17.8k
}
_ZN5doris15RleBatchDecoderItE10NextCountsEv
Line
Count
Source
776
188k
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
188k
    uint32_t indicator_value = 0;
780
188k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
781
2
        return;
782
2
    }
783
784
    // lsb indicates if it is a literal run or repeated run
785
188k
    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
188k
    uint32_t run_len = indicator_value >> 1;
790
188k
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
92.8k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
92.8k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
92.8k
        literal_count_ = cast_set<int32_t>(literal_count);
795
95.2k
    } else {
796
95.2k
        if (UNLIKELY(run_len == 0)) return;
797
95.2k
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
95.2k
        if (UNLIKELY(!result)) return;
799
95.2k
        repeat_count_ = run_len;
800
95.2k
    }
801
188k
}
_ZN5doris15RleBatchDecoderIhE10NextCountsEv
Line
Count
Source
776
8
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
8
    uint32_t indicator_value = 0;
780
8
    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
8
    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
8
    uint32_t run_len = indicator_value >> 1;
790
8
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
7
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
7
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
7
        literal_count_ = cast_set<int32_t>(literal_count);
795
7
    } else {
796
1
        if (UNLIKELY(run_len == 0)) return;
797
1
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
1
        if (UNLIKELY(!result)) return;
799
1
        repeat_count_ = run_len;
800
1
    }
801
8
}
802
803
template <typename T>
804
8.27M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
8.27M
    repeat_count_ -= num_repeats_to_consume;
806
8.27M
    return repeated_value_;
807
8.27M
}
_ZN5doris15RleBatchDecoderIjE16GetRepeatedValueEi
Line
Count
Source
804
1.18k
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
1.18k
    repeat_count_ -= num_repeats_to_consume;
806
1.18k
    return repeated_value_;
807
1.18k
}
_ZN5doris15RleBatchDecoderItE16GetRepeatedValueEi
Line
Count
Source
804
8.27M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
8.27M
    repeat_count_ -= num_repeats_to_consume;
806
8.27M
    return repeated_value_;
807
8.27M
}
_ZN5doris15RleBatchDecoderIhE16GetRepeatedValueEi
Line
Count
Source
804
256
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
256
    repeat_count_ -= num_repeats_to_consume;
806
256
    return repeated_value_;
807
256
}
808
809
template <typename T>
810
878k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
878k
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
878k
}
_ZN5doris15RleBatchDecoderIjE15NextNumLiteralsEv
Line
Count
Source
810
20.7k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
20.7k
    if (literal_count_ > 0) return literal_count_;
812
0
    if (repeat_count_ == 0) NextCounts();
813
0
    return literal_count_;
814
20.7k
}
_ZN5doris15RleBatchDecoderItE15NextNumLiteralsEv
Line
Count
Source
810
857k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
857k
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
857k
}
_ZN5doris15RleBatchDecoderIhE15NextNumLiteralsEv
Line
Count
Source
810
9
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
9
    if (literal_count_ > 0) return literal_count_;
812
0
    if (repeat_count_ == 0) NextCounts();
813
0
    return literal_count_;
814
9
}
815
816
template <typename T>
817
878k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
878k
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
878k
    if (HaveBufferedLiterals()) {
821
765k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
765k
    }
823
824
878k
    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
878k
    int32_t num_to_bypass =
829
878k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
878k
    if (num_to_bypass > 0) {
831
13.5k
        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
13.5k
        if (num_read < num_to_bypass) return false;
834
13.5k
        literal_count_ -= num_to_bypass;
835
13.5k
        num_consumed += num_to_bypass;
836
13.5k
        num_remaining = num_literals_to_consume - num_consumed;
837
13.5k
    }
838
839
878k
    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
113k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
113k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
113k
    }
845
878k
    return true;
846
878k
}
_ZN5doris15RleBatchDecoderIjE16GetLiteralValuesEiPj
Line
Count
Source
817
20.7k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
20.7k
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
20.7k
    if (HaveBufferedLiterals()) {
821
3.47k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
3.47k
    }
823
824
20.7k
    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
20.7k
    int32_t num_to_bypass =
829
20.7k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
20.7k
    if (num_to_bypass > 0) {
831
13.5k
        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
13.5k
        if (num_read < num_to_bypass) return false;
834
13.5k
        literal_count_ -= num_to_bypass;
835
13.5k
        num_consumed += num_to_bypass;
836
13.5k
        num_remaining = num_literals_to_consume - num_consumed;
837
13.5k
    }
838
839
20.7k
    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
17.3k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
17.3k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
17.3k
    }
845
20.7k
    return true;
846
20.7k
}
_ZN5doris15RleBatchDecoderItE16GetLiteralValuesEiPt
Line
Count
Source
817
857k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
857k
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
857k
    if (HaveBufferedLiterals()) {
821
762k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
762k
    }
823
824
857k
    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
857k
    int32_t num_to_bypass =
829
857k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
857k
    if (num_to_bypass > 0) {
831
4
        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
4
        if (num_read < num_to_bypass) return false;
834
4
        literal_count_ -= num_to_bypass;
835
4
        num_consumed += num_to_bypass;
836
4
        num_remaining = num_literals_to_consume - num_consumed;
837
4
    }
838
839
857k
    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
95.6k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
95.6k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
95.6k
    }
845
857k
    return true;
846
857k
}
_ZN5doris15RleBatchDecoderIhE16GetLiteralValuesEiPh
Line
Count
Source
817
9
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
9
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
9
    if (HaveBufferedLiterals()) {
821
2
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
2
    }
823
824
9
    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
9
    int32_t num_to_bypass =
829
9
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
9
    if (num_to_bypass > 0) {
831
0
        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
0
        if (num_read < num_to_bypass) return false;
834
0
        literal_count_ -= num_to_bypass;
835
0
        num_consumed += num_to_bypass;
836
0
        num_remaining = num_literals_to_consume - num_consumed;
837
0
    }
838
839
9
    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
7
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
4
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
4
    }
845
6
    return true;
846
9
}
847
848
template <typename T>
849
113k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
113k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
113k
    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
113k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
113k
    literal_buffer_pos_ = 0;
855
113k
    return true;
856
113k
}
_ZN5doris15RleBatchDecoderIjE17FillLiteralBufferEv
Line
Count
Source
849
17.3k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
17.3k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
17.3k
    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
17.3k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
17.3k
    literal_buffer_pos_ = 0;
855
17.3k
    return true;
856
17.3k
}
_ZN5doris15RleBatchDecoderItE17FillLiteralBufferEv
Line
Count
Source
849
95.6k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
95.6k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
95.6k
    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
95.6k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
95.6k
    literal_buffer_pos_ = 0;
855
95.6k
    return true;
856
95.6k
}
_ZN5doris15RleBatchDecoderIhE17FillLiteralBufferEv
Line
Count
Source
849
7
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
7
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
7
    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
7
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
4
    literal_buffer_pos_ = 0;
855
4
    return true;
856
7
}
857
858
template <typename T>
859
9.12M
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
9.12M
    uint32_t num_consumed = 0;
861
18.2M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
9.13M
        uint32_t num_repeats = NextNumRepeats();
864
9.13M
        if (num_repeats > 0) {
865
8.27M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
8.27M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
27.8M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
19.6M
                values[num_consumed + i] = repeated_value;
869
19.6M
            }
870
8.27M
            num_consumed += num_repeats_to_set;
871
8.27M
            continue;
872
8.27M
        }
873
874
        // Add remaining literal values, if any.
875
864k
        uint32_t num_literals = NextNumLiterals();
876
864k
        if (num_literals == 0) {
877
1
            break;
878
1
        }
879
864k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
864k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
4
            return 0;
882
4
        }
883
864k
        num_consumed += num_literals_to_set;
884
864k
    }
885
9.12M
    return num_consumed;
886
9.12M
}
_ZN5doris15RleBatchDecoderIjE8GetBatchEPjj
Line
Count
Source
859
5.93k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
5.93k
    uint32_t num_consumed = 0;
861
15.6k
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
9.71k
        uint32_t num_repeats = NextNumRepeats();
864
9.71k
        if (num_repeats > 0) {
865
977
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
977
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
1.12M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
1.12M
                values[num_consumed + i] = repeated_value;
869
1.12M
            }
870
977
            num_consumed += num_repeats_to_set;
871
977
            continue;
872
977
        }
873
874
        // Add remaining literal values, if any.
875
8.74k
        uint32_t num_literals = NextNumLiterals();
876
8.74k
        if (num_literals == 0) {
877
0
            break;
878
0
        }
879
8.74k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
8.74k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
0
            return 0;
882
0
        }
883
8.74k
        num_consumed += num_literals_to_set;
884
8.74k
    }
885
5.93k
    return num_consumed;
886
5.93k
}
_ZN5doris15RleBatchDecoderItE8GetBatchEPtj
Line
Count
Source
859
9.12M
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
9.12M
    uint32_t num_consumed = 0;
861
18.2M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
9.12M
        uint32_t num_repeats = NextNumRepeats();
864
9.12M
        if (num_repeats > 0) {
865
8.27M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
8.27M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
25.7M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
17.4M
                values[num_consumed + i] = repeated_value;
869
17.4M
            }
870
8.27M
            num_consumed += num_repeats_to_set;
871
8.27M
            continue;
872
8.27M
        }
873
874
        // Add remaining literal values, if any.
875
855k
        uint32_t num_literals = NextNumLiterals();
876
855k
        if (num_literals == 0) {
877
1
            break;
878
1
        }
879
855k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
855k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
1
            return 0;
882
1
        }
883
855k
        num_consumed += num_literals_to_set;
884
855k
    }
885
9.12M
    return num_consumed;
886
9.12M
}
_ZN5doris15RleBatchDecoderIhE8GetBatchEPhj
Line
Count
Source
859
263
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
263
    uint32_t num_consumed = 0;
861
525
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
265
        uint32_t num_repeats = NextNumRepeats();
864
265
        if (num_repeats > 0) {
865
256
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
256
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
1.04M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
1.04M
                values[num_consumed + i] = repeated_value;
869
1.04M
            }
870
256
            num_consumed += num_repeats_to_set;
871
256
            continue;
872
256
        }
873
874
        // Add remaining literal values, if any.
875
9
        uint32_t num_literals = NextNumLiterals();
876
9
        if (num_literals == 0) {
877
0
            break;
878
0
        }
879
9
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
9
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
3
            return 0;
882
3
        }
883
6
        num_consumed += num_literals_to_set;
884
6
    }
885
260
    return num_consumed;
886
263
}
887
} // namespace doris