Coverage Report

Created: 2024-11-20 18:15

/root/doris/be/src/olap/schema_change.h
Line
Count
Source (jump to first uncovered line)
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 <butil/macros.h>
21
#include <fmt/format.h>
22
#include <gen_cpp/Descriptors_types.h>
23
#include <glog/logging.h>
24
#include <stddef.h>
25
#include <stdint.h>
26
27
#include <memory>
28
#include <ostream>
29
#include <set>
30
#include <shared_mutex>
31
#include <string>
32
#include <unordered_map>
33
#include <unordered_set>
34
#include <utility>
35
#include <vector>
36
37
#include "common/config.h"
38
#include "common/object_pool.h"
39
#include "common/status.h"
40
#include "olap/column_mapping.h"
41
#include "olap/olap_common.h"
42
#include "olap/rowset/pending_rowset_helper.h"
43
#include "olap/rowset/rowset.h"
44
#include "olap/rowset/rowset_reader.h"
45
#include "olap/rowset/rowset_writer.h"
46
#include "olap/rowset/segment_v2/inverted_index_writer.h"
47
#include "olap/storage_engine.h"
48
#include "olap/tablet.h"
49
#include "olap/tablet_fwd.h"
50
#include "olap/tablet_schema.h"
51
#include "runtime/descriptors.h"
52
#include "runtime/memory/mem_tracker.h"
53
#include "vec/data_types/data_type.h"
54
55
namespace doris {
56
class DeleteHandler;
57
class Field;
58
class TAlterInvertedIndexReq;
59
class TAlterTabletReqV2;
60
class TExpr;
61
enum AlterTabletType : int;
62
enum RowsetTypePB : int;
63
enum SegmentsOverlapPB : int;
64
65
namespace vectorized {
66
class Block;
67
class OlapBlockDataConvertor;
68
} // namespace vectorized
69
70
class BlockChanger {
71
public:
72
    BlockChanger(TabletSchemaSPtr tablet_schema, DescriptorTbl desc_tbl);
73
74
    ~BlockChanger();
75
76
    ColumnMapping* get_mutable_column_mapping(size_t column_index);
77
78
    Status change_block(vectorized::Block* ref_block, vectorized::Block* new_block) const;
79
80
0
    void set_where_expr(const std::shared_ptr<TExpr>& where_expr) { _where_expr = where_expr; }
81
82
0
    void set_type(AlterTabletType type) { _type = type; }
83
84
0
    void set_compatible_version(int32_t version) noexcept { _fe_compatible_version = version; }
85
86
0
    bool has_where() const { return _where_expr != nullptr; }
87
88
private:
89
    static Status _check_cast_valid(vectorized::ColumnPtr ref_column,
90
                                    vectorized::ColumnPtr new_column);
91
92
    // @brief column-mapping specification of new schema
93
    SchemaMapping _schema_mapping;
94
95
    DescriptorTbl _desc_tbl;
96
97
    std::shared_ptr<TExpr> _where_expr;
98
99
    AlterTabletType _type;
100
101
    int32_t _fe_compatible_version = -1;
102
};
103
104
class SchemaChange {
105
public:
106
0
    SchemaChange() = default;
107
0
    virtual ~SchemaChange() = default;
108
109
    virtual Status process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
110
                           BaseTabletSPtr new_tablet, BaseTabletSPtr base_tablet,
111
                           TabletSchemaSPtr base_tablet_schema,
112
0
                           TabletSchemaSPtr new_tablet_schema) {
113
0
        if (rowset_reader->rowset()->empty() || rowset_reader->rowset()->num_rows() == 0) {
114
0
            RETURN_IF_ERROR(rowset_writer->flush());
115
0
            return Status::OK();
116
0
        }
117
118
0
        _filtered_rows = 0;
119
0
        _merged_rows = 0;
120
0
        RETURN_IF_ERROR_OR_CATCH_EXCEPTION(_inner_process(rowset_reader, rowset_writer, new_tablet,
121
0
                                                          base_tablet_schema, new_tablet_schema));
122
123
        // Check row num changes
124
0
        if (!_check_row_nums(rowset_reader, *rowset_writer)) {
125
0
            return Status::Error<ErrorCode::ALTER_STATUS_ERR>("SchemaChange check row nums failed");
126
0
        }
127
128
0
        LOG(INFO) << "all row nums. source_rows=" << rowset_reader->rowset()->num_rows()
129
0
                  << ", source_filtered_rows=" << rowset_reader->filtered_rows()
130
0
                  << ", source_merged_rows=" << rowset_reader->merged_rows()
131
0
                  << ", merged_rows=" << merged_rows() << ", filtered_rows=" << filtered_rows()
132
0
                  << ", new_index_rows=" << rowset_writer->num_rows()
133
0
                  << ", writer_filtered_rows=" << rowset_writer->num_rows_filtered();
134
0
        return Status::OK();
135
0
    }
136
137
0
    uint64_t filtered_rows() const { return _filtered_rows; }
138
139
0
    uint64_t merged_rows() const { return _merged_rows; }
140
141
protected:
142
0
    void _add_filtered_rows(uint64_t filtered_rows) { _filtered_rows += filtered_rows; }
143
144
0
    void _add_merged_rows(uint64_t merged_rows) { _merged_rows += merged_rows; }
145
146
    virtual Status _inner_process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
147
                                  BaseTabletSPtr new_tablet, TabletSchemaSPtr base_tablet_schema,
148
0
                                  TabletSchemaSPtr new_tablet_schema) {
149
0
        return Status::NotSupported("inner process unsupported.");
150
0
    }
151
152
0
    virtual bool _check_row_nums(RowsetReaderSharedPtr reader, const RowsetWriter& writer) const {
153
0
        if (reader->rowset()->num_rows() - reader->filtered_rows() - reader->merged_rows() !=
154
0
            writer.num_rows() + writer.num_rows_filtered() + _merged_rows + _filtered_rows) {
155
0
            LOG(WARNING) << "fail to check row num! "
156
0
                         << "source_rows=" << reader->rowset()->num_rows()
157
0
                         << ", source_filtered_rows=" << reader->filtered_rows()
158
0
                         << ", source_merged_rows=" << reader->merged_rows()
159
0
                         << ", written_rows=" << writer.num_rows()
160
0
                         << ", writer_filtered_rows=" << writer.num_rows_filtered()
161
0
                         << ", merged_rows=" << merged_rows()
162
0
                         << ", filtered_rows=" << filtered_rows();
163
0
            if (!config::ignore_schema_change_check) {
164
0
                return false;
165
0
            }
166
0
        }
167
0
        return true;
168
0
    }
169
170
private:
171
    uint64_t _filtered_rows {};
172
    uint64_t _merged_rows {};
173
};
174
175
class LinkedSchemaChange : public SchemaChange {
176
public:
177
0
    LinkedSchemaChange() = default;
178
0
    ~LinkedSchemaChange() override = default;
179
180
    Status process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
181
                   BaseTabletSPtr new_tablet, BaseTabletSPtr base_tablet,
182
                   TabletSchemaSPtr base_tablet_schema,
183
                   TabletSchemaSPtr new_tablet_schema) override;
184
185
private:
186
    DISALLOW_COPY_AND_ASSIGN(LinkedSchemaChange);
187
};
188
189
class VSchemaChangeDirectly : public SchemaChange {
190
public:
191
0
    VSchemaChangeDirectly(const BlockChanger& changer) : _changer(changer) {}
192
193
private:
194
    Status _inner_process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
195
                          BaseTabletSPtr new_tablet, TabletSchemaSPtr base_tablet_schema,
196
                          TabletSchemaSPtr new_tablet_schema) override;
197
198
0
    bool _check_row_nums(RowsetReaderSharedPtr reader, const RowsetWriter& writer) const override {
199
0
        return _changer.has_where() || SchemaChange::_check_row_nums(reader, writer);
200
0
    }
201
202
    const BlockChanger& _changer;
203
};
204
205
class VBaseSchemaChangeWithSorting : public SchemaChange {
206
public:
207
    VBaseSchemaChangeWithSorting(const BlockChanger& changer, size_t memory_limitation);
208
0
    ~VBaseSchemaChangeWithSorting() override = default;
209
210
    Status _inner_process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
211
                          BaseTabletSPtr new_tablet, TabletSchemaSPtr base_tablet_schema,
212
                          TabletSchemaSPtr new_tablet_schema) override;
213
214
    virtual Result<RowsetSharedPtr> _internal_sorting(
215
            const std::vector<std::unique_ptr<vectorized::Block>>& blocks,
216
            const Version& temp_delta_versions, int64_t newest_write_timestamp,
217
            BaseTabletSPtr new_tablet, RowsetTypePB new_rowset_type,
218
            SegmentsOverlapPB segments_overlap, TabletSchemaSPtr new_tablet_schema);
219
220
    Status _external_sorting(std::vector<RowsetSharedPtr>& src_rowsets, RowsetWriter* rowset_writer,
221
                             BaseTabletSPtr new_tablet, TabletSchemaSPtr new_tablet_schema);
222
223
protected:
224
    // for external sorting
225
    // src_rowsets to store the rowset generated by internal sorting
226
    std::vector<RowsetSharedPtr> _src_rowsets;
227
228
private:
229
0
    bool _check_row_nums(RowsetReaderSharedPtr reader, const RowsetWriter& writer) const override {
230
0
        return _changer.has_where() || SchemaChange::_check_row_nums(reader, writer);
231
0
    }
232
233
    const BlockChanger& _changer;
234
    size_t _memory_limitation;
235
    Version _temp_delta_versions;
236
    std::unique_ptr<MemTracker> _mem_tracker;
237
};
238
239
// @breif schema change with sorting
240
// Mixin for local StorageEngine
241
class VLocalSchemaChangeWithSorting final : public VBaseSchemaChangeWithSorting {
242
public:
243
    VLocalSchemaChangeWithSorting(const BlockChanger& changer, size_t memory_limitation,
244
                                  StorageEngine& local_storage_engine)
245
            : VBaseSchemaChangeWithSorting(changer, memory_limitation),
246
0
              _local_storage_engine(local_storage_engine) {}
247
0
    ~VLocalSchemaChangeWithSorting() override = default;
248
249
    Status _inner_process(RowsetReaderSharedPtr rowset_reader, RowsetWriter* rowset_writer,
250
                          BaseTabletSPtr new_tablet, TabletSchemaSPtr base_tablet_schema,
251
                          TabletSchemaSPtr new_tablet_schema) override;
252
253
    Result<RowsetSharedPtr> _internal_sorting(
254
            const std::vector<std::unique_ptr<vectorized::Block>>& blocks,
255
            const Version& temp_delta_versions, int64_t newest_write_timestamp,
256
            BaseTabletSPtr new_tablet, RowsetTypePB new_rowset_type,
257
            SegmentsOverlapPB segments_overlap, TabletSchemaSPtr new_tablet_schema) override;
258
259
private:
260
    StorageEngine& _local_storage_engine;
261
    std::vector<PendingRowsetGuard> _pending_rs_guards;
262
};
263
264
struct AlterMaterializedViewParam {
265
    std::string column_name;
266
    std::string origin_column_name;
267
    std::shared_ptr<TExpr> expr;
268
};
269
270
struct SchemaChangeParams {
271
    AlterTabletType alter_tablet_type;
272
    bool enable_unique_key_merge_on_write = false;
273
    std::vector<RowsetReaderSharedPtr> ref_rowset_readers;
274
    DeleteHandler* delete_handler = nullptr;
275
    std::unordered_map<std::string, AlterMaterializedViewParam> materialized_params_map;
276
    DescriptorTbl* desc_tbl = nullptr;
277
    ObjectPool pool;
278
    int32_t be_exec_version;
279
    std::string vault_id;
280
};
281
282
class SchemaChangeJob {
283
public:
284
    SchemaChangeJob(StorageEngine& local_storage_engine, const TAlterTabletReqV2& request,
285
                    const std::string& job_id);
286
    Status process_alter_tablet(const TAlterTabletReqV2& request);
287
288
    bool tablet_in_converting(int64_t tablet_id);
289
290
    static Status parse_request(const SchemaChangeParams& sc_params,
291
                                TabletSchema* base_tablet_schema, TabletSchema* new_tablet_schema,
292
                                BlockChanger* changer, bool* sc_sorting, bool* sc_directly);
293
294
private:
295
    std::unique_ptr<SchemaChange> _get_sc_procedure(const BlockChanger& changer, bool sc_sorting,
296
0
                                                    bool sc_directly, int64_t mem_limit) {
297
0
        if (sc_sorting) {
298
0
            return std::make_unique<VLocalSchemaChangeWithSorting>(changer, mem_limit,
299
0
                                                                   _local_storage_engine);
300
0
        }
301
302
0
        if (sc_directly) {
303
0
            return std::make_unique<VSchemaChangeDirectly>(changer);
304
0
        }
305
306
0
        return std::make_unique<LinkedSchemaChange>();
307
0
    }
308
309
    Status _get_versions_to_be_changed(std::vector<Version>* versions_to_be_changed,
310
                                       RowsetSharedPtr* max_rowset);
311
312
    Status _do_process_alter_tablet(const TAlterTabletReqV2& request);
313
314
    Status _validate_alter_result(const TAlterTabletReqV2& request);
315
316
    Status _convert_historical_rowsets(const SchemaChangeParams& sc_params,
317
                                       int64_t* real_alter_version);
318
319
    // Initialization Settings for creating a default value
320
    static Status _init_column_mapping(ColumnMapping* column_mapping,
321
                                       const TabletColumn& column_schema, const std::string& value);
322
323
    Status _calc_delete_bitmap_for_mow_table(int64_t alter_version);
324
325
    StorageEngine& _local_storage_engine;
326
    TabletSharedPtr _base_tablet;
327
    TabletSharedPtr _new_tablet;
328
    TabletSchemaSPtr _base_tablet_schema;
329
    TabletSchemaSPtr _new_tablet_schema;
330
    std::shared_mutex _mutex;
331
    std::unordered_set<int64_t> _tablet_ids_in_converting;
332
    std::set<std::string> _supported_functions;
333
    std::string _job_id;
334
};
335
} // namespace doris