Coverage Report

Created: 2025-07-23 22:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/memtable.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/memtable.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/olap_file.pb.h>
22
#include <pdqsort.h>
23
24
#include <algorithm>
25
#include <limits>
26
#include <string>
27
#include <vector>
28
29
#include "bvar/bvar.h"
30
#include "common/config.h"
31
#include "olap/memtable_memory_limiter.h"
32
#include "olap/olap_define.h"
33
#include "olap/tablet_schema.h"
34
#include "runtime/descriptors.h"
35
#include "runtime/exec_env.h"
36
#include "runtime/thread_context.h"
37
#include "util/debug_points.h"
38
#include "util/runtime_profile.h"
39
#include "util/stopwatch.hpp"
40
#include "vec/aggregate_functions/aggregate_function_reader.h"
41
#include "vec/aggregate_functions/aggregate_function_simple_factory.h"
42
#include "vec/columns/column.h"
43
44
namespace doris {
45
#include "common/compile_check_begin.h"
46
47
bvar::Adder<int64_t> g_memtable_cnt("memtable_cnt");
48
49
using namespace ErrorCode;
50
51
MemTable::MemTable(int64_t tablet_id, std::shared_ptr<TabletSchema> tablet_schema,
52
                   const std::vector<SlotDescriptor*>* slot_descs, TupleDescriptor* tuple_desc,
53
                   bool enable_unique_key_mow, PartialUpdateInfo* partial_update_info,
54
                   const std::shared_ptr<ResourceContext>& resource_ctx)
55
15
        : _mem_type(MemType::ACTIVE),
56
15
          _tablet_id(tablet_id),
57
15
          _enable_unique_key_mow(enable_unique_key_mow),
58
15
          _keys_type(tablet_schema->keys_type()),
59
15
          _tablet_schema(tablet_schema),
60
15
          _resource_ctx(resource_ctx),
61
15
          _is_first_insertion(true),
62
15
          _agg_functions(tablet_schema->num_columns()),
63
15
          _offsets_of_aggregate_states(tablet_schema->num_columns()),
64
15
          _total_size_of_aggregate_states(0) {
65
15
    g_memtable_cnt << 1;
66
15
    _mem_tracker = std::make_shared<MemTracker>();
67
15
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
68
15
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
69
15
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
70
15
    _vec_row_comparator = std::make_shared<RowInBlockComparator>(_tablet_schema);
71
15
    _num_columns = _tablet_schema->num_columns();
72
15
    if (partial_update_info != nullptr) {
73
15
        _partial_update_mode = partial_update_info->update_mode();
74
15
        if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
75
0
            _num_columns = partial_update_info->partial_update_input_columns.size();
76
0
            if (partial_update_info->is_schema_contains_auto_inc_column &&
77
0
                !partial_update_info->is_input_columns_contains_auto_inc_column) {
78
0
                _is_partial_update_and_auto_inc = true;
79
0
                _num_columns += 1;
80
0
            }
81
0
        }
82
15
    }
83
    // TODO: Support ZOrderComparator in the future
84
15
    _init_columns_offset_by_slot_descs(slot_descs, tuple_desc);
85
15
    _row_in_blocks = std::make_unique<DorisVector<std::shared_ptr<RowInBlock>>>();
86
15
}
87
88
void MemTable::_init_columns_offset_by_slot_descs(const std::vector<SlotDescriptor*>* slot_descs,
89
15
                                                  const TupleDescriptor* tuple_desc) {
90
91
    for (auto slot_desc : *slot_descs) {
91
91
        const auto& slots = tuple_desc->slots();
92
630
        for (int j = 0; j < slots.size(); ++j) {
93
630
            if (slot_desc->id() == slots[j]->id()) {
94
91
                _column_offset.emplace_back(j);
95
91
                break;
96
91
            }
97
630
        }
98
91
    }
99
15
    if (_is_partial_update_and_auto_inc) {
100
0
        _column_offset.emplace_back(_column_offset.size());
101
0
    }
102
15
}
103
104
12
void MemTable::_init_agg_functions(const vectorized::Block* block) {
105
42
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
106
30
        vectorized::AggregateFunctionPtr function;
107
30
        if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow) {
108
            // In such table, non-key column's aggregation type is NONE, so we need to construct
109
            // the aggregate function manually.
110
9
            if (_skip_bitmap_col_idx != cid) {
111
9
                function = vectorized::AggregateFunctionSimpleFactory::instance().get(
112
9
                        "replace_load", {block->get_data_type(cid)},
113
9
                        block->get_data_type(cid)->is_nullable(),
114
9
                        BeExecVersionManager::get_newest_version());
115
9
            } else {
116
0
                function = vectorized::AggregateFunctionSimpleFactory::instance().get(
117
0
                        "bitmap_intersect", {block->get_data_type(cid)}, false,
118
0
                        BeExecVersionManager::get_newest_version());
119
0
            }
120
21
        } else {
121
21
            function = _tablet_schema->column(cid).get_aggregate_function(
122
21
                    vectorized::AGG_LOAD_SUFFIX, _tablet_schema->column(cid).get_be_exec_version());
123
21
            if (function == nullptr) {
124
0
                LOG(WARNING) << "column get aggregate function failed, column="
125
0
                             << _tablet_schema->column(cid).name();
126
0
            }
127
21
        }
128
129
30
        DCHECK(function != nullptr);
130
30
        _agg_functions[cid] = function;
131
30
    }
132
133
42
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
134
30
        _offsets_of_aggregate_states[cid] = _total_size_of_aggregate_states;
135
30
        _total_size_of_aggregate_states += _agg_functions[cid]->size_of_data();
136
137
        // If not the last aggregate_state, we need pad it so that next aggregate_state will be aligned.
138
30
        if (cid + 1 < _num_columns) {
139
22
            size_t alignment_of_next_state = _agg_functions[cid + 1]->align_of_data();
140
141
            /// Extend total_size to next alignment requirement
142
            /// Add padding by rounding up 'total_size_of_aggregate_states' to be a multiplier of alignment_of_next_state.
143
22
            _total_size_of_aggregate_states =
144
22
                    (_total_size_of_aggregate_states + alignment_of_next_state - 1) /
145
22
                    alignment_of_next_state * alignment_of_next_state;
146
22
        }
147
30
    }
148
12
}
149
150
15
MemTable::~MemTable() {
151
15
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
152
15
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
153
15
    {
154
15
        SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
155
15
        g_memtable_cnt << -1;
156
15
        if (_keys_type != KeysType::DUP_KEYS) {
157
35
            for (auto it = _row_in_blocks->begin(); it != _row_in_blocks->end(); it++) {
158
20
                if (!(*it)->has_init_agg()) {
159
20
                    continue;
160
20
                }
161
                // We should release agg_places here, because they are not released when a
162
                // load is canceled.
163
0
                for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
164
0
                    auto function = _agg_functions[i];
165
0
                    DCHECK(function != nullptr);
166
0
                    function->destroy((*it)->agg_places(i));
167
0
                }
168
0
            }
169
15
        }
170
171
15
        _arena.clear(true);
172
15
        _vec_row_comparator.reset();
173
15
        _row_in_blocks.reset();
174
15
        _agg_functions.clear();
175
15
        _input_mutable_block.clear();
176
15
        _output_mutable_block.clear();
177
15
    }
178
15
    if (_is_flush_success) {
179
        // If the memtable is flush success, then its memtracker's consumption should be 0
180
12
        if (_mem_tracker->consumption() != 0 && config::crash_in_memory_tracker_inaccurate) {
181
0
            LOG(FATAL) << "memtable flush success but cosumption is not 0, it is "
182
0
                       << _mem_tracker->consumption();
183
0
        }
184
12
    }
185
15
}
186
187
6
int RowInBlockComparator::operator()(const RowInBlock* left, const RowInBlock* right) const {
188
6
    return _pblock->compare_at(left->_row_pos, right->_row_pos, _tablet_schema->num_key_columns(),
189
6
                               *_pblock, -1);
190
6
}
191
192
Status MemTable::insert(const vectorized::Block* input_block,
193
20
                        const DorisVector<uint32_t>& row_idxs) {
194
20
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
195
20
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
196
20
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
197
198
20
    if (_is_first_insertion) {
199
12
        _is_first_insertion = false;
200
12
        auto clone_block = input_block->clone_without_columns(&_column_offset);
201
12
        _input_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
202
12
        _vec_row_comparator->set_block(&_input_mutable_block);
203
12
        _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
204
12
        if (_tablet_schema->has_sequence_col()) {
205
7
            if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
206
                // for unique key fixed partial update, sequence column index in block
207
                // may be different with the index in `_tablet_schema`
208
0
                for (int32_t i = 0; i < clone_block.columns(); i++) {
209
0
                    if (clone_block.get_by_position(i).name == SEQUENCE_COL) {
210
0
                        _seq_col_idx_in_block = i;
211
0
                        break;
212
0
                    }
213
0
                }
214
7
            } else {
215
7
                _seq_col_idx_in_block = _tablet_schema->sequence_col_idx();
216
7
            }
217
7
        }
218
12
        if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS &&
219
12
            _tablet_schema->has_skip_bitmap_col()) {
220
            // init of _skip_bitmap_col_idx and _delete_sign_col_idx must be before _init_agg_functions()
221
0
            _skip_bitmap_col_idx = _tablet_schema->skip_bitmap_col_idx();
222
0
            _delete_sign_col_idx = _tablet_schema->delete_sign_idx();
223
0
            _delete_sign_col_unique_id = _tablet_schema->column(_delete_sign_col_idx).unique_id();
224
0
            if (_seq_col_idx_in_block != -1) {
225
0
                _seq_col_unique_id = _tablet_schema->column(_seq_col_idx_in_block).unique_id();
226
0
            }
227
0
        }
228
12
        if (_keys_type != KeysType::DUP_KEYS) {
229
            // there may be additional intermediate columns in input_block
230
            // we only need columns indicated by column offset in the output
231
12
            RETURN_IF_CATCH_EXCEPTION(_init_agg_functions(&clone_block));
232
12
        }
233
12
    }
234
235
20
    auto num_rows = row_idxs.size();
236
20
    size_t cursor_in_mutableblock = _input_mutable_block.rows();
237
20
    RETURN_IF_ERROR(_input_mutable_block.add_rows(input_block, row_idxs.data(),
238
20
                                                  row_idxs.data() + num_rows, &_column_offset));
239
40
    for (int i = 0; i < num_rows; i++) {
240
20
        _row_in_blocks->emplace_back(std::make_shared<RowInBlock>(cursor_in_mutableblock + i));
241
20
    }
242
243
20
    _stat.raw_rows += num_rows;
244
20
    return Status::OK();
245
20
}
246
247
template <bool has_skip_bitmap_col>
248
void MemTable::_aggregate_two_row_in_block(vectorized::MutableBlock& mutable_block,
249
3
                                           RowInBlock* src_row, RowInBlock* dst_row) {
250
    // for flexible partial update, the caller must guarantees that either src_row and dst_row
251
    // both specify the sequence column, or src_row and dst_row both don't specify the
252
    // sequence column
253
3
    if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) {
254
3
        DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns());
255
3
        auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get();
256
3
        auto res = col_ptr->compare_at(dst_row->_row_pos, src_row->_row_pos, *col_ptr, -1);
257
        // dst sequence column larger than src, don't need to update
258
3
        if (res > 0) {
259
3
            return;
260
3
        }
261
        // need to update the row pos in dst row to the src row pos when has
262
        // sequence column
263
0
        dst_row->_row_pos = src_row->_row_pos;
264
0
    }
265
    // dst is non-sequence row, or dst sequence is smaller
266
0
    if constexpr (!has_skip_bitmap_col) {
267
0
        DCHECK(_skip_bitmap_col_idx == -1);
268
0
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
269
0
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
270
0
            _agg_functions[cid]->add(dst_row->agg_places(cid),
271
0
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
272
0
                                     src_row->_row_pos, _arena);
273
0
        }
274
0
    } else {
275
0
        DCHECK(_skip_bitmap_col_idx != -1);
276
0
        DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns());
277
0
        const BitmapValue& skip_bitmap =
278
0
                assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>(
279
0
                        mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
280
0
                        ->get_data()[src_row->_row_pos];
281
0
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
282
0
            const auto& col = _tablet_schema->column(cid);
283
0
            if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) {
284
0
                continue;
285
0
            }
286
0
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
287
0
            _agg_functions[cid]->add(dst_row->agg_places(cid),
288
0
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
289
0
                                     src_row->_row_pos, _arena);
290
0
        }
291
0
    }
292
0
}
_ZN5doris8MemTable27_aggregate_two_row_in_blockILb0EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_
Line
Count
Source
249
3
                                           RowInBlock* src_row, RowInBlock* dst_row) {
250
    // for flexible partial update, the caller must guarantees that either src_row and dst_row
251
    // both specify the sequence column, or src_row and dst_row both don't specify the
252
    // sequence column
253
3
    if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) {
254
3
        DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns());
255
3
        auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get();
256
3
        auto res = col_ptr->compare_at(dst_row->_row_pos, src_row->_row_pos, *col_ptr, -1);
257
        // dst sequence column larger than src, don't need to update
258
3
        if (res > 0) {
259
3
            return;
260
3
        }
261
        // need to update the row pos in dst row to the src row pos when has
262
        // sequence column
263
0
        dst_row->_row_pos = src_row->_row_pos;
264
0
    }
265
    // dst is non-sequence row, or dst sequence is smaller
266
0
    if constexpr (!has_skip_bitmap_col) {
267
0
        DCHECK(_skip_bitmap_col_idx == -1);
268
0
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
269
0
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
270
0
            _agg_functions[cid]->add(dst_row->agg_places(cid),
271
0
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
272
0
                                     src_row->_row_pos, _arena);
273
0
        }
274
    } else {
275
        DCHECK(_skip_bitmap_col_idx != -1);
276
        DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns());
277
        const BitmapValue& skip_bitmap =
278
                assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>(
279
                        mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
280
                        ->get_data()[src_row->_row_pos];
281
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
282
            const auto& col = _tablet_schema->column(cid);
283
            if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) {
284
                continue;
285
            }
286
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
287
            _agg_functions[cid]->add(dst_row->agg_places(cid),
288
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
289
                                     src_row->_row_pos, _arena);
290
        }
291
    }
292
0
}
Unexecuted instantiation: _ZN5doris8MemTable27_aggregate_two_row_in_blockILb1EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_
293
9
Status MemTable::_put_into_output(vectorized::Block& in_block) {
294
9
    SCOPED_RAW_TIMER(&_stat.put_into_output_ns);
295
9
    DorisVector<uint32_t> row_pos_vec;
296
9
    DCHECK(in_block.rows() <= std::numeric_limits<int>::max());
297
9
    row_pos_vec.reserve(in_block.rows());
298
20
    for (int i = 0; i < _row_in_blocks->size(); i++) {
299
11
        row_pos_vec.emplace_back((*_row_in_blocks)[i]->_row_pos);
300
11
    }
301
9
    return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(),
302
9
                                          row_pos_vec.data() + in_block.rows());
303
9
}
304
305
12
size_t MemTable::_sort() {
306
12
    SCOPED_RAW_TIMER(&_stat.sort_ns);
307
12
    _stat.sort_times++;
308
12
    size_t same_keys_num = 0;
309
    // sort new rows
310
12
    Tie tie = Tie(_last_sorted_pos, _row_in_blocks->size());
311
43
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); i++) {
312
31
        auto cmp = [&](RowInBlock* lhs, RowInBlock* rhs) -> int {
313
30
            return _input_mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, i, -1);
314
30
        };
315
31
        _sort_one_column(*_row_in_blocks, tie, cmp);
316
31
    }
317
12
    bool is_dup = (_keys_type == KeysType::DUP_KEYS);
318
    // sort extra round by _row_pos to make the sort stable
319
12
    auto iter = tie.iter();
320
15
    while (iter.next()) {
321
3
        pdqsort(std::next(_row_in_blocks->begin(), iter.left()),
322
3
                std::next(_row_in_blocks->begin(), iter.right()),
323
3
                [&is_dup](const std::shared_ptr<RowInBlock>& lhs,
324
3
                          const std::shared_ptr<RowInBlock>& rhs) -> bool {
325
3
                    return is_dup ? lhs->_row_pos > rhs->_row_pos : lhs->_row_pos < rhs->_row_pos;
326
3
                });
327
3
        same_keys_num += iter.right() - iter.left();
328
3
    }
329
    // merge new rows and old rows
330
12
    _vec_row_comparator->set_block(&_input_mutable_block);
331
12
    auto cmp_func = [this, is_dup, &same_keys_num](const std::shared_ptr<RowInBlock>& l,
332
12
                                                   const std::shared_ptr<RowInBlock>& r) -> bool {
333
0
        auto value = (*(this->_vec_row_comparator))(l.get(), r.get());
334
0
        if (value == 0) {
335
0
            same_keys_num++;
336
0
            return is_dup ? l->_row_pos > r->_row_pos : l->_row_pos < r->_row_pos;
337
0
        } else {
338
0
            return value < 0;
339
0
        }
340
0
    };
341
12
    auto new_row_it = std::next(_row_in_blocks->begin(), _last_sorted_pos);
342
12
    std::inplace_merge(_row_in_blocks->begin(), new_row_it, _row_in_blocks->end(), cmp_func);
343
12
    _last_sorted_pos = _row_in_blocks->size();
344
12
    return same_keys_num;
345
12
}
346
347
1
Status MemTable::_sort_by_cluster_keys() {
348
1
    SCOPED_RAW_TIMER(&_stat.sort_ns);
349
1
    _stat.sort_times++;
350
    // sort all rows
351
1
    vectorized::Block in_block = _output_mutable_block.to_block();
352
1
    vectorized::MutableBlock mutable_block =
353
1
            vectorized::MutableBlock::build_mutable_block(&in_block);
354
1
    auto clone_block = in_block.clone_without_columns();
355
1
    _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
356
357
1
    DorisVector<std::shared_ptr<RowInBlock>> row_in_blocks;
358
1
    row_in_blocks.reserve(mutable_block.rows());
359
5
    for (size_t i = 0; i < mutable_block.rows(); i++) {
360
4
        row_in_blocks.emplace_back(std::make_shared<RowInBlock>(i));
361
4
    }
362
1
    Tie tie = Tie(0, mutable_block.rows());
363
364
2
    for (auto cid : _tablet_schema->cluster_key_uids()) {
365
2
        auto index = _tablet_schema->field_index(cid);
366
2
        if (index == -1) {
367
0
            return Status::InternalError("could not find cluster key column with unique_id=" +
368
0
                                         std::to_string(cid) + " in tablet schema");
369
0
        }
370
8
        auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int {
371
8
            return mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, index, -1);
372
8
        };
373
2
        _sort_one_column(row_in_blocks, tie, cmp);
374
2
    }
375
376
    // sort extra round by _row_pos to make the sort stable
377
1
    auto iter = tie.iter();
378
1
    while (iter.next()) {
379
0
        pdqsort(std::next(row_in_blocks.begin(), iter.left()),
380
0
                std::next(row_in_blocks.begin(), iter.right()),
381
0
                [](const std::shared_ptr<RowInBlock>& lhs, const std::shared_ptr<RowInBlock>& rhs)
382
0
                        -> bool { return lhs->_row_pos < rhs->_row_pos; });
383
0
    }
384
385
1
    in_block = mutable_block.to_block();
386
1
    SCOPED_RAW_TIMER(&_stat.put_into_output_ns);
387
1
    DorisVector<uint32_t> row_pos_vec;
388
1
    DCHECK(in_block.rows() <= std::numeric_limits<int>::max());
389
1
    row_pos_vec.reserve(in_block.rows());
390
5
    for (int i = 0; i < row_in_blocks.size(); i++) {
391
4
        row_pos_vec.emplace_back(row_in_blocks[i]->_row_pos);
392
4
    }
393
1
    std::vector<int> column_offset;
394
6
    for (int i = 0; i < _column_offset.size(); ++i) {
395
5
        column_offset.emplace_back(i);
396
5
    }
397
1
    return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(),
398
1
                                          row_pos_vec.data() + in_block.rows(), &column_offset);
399
1
}
400
401
void MemTable::_sort_one_column(DorisVector<std::shared_ptr<RowInBlock>>& row_in_blocks, Tie& tie,
402
33
                                std::function<int(RowInBlock*, RowInBlock*)> cmp) {
403
33
    auto iter = tie.iter();
404
43
    while (iter.next()) {
405
10
        pdqsort(std::next(row_in_blocks.begin(), static_cast<int>(iter.left())),
406
10
                std::next(row_in_blocks.begin(), static_cast<int>(iter.right())),
407
21
                [&cmp](auto lhs, auto rhs) -> bool { return cmp(lhs.get(), rhs.get()) < 0; });
408
10
        tie[iter.left()] = 0;
409
27
        for (auto i = iter.left() + 1; i < iter.right(); i++) {
410
17
            tie[i] = (cmp(row_in_blocks[i - 1].get(), row_in_blocks[i].get()) == 0);
411
17
        }
412
10
    }
413
33
}
414
415
template <bool is_final>
416
void MemTable::_finalize_one_row(RowInBlock* row,
417
                                 const vectorized::ColumnsWithTypeAndName& block_data,
418
6
                                 int row_pos) {
419
    // move key columns
420
18
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) {
421
12
        _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(),
422
12
                                                                     row->_row_pos);
423
12
    }
424
6
    if (row->has_init_agg()) {
425
        // get value columns from agg_places
426
12
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
427
9
            auto function = _agg_functions[i];
428
9
            auto* agg_place = row->agg_places(i);
429
9
            auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
430
9
            function->insert_result_into(agg_place, *col_ptr);
431
432
9
            if constexpr (is_final) {
433
9
                function->destroy(agg_place);
434
9
            } else {
435
0
                function->reset(agg_place);
436
0
            }
437
9
        }
438
439
3
        if constexpr (is_final) {
440
3
            row->remove_init_agg();
441
3
        } else {
442
0
            for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
443
0
                auto function = _agg_functions[i];
444
0
                auto* agg_place = row->agg_places(i);
445
0
                auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
446
0
                function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr),
447
0
                              row_pos, _arena);
448
0
            }
449
0
        }
450
3
    } else {
451
        // move columns for rows do not need agg
452
12
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
453
9
            _output_mutable_block.get_column_by_position(i)->insert_from(
454
9
                    *block_data[i].column.get(), row->_row_pos);
455
9
        }
456
3
    }
457
6
    if constexpr (!is_final) {
458
0
        row->_row_pos = row_pos;
459
0
    }
460
6
}
Unexecuted instantiation: _ZN5doris8MemTable17_finalize_one_rowILb0EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi
_ZN5doris8MemTable17_finalize_one_rowILb1EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi
Line
Count
Source
418
6
                                 int row_pos) {
419
    // move key columns
420
18
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) {
421
12
        _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(),
422
12
                                                                     row->_row_pos);
423
12
    }
424
6
    if (row->has_init_agg()) {
425
        // get value columns from agg_places
426
12
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
427
9
            auto function = _agg_functions[i];
428
9
            auto* agg_place = row->agg_places(i);
429
9
            auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
430
9
            function->insert_result_into(agg_place, *col_ptr);
431
432
9
            if constexpr (is_final) {
433
9
                function->destroy(agg_place);
434
            } else {
435
                function->reset(agg_place);
436
            }
437
9
        }
438
439
3
        if constexpr (is_final) {
440
3
            row->remove_init_agg();
441
        } else {
442
            for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
443
                auto function = _agg_functions[i];
444
                auto* agg_place = row->agg_places(i);
445
                auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
446
                function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr),
447
                              row_pos, _arena);
448
            }
449
        }
450
3
    } else {
451
        // move columns for rows do not need agg
452
12
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
453
9
            _output_mutable_block.get_column_by_position(i)->insert_from(
454
9
                    *block_data[i].column.get(), row->_row_pos);
455
9
        }
456
3
    }
457
    if constexpr (!is_final) {
458
        row->_row_pos = row_pos;
459
    }
460
6
}
461
462
3
void MemTable::_init_row_for_agg(RowInBlock* row, vectorized::MutableBlock& mutable_block) {
463
3
    row->init_agg_places(_arena.aligned_alloc(_total_size_of_aggregate_states, 16),
464
3
                         _offsets_of_aggregate_states.data());
465
12
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) {
466
9
        auto* col_ptr = mutable_block.mutable_columns()[cid].get();
467
9
        auto* data = row->agg_places(cid);
468
9
        _agg_functions[cid]->create(data);
469
9
        _agg_functions[cid]->add(data, const_cast<const doris::vectorized::IColumn**>(&col_ptr),
470
9
                                 row->_row_pos, _arena);
471
9
    }
472
3
}
473
0
void MemTable::_clear_row_agg(RowInBlock* row) {
474
0
    if (row->has_init_agg()) {
475
0
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
476
0
            auto function = _agg_functions[i];
477
0
            auto* agg_place = row->agg_places(i);
478
0
            function->destroy(agg_place);
479
0
        }
480
0
        row->remove_init_agg();
481
0
    }
482
0
}
483
484
template <bool is_final, bool has_skip_bitmap_col>
485
3
void MemTable::_aggregate() {
486
3
    SCOPED_RAW_TIMER(&_stat.agg_ns);
487
3
    _stat.agg_times++;
488
3
    vectorized::Block in_block = _input_mutable_block.to_block();
489
3
    vectorized::MutableBlock mutable_block =
490
3
            vectorized::MutableBlock::build_mutable_block(&in_block);
491
3
    _vec_row_comparator->set_block(&mutable_block);
492
3
    auto& block_data = in_block.get_columns_with_type_and_name();
493
3
    DorisVector<std::shared_ptr<RowInBlock>> temp_row_in_blocks;
494
3
    temp_row_in_blocks.reserve(_last_sorted_pos);
495
    //only init agg if needed
496
497
3
    if constexpr (!has_skip_bitmap_col) {
498
3
        RowInBlock* prev_row = nullptr;
499
3
        int row_pos = -1;
500
9
        for (const auto& cur_row_ptr : *_row_in_blocks) {
501
9
            RowInBlock* cur_row = cur_row_ptr.get();
502
9
            if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) {
503
3
                if (!prev_row->has_init_agg()) {
504
3
                    _init_row_for_agg(prev_row, mutable_block);
505
3
                }
506
3
                _stat.merged_rows++;
507
3
                _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row);
508
6
            } else {
509
6
                prev_row = cur_row;
510
6
                if (!temp_row_in_blocks.empty()) {
511
                    // no more rows to merge for prev row, finalize it
512
3
                    _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data,
513
3
                                                row_pos);
514
3
                }
515
6
                temp_row_in_blocks.push_back(cur_row_ptr);
516
6
                row_pos++;
517
6
            }
518
9
        }
519
3
        if (!temp_row_in_blocks.empty()) {
520
            // finalize the last low
521
3
            _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data, row_pos);
522
3
        }
523
3
    } else {
524
0
        DCHECK(_delete_sign_col_idx != -1);
525
0
        if (_seq_col_idx_in_block == -1) {
526
0
            _aggregate_for_flexible_partial_update_without_seq_col<is_final>(
527
0
                    block_data, mutable_block, temp_row_in_blocks);
528
0
        } else {
529
0
            _aggregate_for_flexible_partial_update_with_seq_col<is_final>(block_data, mutable_block,
530
0
                                                                          temp_row_in_blocks);
531
0
        }
532
0
    }
533
3
    if constexpr (!is_final) {
534
        // if is not final, we collect the agg results to input_block and then continue to insert
535
0
        _input_mutable_block.swap(_output_mutable_block);
536
        //TODO(weixang):opt here.
537
0
        std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0);
538
0
        _output_mutable_block =
539
0
                vectorized::MutableBlock::build_mutable_block(empty_input_block.get());
540
0
        _output_mutable_block.clear_column_data();
541
0
        *_row_in_blocks = temp_row_in_blocks;
542
0
        _last_sorted_pos = _row_in_blocks->size();
543
0
    }
544
3
}
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb0EEEvv
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb1EEEvv
_ZN5doris8MemTable10_aggregateILb1ELb0EEEvv
Line
Count
Source
485
3
void MemTable::_aggregate() {
486
3
    SCOPED_RAW_TIMER(&_stat.agg_ns);
487
3
    _stat.agg_times++;
488
3
    vectorized::Block in_block = _input_mutable_block.to_block();
489
3
    vectorized::MutableBlock mutable_block =
490
3
            vectorized::MutableBlock::build_mutable_block(&in_block);
491
3
    _vec_row_comparator->set_block(&mutable_block);
492
3
    auto& block_data = in_block.get_columns_with_type_and_name();
493
3
    DorisVector<std::shared_ptr<RowInBlock>> temp_row_in_blocks;
494
3
    temp_row_in_blocks.reserve(_last_sorted_pos);
495
    //only init agg if needed
496
497
3
    if constexpr (!has_skip_bitmap_col) {
498
3
        RowInBlock* prev_row = nullptr;
499
3
        int row_pos = -1;
500
9
        for (const auto& cur_row_ptr : *_row_in_blocks) {
501
9
            RowInBlock* cur_row = cur_row_ptr.get();
502
9
            if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) {
503
3
                if (!prev_row->has_init_agg()) {
504
3
                    _init_row_for_agg(prev_row, mutable_block);
505
3
                }
506
3
                _stat.merged_rows++;
507
3
                _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row);
508
6
            } else {
509
6
                prev_row = cur_row;
510
6
                if (!temp_row_in_blocks.empty()) {
511
                    // no more rows to merge for prev row, finalize it
512
3
                    _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data,
513
3
                                                row_pos);
514
3
                }
515
6
                temp_row_in_blocks.push_back(cur_row_ptr);
516
6
                row_pos++;
517
6
            }
518
9
        }
519
3
        if (!temp_row_in_blocks.empty()) {
520
            // finalize the last low
521
3
            _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data, row_pos);
522
3
        }
523
    } else {
524
        DCHECK(_delete_sign_col_idx != -1);
525
        if (_seq_col_idx_in_block == -1) {
526
            _aggregate_for_flexible_partial_update_without_seq_col<is_final>(
527
                    block_data, mutable_block, temp_row_in_blocks);
528
        } else {
529
            _aggregate_for_flexible_partial_update_with_seq_col<is_final>(block_data, mutable_block,
530
                                                                          temp_row_in_blocks);
531
        }
532
    }
533
    if constexpr (!is_final) {
534
        // if is not final, we collect the agg results to input_block and then continue to insert
535
        _input_mutable_block.swap(_output_mutable_block);
536
        //TODO(weixang):opt here.
537
        std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0);
538
        _output_mutable_block =
539
                vectorized::MutableBlock::build_mutable_block(empty_input_block.get());
540
        _output_mutable_block.clear_column_data();
541
        *_row_in_blocks = temp_row_in_blocks;
542
        _last_sorted_pos = _row_in_blocks->size();
543
    }
544
3
}
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb1ELb1EEEvv
545
546
template <bool is_final>
547
void MemTable::_aggregate_for_flexible_partial_update_without_seq_col(
548
        const vectorized::ColumnsWithTypeAndName& block_data,
549
        vectorized::MutableBlock& mutable_block,
550
0
        DorisVector<std::shared_ptr<RowInBlock>>& temp_row_in_blocks) {
551
0
    std::shared_ptr<RowInBlock> prev_row {nullptr};
552
0
    int row_pos = -1;
553
0
    auto& skip_bitmaps = assert_cast<vectorized::ColumnBitmap*>(
554
0
                                 mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
555
0
                                 ->get_data();
556
0
    auto& delete_signs = assert_cast<vectorized::ColumnInt8*>(
557
0
                                 mutable_block.mutable_columns()[_delete_sign_col_idx].get())
558
0
                                 ->get_data();
559
0
    std::shared_ptr<RowInBlock> row_with_delete_sign {nullptr};
560
0
    std::shared_ptr<RowInBlock> row_without_delete_sign {nullptr};
561
562
0
    auto finalize_rows = [&]() {
563
0
        if (row_with_delete_sign != nullptr) {
564
0
            temp_row_in_blocks.push_back(row_with_delete_sign);
565
0
            _finalize_one_row<is_final>(row_with_delete_sign.get(), block_data, ++row_pos);
566
0
            row_with_delete_sign = nullptr;
567
0
        }
568
0
        if (row_without_delete_sign != nullptr) {
569
0
            temp_row_in_blocks.push_back(row_without_delete_sign);
570
0
            _finalize_one_row<is_final>(row_without_delete_sign.get(), block_data, ++row_pos);
571
0
            row_without_delete_sign = nullptr;
572
0
        }
573
        // _arena.clear();
574
0
    };
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlvE_clEv
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlvE_clEv
575
576
0
    auto add_row = [&](std::shared_ptr<RowInBlock> row, bool with_delete_sign) {
577
0
        if (with_delete_sign) {
578
0
            row_with_delete_sign = std::move(row);
579
0
        } else {
580
0
            row_without_delete_sign = std::move(row);
581
0
        }
582
0
    };
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlSD_bE_clESD_b
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlSD_bE_clESD_b
583
0
    for (const auto& cur_row_ptr : *_row_in_blocks) {
584
0
        RowInBlock* cur_row = cur_row_ptr.get();
585
0
        const BitmapValue& skip_bitmap = skip_bitmaps[cur_row->_row_pos];
586
0
        bool cur_row_has_delete_sign = (!skip_bitmap.contains(_delete_sign_col_unique_id) &&
587
0
                                        delete_signs[cur_row->_row_pos] != 0);
588
0
        prev_row =
589
0
                (row_with_delete_sign == nullptr) ? row_without_delete_sign : row_with_delete_sign;
590
        // compare keys, the keys of row_with_delete_sign and row_without_delete_sign is the same,
591
        // choose any of them if it's valid
592
0
        if (prev_row != nullptr && (*_vec_row_comparator)(prev_row.get(), cur_row) == 0) {
593
0
            if (cur_row_has_delete_sign) {
594
0
                if (row_without_delete_sign != nullptr) {
595
                    // if there exits row without delete sign, remove it first
596
0
                    _clear_row_agg(row_without_delete_sign.get());
597
0
                    _stat.merged_rows++;
598
0
                    row_without_delete_sign = nullptr;
599
0
                }
600
                // and then unconditionally replace the previous row
601
0
                prev_row = row_with_delete_sign;
602
0
            } else {
603
0
                prev_row = row_without_delete_sign;
604
0
            }
605
606
0
            if (prev_row == nullptr) {
607
0
                add_row(cur_row_ptr, cur_row_has_delete_sign);
608
0
            } else {
609
0
                if (!prev_row->has_init_agg()) {
610
0
                    _init_row_for_agg(prev_row.get(), mutable_block);
611
0
                }
612
0
                _stat.merged_rows++;
613
0
                _aggregate_two_row_in_block<true>(mutable_block, cur_row, prev_row.get());
614
0
            }
615
0
        } else {
616
0
            finalize_rows();
617
0
            add_row(cur_row_ptr, cur_row_has_delete_sign);
618
0
        }
619
0
    }
620
    // finalize the last lows
621
0
    finalize_rows();
622
0
}
Unexecuted instantiation: _ZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
Unexecuted instantiation: _ZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
623
624
template <bool is_final>
625
void MemTable::_aggregate_for_flexible_partial_update_with_seq_col(
626
        const vectorized::ColumnsWithTypeAndName& block_data,
627
        vectorized::MutableBlock& mutable_block,
628
0
        DorisVector<std::shared_ptr<RowInBlock>>& temp_row_in_blocks) {
629
    // For flexible partial update, when table has sequence column, we don't do any aggregation
630
    // in memtable. These duplicate rows will be aggregated in VerticalSegmentWriter
631
0
    int row_pos = -1;
632
0
    for (const auto& row_ptr : *_row_in_blocks) {
633
0
        RowInBlock* row = row_ptr.get();
634
0
        temp_row_in_blocks.push_back(row_ptr);
635
0
        _finalize_one_row<is_final>(row, block_data, ++row_pos);
636
0
    }
637
0
}
Unexecuted instantiation: _ZN5doris8MemTable51_aggregate_for_flexible_partial_update_with_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
Unexecuted instantiation: _ZN5doris8MemTable51_aggregate_for_flexible_partial_update_with_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
638
639
0
void MemTable::shrink_memtable_by_agg() {
640
0
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
641
0
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
642
0
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
643
0
    if (_keys_type == KeysType::DUP_KEYS) {
644
0
        return;
645
0
    }
646
0
    size_t same_keys_num = _sort();
647
0
    if (same_keys_num != 0) {
648
0
        (_skip_bitmap_col_idx == -1) ? _aggregate<false, false>() : _aggregate<false, true>();
649
0
    }
650
0
}
651
652
20
bool MemTable::need_flush() const {
653
20
    DBUG_EXECUTE_IF("MemTable.need_flush", { return true; });
654
20
    auto max_size = config::write_buffer_size;
655
20
    if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
656
0
        auto update_columns_size = _num_columns;
657
0
        max_size = max_size * update_columns_size / _tablet_schema->num_columns();
658
0
        max_size = max_size > 1048576 ? max_size : 1048576;
659
0
    }
660
20
    return memory_usage() >= max_size;
661
20
}
662
663
20
bool MemTable::need_agg() const {
664
20
    if (_keys_type == KeysType::AGG_KEYS) {
665
5
        auto max_size = config::write_buffer_size_for_agg;
666
5
        return memory_usage() >= max_size;
667
5
    }
668
15
    return false;
669
20
}
670
671
12
size_t MemTable::get_flush_reserve_memory_size() const {
672
12
    if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) {
673
0
        return 0; // no need to reserve
674
0
    }
675
12
    return static_cast<size_t>(static_cast<double>(_input_mutable_block.allocated_bytes()) * 1.2);
676
12
}
677
678
12
Status MemTable::_to_block(std::unique_ptr<vectorized::Block>* res) {
679
12
    size_t same_keys_num = _sort();
680
12
    if (_keys_type == KeysType::DUP_KEYS || same_keys_num == 0) {
681
9
        if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) {
682
0
            _output_mutable_block.swap(_input_mutable_block);
683
9
        } else {
684
9
            vectorized::Block in_block = _input_mutable_block.to_block();
685
9
            RETURN_IF_ERROR(_put_into_output(in_block));
686
9
        }
687
9
    } else {
688
3
        (_skip_bitmap_col_idx == -1) ? _aggregate<true, false>() : _aggregate<true, true>();
689
3
    }
690
12
    if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow &&
691
12
        !_tablet_schema->cluster_key_uids().empty()) {
692
1
        if (_partial_update_mode != UniqueKeyUpdateModePB::UPSERT) {
693
0
            return Status::InternalError(
694
0
                    "Partial update for mow with cluster keys is not supported");
695
0
        }
696
1
        RETURN_IF_ERROR(_sort_by_cluster_keys());
697
1
    }
698
12
    _input_mutable_block.clear();
699
12
    *res = vectorized::Block::create_unique(_output_mutable_block.to_block());
700
12
    return Status::OK();
701
12
}
702
703
12
Status MemTable::to_block(std::unique_ptr<vectorized::Block>* res) {
704
12
    RETURN_IF_ERROR_OR_CATCH_EXCEPTION(_to_block(res));
705
12
    return Status::OK();
706
12
}
707
708
#include "common/compile_check_end.h"
709
} // namespace doris