Coverage Report

Created: 2025-12-31 20:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/merger.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 "olap/merger.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
#include <gen_cpp/types.pb.h>
22
#include <stddef.h>
23
#include <unistd.h>
24
25
#include <algorithm>
26
#include <iterator>
27
#include <memory>
28
#include <mutex>
29
#include <numeric>
30
#include <ostream>
31
#include <shared_mutex>
32
#include <string>
33
#include <utility>
34
#include <vector>
35
36
#include "cloud/config.h"
37
#include "common/config.h"
38
#include "common/logging.h"
39
#include "common/status.h"
40
#include "olap/base_tablet.h"
41
#include "olap/iterators.h"
42
#include "olap/olap_common.h"
43
#include "olap/olap_define.h"
44
#include "olap/rowid_conversion.h"
45
#include "olap/rowset/rowset.h"
46
#include "olap/rowset/rowset_meta.h"
47
#include "olap/rowset/rowset_writer.h"
48
#include "olap/rowset/segment_v2/segment_writer.h"
49
#include "olap/storage_engine.h"
50
#include "olap/tablet.h"
51
#include "olap/tablet_fwd.h"
52
#include "olap/tablet_meta.h"
53
#include "olap/tablet_reader.h"
54
#include "olap/utils.h"
55
#include "util/slice.h"
56
#include "vec/core/block.h"
57
#include "vec/olap/block_reader.h"
58
#include "vec/olap/vertical_block_reader.h"
59
#include "vec/olap/vertical_merge_iterator.h"
60
61
namespace doris {
62
#include "common/compile_check_begin.h"
63
Status Merger::vmerge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type,
64
                              const TabletSchema& cur_tablet_schema,
65
                              const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
66
62
                              RowsetWriter* dst_rowset_writer, Statistics* stats_output) {
67
62
    if (!cur_tablet_schema.cluster_key_uids().empty()) {
68
0
        return Status::InternalError(
69
0
                "mow table with cluster keys does not support non vertical compaction");
70
0
    }
71
62
    vectorized::BlockReader reader;
72
62
    TabletReader::ReaderParams reader_params;
73
62
    reader_params.tablet = tablet;
74
62
    reader_params.reader_type = reader_type;
75
76
62
    TabletReadSource read_source;
77
62
    read_source.rs_splits.reserve(src_rowset_readers.size());
78
158
    for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) {
79
158
        read_source.rs_splits.emplace_back(rs_reader);
80
158
    }
81
62
    read_source.fill_delete_predicates();
82
62
    reader_params.set_read_source(std::move(read_source));
83
84
62
    reader_params.version = dst_rowset_writer->version();
85
86
62
    TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>();
87
62
    merge_tablet_schema->copy_from(cur_tablet_schema);
88
89
    // Merge the columns in delete predicate that not in latest schema in to current tablet schema
90
62
    for (auto& del_pred_rs : reader_params.delete_predicates) {
91
24
        merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema());
92
24
    }
93
62
    reader_params.tablet_schema = merge_tablet_schema;
94
62
    if (!tablet->tablet_schema()->cluster_key_uids().empty()) {
95
0
        reader_params.delete_bitmap = tablet->tablet_meta()->delete_bitmap_ptr();
96
0
    }
97
98
62
    if (stats_output && stats_output->rowid_conversion) {
99
48
        reader_params.record_rowids = true;
100
48
        reader_params.rowid_conversion = stats_output->rowid_conversion;
101
48
        stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id());
102
48
    }
103
104
62
    reader_params.return_columns.resize(cur_tablet_schema.num_columns());
105
62
    std::iota(reader_params.return_columns.begin(), reader_params.return_columns.end(), 0);
106
62
    reader_params.origin_return_columns = &reader_params.return_columns;
107
62
    RETURN_IF_ERROR(reader.init(reader_params));
108
109
62
    vectorized::Block block = cur_tablet_schema.create_block(reader_params.return_columns);
110
62
    size_t output_rows = 0;
111
62
    bool eof = false;
112
653
    while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) {
113
591
        auto tablet_state = tablet->tablet_state();
114
591
        if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) {
115
0
            tablet->clear_cache();
116
0
            return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more",
117
0
                                                 tablet->tablet_id());
118
0
        }
119
120
        // Read one block from block reader
121
591
        RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof),
122
591
                                       "failed to read next block when merging rowsets of tablet " +
123
591
                                               std::to_string(tablet->tablet_id()));
124
591
        RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->add_block(&block),
125
591
                                       "failed to write block when merging rowsets of tablet " +
126
591
                                               std::to_string(tablet->tablet_id()));
127
128
591
        if (reader_params.record_rowids && block.rows() > 0) {
129
578
            std::vector<uint32_t> segment_num_rows;
130
578
            RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows));
131
578
            stats_output->rowid_conversion->add(reader.current_block_row_locations(),
132
578
                                                segment_num_rows);
133
578
        }
134
135
591
        output_rows += block.rows();
136
591
        block.clear_column_data();
137
591
    }
138
62
    if (ExecEnv::GetInstance()->storage_engine().stopped()) {
139
0
        return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped",
140
0
                                             tablet->tablet_id());
141
0
    }
142
143
62
    if (stats_output != nullptr) {
144
62
        stats_output->output_rows = output_rows;
145
62
        stats_output->merged_rows = reader.merged_rows();
146
62
        stats_output->filtered_rows = reader.filtered_rows();
147
62
        stats_output->bytes_read_from_local = reader.stats().file_cache_stats.bytes_read_from_local;
148
62
        stats_output->bytes_read_from_remote =
149
62
                reader.stats().file_cache_stats.bytes_read_from_remote;
150
62
        stats_output->cached_bytes_total = reader.stats().file_cache_stats.bytes_write_into_cache;
151
62
        if (config::is_cloud_mode()) {
152
0
            stats_output->cloud_local_read_time =
153
0
                    reader.stats().file_cache_stats.local_io_timer / 1000;
154
0
            stats_output->cloud_remote_read_time =
155
0
                    reader.stats().file_cache_stats.remote_io_timer / 1000;
156
0
        }
157
62
    }
158
159
62
    RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->flush(),
160
62
                                   "failed to flush rowset when merging rowsets of tablet " +
161
62
                                           std::to_string(tablet->tablet_id()));
162
163
62
    return Status::OK();
164
62
}
165
166
// split columns into several groups, make sure all keys in one group
167
// unique_key should consider sequence&delete column
168
void Merger::vertical_split_columns(const TabletSchema& tablet_schema,
169
                                    std::vector<std::vector<uint32_t>>* column_groups,
170
741
                                    std::vector<uint32_t>* key_group_cluster_key_idxes) {
171
741
    size_t num_key_cols = tablet_schema.num_key_columns();
172
741
    size_t total_cols = tablet_schema.num_columns();
173
741
    std::vector<uint32_t> key_columns;
174
2.44k
    for (auto i = 0; i < num_key_cols; ++i) {
175
1.70k
        key_columns.emplace_back(i);
176
1.70k
    }
177
    // in unique key, sequence & delete sign column should merge with key columns
178
741
    int32_t sequence_col_idx = -1;
179
741
    int32_t delete_sign_idx = -1;
180
    // in key column compaction, seq_col real index is _num_key_columns
181
    // and delete_sign column is _block->columns() - 1
182
741
    if (tablet_schema.keys_type() == KeysType::UNIQUE_KEYS) {
183
340
        if (tablet_schema.has_sequence_col()) {
184
17
            sequence_col_idx = tablet_schema.sequence_col_idx();
185
17
            key_columns.emplace_back(sequence_col_idx);
186
17
        }
187
340
        delete_sign_idx = tablet_schema.field_index(DELETE_SIGN);
188
340
        if (delete_sign_idx != -1) {
189
334
            key_columns.emplace_back(delete_sign_idx);
190
334
        }
191
340
        if (!tablet_schema.cluster_key_uids().empty()) {
192
41
            for (const auto& cid : tablet_schema.cluster_key_uids()) {
193
41
                auto idx = tablet_schema.field_index(cid);
194
41
                DCHECK(idx >= 0) << "could not find cluster key column with unique_id=" << cid
195
0
                                 << " in tablet schema, table_id=" << tablet_schema.table_id();
196
41
                if (idx >= num_key_cols) {
197
41
                    key_columns.emplace_back(idx);
198
41
                }
199
41
            }
200
            // tablet schema unique ids: [1, 2, 5, 3, 6, 4], [1 2] is key columns
201
            // cluster key unique ids: [3, 1, 4]
202
            // the key_columns should be [0, 1, 3, 5]
203
            // the key_group_cluster_key_idxes should be [2, 1, 3]
204
41
            for (const auto& cid : tablet_schema.cluster_key_uids()) {
205
41
                auto idx = tablet_schema.field_index(cid);
206
248
                for (auto i = 0; i < key_columns.size(); ++i) {
207
248
                    if (idx == key_columns[i]) {
208
41
                        key_group_cluster_key_idxes->emplace_back(i);
209
41
                        break;
210
41
                    }
211
248
                }
212
41
            }
213
27
        }
214
340
    }
215
741
    VLOG_NOTICE << "sequence_col_idx=" << sequence_col_idx
216
59
                << ", delete_sign_idx=" << delete_sign_idx;
217
    // for duplicate no keys
218
741
    if (!key_columns.empty()) {
219
724
        column_groups->emplace_back(key_columns);
220
724
    }
221
222
741
    std::vector<uint32_t> value_columns;
223
224
8.14k
    for (size_t i = num_key_cols; i < total_cols; ++i) {
225
7.39k
        if (i == sequence_col_idx || i == delete_sign_idx ||
226
7.39k
            key_columns.end() != std::find(key_columns.begin(), key_columns.end(), i)) {
227
392
            continue;
228
392
        }
229
230
7.00k
        if (!value_columns.empty() &&
231
7.00k
            value_columns.size() % config::vertical_compaction_num_columns_per_group == 0) {
232
1.06k
            column_groups->push_back(value_columns);
233
1.06k
            value_columns.clear();
234
1.06k
        }
235
7.00k
        value_columns.push_back(cast_set<uint32_t>(i));
236
7.00k
    }
237
238
741
    if (!value_columns.empty()) {
239
735
        column_groups->push_back(value_columns);
240
735
    }
241
741
}
242
243
Status Merger::vertical_compact_one_group(
244
        BaseTabletSPtr tablet, ReaderType reader_type, const TabletSchema& tablet_schema,
245
        bool is_key, const std::vector<uint32_t>& column_group,
246
        vectorized::RowSourcesBuffer* row_source_buf,
247
        const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
248
        RowsetWriter* dst_rowset_writer, uint32_t max_rows_per_segment, Statistics* stats_output,
249
        std::vector<uint32_t> key_group_cluster_key_idxes, int64_t batch_size,
250
2.43k
        CompactionSampleInfo* sample_info) {
251
    // build tablet reader
252
2.43k
    VLOG_NOTICE << "vertical compact one group, max_rows_per_segment=" << max_rows_per_segment;
253
2.43k
    vectorized::VerticalBlockReader reader(row_source_buf);
254
2.43k
    TabletReader::ReaderParams reader_params;
255
2.43k
    reader_params.is_key_column_group = is_key;
256
2.43k
    reader_params.key_group_cluster_key_idxes = key_group_cluster_key_idxes;
257
2.43k
    reader_params.tablet = tablet;
258
2.43k
    reader_params.reader_type = reader_type;
259
260
2.43k
    TabletReadSource read_source;
261
2.43k
    read_source.rs_splits.reserve(src_rowset_readers.size());
262
19.1k
    for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) {
263
19.1k
        read_source.rs_splits.emplace_back(rs_reader);
264
19.1k
    }
265
2.43k
    read_source.fill_delete_predicates();
266
2.43k
    reader_params.set_read_source(std::move(read_source));
267
268
2.43k
    reader_params.version = dst_rowset_writer->version();
269
270
2.43k
    TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>();
271
2.43k
    merge_tablet_schema->copy_from(tablet_schema);
272
273
2.43k
    for (auto& del_pred_rs : reader_params.delete_predicates) {
274
493
        merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema());
275
493
    }
276
277
2.43k
    reader_params.tablet_schema = merge_tablet_schema;
278
2.43k
    bool has_cluster_key = false;
279
2.43k
    if (!tablet->tablet_schema()->cluster_key_uids().empty()) {
280
52
        reader_params.delete_bitmap = tablet->tablet_meta()->delete_bitmap_ptr();
281
52
        has_cluster_key = true;
282
52
    }
283
284
2.43k
    if (is_key && stats_output && stats_output->rowid_conversion) {
285
410
        reader_params.record_rowids = true;
286
410
        reader_params.rowid_conversion = stats_output->rowid_conversion;
287
410
        stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id());
288
410
    }
289
290
2.43k
    reader_params.return_columns = column_group;
291
2.43k
    reader_params.origin_return_columns = &reader_params.return_columns;
292
2.43k
    reader_params.batch_size = batch_size;
293
2.43k
    RETURN_IF_ERROR(reader.init(reader_params, sample_info));
294
295
2.42k
    vectorized::Block block = tablet_schema.create_block(reader_params.return_columns);
296
2.42k
    size_t output_rows = 0;
297
2.42k
    bool eof = false;
298
20.5k
    while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) {
299
18.1k
        auto tablet_state = tablet->tablet_state();
300
18.1k
        if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) {
301
0
            tablet->clear_cache();
302
0
            return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more",
303
0
                                                 tablet->tablet_id());
304
0
        }
305
        // Read one block from block reader
306
18.1k
        RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof),
307
18.1k
                                       "failed to read next block when merging rowsets of tablet " +
308
18.1k
                                               std::to_string(tablet->tablet_id()));
309
18.1k
        RETURN_NOT_OK_STATUS_WITH_WARN(
310
18.1k
                dst_rowset_writer->add_columns(&block, column_group, is_key, max_rows_per_segment,
311
18.1k
                                               has_cluster_key),
312
18.1k
                "failed to write block when merging rowsets of tablet " +
313
18.1k
                        std::to_string(tablet->tablet_id()));
314
315
18.1k
        if (is_key && reader_params.record_rowids && block.rows() > 0) {
316
5.13k
            std::vector<uint32_t> segment_num_rows;
317
5.13k
            RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows));
318
5.13k
            stats_output->rowid_conversion->add(reader.current_block_row_locations(),
319
5.13k
                                                segment_num_rows);
320
5.13k
        }
321
18.1k
        output_rows += block.rows();
322
18.1k
        block.clear_column_data();
323
18.1k
    }
324
2.42k
    if (ExecEnv::GetInstance()->storage_engine().stopped()) {
325
0
        return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped",
326
0
                                             tablet->tablet_id());
327
0
    }
328
329
2.42k
    if (stats_output != nullptr) {
330
2.42k
        if (is_key) {
331
711
            stats_output->output_rows = output_rows;
332
711
            stats_output->merged_rows = reader.merged_rows();
333
711
            stats_output->filtered_rows = reader.filtered_rows();
334
711
        }
335
2.42k
        stats_output->bytes_read_from_local = reader.stats().file_cache_stats.bytes_read_from_local;
336
2.42k
        stats_output->bytes_read_from_remote =
337
2.42k
                reader.stats().file_cache_stats.bytes_read_from_remote;
338
2.42k
        stats_output->cached_bytes_total = reader.stats().file_cache_stats.bytes_write_into_cache;
339
2.42k
        if (config::is_cloud_mode()) {
340
0
            stats_output->cloud_local_read_time =
341
0
                    reader.stats().file_cache_stats.local_io_timer / 1000;
342
0
            stats_output->cloud_remote_read_time =
343
0
                    reader.stats().file_cache_stats.remote_io_timer / 1000;
344
0
        }
345
2.42k
    }
346
2.42k
    RETURN_IF_ERROR(dst_rowset_writer->flush_columns(is_key));
347
348
2.42k
    return Status::OK();
349
2.42k
}
350
351
// for segcompaction
352
Status Merger::vertical_compact_one_group(
353
        int64_t tablet_id, ReaderType reader_type, const TabletSchema& tablet_schema, bool is_key,
354
        const std::vector<uint32_t>& column_group, vectorized::RowSourcesBuffer* row_source_buf,
355
        vectorized::VerticalBlockReader& src_block_reader,
356
        segment_v2::SegmentWriter& dst_segment_writer, Statistics* stats_output,
357
56
        uint64_t* index_size, KeyBoundsPB& key_bounds, SimpleRowIdConversion* rowid_conversion) {
358
    // TODO: record_rowids
359
56
    vectorized::Block block = tablet_schema.create_block(column_group);
360
56
    size_t output_rows = 0;
361
56
    bool eof = false;
362
450
    while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) {
363
        // Read one block from block reader
364
394
        RETURN_NOT_OK_STATUS_WITH_WARN(src_block_reader.next_block_with_aggregation(&block, &eof),
365
394
                                       "failed to read next block when merging rowsets of tablet " +
366
394
                                               std::to_string(tablet_id));
367
394
        if (!block.rows()) {
368
0
            break;
369
0
        }
370
394
        RETURN_NOT_OK_STATUS_WITH_WARN(dst_segment_writer.append_block(&block, 0, block.rows()),
371
394
                                       "failed to write block when merging rowsets of tablet " +
372
394
                                               std::to_string(tablet_id));
373
374
394
        if (is_key && rowid_conversion != nullptr) {
375
45
            rowid_conversion->add(src_block_reader.current_block_row_locations());
376
45
        }
377
394
        output_rows += block.rows();
378
394
        block.clear_column_data();
379
394
    }
380
56
    if (ExecEnv::GetInstance()->storage_engine().stopped()) {
381
0
        return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped",
382
0
                                             tablet_id);
383
0
    }
384
385
56
    if (stats_output != nullptr) {
386
56
        if (is_key) {
387
28
            stats_output->output_rows = output_rows;
388
28
            stats_output->merged_rows = src_block_reader.merged_rows();
389
28
            stats_output->filtered_rows = src_block_reader.filtered_rows();
390
28
        }
391
56
        stats_output->bytes_read_from_local =
392
56
                src_block_reader.stats().file_cache_stats.bytes_read_from_local;
393
56
        stats_output->bytes_read_from_remote =
394
56
                src_block_reader.stats().file_cache_stats.bytes_read_from_remote;
395
56
        stats_output->cached_bytes_total =
396
56
                src_block_reader.stats().file_cache_stats.bytes_write_into_cache;
397
56
    }
398
399
    // segcompaction produce only one segment at once
400
56
    RETURN_IF_ERROR(dst_segment_writer.finalize_columns_data());
401
56
    RETURN_IF_ERROR(dst_segment_writer.finalize_columns_index(index_size));
402
403
56
    if (is_key) {
404
28
        Slice min_key = dst_segment_writer.min_encoded_key();
405
28
        Slice max_key = dst_segment_writer.max_encoded_key();
406
28
        DCHECK_LE(min_key.compare(max_key), 0);
407
28
        key_bounds.set_min_key(min_key.to_string());
408
28
        key_bounds.set_max_key(max_key.to_string());
409
28
    }
410
411
56
    return Status::OK();
412
56
}
413
414
2.23k
int64_t estimate_batch_size(int group_index, BaseTabletSPtr tablet, int64_t way_cnt) {
415
2.23k
    std::unique_lock<std::mutex> lock(tablet->sample_info_lock);
416
2.23k
    CompactionSampleInfo info = tablet->sample_infos[group_index];
417
2.23k
    if (way_cnt <= 0) {
418
84
        LOG(INFO) << "estimate batch size for vertical compaction, tablet id: "
419
84
                  << tablet->tablet_id() << " way cnt: " << way_cnt;
420
84
        return 4096 - 32;
421
84
    }
422
2.14k
    int64_t block_mem_limit = config::compaction_memory_bytes_limit / way_cnt;
423
2.14k
    if (tablet->last_compaction_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>()) {
424
0
        block_mem_limit /= 4;
425
0
    }
426
427
2.14k
    int64_t group_data_size = 0;
428
2.14k
    if (info.group_data_size > 0 && info.bytes > 0 && info.rows > 0) {
429
0
        double smoothing_factor = 0.5;
430
0
        group_data_size =
431
0
                int64_t((cast_set<double>(info.group_data_size) * (1 - smoothing_factor)) +
432
0
                        (cast_set<double>(info.bytes / info.rows) * smoothing_factor));
433
0
        tablet->sample_infos[group_index].group_data_size = group_data_size;
434
2.14k
    } else if (info.group_data_size > 0 && (info.bytes <= 0 || info.rows <= 0)) {
435
0
        group_data_size = info.group_data_size;
436
2.15k
    } else if (info.group_data_size <= 0 && info.bytes > 0 && info.rows > 0) {
437
1.05k
        group_data_size = info.bytes / info.rows;
438
1.05k
        tablet->sample_infos[group_index].group_data_size = group_data_size;
439
1.08k
    } else {
440
1.08k
        LOG(INFO) << "estimate batch size for vertical compaction, tablet id: "
441
1.08k
                  << tablet->tablet_id() << " group data size: " << info.group_data_size
442
1.08k
                  << " row num: " << info.rows << " consume bytes: " << info.bytes;
443
1.08k
        return 1024 - 32;
444
1.08k
    }
445
446
1.05k
    if (group_data_size <= 0) {
447
0
        LOG(WARNING) << "estimate batch size for vertical compaction, tablet id: "
448
0
                     << tablet->tablet_id() << " unexpected group data size: " << group_data_size;
449
0
        return 4096 - 32;
450
0
    }
451
452
1.05k
    tablet->sample_infos[group_index].bytes = 0;
453
1.05k
    tablet->sample_infos[group_index].rows = 0;
454
455
1.05k
    int64_t batch_size = block_mem_limit / group_data_size;
456
1.05k
    int64_t res = std::max(std::min(batch_size, int64_t(4096 - 32)), int64_t(32L));
457
1.05k
    LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " << tablet->tablet_id()
458
1.05k
              << " group data size: " << info.group_data_size << " row num: " << info.rows
459
1.05k
              << " consume bytes: " << info.bytes << " way cnt: " << way_cnt
460
1.05k
              << " batch size: " << res;
461
1.05k
    return res;
462
1.05k
}
463
464
// steps to do vertical merge:
465
// 1. split columns into column groups
466
// 2. compact groups one by one, generate a row_source_buf when compact key group
467
// and use this row_source_buf to compact value column groups
468
// 3. build output rowset
469
Status Merger::vertical_merge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type,
470
                                      const TabletSchema& tablet_schema,
471
                                      const std::vector<RowsetReaderSharedPtr>& src_rowset_readers,
472
                                      RowsetWriter* dst_rowset_writer,
473
                                      uint32_t max_rows_per_segment, int64_t merge_way_num,
474
713
                                      Statistics* stats_output) {
475
713
    LOG(INFO) << "Start to do vertical compaction, tablet_id: " << tablet->tablet_id();
476
713
    std::vector<std::vector<uint32_t>> column_groups;
477
713
    std::vector<uint32_t> key_group_cluster_key_idxes;
478
713
    vertical_split_columns(tablet_schema, &column_groups, &key_group_cluster_key_idxes);
479
480
713
    vectorized::RowSourcesBuffer row_sources_buf(
481
713
            tablet->tablet_id(), dst_rowset_writer->context().tablet_path, reader_type);
482
713
    Merger::Statistics total_stats;
483
713
    if (stats_output != nullptr) {
484
713
        total_stats.rowid_conversion = stats_output->rowid_conversion;
485
713
    }
486
713
    {
487
713
        std::unique_lock<std::mutex> lock(tablet->sample_info_lock);
488
713
        tablet->sample_infos.resize(column_groups.size());
489
713
    }
490
    // compact group one by one
491
3.13k
    for (auto i = 0; i < column_groups.size(); ++i) {
492
2.42k
        VLOG_NOTICE << "row source size: " << row_sources_buf.total_size();
493
2.42k
        bool is_key = (i == 0);
494
2.42k
        int64_t batch_size = config::compaction_batch_size != -1
495
2.42k
                                     ? config::compaction_batch_size
496
2.42k
                                     : estimate_batch_size(i, tablet, merge_way_num);
497
2.42k
        CompactionSampleInfo sample_info;
498
2.42k
        Merger::Statistics group_stats;
499
2.42k
        group_stats.rowid_conversion = total_stats.rowid_conversion;
500
18.4E
        Merger::Statistics* group_stats_ptr = stats_output != nullptr ? &group_stats : nullptr;
501
2.42k
        Status st = vertical_compact_one_group(
502
2.42k
                tablet, reader_type, tablet_schema, is_key, column_groups[i], &row_sources_buf,
503
2.42k
                src_rowset_readers, dst_rowset_writer, max_rows_per_segment, group_stats_ptr,
504
2.42k
                key_group_cluster_key_idxes, batch_size, &sample_info);
505
2.42k
        {
506
2.42k
            std::unique_lock<std::mutex> lock(tablet->sample_info_lock);
507
2.42k
            tablet->sample_infos[i] = sample_info;
508
2.42k
        }
509
2.42k
        RETURN_IF_ERROR(st);
510
2.42k
        if (stats_output != nullptr) {
511
2.42k
            total_stats.bytes_read_from_local += group_stats.bytes_read_from_local;
512
2.42k
            total_stats.bytes_read_from_remote += group_stats.bytes_read_from_remote;
513
2.42k
            total_stats.cached_bytes_total += group_stats.cached_bytes_total;
514
2.42k
            total_stats.cloud_local_read_time += group_stats.cloud_local_read_time;
515
2.42k
            total_stats.cloud_remote_read_time += group_stats.cloud_remote_read_time;
516
2.42k
            if (is_key) {
517
711
                total_stats.output_rows = group_stats.output_rows;
518
711
                total_stats.merged_rows = group_stats.merged_rows;
519
711
                total_stats.filtered_rows = group_stats.filtered_rows;
520
711
                total_stats.rowid_conversion = group_stats.rowid_conversion;
521
711
            }
522
2.42k
        }
523
2.42k
        if (is_key) {
524
711
            RETURN_IF_ERROR(row_sources_buf.flush());
525
711
        }
526
2.42k
        RETURN_IF_ERROR(row_sources_buf.seek_to_begin());
527
2.42k
    }
528
529
    // finish compact, build output rowset
530
707
    VLOG_NOTICE << "finish compact groups";
531
707
    RETURN_IF_ERROR(dst_rowset_writer->final_flush());
532
533
707
    if (stats_output != nullptr) {
534
707
        *stats_output = total_stats;
535
707
    }
536
537
707
    return Status::OK();
538
707
}
539
#include "common/compile_check_end.h"
540
} // namespace doris