Coverage Report

Created: 2026-08-06 16:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/vertical_segment_writer.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
18
#pragma once
19
20
#include <gen_cpp/olap_file.pb.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
23
#include <cstddef>
24
#include <cstdint>
25
#include <functional>
26
#include <map>
27
#include <memory> // unique_ptr
28
#include <string>
29
#include <unordered_set>
30
#include <vector>
31
32
#include "common/status.h" // Status
33
#include "storage/index/index_file_writer.h"
34
#include "storage/key/row_key_encoder.h"
35
#include "storage/olap_define.h"
36
#include "storage/partial_update_info.h"
37
#include "storage/segment/column_writer.h"
38
#include "storage/segment/segment_index_file_cache_loader.h"
39
#include "storage/tablet/tablet.h"
40
#include "storage/tablet/tablet_schema.h"
41
#include "util/faststring.h"
42
#include "util/slice.h"
43
44
namespace doris {
45
class Block;
46
class IOlapColumnDataAccessor;
47
class OlapBlockDataConvertor;
48
49
class DataDir;
50
class MemTracker;
51
class ShortKeyIndexBuilder;
52
class PrimaryKeyIndexBuilder;
53
class KeyCoder;
54
struct RowsetWriterContext;
55
56
namespace io {
57
class FileWriter;
58
class FileSystem;
59
} // namespace io
60
namespace segment_v2 {
61
class IndexFileWriter;
62
class MowKeyProbe;
63
64
struct VerticalSegmentWriterOptions {
65
    uint32_t num_rows_per_block = 1024;
66
    bool enable_unique_key_merge_on_write = false;
67
    CompressionTypePB compression_type = UNKNOWN_COMPRESSION;
68
69
    RowsetWriterContext* rowset_ctx = nullptr;
70
    DataWriteType write_type = DataWriteType::TYPE_DEFAULT;
71
    std::shared_ptr<MowContext> mow_ctx;
72
};
73
74
struct RowsInBlock {
75
    const Block* block;
76
    size_t row_pos;
77
    size_t num_rows;
78
};
79
80
class VerticalSegmentWriter {
81
public:
82
    explicit VerticalSegmentWriter(io::FileWriter* file_writer, uint32_t segment_id,
83
                                   TabletSchemaSPtr tablet_schema, BaseTabletSPtr tablet,
84
                                   DataDir* data_dir, const VerticalSegmentWriterOptions& opts,
85
                                   IndexFileWriter* index_file_writer);
86
    ~VerticalSegmentWriter();
87
88
    VerticalSegmentWriter(const VerticalSegmentWriter&) = delete;
89
    const VerticalSegmentWriter& operator=(const VerticalSegmentWriter&) = delete;
90
91
    Status init();
92
93
    // Add one block to batch, memory is owned by the caller.
94
    // The batched blocks will be flushed in write_batch.
95
    // Once write_batch is called, no more blocks shoud be added.
96
    Status batch_block(const Block* block, size_t row_pos, size_t num_rows);
97
    Status write_batch();
98
99
0
    [[nodiscard]] std::string data_dir_path() const {
100
0
        return _data_dir == nullptr ? "" : _data_dir->path();
101
0
    }
102
103
136
    [[nodiscard]] uint32_t num_rows_written() const { return _num_rows_written; }
104
105
    // for partial update
106
136
    [[nodiscard]] int64_t num_rows_updated() const { return _num_rows_updated; }
107
136
    [[nodiscard]] int64_t num_rows_deleted() const { return _num_rows_deleted; }
108
136
    [[nodiscard]] int64_t num_rows_new_added() const { return _num_rows_new_added; }
109
136
    [[nodiscard]] int64_t num_rows_filtered() const { return _num_rows_filtered; }
110
0
    [[nodiscard]] uint32_t row_count() const { return _row_count; }
111
136
    [[nodiscard]] uint32_t segment_id() const { return _segment_id; }
112
113
    Status finalize(uint64_t* segment_file_size, uint64_t* index_size,
114
                    SegmentIndexFileCacheInfo* index_file_cache_info = nullptr);
115
116
    Status finalize_columns_index(uint64_t* index_size);
117
    Status finalize_footer(uint64_t* segment_file_size,
118
                           SegmentIndexFileCacheInfo* index_file_cache_info = nullptr);
119
120
    Slice min_encoded_key();
121
    Slice max_encoded_key();
122
123
    void clear();
124
125
136
    Status close_inverted_index(int64_t* inverted_index_file_size) {
126
        // no inverted index
127
136
        if (_index_file_writer == nullptr) {
128
114
            *inverted_index_file_size = 0;
129
114
            return Status::OK();
130
114
        }
131
22
        RETURN_IF_ERROR(_index_file_writer->begin_close());
132
22
        *inverted_index_file_size = _index_file_writer->get_index_file_total_size();
133
22
        return Status::OK();
134
22
    }
135
136
private:
137
    void _init_column_meta(ColumnMetaPB* meta, uint32_t column_id, const TabletColumn& column,
138
                           const ColumnWriterOptions& opts);
139
    Status _create_column_writer(uint32_t cid, const TabletColumn& column,
140
                                 const TabletSchemaSPtr& schema);
141
    uint64_t _estimated_remaining_size();
142
    Status _write_ordinal_index();
143
    Status _write_zone_map();
144
    Status _write_inverted_index();
145
    Status _write_ann_index();
146
    Status _write_bloom_filter_index();
147
    Status _write_short_key_index();
148
    Status _write_primary_key_index();
149
    Status _write_footer();
150
    Status _write_raw_data(const std::vector<Slice>& slices);
151
    void _set_min_max_key(const Slice& key);
152
    void _set_min_key(const Slice& key);
153
    void _set_max_key(const Slice& key);
154
    Status _append_row_store_column(const Block& block, size_t row_pos, size_t num_rows,
155
                                    uint32_t cid);
156
    // Thin wrapper over MowKeyProbe that translates a ProbeOutcome back into the out-parameters the
157
    // partial update fill loops use. `found_cb` receives the rowset that holds `loc`: the fixed
158
    // path pins it in its HistoricalRowFetcher, the flexible path in `_rsid_to_rowset`, which its
159
    // fill still reads from.
160
    Status _probe_key_for_mow(
161
            const MowKeyProbe& probe, std::string key, std::size_t segment_pos,
162
            bool have_input_seq_column, bool have_delete_sign,
163
            const std::vector<RowsetSharedPtr>& specified_rowsets,
164
            std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches,
165
            bool& has_default_or_nullable, std::vector<bool>& use_default_or_null_flag,
166
            const std::function<void(const RowLocation& loc, const RowsetSharedPtr& rowset)>&
167
                    found_cb,
168
            const std::function<Status()>& not_found_cb, PartialUpdateStats& stats);
169
    Status _partial_update_preconditions_check(size_t row_pos, bool is_flexible_update);
170
    Status _append_block_with_partial_content(RowsInBlock& data, Block& full_block);
171
    Status _append_block_with_flexible_partial_content(RowsInBlock& data, Block& full_block);
172
    Status _generate_encoded_default_seq_value(const TabletSchema& tablet_schema,
173
                                               const PartialUpdateInfo& info,
174
                                               std::string* encoded_value);
175
    Status _generate_flexible_read_plan(
176
            FlexibleReadPlan& read_plan, RowsInBlock& data, size_t segment_start_pos,
177
            bool schema_has_sequence_col, int32_t seq_map_col_unique_id,
178
            std::vector<BitmapValue>* skip_bitmaps,
179
            const std::vector<IOlapColumnDataAccessor*>& key_columns,
180
            IOlapColumnDataAccessor* seq_column, const signed char* delete_signs,
181
            const std::vector<RowsetSharedPtr>& specified_rowsets,
182
            std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches,
183
            bool& has_default_or_nullable, std::vector<bool>& use_default_or_null_flag,
184
            PartialUpdateStats& stats);
185
    Status _generate_key_index(RowsInBlock& data,
186
                               std::vector<IOlapColumnDataAccessor*>& key_columns,
187
                               IOlapColumnDataAccessor* seq_column,
188
                               std::map<uint32_t, IOlapColumnDataAccessor*>& cid_to_column);
189
    Status _generate_primary_key_index(
190
            const std::vector<IOlapColumnDataAccessor*>& primary_key_columns,
191
            IOlapColumnDataAccessor* seq_column, size_t num_rows, bool need_sort);
192
    Status _generate_short_key_index(std::vector<IOlapColumnDataAccessor*>& key_columns,
193
                                     size_t num_rows, const std::vector<size_t>& short_key_pos);
194
    Status _check_column_writer_disk_capacity(size_t cid);
195
    Status _finalize_column_writer_and_update_meta(size_t cid);
196
197
2.14k
    bool _is_mow() {
198
2.14k
        return _tablet_schema->keys_type() == UNIQUE_KEYS && _opts.enable_unique_key_merge_on_write;
199
2.14k
    }
200
1.58k
    bool _is_mow_with_cluster_key() {
201
1.58k
        return _is_mow() && !_tablet_schema->cluster_key_uids().empty();
202
1.58k
    }
203
204
private:
205
    friend class ::doris::BlockAggregator;
206
    uint32_t _segment_id;
207
    TabletSchemaSPtr _tablet_schema;
208
    BaseTabletSPtr _tablet;
209
    DataDir* _data_dir = nullptr;
210
    VerticalSegmentWriterOptions _opts;
211
212
    // Not owned. owned by RowsetWriter
213
    io::FileWriter* _file_writer = nullptr;
214
    // Not owned. owned by RowsetWriter or SegmentFlusher
215
    IndexFileWriter* _index_file_writer = nullptr;
216
217
    SegmentFooterPB _footer;
218
    SegmentIndexFileCacheInfo _index_file_cache_info;
219
    size_t _num_short_key_columns;
220
221
    std::unique_ptr<ShortKeyIndexBuilder> _short_key_index_builder;
222
    std::unique_ptr<PrimaryKeyIndexBuilder> _primary_key_index_builder;
223
    std::vector<std::unique_ptr<ColumnWriter>> _column_writers;
224
    std::unique_ptr<MemTracker> _mem_tracker;
225
226
    std::unique_ptr<OlapBlockDataConvertor> _olap_data_convertor;
227
    // used for building short key index or primary key index during vectorized write.
228
    // NOTE: must stay declared after _tablet_schema and _opts, the constructor
229
    // init list reads both through _is_mow().
230
    RowKeyEncoder _key_encoder;
231
    size_t _short_key_row_pos = 0;
232
233
    // _num_rows_written means row count already written in this current column group
234
    uint32_t _num_rows_written = 0;
235
236
    /** for partial update stats **/
237
    int64_t _num_rows_updated = 0;
238
    int64_t _num_rows_new_added = 0;
239
    int64_t _num_rows_deleted = 0;
240
    // number of rows filtered in strict mode partial update
241
    int64_t _num_rows_filtered = 0;
242
243
    // _row_count means total row count of this segment
244
    // In vertical compaction row count is recorded when key columns group finish
245
    //  and _num_rows_written will be updated in value column group
246
    uint32_t _row_count = 0;
247
248
    bool _is_first_row = true;
249
    faststring _min_key;
250
    faststring _max_key;
251
252
    std::shared_ptr<MowContext> _mow_context;
253
    // group every rowset-segment row id to speed up reader
254
    std::map<RowsetId, RowsetSharedPtr> _rsid_to_rowset;
255
256
    std::vector<RowsInBlock> _batched_blocks;
257
258
    BlockAggregator _block_aggregator;
259
};
260
261
} // namespace segment_v2
262
} // namespace doris