Coverage Report

Created: 2026-05-29 15:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/binary_dict_page.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "storage/segment/binary_dict_page.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
23
#include <algorithm>
24
#include <ostream>
25
#include <utility>
26
27
#include "common/compiler_util.h" // IWYU pragma: keep
28
#include "common/config.h"
29
#include "common/logging.h"
30
#include "common/status.h"
31
#include "core/column/column.h"
32
#include "core/column/column_string.h"
33
#include "storage/segment/binary_plain_page_v2.h"
34
#include "storage/segment/bitshuffle_page.h"
35
#include "storage/segment/encoding_info.h"
36
#include "util/coding.h"
37
#include "util/slice.h" // for Slice
38
39
namespace doris {
40
struct StringRef;
41
42
namespace segment_v2 {
43
44
BinaryDictPageBuilder::BinaryDictPageBuilder(const PageBuilderOptions& options)
45
3.81k
        : _options(options),
46
3.81k
          _finished(false),
47
3.81k
          _data_page_builder(nullptr),
48
3.81k
          _dict_builder(nullptr),
49
3.81k
          _encoding_type(DICT_ENCODING),
50
          _binary_plain_encoding_type(
51
3.81k
                  options.dict_binary_plain_encoding ==
52
3.81k
                                  BinaryPlainEncodingTypePB::BINARY_PLAIN_ENCODING_V2
53
3.81k
                          ? PLAIN_ENCODING_V2
54
3.81k
                          : PLAIN_ENCODING) {}
55
56
3.81k
Status BinaryDictPageBuilder::init() {
57
    // initially use DICT_ENCODING
58
    // TODO: the data page builder type can be created by Factory according to user config
59
3.81k
    PageBuilder* data_page_builder_ptr = nullptr;
60
3.81k
    RETURN_IF_ERROR(BitshufflePageBuilder<FieldType::OLAP_FIELD_TYPE_INT>::create(
61
3.81k
            &data_page_builder_ptr, _options));
62
3.81k
    _data_page_builder.reset(data_page_builder_ptr);
63
3.81k
    PageBuilderOptions dict_builder_options;
64
    // here the binary plain page is used to store the dictionary items so
65
    // the data page size is set to the same as the dict page size
66
3.81k
    dict_builder_options.data_page_size = _options.dict_page_size;
67
3.81k
    dict_builder_options.dict_page_size = _options.dict_page_size;
68
3.81k
    dict_builder_options.is_dict_page = true;
69
70
3.81k
    const EncodingInfo* encoding_info;
71
3.81k
    RETURN_IF_ERROR(EncodingInfo::get(FieldType::OLAP_FIELD_TYPE_VARCHAR,
72
3.81k
                                      _binary_plain_encoding_type, &encoding_info));
73
3.81k
    RETURN_IF_ERROR(encoding_info->create_page_builder(dict_builder_options, _dict_builder));
74
3.81k
    return reset();
75
3.81k
}
76
77
1.06M
bool BinaryDictPageBuilder::is_page_full() {
78
1.06M
    if (_data_page_builder->is_page_full()) {
79
2.00k
        return true;
80
2.00k
    }
81
1.06M
    if (_encoding_type == DICT_ENCODING && _dict_builder->is_page_full()) {
82
3.13k
        return true;
83
3.13k
    }
84
1.05M
    return false;
85
1.06M
}
86
87
219k
Status BinaryDictPageBuilder::add(const uint8_t* vals, size_t* count) {
88
219k
    if (_encoding_type == DICT_ENCODING) {
89
16.4k
        DCHECK(!_finished);
90
16.4k
        DCHECK_GT(*count, 0);
91
16.4k
        const Slice* src = reinterpret_cast<const Slice*>(vals);
92
16.4k
        size_t num_added = 0;
93
16.4k
        uint32_t value_code = -1;
94
16.4k
        auto* actual_builder = dynamic_cast<BitshufflePageBuilder<FieldType::OLAP_FIELD_TYPE_INT>*>(
95
16.4k
                _data_page_builder.get());
96
97
858k
        for (int i = 0; i < *count; ++i, ++src) {
98
844k
            if (is_page_full()) {
99
1.56k
                break;
100
1.56k
            }
101
102
842k
            if (src->empty() && _has_empty) {
103
87.8k
                value_code = _empty_code;
104
754k
            } else if (auto iter = _dictionary.find(*src); iter != _dictionary.end()) {
105
631k
                value_code = iter->second;
106
631k
            } else {
107
123k
                Slice dict_item(src->data, src->size);
108
123k
                if (src->size > 0) {
109
123k
                    char* item_mem = _arena.alloc(src->size);
110
123k
                    if (item_mem == nullptr) {
111
0
                        return Status::MemoryAllocFailed("memory allocate failed, size:{}",
112
0
                                                         src->size);
113
0
                    }
114
123k
                    dict_item.relocate(item_mem);
115
123k
                }
116
123k
                value_code = cast_set<uint32_t>(_dictionary.size());
117
123k
                size_t add_count = 1;
118
123k
                RETURN_IF_ERROR(_dict_builder->add(reinterpret_cast<const uint8_t*>(&dict_item),
119
123k
                                                   &add_count));
120
123k
                if (add_count == 0) {
121
                    // current dict page is full, stop processing remaining inputs
122
0
                    break;
123
0
                }
124
123k
                _dictionary.emplace(dict_item, value_code);
125
123k
                if (src->empty()) {
126
311
                    _has_empty = true;
127
311
                    _empty_code = value_code;
128
311
                }
129
123k
            }
130
842k
            size_t add_count = 1;
131
842k
            RETURN_IF_ERROR(actual_builder->single_add(
132
842k
                    reinterpret_cast<const uint8_t*>(&value_code), &add_count));
133
842k
            if (add_count == 0) {
134
                // current data page is full, stop processing remaining inputs
135
0
                break;
136
0
            }
137
            // Track raw data size: the original string size
138
842k
            _raw_data_size += src->size;
139
842k
            num_added += 1;
140
842k
        }
141
16.4k
        *count = num_added;
142
16.4k
        return Status::OK();
143
203k
    } else {
144
203k
        DCHECK(_encoding_type == PLAIN_ENCODING || _encoding_type == PLAIN_ENCODING_V2);
145
203k
        RETURN_IF_ERROR(_data_page_builder->add(vals, count));
146
        // For plain encoding, track raw data size from the input
147
203k
        const Slice* src = reinterpret_cast<const Slice*>(vals);
148
443k
        for (size_t i = 0; i < *count; ++i) {
149
240k
            _raw_data_size += src[i].size;
150
240k
        }
151
203k
        return Status::OK();
152
203k
    }
153
219k
}
154
155
7.30k
Status BinaryDictPageBuilder::finish(OwnedSlice* slice) {
156
7.30k
    if (VLOG_DEBUG_IS_ON && _encoding_type == DICT_ENCODING) {
157
0
        VLOG_DEBUG << "dict page size:" << _dict_builder->size();
158
0
    }
159
160
7.30k
    DCHECK(!_finished);
161
7.30k
    _finished = true;
162
163
7.30k
    OwnedSlice data_slice;
164
7.30k
    RETURN_IF_ERROR(_data_page_builder->finish(&data_slice));
165
    // TODO(gaodayue) separate page header and content to avoid this copy
166
7.30k
    RETURN_IF_CATCH_EXCEPTION(
167
7.30k
            { _buffer.append(data_slice.slice().data, data_slice.slice().size); });
168
7.30k
    encode_fixed32_le(&_buffer[0], _encoding_type);
169
7.30k
    *slice = _buffer.build();
170
7.30k
    return Status::OK();
171
7.30k
}
172
173
11.1k
Status BinaryDictPageBuilder::reset() {
174
11.1k
    RETURN_IF_CATCH_EXCEPTION({
175
11.1k
        _finished = false;
176
11.1k
        _raw_data_size = 0;
177
11.1k
        _buffer.reserve(_options.data_page_size + BINARY_DICT_PAGE_HEADER_SIZE);
178
11.1k
        _buffer.resize(BINARY_DICT_PAGE_HEADER_SIZE);
179
180
11.1k
        if (_encoding_type == DICT_ENCODING && _dict_builder->is_page_full()) {
181
11.1k
            const EncodingInfo* encoding_info;
182
11.1k
            RETURN_IF_ERROR(EncodingInfo::get(FieldType::OLAP_FIELD_TYPE_VARCHAR,
183
11.1k
                                              _binary_plain_encoding_type, &encoding_info));
184
11.1k
            RETURN_IF_ERROR(encoding_info->create_page_builder(_options, _data_page_builder));
185
11.1k
            _encoding_type = _binary_plain_encoding_type;
186
11.1k
        } else {
187
11.1k
            RETURN_IF_ERROR(_data_page_builder->reset());
188
11.1k
        }
189
11.1k
    });
190
11.1k
    return Status::OK();
191
11.1k
}
192
193
3
size_t BinaryDictPageBuilder::count() const {
194
3
    return _data_page_builder->count();
195
3
}
196
197
3.47k
uint64_t BinaryDictPageBuilder::size() const {
198
3.47k
    return _arena.used_size() + _data_page_builder->size();
199
3.47k
}
200
201
3.78k
Status BinaryDictPageBuilder::get_dictionary_page(OwnedSlice* dictionary_page) {
202
3.78k
    return _dict_builder->finish(dictionary_page);
203
3.78k
}
204
205
3.78k
Status BinaryDictPageBuilder::get_dictionary_page_encoding(EncodingTypePB* encoding) const {
206
3.78k
    *encoding = _binary_plain_encoding_type;
207
3.78k
    return Status::OK();
208
3.78k
}
209
210
7.20k
uint64_t BinaryDictPageBuilder::get_raw_data_size() const {
211
7.20k
    return _raw_data_size;
212
7.20k
}
213
214
BinaryDictPageDecoder::BinaryDictPageDecoder(Slice data, const PageDecoderOptions& options)
215
4.68k
        : _data(data),
216
4.68k
          _options(options),
217
4.68k
          _data_page_decoder(nullptr),
218
4.68k
          _parsed(false),
219
4.68k
          _encoding_type(UNKNOWN_ENCODING) {}
220
221
4.68k
Status BinaryDictPageDecoder::init() {
222
4.68k
    CHECK(!_parsed);
223
4.68k
    if (_data.size < BINARY_DICT_PAGE_HEADER_SIZE) {
224
0
        return Status::Corruption("invalid data size:{}, header size:{}", _data.size,
225
0
                                  BINARY_DICT_PAGE_HEADER_SIZE);
226
0
    }
227
4.68k
    size_t type = decode_fixed32_le((const uint8_t*)&_data.data[0]);
228
4.68k
    _encoding_type = static_cast<EncodingTypePB>(type);
229
4.68k
    _data.remove_prefix(BINARY_DICT_PAGE_HEADER_SIZE);
230
4.68k
    if (_encoding_type == DICT_ENCODING) {
231
2.75k
        _data_page_decoder.reset(
232
2.75k
                _bit_shuffle_ptr =
233
2.75k
                        new BitShufflePageDecoder<FieldType::OLAP_FIELD_TYPE_INT>(_data, _options));
234
2.75k
    } else if (_encoding_type == PLAIN_ENCODING) {
235
1.78k
        _data_page_decoder.reset(
236
1.78k
                new BinaryPlainPageDecoder<FieldType::OLAP_FIELD_TYPE_VARCHAR>(_data, _options));
237
1.78k
    } else if (_encoding_type == PLAIN_ENCODING_V2) {
238
141
        _data_page_decoder.reset(
239
141
                new BinaryPlainPageV2Decoder<FieldType::OLAP_FIELD_TYPE_VARCHAR>(_data, _options));
240
141
    } else {
241
0
        LOG(WARNING) << "invalid encoding type:" << _encoding_type;
242
0
        return Status::Corruption("invalid encoding type:{}", _encoding_type);
243
0
    }
244
245
4.68k
    RETURN_IF_ERROR(_data_page_decoder->init());
246
4.68k
    _parsed = true;
247
4.68k
    return Status::OK();
248
4.68k
}
249
250
4.68k
BinaryDictPageDecoder::~BinaryDictPageDecoder() {}
251
252
521
Status BinaryDictPageDecoder::seek_to_position_in_page(size_t pos) {
253
521
    return _data_page_decoder->seek_to_position_in_page(pos);
254
521
}
255
256
18.3k
bool BinaryDictPageDecoder::is_dict_encoding() const {
257
18.3k
    return _encoding_type == DICT_ENCODING;
258
18.3k
}
259
260
2.75k
void BinaryDictPageDecoder::set_dict_decoder(uint32_t num_dict_items, StringRef* dict_word_info) {
261
2.75k
    _num_dict_items = num_dict_items;
262
2.75k
    _dict_word_info = dict_word_info;
263
2.75k
};
264
265
8.17k
Status BinaryDictPageDecoder::next_batch(size_t* n, MutableColumnPtr& dst) {
266
8.17k
    if (!is_dict_encoding()) {
267
1.42k
        dst = dst->convert_to_predicate_column_if_dictionary();
268
1.42k
        return _data_page_decoder->next_batch(n, dst);
269
1.42k
    }
270
    // dictionary encoding
271
8.17k
    DCHECK(_parsed);
272
6.74k
    DCHECK(_dict_word_info != nullptr) << "_dict_word_info is nullptr";
273
274
6.74k
    if (*n == 0 || _bit_shuffle_ptr->_cur_index >= _bit_shuffle_ptr->_num_elements) [[unlikely]] {
275
0
        *n = 0;
276
0
        return Status::OK();
277
0
    }
278
279
6.74k
    size_t max_fetch = std::min(*n, static_cast<size_t>(_bit_shuffle_ptr->_num_elements -
280
6.74k
                                                        _bit_shuffle_ptr->_cur_index));
281
6.74k
    *n = max_fetch;
282
283
6.74k
    if (_options.only_read_offsets) {
284
        // OFFSET_ONLY mode: resolve dict codes to get real string lengths
285
        // without copying actual char data. This allows length() to work.
286
        // ColumnDictI32 does not implement insert_offsets_from_lengths, so convert
287
        // it to a predicate column (ColumnString) first. This is a no-op for
288
        // non-dictionary columns and for ColumnNullable it converts the nested column.
289
0
        dst = dst->convert_to_predicate_column_if_dictionary();
290
0
        const auto* data_array = reinterpret_cast<const int32_t*>(_bit_shuffle_ptr->get_data(0));
291
0
        size_t start_index = _bit_shuffle_ptr->_cur_index;
292
        // Reuse _buffer (int32_t vector) to store uint32_t lengths.
293
        // int32_t and uint32_t have the same size/alignment, and string
294
        // lengths are always non-negative, so the bit patterns are identical.
295
0
        _buffer.resize(max_fetch);
296
0
        for (size_t i = 0; i < max_fetch; ++i) {
297
0
            int32_t codeword = data_array[start_index + i];
298
0
            _buffer[i] = static_cast<int32_t>(_dict_word_info[codeword].size);
299
0
        }
300
0
        dst->insert_offsets_from_lengths(reinterpret_cast<const uint32_t*>(_buffer.data()),
301
0
                                         max_fetch);
302
6.74k
    } else {
303
6.74k
        const auto* data_array = reinterpret_cast<const int32_t*>(_bit_shuffle_ptr->get_data(0));
304
6.74k
        size_t start_index = _bit_shuffle_ptr->_cur_index;
305
306
6.74k
        dst->insert_many_dict_data(data_array, start_index, _dict_word_info, max_fetch,
307
6.74k
                                   _num_dict_items);
308
6.74k
    }
309
310
6.74k
    _bit_shuffle_ptr->_cur_index += max_fetch;
311
312
6.74k
    return Status::OK();
313
6.74k
}
314
315
Status BinaryDictPageDecoder::read_by_rowids(const rowid_t* rowids, ordinal_t page_first_ordinal,
316
1.13k
                                             size_t* n, MutableColumnPtr& dst) {
317
1.13k
    if (!is_dict_encoding()) {
318
507
        dst = dst->convert_to_predicate_column_if_dictionary();
319
507
        return _data_page_decoder->read_by_rowids(rowids, page_first_ordinal, n, dst);
320
507
    }
321
1.13k
    DCHECK(_parsed);
322
626
    DCHECK(_dict_word_info != nullptr) << "_dict_word_info is nullptr";
323
324
626
    if (*n == 0) [[unlikely]] {
325
0
        *n = 0;
326
0
        return Status::OK();
327
0
    }
328
329
626
    auto total = *n;
330
331
626
    if (_options.only_read_offsets) {
332
        // OFFSET_ONLY mode: resolve dict codes to get real string lengths
333
        // without copying actual char data. This allows length() to work correctly.
334
        // ColumnDictI32 does not implement insert_offsets_from_lengths, so convert
335
        // it to a predicate column (ColumnString) first.
336
0
        dst = dst->convert_to_predicate_column_if_dictionary();
337
0
        const auto* data_array = reinterpret_cast<const int32_t*>(_bit_shuffle_ptr->get_data(0));
338
0
        size_t read_count = 0;
339
0
        _buffer.resize(total);
340
0
        for (size_t i = 0; i < total; ++i) {
341
0
            ordinal_t ord = rowids[i] - page_first_ordinal;
342
0
            if (ord >= _bit_shuffle_ptr->_num_elements) [[unlikely]] {
343
0
                break;
344
0
            }
345
0
            int32_t codeword = data_array[ord];
346
0
            _buffer[read_count] = static_cast<int32_t>(_dict_word_info[codeword].size);
347
0
            read_count++;
348
0
        }
349
0
        if (read_count > 0) {
350
0
            dst->insert_offsets_from_lengths(reinterpret_cast<const uint32_t*>(_buffer.data()),
351
0
                                             read_count);
352
0
        }
353
0
        *n = read_count;
354
0
        return Status::OK();
355
0
    }
356
357
626
    const auto* data_array = reinterpret_cast<const int32_t*>(_bit_shuffle_ptr->get_data(0));
358
626
    size_t read_count = 0;
359
626
    _buffer.resize(total);
360
25.6k
    for (size_t i = 0; i < total; ++i) {
361
25.0k
        ordinal_t ord = rowids[i] - page_first_ordinal;
362
25.0k
        if (ord >= _bit_shuffle_ptr->_num_elements) [[unlikely]] {
363
0
            break;
364
0
        }
365
366
25.0k
        _buffer[read_count++] = data_array[ord];
367
25.0k
    }
368
369
626
    if (LIKELY(read_count > 0)) {
370
626
        dst->insert_many_dict_data(_buffer.data(), 0, _dict_word_info, read_count, _num_dict_items);
371
626
    }
372
626
    *n = read_count;
373
626
    return Status::OK();
374
626
}
375
376
} // namespace segment_v2
377
} // namespace doris