Coverage Report

Created: 2026-08-14 23:14

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