Coverage Report

Created: 2026-07-15 14:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/primary_key_index.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/index/primary_key_index.h"
19
20
#include <butil/time.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
23
#include <utility>
24
25
#include "common/compiler_util.h" // IWYU pragma: keep
26
#include "common/config.h"
27
#include "io/fs/file_writer.h"
28
#include "storage/index/bloom_filter/bloom_filter_index_reader.h"
29
#include "storage/index/bloom_filter/bloom_filter_index_writer.h"
30
#include "storage/olap_common.h"
31
#include "storage/segment/encoding_info.h"
32
#include "storage/types.h"
33
34
namespace doris {
35
36
namespace {
37
67
io::IOContext create_index_io_context(const io::IOContext* source, OlapReaderStatistics* stats) {
38
67
    io::IOContext io_ctx;
39
67
    if (source != nullptr) {
40
5
        io_ctx = *source;
41
5
    }
42
67
    io_ctx.is_index_data = true;
43
67
    io_ctx.is_inverted_index = false;
44
67
    io_ctx.file_cache_stats = stats ? &stats->file_cache_stats : nullptr;
45
67
    return io_ctx;
46
67
}
47
} // namespace
48
49
static bvar::Adder<size_t> g_primary_key_index_memory_bytes("doris_primary_key_index_memory_bytes");
50
51
82
Status PrimaryKeyIndexBuilder::init() {
52
    // TODO(liaoxin) using the column type directly if there's only one column in unique key columns
53
82
    constexpr FieldType type = FieldType::OLAP_FIELD_TYPE_VARCHAR;
54
82
    segment_v2::IndexedColumnWriterOptions options;
55
82
    options.write_ordinal_index = true;
56
82
    options.write_value_index = true;
57
82
    options.data_page_size = config::primary_key_data_page_size;
58
82
    options.encoding = segment_v2::EncodingInfo::get_index_column_encoding(type);
59
82
    options.compression = segment_v2::ZSTD;
60
82
    _primary_key_index_builder.reset(
61
82
            new segment_v2::IndexedColumnWriter(options, type, _file_writer));
62
82
    RETURN_IF_ERROR(_primary_key_index_builder->init());
63
64
82
    auto opt = segment_v2::BloomFilterOptions();
65
82
    opt.fpp = 0.01;
66
82
    RETURN_IF_ERROR(segment_v2::PrimaryKeyBloomFilterIndexWriterImpl::create(
67
82
            opt, type, &_bloom_filter_index_builder));
68
82
    return Status::OK();
69
82
}
70
71
134k
Status PrimaryKeyIndexBuilder::add_item(const Slice& key) {
72
134k
    RETURN_IF_ERROR(_primary_key_index_builder->add(&key));
73
134k
    Slice key_without_seq = Slice(key.get_data(), key.get_size() - _seq_col_length - _rowid_length);
74
134k
    RETURN_IF_ERROR(_bloom_filter_index_builder->add_values(&key_without_seq, 1));
75
    // the key is already sorted, so the first key is min_key, and
76
    // the last key is max_key.
77
134k
    if (UNLIKELY(_num_rows == 0)) {
78
82
        _min_key.append(key.get_data(), key.get_size());
79
82
    }
80
134k
    DCHECK(key.compare(_max_key) > 0)
81
0
            << "found duplicate key or key is not sorted! current key: " << key
82
0
            << ", last max key: " << _max_key;
83
134k
    _max_key.clear();
84
134k
    _max_key.append(key.get_data(), key.get_size());
85
134k
    _num_rows++;
86
134k
    _size += key.get_size();
87
134k
    return Status::OK();
88
134k
}
89
90
82
Status PrimaryKeyIndexBuilder::finalize(segment_v2::PrimaryKeyIndexMetaPB* meta) {
91
    // finish primary key index
92
82
    RETURN_IF_ERROR(_primary_key_index_builder->finish(meta->mutable_primary_key_index()));
93
82
    _disk_size += _primary_key_index_builder->disk_size();
94
95
    // set min_max key, the sequence column should be removed
96
82
    meta->set_min_key(min_key().to_string());
97
82
    meta->set_max_key(max_key().to_string());
98
99
    // finish bloom filter index
100
82
    RETURN_IF_ERROR(_bloom_filter_index_builder->flush());
101
82
    uint64_t start_size = _file_writer->bytes_appended();
102
82
    RETURN_IF_ERROR(
103
82
            _bloom_filter_index_builder->finish(_file_writer, meta->mutable_bloom_filter_index()));
104
82
    _disk_size += _file_writer->bytes_appended() - start_size;
105
82
    _primary_key_index_builder.reset(nullptr);
106
82
    _bloom_filter_index_builder.reset(nullptr);
107
82
    return Status::OK();
108
82
}
109
110
Status PrimaryKeyIndexReader::parse_index(io::FileReaderSPtr file_reader,
111
                                          const segment_v2::PrimaryKeyIndexMetaPB& meta,
112
                                          OlapReaderStatistics* pk_index_load_stats,
113
61
                                          const io::IOContext* source_io_ctx) {
114
    // parse primary key index
115
61
    _index_reader.reset(new segment_v2::IndexedColumnReader(file_reader, meta.primary_key_index()));
116
61
    _index_reader->set_is_pk_index(true);
117
61
    auto io_ctx = create_index_io_context(source_io_ctx, pk_index_load_stats);
118
61
    RETURN_IF_ERROR(_index_reader->load(!config::disable_pk_storage_page_cache, false,
119
61
                                        pk_index_load_stats, &io_ctx));
120
121
61
    _index_parsed = true;
122
61
    return Status::OK();
123
61
}
124
125
Status PrimaryKeyIndexReader::parse_bf(io::FileReaderSPtr file_reader,
126
                                       const segment_v2::PrimaryKeyIndexMetaPB& meta,
127
                                       OlapReaderStatistics* pk_index_load_stats,
128
6
                                       const io::IOContext* source_io_ctx) {
129
    // parse bloom filter
130
6
    segment_v2::ColumnIndexMetaPB column_index_meta = meta.bloom_filter_index();
131
6
    segment_v2::BloomFilterIndexReader bf_index_reader(std::move(file_reader),
132
6
                                                       column_index_meta.bloom_filter_index());
133
6
    auto io_ctx = create_index_io_context(source_io_ctx, pk_index_load_stats);
134
6
    RETURN_IF_ERROR(bf_index_reader.load(!config::disable_pk_storage_page_cache, false,
135
6
                                         pk_index_load_stats, &io_ctx));
136
6
    std::unique_ptr<segment_v2::BloomFilterIndexIterator> bf_iter;
137
6
    RETURN_IF_ERROR(bf_index_reader.new_iterator(&bf_iter, pk_index_load_stats, &io_ctx));
138
6
    RETURN_IF_ERROR(bf_iter->read_bloom_filter(0, &_bf));
139
6
    segment_v2::g_pk_total_bloom_filter_num << 1;
140
6
    segment_v2::g_pk_total_bloom_filter_total_bytes << _bf->size();
141
6
    segment_v2::g_pk_read_bloom_filter_num << 1;
142
6
    segment_v2::g_pk_read_bloom_filter_total_bytes << _bf->size();
143
6
    _bf_num += 1;
144
6
    _bf_bytes += _bf->size();
145
146
6
    _bf_parsed = true;
147
148
6
    return Status::OK();
149
6
}
150
151
} // namespace doris