Coverage Report

Created: 2025-09-02 13:40

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
333k
        : _mem_type(MemType::ACTIVE),
56
333k
          _tablet_id(tablet_id),
57
333k
          _enable_unique_key_mow(enable_unique_key_mow),
58
333k
          _keys_type(tablet_schema->keys_type()),
59
333k
          _tablet_schema(tablet_schema),
60
333k
          _resource_ctx(resource_ctx),
61
333k
          _is_first_insertion(true),
62
333k
          _agg_functions(tablet_schema->num_columns()),
63
333k
          _offsets_of_aggregate_states(tablet_schema->num_columns()),
64
333k
          _total_size_of_aggregate_states(0) {
65
333k
    g_memtable_cnt << 1;
66
333k
    _mem_tracker = std::make_shared<MemTracker>();
67
333k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
68
333k
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
69
333k
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
70
333k
    _vec_row_comparator = std::make_shared<RowInBlockComparator>(_tablet_schema);
71
333k
    _num_columns = _tablet_schema->num_columns();
72
333k
    if (partial_update_info != nullptr) {
73
332k
        _partial_update_mode = partial_update_info->update_mode();
74
332k
        if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
75
2.52k
            _num_columns = partial_update_info->partial_update_input_columns.size();
76
2.52k
            if (partial_update_info->is_schema_contains_auto_inc_column &&
77
2.52k
                !partial_update_info->is_input_columns_contains_auto_inc_column) {
78
193
                _is_partial_update_and_auto_inc = true;
79
193
                _num_columns += 1;
80
193
            }
81
2.52k
        }
82
332k
    }
83
    // TODO: Support ZOrderComparator in the future
84
333k
    _init_columns_offset_by_slot_descs(slot_descs, tuple_desc);
85
333k
    _row_in_blocks = std::make_unique<DorisVector<std::shared_ptr<RowInBlock>>>();
86
333k
}
87
88
void MemTable::_init_columns_offset_by_slot_descs(const std::vector<SlotDescriptor*>* slot_descs,
89
331k
                                                  const TupleDescriptor* tuple_desc) {
90
3.13M
    for (auto slot_desc : *slot_descs) {
91
3.13M
        const auto& slots = tuple_desc->slots();
92
41.6M
        for (int j = 0; j < slots.size(); ++j) {
93
41.5M
            if (slot_desc->id() == slots[j]->id()) {
94
3.10M
                _column_offset.emplace_back(j);
95
3.10M
                break;
96
3.10M
            }
97
41.5M
        }
98
3.13M
    }
99
331k
    if (_is_partial_update_and_auto_inc) {
100
192
        _column_offset.emplace_back(_column_offset.size());
101
192
    }
102
331k
}
103
104
29.5k
void MemTable::_init_agg_functions(const vectorized::Block* block) {
105
348k
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
106
318k
        vectorized::AggregateFunctionPtr function;
107
318k
        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
274k
            if (_skip_bitmap_col_idx != cid) {
111
273k
                function = vectorized::AggregateFunctionSimpleFactory::instance().get(
112
273k
                        "replace_load", {block->get_data_type(cid)},
113
273k
                        block->get_data_type(cid)->is_nullable(),
114
273k
                        BeExecVersionManager::get_newest_version());
115
273k
            } else {
116
275
                function = vectorized::AggregateFunctionSimpleFactory::instance().get(
117
275
                        "bitmap_intersect", {block->get_data_type(cid)}, false,
118
275
                        BeExecVersionManager::get_newest_version());
119
275
            }
120
274k
        } else {
121
44.5k
            function = _tablet_schema->column(cid).get_aggregate_function(
122
44.5k
                    vectorized::AGG_LOAD_SUFFIX, _tablet_schema->column(cid).get_be_exec_version());
123
44.5k
            if (function == nullptr) {
124
0
                LOG(WARNING) << "column get aggregate function failed, column="
125
0
                             << _tablet_schema->column(cid).name();
126
0
            }
127
44.5k
        }
128
129
318k
        DCHECK(function != nullptr);
130
318k
        _agg_functions[cid] = function;
131
318k
    }
132
133
348k
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
134
318k
        _offsets_of_aggregate_states[cid] = _total_size_of_aggregate_states;
135
318k
        _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
318k
        if (cid + 1 < _num_columns) {
139
289k
            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
289k
            _total_size_of_aggregate_states =
144
289k
                    (_total_size_of_aggregate_states + alignment_of_next_state - 1) /
145
289k
                    alignment_of_next_state * alignment_of_next_state;
146
289k
        }
147
318k
    }
148
29.5k
}
149
150
334k
MemTable::~MemTable() {
151
334k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
152
334k
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
153
334k
    {
154
334k
        SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
155
334k
        g_memtable_cnt << -1;
156
334k
        if (_keys_type != KeysType::DUP_KEYS) {
157
14.2M
            for (auto it = _row_in_blocks->begin(); it != _row_in_blocks->end(); it++) {
158
14.1M
                if (!(*it)->has_init_agg()) {
159
14.1M
                    continue;
160
14.1M
                }
161
                // We should release agg_places here, because they are not released when a
162
                // load is canceled.
163
81
                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
81
            }
169
83.8k
        }
170
171
334k
        _arena.clear(true);
172
334k
        _vec_row_comparator.reset();
173
334k
        _row_in_blocks.reset();
174
334k
        _agg_functions.clear();
175
334k
        _input_mutable_block.clear();
176
334k
        _output_mutable_block.clear();
177
334k
    }
178
334k
    if (_is_flush_success) {
179
        // If the memtable is flush success, then its memtracker's consumption should be 0
180
64.7k
        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
64.7k
    }
185
334k
}
186
187
399k
int RowInBlockComparator::operator()(const RowInBlock* left, const RowInBlock* right) const {
188
399k
    return _pblock->compare_at(left->_row_pos, right->_row_pos, _tablet_schema->num_key_columns(),
189
399k
                               *_pblock, -1);
190
399k
}
191
192
Status MemTable::insert(const vectorized::Block* input_block,
193
130k
                        const DorisVector<uint32_t>& row_idxs) {
194
130k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(
195
130k
            _resource_ctx->memory_context()->mem_tracker()->write_tracker());
196
130k
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
197
198
130k
    if (_is_first_insertion) {
199
64.8k
        _is_first_insertion = false;
200
64.8k
        auto clone_block = input_block->clone_without_columns(&_column_offset);
201
64.8k
        _input_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
202
64.8k
        _vec_row_comparator->set_block(&_input_mutable_block);
203
64.8k
        _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
204
64.8k
        if (_tablet_schema->has_sequence_col()) {
205
1.23k
            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
538
                for (int32_t i = 0; i < clone_block.columns(); i++) {
209
433
                    if (clone_block.get_by_position(i).name == SEQUENCE_COL) {
210
38
                        _seq_col_idx_in_block = i;
211
38
                        break;
212
38
                    }
213
433
                }
214
1.08k
            } else {
215
1.08k
                _seq_col_idx_in_block = _tablet_schema->sequence_col_idx();
216
1.08k
            }
217
1.23k
        }
218
64.8k
        if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS &&
219
64.8k
            _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
265
            _skip_bitmap_col_idx = _tablet_schema->skip_bitmap_col_idx();
222
265
            _delete_sign_col_idx = _tablet_schema->delete_sign_idx();
223
265
            _delete_sign_col_unique_id = _tablet_schema->column(_delete_sign_col_idx).unique_id();
224
265
            if (_seq_col_idx_in_block != -1) {
225
25
                _seq_col_unique_id = _tablet_schema->column(_seq_col_idx_in_block).unique_id();
226
25
            }
227
265
        }
228
64.8k
        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
29.5k
            RETURN_IF_CATCH_EXCEPTION(_init_agg_functions(&clone_block));
232
29.5k
        }
233
64.8k
    }
234
235
130k
    auto num_rows = row_idxs.size();
236
130k
    size_t cursor_in_mutableblock = _input_mutable_block.rows();
237
130k
    RETURN_IF_ERROR(_input_mutable_block.add_rows(input_block, row_idxs.data(),
238
130k
                                                  row_idxs.data() + num_rows, &_column_offset));
239
49.7M
    for (int i = 0; i < num_rows; i++) {
240
49.6M
        _row_in_blocks->emplace_back(std::make_shared<RowInBlock>(cursor_in_mutableblock + i));
241
49.6M
    }
242
243
130k
    _stat.raw_rows += num_rows;
244
130k
    return Status::OK();
245
130k
}
246
247
template <bool has_skip_bitmap_col>
248
void MemTable::_aggregate_two_row_in_block(vectorized::MutableBlock& mutable_block,
249
80.9k
                                           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
80.9k
    if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) {
254
170
        DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns());
255
170
        auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get();
256
170
        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
170
        if (res > 0) {
259
88
            return;
260
88
        }
261
        // need to update the row pos in dst row to the src row pos when has
262
        // sequence column
263
82
        dst_row->_row_pos = src_row->_row_pos;
264
82
    }
265
    // dst is non-sequence row, or dst sequence is smaller
266
80.8k
    if constexpr (!has_skip_bitmap_col) {
267
76.9k
        DCHECK(_skip_bitmap_col_idx == -1);
268
354k
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
269
277k
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
270
277k
            _agg_functions[cid]->add(dst_row->agg_places(cid),
271
277k
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
272
277k
                                     src_row->_row_pos, _arena);
273
277k
        }
274
76.9k
    } else {
275
3.96k
        DCHECK(_skip_bitmap_col_idx != -1);
276
3.96k
        DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns());
277
3.96k
        const BitmapValue& skip_bitmap =
278
3.96k
                assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>(
279
3.96k
                        mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
280
3.96k
                        ->get_data()[src_row->_row_pos];
281
72.3k
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
282
68.3k
            const auto& col = _tablet_schema->column(cid);
283
68.3k
            if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) {
284
34.2k
                continue;
285
34.2k
            }
286
34.1k
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
287
34.1k
            _agg_functions[cid]->add(dst_row->agg_places(cid),
288
34.1k
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
289
34.1k
                                     src_row->_row_pos, _arena);
290
34.1k
        }
291
3.96k
    }
292
80.8k
}
_ZN5doris8MemTable27_aggregate_two_row_in_blockILb0EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_
Line
Count
Source
249
77.0k
                                           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
77.0k
    if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) {
254
170
        DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns());
255
170
        auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get();
256
170
        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
170
        if (res > 0) {
259
88
            return;
260
88
        }
261
        // need to update the row pos in dst row to the src row pos when has
262
        // sequence column
263
82
        dst_row->_row_pos = src_row->_row_pos;
264
82
    }
265
    // dst is non-sequence row, or dst sequence is smaller
266
76.9k
    if constexpr (!has_skip_bitmap_col) {
267
76.9k
        DCHECK(_skip_bitmap_col_idx == -1);
268
354k
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
269
277k
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
270
277k
            _agg_functions[cid]->add(dst_row->agg_places(cid),
271
277k
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
272
277k
                                     src_row->_row_pos, _arena);
273
277k
        }
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
76.9k
}
_ZN5doris8MemTable27_aggregate_two_row_in_blockILb1EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_
Line
Count
Source
249
3.96k
                                           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.96k
    if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) {
254
0
        DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns());
255
0
        auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get();
256
0
        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
0
        if (res > 0) {
259
0
            return;
260
0
        }
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
    if constexpr (!has_skip_bitmap_col) {
267
        DCHECK(_skip_bitmap_col_idx == -1);
268
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
269
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
270
            _agg_functions[cid]->add(dst_row->agg_places(cid),
271
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
272
                                     src_row->_row_pos, _arena);
273
        }
274
3.96k
    } else {
275
3.96k
        DCHECK(_skip_bitmap_col_idx != -1);
276
3.96k
        DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns());
277
3.96k
        const BitmapValue& skip_bitmap =
278
3.96k
                assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>(
279
3.96k
                        mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
280
3.96k
                        ->get_data()[src_row->_row_pos];
281
72.3k
        for (size_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) {
282
68.3k
            const auto& col = _tablet_schema->column(cid);
283
68.3k
            if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) {
284
34.2k
                continue;
285
34.2k
            }
286
34.1k
            auto* col_ptr = mutable_block.mutable_columns()[cid].get();
287
34.1k
            _agg_functions[cid]->add(dst_row->agg_places(cid),
288
34.1k
                                     const_cast<const doris::vectorized::IColumn**>(&col_ptr),
289
34.1k
                                     src_row->_row_pos, _arena);
290
34.1k
        }
291
3.96k
    }
292
3.96k
}
293
62.4k
Status MemTable::_put_into_output(vectorized::Block& in_block) {
294
62.4k
    SCOPED_RAW_TIMER(&_stat.put_into_output_ns);
295
62.4k
    DorisVector<uint32_t> row_pos_vec;
296
62.4k
    DCHECK(in_block.rows() <= std::numeric_limits<int>::max());
297
62.4k
    row_pos_vec.reserve(in_block.rows());
298
49.1M
    for (int i = 0; i < _row_in_blocks->size(); i++) {
299
49.0M
        row_pos_vec.emplace_back((*_row_in_blocks)[i]->_row_pos);
300
49.0M
    }
301
62.4k
    return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(),
302
62.4k
                                          row_pos_vec.data() + in_block.rows());
303
62.4k
}
304
305
64.6k
size_t MemTable::_sort() {
306
64.6k
    SCOPED_RAW_TIMER(&_stat.sort_ns);
307
64.6k
    _stat.sort_times++;
308
64.6k
    size_t same_keys_num = 0;
309
    // sort new rows
310
64.6k
    Tie tie = Tie(_last_sorted_pos, _row_in_blocks->size());
311
263k
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); i++) {
312
435M
        auto cmp = [&](RowInBlock* lhs, RowInBlock* rhs) -> int {
313
435M
            return _input_mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, i, -1);
314
435M
        };
315
198k
        _sort_one_column(*_row_in_blocks, tie, cmp);
316
198k
    }
317
64.6k
    bool is_dup = (_keys_type == KeysType::DUP_KEYS);
318
    // sort extra round by _row_pos to make the sort stable
319
64.6k
    auto iter = tie.iter();
320
314k
    while (iter.next()) {
321
249k
        pdqsort(std::next(_row_in_blocks->begin(), iter.left()),
322
249k
                std::next(_row_in_blocks->begin(), iter.right()),
323
249k
                [&is_dup](const std::shared_ptr<RowInBlock>& lhs,
324
65.5M
                          const std::shared_ptr<RowInBlock>& rhs) -> bool {
325
65.5M
                    return is_dup ? lhs->_row_pos > rhs->_row_pos : lhs->_row_pos < rhs->_row_pos;
326
65.5M
                });
327
249k
        same_keys_num += iter.right() - iter.left();
328
249k
    }
329
    // merge new rows and old rows
330
64.6k
    _vec_row_comparator->set_block(&_input_mutable_block);
331
64.6k
    auto cmp_func = [this, is_dup, &same_keys_num](const std::shared_ptr<RowInBlock>& l,
332
64.6k
                                                   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
64.6k
    auto new_row_it = std::next(_row_in_blocks->begin(), _last_sorted_pos);
342
64.6k
    std::inplace_merge(_row_in_blocks->begin(), new_row_it, _row_in_blocks->end(), cmp_func);
343
64.6k
    _last_sorted_pos = _row_in_blocks->size();
344
64.6k
    return same_keys_num;
345
64.6k
}
346
347
1.55k
Status MemTable::_sort_by_cluster_keys() {
348
1.55k
    SCOPED_RAW_TIMER(&_stat.sort_ns);
349
1.55k
    _stat.sort_times++;
350
    // sort all rows
351
1.55k
    vectorized::Block in_block = _output_mutable_block.to_block();
352
1.55k
    vectorized::MutableBlock mutable_block =
353
1.55k
            vectorized::MutableBlock::build_mutable_block(&in_block);
354
1.55k
    auto clone_block = in_block.clone_without_columns();
355
1.55k
    _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block);
356
357
1.55k
    DorisVector<std::shared_ptr<RowInBlock>> row_in_blocks;
358
1.55k
    row_in_blocks.reserve(mutable_block.rows());
359
813k
    for (size_t i = 0; i < mutable_block.rows(); i++) {
360
811k
        row_in_blocks.emplace_back(std::make_shared<RowInBlock>(i));
361
811k
    }
362
1.55k
    Tie tie = Tie(0, mutable_block.rows());
363
364
3.77k
    for (auto cid : _tablet_schema->cluster_key_uids()) {
365
3.77k
        auto index = _tablet_schema->field_index(cid);
366
3.77k
        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.59M
        auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int {
371
8.59M
            return mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, index, -1);
372
8.59M
        };
373
3.77k
        _sort_one_column(row_in_blocks, tie, cmp);
374
3.77k
    }
375
376
    // sort extra round by _row_pos to make the sort stable
377
1.55k
    auto iter = tie.iter();
378
119k
    while (iter.next()) {
379
117k
        pdqsort(std::next(row_in_blocks.begin(), iter.left()),
380
117k
                std::next(row_in_blocks.begin(), iter.right()),
381
117k
                [](const std::shared_ptr<RowInBlock>& lhs, const std::shared_ptr<RowInBlock>& rhs)
382
847k
                        -> bool { return lhs->_row_pos < rhs->_row_pos; });
383
117k
    }
384
385
1.55k
    in_block = mutable_block.to_block();
386
1.55k
    SCOPED_RAW_TIMER(&_stat.put_into_output_ns);
387
1.55k
    DorisVector<uint32_t> row_pos_vec;
388
1.55k
    DCHECK(in_block.rows() <= std::numeric_limits<int>::max());
389
1.55k
    row_pos_vec.reserve(in_block.rows());
390
819k
    for (int i = 0; i < row_in_blocks.size(); i++) {
391
817k
        row_pos_vec.emplace_back(row_in_blocks[i]->_row_pos);
392
817k
    }
393
1.55k
    std::vector<int> column_offset;
394
21.5k
    for (int i = 0; i < _column_offset.size(); ++i) {
395
20.0k
        column_offset.emplace_back(i);
396
20.0k
    }
397
1.55k
    return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(),
398
1.55k
                                          row_pos_vec.data() + in_block.rows(), &column_offset);
399
1.55k
}
400
401
void MemTable::_sort_one_column(DorisVector<std::shared_ptr<RowInBlock>>& row_in_blocks, Tie& tie,
402
201k
                                std::function<int(RowInBlock*, RowInBlock*)> cmp) {
403
201k
    auto iter = tie.iter();
404
6.33M
    while (iter.next()) {
405
6.13M
        pdqsort(std::next(row_in_blocks.begin(), static_cast<int>(iter.left())),
406
6.13M
                std::next(row_in_blocks.begin(), static_cast<int>(iter.right())),
407
379M
                [&cmp](auto lhs, auto rhs) -> bool { return cmp(lhs.get(), rhs.get()) < 0; });
408
6.13M
        tie[iter.left()] = 0;
409
79.1M
        for (auto i = iter.left() + 1; i < iter.right(); i++) {
410
72.9M
            tie[i] = (cmp(row_in_blocks[i - 1].get(), row_in_blocks[i].get()) == 0);
411
72.9M
        }
412
6.13M
    }
413
201k
}
414
415
template <bool is_final>
416
void MemTable::_finalize_one_row(RowInBlock* row,
417
                                 const vectorized::ColumnsWithTypeAndName& block_data,
418
320k
                                 int row_pos) {
419
    // move key columns
420
845k
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) {
421
524k
        _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(),
422
524k
                                                                     row->_row_pos);
423
524k
    }
424
320k
    if (row->has_init_agg()) {
425
        // get value columns from agg_places
426
81.2k
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
427
74.0k
            auto function = _agg_functions[i];
428
74.0k
            auto* agg_place = row->agg_places(i);
429
74.0k
            auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
430
74.0k
            function->insert_result_into(agg_place, *col_ptr);
431
432
74.0k
            if constexpr (is_final) {
433
74.0k
                function->destroy(agg_place);
434
74.0k
            } else {
435
0
                function->reset(agg_place);
436
0
            }
437
74.0k
        }
438
439
7.16k
        if constexpr (is_final) {
440
7.16k
            row->remove_init_agg();
441
7.16k
        } 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
313k
    } else {
451
        // move columns for rows do not need agg
452
5.63M
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
453
5.31M
            _output_mutable_block.get_column_by_position(i)->insert_from(
454
5.31M
                    *block_data[i].column.get(), row->_row_pos);
455
5.31M
        }
456
313k
    }
457
320k
    if constexpr (!is_final) {
458
0
        row->_row_pos = row_pos;
459
0
    }
460
320k
}
Unexecuted instantiation: _ZN5doris8MemTable17_finalize_one_rowILb0EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi
_ZN5doris8MemTable17_finalize_one_rowILb1EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi
Line
Count
Source
418
320k
                                 int row_pos) {
419
    // move key columns
420
845k
    for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) {
421
524k
        _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(),
422
524k
                                                                     row->_row_pos);
423
524k
    }
424
320k
    if (row->has_init_agg()) {
425
        // get value columns from agg_places
426
81.2k
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
427
74.0k
            auto function = _agg_functions[i];
428
74.0k
            auto* agg_place = row->agg_places(i);
429
74.0k
            auto* col_ptr = _output_mutable_block.get_column_by_position(i).get();
430
74.0k
            function->insert_result_into(agg_place, *col_ptr);
431
432
74.0k
            if constexpr (is_final) {
433
74.0k
                function->destroy(agg_place);
434
            } else {
435
                function->reset(agg_place);
436
            }
437
74.0k
        }
438
439
7.16k
        if constexpr (is_final) {
440
7.16k
            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
313k
    } else {
451
        // move columns for rows do not need agg
452
5.63M
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
453
5.31M
            _output_mutable_block.get_column_by_position(i)->insert_from(
454
5.31M
                    *block_data[i].column.get(), row->_row_pos);
455
5.31M
        }
456
313k
    }
457
    if constexpr (!is_final) {
458
        row->_row_pos = row_pos;
459
    }
460
320k
}
461
462
7.16k
void MemTable::_init_row_for_agg(RowInBlock* row, vectorized::MutableBlock& mutable_block) {
463
7.16k
    row->init_agg_places(_arena.aligned_alloc(_total_size_of_aggregate_states, 16),
464
7.16k
                         _offsets_of_aggregate_states.data());
465
81.2k
    for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) {
466
74.0k
        auto* col_ptr = mutable_block.mutable_columns()[cid].get();
467
74.0k
        auto* data = row->agg_places(cid);
468
74.0k
        _agg_functions[cid]->create(data);
469
74.0k
        _agg_functions[cid]->add(data, const_cast<const doris::vectorized::IColumn**>(&col_ptr),
470
74.0k
                                 row->_row_pos, _arena);
471
74.0k
    }
472
7.16k
}
473
13
void MemTable::_clear_row_agg(RowInBlock* row) {
474
13
    if (row->has_init_agg()) {
475
54
        for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
476
48
            auto function = _agg_functions[i];
477
48
            auto* agg_place = row->agg_places(i);
478
48
            function->destroy(agg_place);
479
48
        }
480
6
        row->remove_init_agg();
481
6
    }
482
13
}
483
484
template <bool is_final, bool has_skip_bitmap_col>
485
1.99k
void MemTable::_aggregate() {
486
1.99k
    SCOPED_RAW_TIMER(&_stat.agg_ns);
487
1.99k
    _stat.agg_times++;
488
1.99k
    vectorized::Block in_block = _input_mutable_block.to_block();
489
1.99k
    vectorized::MutableBlock mutable_block =
490
1.99k
            vectorized::MutableBlock::build_mutable_block(&in_block);
491
1.99k
    _vec_row_comparator->set_block(&mutable_block);
492
1.99k
    auto& block_data = in_block.get_columns_with_type_and_name();
493
1.99k
    DorisVector<std::shared_ptr<RowInBlock>> temp_row_in_blocks;
494
1.99k
    temp_row_in_blocks.reserve(_last_sorted_pos);
495
    //only init agg if needed
496
497
1.99k
    if constexpr (!has_skip_bitmap_col) {
498
1.82k
        RowInBlock* prev_row = nullptr;
499
1.82k
        int row_pos = -1;
500
395k
        for (const auto& cur_row_ptr : *_row_in_blocks) {
501
395k
            RowInBlock* cur_row = cur_row_ptr.get();
502
395k
            if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) {
503
76.8k
                if (!prev_row->has_init_agg()) {
504
5.72k
                    _init_row_for_agg(prev_row, mutable_block);
505
5.72k
                }
506
76.8k
                _stat.merged_rows++;
507
76.8k
                _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row);
508
319k
            } else {
509
319k
                prev_row = cur_row;
510
319k
                if (!temp_row_in_blocks.empty()) {
511
                    // no more rows to merge for prev row, finalize it
512
316k
                    _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data,
513
316k
                                                row_pos);
514
316k
                }
515
319k
                temp_row_in_blocks.push_back(cur_row_ptr);
516
319k
                row_pos++;
517
319k
            }
518
395k
        }
519
1.84k
        if (!temp_row_in_blocks.empty()) {
520
            // finalize the last low
521
1.84k
            _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data, row_pos);
522
1.84k
        }
523
1.82k
    } else {
524
174
        DCHECK(_delete_sign_col_idx != -1);
525
174
        if (_seq_col_idx_in_block == -1) {
526
160
            _aggregate_for_flexible_partial_update_without_seq_col<is_final>(
527
160
                    block_data, mutable_block, temp_row_in_blocks);
528
160
        } else {
529
14
            _aggregate_for_flexible_partial_update_with_seq_col<is_final>(block_data, mutable_block,
530
14
                                                                          temp_row_in_blocks);
531
14
        }
532
174
    }
533
1.99k
    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
1.99k
}
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb0EEEvv
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb1EEEvv
_ZN5doris8MemTable10_aggregateILb1ELb0EEEvv
Line
Count
Source
485
1.82k
void MemTable::_aggregate() {
486
1.82k
    SCOPED_RAW_TIMER(&_stat.agg_ns);
487
1.82k
    _stat.agg_times++;
488
1.82k
    vectorized::Block in_block = _input_mutable_block.to_block();
489
1.82k
    vectorized::MutableBlock mutable_block =
490
1.82k
            vectorized::MutableBlock::build_mutable_block(&in_block);
491
1.82k
    _vec_row_comparator->set_block(&mutable_block);
492
1.82k
    auto& block_data = in_block.get_columns_with_type_and_name();
493
1.82k
    DorisVector<std::shared_ptr<RowInBlock>> temp_row_in_blocks;
494
1.82k
    temp_row_in_blocks.reserve(_last_sorted_pos);
495
    //only init agg if needed
496
497
1.82k
    if constexpr (!has_skip_bitmap_col) {
498
1.82k
        RowInBlock* prev_row = nullptr;
499
1.82k
        int row_pos = -1;
500
395k
        for (const auto& cur_row_ptr : *_row_in_blocks) {
501
395k
            RowInBlock* cur_row = cur_row_ptr.get();
502
395k
            if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) {
503
76.8k
                if (!prev_row->has_init_agg()) {
504
5.72k
                    _init_row_for_agg(prev_row, mutable_block);
505
5.72k
                }
506
76.8k
                _stat.merged_rows++;
507
76.8k
                _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row);
508
319k
            } else {
509
319k
                prev_row = cur_row;
510
319k
                if (!temp_row_in_blocks.empty()) {
511
                    // no more rows to merge for prev row, finalize it
512
316k
                    _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data,
513
316k
                                                row_pos);
514
316k
                }
515
319k
                temp_row_in_blocks.push_back(cur_row_ptr);
516
319k
                row_pos++;
517
319k
            }
518
395k
        }
519
1.84k
        if (!temp_row_in_blocks.empty()) {
520
            // finalize the last low
521
1.84k
            _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data, row_pos);
522
1.84k
        }
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
1.82k
}
_ZN5doris8MemTable10_aggregateILb1ELb1EEEvv
Line
Count
Source
485
174
void MemTable::_aggregate() {
486
174
    SCOPED_RAW_TIMER(&_stat.agg_ns);
487
174
    _stat.agg_times++;
488
174
    vectorized::Block in_block = _input_mutable_block.to_block();
489
174
    vectorized::MutableBlock mutable_block =
490
174
            vectorized::MutableBlock::build_mutable_block(&in_block);
491
174
    _vec_row_comparator->set_block(&mutable_block);
492
174
    auto& block_data = in_block.get_columns_with_type_and_name();
493
174
    DorisVector<std::shared_ptr<RowInBlock>> temp_row_in_blocks;
494
174
    temp_row_in_blocks.reserve(_last_sorted_pos);
495
    //only init agg if needed
496
497
    if constexpr (!has_skip_bitmap_col) {
498
        RowInBlock* prev_row = nullptr;
499
        int row_pos = -1;
500
        for (const auto& cur_row_ptr : *_row_in_blocks) {
501
            RowInBlock* cur_row = cur_row_ptr.get();
502
            if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) {
503
                if (!prev_row->has_init_agg()) {
504
                    _init_row_for_agg(prev_row, mutable_block);
505
                }
506
                _stat.merged_rows++;
507
                _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row);
508
            } else {
509
                prev_row = cur_row;
510
                if (!temp_row_in_blocks.empty()) {
511
                    // no more rows to merge for prev row, finalize it
512
                    _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data,
513
                                                row_pos);
514
                }
515
                temp_row_in_blocks.push_back(cur_row_ptr);
516
                row_pos++;
517
            }
518
        }
519
        if (!temp_row_in_blocks.empty()) {
520
            // finalize the last low
521
            _finalize_one_row<is_final>(temp_row_in_blocks.back().get(), block_data, row_pos);
522
        }
523
174
    } else {
524
174
        DCHECK(_delete_sign_col_idx != -1);
525
174
        if (_seq_col_idx_in_block == -1) {
526
160
            _aggregate_for_flexible_partial_update_without_seq_col<is_final>(
527
160
                    block_data, mutable_block, temp_row_in_blocks);
528
160
        } else {
529
14
            _aggregate_for_flexible_partial_update_with_seq_col<is_final>(block_data, mutable_block,
530
14
                                                                          temp_row_in_blocks);
531
14
        }
532
174
    }
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
174
}
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
160
        DorisVector<std::shared_ptr<RowInBlock>>& temp_row_in_blocks) {
551
160
    std::shared_ptr<RowInBlock> prev_row {nullptr};
552
160
    int row_pos = -1;
553
160
    auto& skip_bitmaps = assert_cast<vectorized::ColumnBitmap*>(
554
160
                                 mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
555
160
                                 ->get_data();
556
160
    auto& delete_signs = assert_cast<vectorized::ColumnInt8*>(
557
160
                                 mutable_block.mutable_columns()[_delete_sign_col_idx].get())
558
160
                                 ->get_data();
559
160
    std::shared_ptr<RowInBlock> row_with_delete_sign {nullptr};
560
160
    std::shared_ptr<RowInBlock> row_without_delete_sign {nullptr};
561
562
1.91k
    auto finalize_rows = [&]() {
563
1.91k
        if (row_with_delete_sign != nullptr) {
564
14
            temp_row_in_blocks.push_back(row_with_delete_sign);
565
14
            _finalize_one_row<is_final>(row_with_delete_sign.get(), block_data, ++row_pos);
566
14
            row_with_delete_sign = nullptr;
567
14
        }
568
1.91k
        if (row_without_delete_sign != nullptr) {
569
1.74k
            temp_row_in_blocks.push_back(row_without_delete_sign);
570
1.74k
            _finalize_one_row<is_final>(row_without_delete_sign.get(), block_data, ++row_pos);
571
1.74k
            row_without_delete_sign = nullptr;
572
1.74k
        }
573
        // _arena.clear();
574
1.91k
    };
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlvE_clEv
_ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlvE_clEv
Line
Count
Source
562
1.91k
    auto finalize_rows = [&]() {
563
1.91k
        if (row_with_delete_sign != nullptr) {
564
14
            temp_row_in_blocks.push_back(row_with_delete_sign);
565
14
            _finalize_one_row<is_final>(row_with_delete_sign.get(), block_data, ++row_pos);
566
14
            row_with_delete_sign = nullptr;
567
14
        }
568
1.91k
        if (row_without_delete_sign != nullptr) {
569
1.74k
            temp_row_in_blocks.push_back(row_without_delete_sign);
570
1.74k
            _finalize_one_row<is_final>(row_without_delete_sign.get(), block_data, ++row_pos);
571
1.74k
            row_without_delete_sign = nullptr;
572
1.74k
        }
573
        // _arena.clear();
574
1.91k
    };
575
576
1.77k
    auto add_row = [&](std::shared_ptr<RowInBlock> row, bool with_delete_sign) {
577
1.77k
        if (with_delete_sign) {
578
14
            row_with_delete_sign = std::move(row);
579
1.75k
        } else {
580
1.75k
            row_without_delete_sign = std::move(row);
581
1.75k
        }
582
1.77k
    };
Unexecuted instantiation: _ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlSD_bE_clESD_b
_ZZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENKUlSD_bE_clESD_b
Line
Count
Source
576
1.77k
    auto add_row = [&](std::shared_ptr<RowInBlock> row, bool with_delete_sign) {
577
1.77k
        if (with_delete_sign) {
578
14
            row_with_delete_sign = std::move(row);
579
1.75k
        } else {
580
1.75k
            row_without_delete_sign = std::move(row);
581
1.75k
        }
582
1.77k
    };
583
5.73k
    for (const auto& cur_row_ptr : *_row_in_blocks) {
584
5.73k
        RowInBlock* cur_row = cur_row_ptr.get();
585
5.73k
        const BitmapValue& skip_bitmap = skip_bitmaps[cur_row->_row_pos];
586
5.73k
        bool cur_row_has_delete_sign = (!skip_bitmap.contains(_delete_sign_col_unique_id) &&
587
5.73k
                                        delete_signs[cur_row->_row_pos] != 0);
588
5.73k
        prev_row =
589
5.73k
                (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
5.73k
        if (prev_row != nullptr && (*_vec_row_comparator)(prev_row.get(), cur_row) == 0) {
593
3.98k
            if (cur_row_has_delete_sign) {
594
24
                if (row_without_delete_sign != nullptr) {
595
                    // if there exits row without delete sign, remove it first
596
13
                    _clear_row_agg(row_without_delete_sign.get());
597
13
                    _stat.merged_rows++;
598
13
                    row_without_delete_sign = nullptr;
599
13
                }
600
                // and then unconditionally replace the previous row
601
24
                prev_row = row_with_delete_sign;
602
3.95k
            } else {
603
3.95k
                prev_row = row_without_delete_sign;
604
3.95k
            }
605
606
3.98k
            if (prev_row == nullptr) {
607
17
                add_row(cur_row_ptr, cur_row_has_delete_sign);
608
3.96k
            } else {
609
3.96k
                if (!prev_row->has_init_agg()) {
610
1.44k
                    _init_row_for_agg(prev_row.get(), mutable_block);
611
1.44k
                }
612
3.96k
                _stat.merged_rows++;
613
3.96k
                _aggregate_two_row_in_block<true>(mutable_block, cur_row, prev_row.get());
614
3.96k
            }
615
3.98k
        } else {
616
1.75k
            finalize_rows();
617
1.75k
            add_row(cur_row_ptr, cur_row_has_delete_sign);
618
1.75k
        }
619
5.73k
    }
620
    // finalize the last lows
621
160
    finalize_rows();
622
160
}
Unexecuted instantiation: _ZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
_ZN5doris8MemTable54_aggregate_for_flexible_partial_update_without_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
Line
Count
Source
550
160
        DorisVector<std::shared_ptr<RowInBlock>>& temp_row_in_blocks) {
551
160
    std::shared_ptr<RowInBlock> prev_row {nullptr};
552
160
    int row_pos = -1;
553
160
    auto& skip_bitmaps = assert_cast<vectorized::ColumnBitmap*>(
554
160
                                 mutable_block.mutable_columns()[_skip_bitmap_col_idx].get())
555
160
                                 ->get_data();
556
160
    auto& delete_signs = assert_cast<vectorized::ColumnInt8*>(
557
160
                                 mutable_block.mutable_columns()[_delete_sign_col_idx].get())
558
160
                                 ->get_data();
559
160
    std::shared_ptr<RowInBlock> row_with_delete_sign {nullptr};
560
160
    std::shared_ptr<RowInBlock> row_without_delete_sign {nullptr};
561
562
160
    auto finalize_rows = [&]() {
563
160
        if (row_with_delete_sign != nullptr) {
564
160
            temp_row_in_blocks.push_back(row_with_delete_sign);
565
160
            _finalize_one_row<is_final>(row_with_delete_sign.get(), block_data, ++row_pos);
566
160
            row_with_delete_sign = nullptr;
567
160
        }
568
160
        if (row_without_delete_sign != nullptr) {
569
160
            temp_row_in_blocks.push_back(row_without_delete_sign);
570
160
            _finalize_one_row<is_final>(row_without_delete_sign.get(), block_data, ++row_pos);
571
160
            row_without_delete_sign = nullptr;
572
160
        }
573
        // _arena.clear();
574
160
    };
575
576
160
    auto add_row = [&](std::shared_ptr<RowInBlock> row, bool with_delete_sign) {
577
160
        if (with_delete_sign) {
578
160
            row_with_delete_sign = std::move(row);
579
160
        } else {
580
160
            row_without_delete_sign = std::move(row);
581
160
        }
582
160
    };
583
5.73k
    for (const auto& cur_row_ptr : *_row_in_blocks) {
584
5.73k
        RowInBlock* cur_row = cur_row_ptr.get();
585
5.73k
        const BitmapValue& skip_bitmap = skip_bitmaps[cur_row->_row_pos];
586
5.73k
        bool cur_row_has_delete_sign = (!skip_bitmap.contains(_delete_sign_col_unique_id) &&
587
5.73k
                                        delete_signs[cur_row->_row_pos] != 0);
588
5.73k
        prev_row =
589
5.73k
                (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
5.73k
        if (prev_row != nullptr && (*_vec_row_comparator)(prev_row.get(), cur_row) == 0) {
593
3.98k
            if (cur_row_has_delete_sign) {
594
24
                if (row_without_delete_sign != nullptr) {
595
                    // if there exits row without delete sign, remove it first
596
13
                    _clear_row_agg(row_without_delete_sign.get());
597
13
                    _stat.merged_rows++;
598
13
                    row_without_delete_sign = nullptr;
599
13
                }
600
                // and then unconditionally replace the previous row
601
24
                prev_row = row_with_delete_sign;
602
3.95k
            } else {
603
3.95k
                prev_row = row_without_delete_sign;
604
3.95k
            }
605
606
3.98k
            if (prev_row == nullptr) {
607
17
                add_row(cur_row_ptr, cur_row_has_delete_sign);
608
3.96k
            } else {
609
3.96k
                if (!prev_row->has_init_agg()) {
610
1.44k
                    _init_row_for_agg(prev_row.get(), mutable_block);
611
1.44k
                }
612
3.96k
                _stat.merged_rows++;
613
3.96k
                _aggregate_two_row_in_block<true>(mutable_block, cur_row, prev_row.get());
614
3.96k
            }
615
3.98k
        } else {
616
1.75k
            finalize_rows();
617
1.75k
            add_row(cur_row_ptr, cur_row_has_delete_sign);
618
1.75k
        }
619
5.73k
    }
620
    // finalize the last lows
621
160
    finalize_rows();
622
160
}
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
15
        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
15
    int row_pos = -1;
632
482
    for (const auto& row_ptr : *_row_in_blocks) {
633
482
        RowInBlock* row = row_ptr.get();
634
482
        temp_row_in_blocks.push_back(row_ptr);
635
482
        _finalize_one_row<is_final>(row, block_data, ++row_pos);
636
482
    }
637
15
}
Unexecuted instantiation: _ZN5doris8MemTable51_aggregate_for_flexible_partial_update_with_seq_colILb0EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
_ZN5doris8MemTable51_aggregate_for_flexible_partial_update_with_seq_colILb1EEEvRKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS4_EERNS3_12MutableBlockERS2_ISt10shared_ptrINS_10RowInBlockEENS_18CustomStdAllocatorISD_NS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEE
Line
Count
Source
628
15
        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
15
    int row_pos = -1;
632
482
    for (const auto& row_ptr : *_row_in_blocks) {
633
482
        RowInBlock* row = row_ptr.get();
634
482
        temp_row_in_blocks.push_back(row_ptr);
635
482
        _finalize_one_row<is_final>(row, block_data, ++row_pos);
636
482
    }
637
15
}
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
    _last_agg_pos = memory_usage();
651
0
}
652
653
130k
bool MemTable::need_flush() const {
654
130k
    DBUG_EXECUTE_IF("MemTable.need_flush", { return true; });
655
130k
    auto max_size = config::write_buffer_size;
656
130k
    if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
657
990
        auto update_columns_size = _num_columns;
658
990
        auto min_buffer_size = config::min_write_buffer_size_for_partial_update;
659
990
        max_size = max_size * update_columns_size / _tablet_schema->num_columns();
660
990
        max_size = max_size > min_buffer_size ? max_size : min_buffer_size;
661
990
    }
662
130k
    return memory_usage() >= max_size;
663
130k
}
664
665
130k
bool MemTable::need_agg() const {
666
130k
    if (_keys_type == KeysType::AGG_KEYS) {
667
4.01k
        auto max_size = _last_agg_pos + config::write_buffer_size_for_agg;
668
4.01k
        return memory_usage() >= max_size;
669
4.01k
    }
670
126k
    return false;
671
130k
}
672
673
64.7k
size_t MemTable::get_flush_reserve_memory_size() const {
674
64.7k
    if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) {
675
17
        return 0; // no need to reserve
676
17
    }
677
64.7k
    return static_cast<size_t>(static_cast<double>(_input_mutable_block.allocated_bytes()) * 1.2);
678
64.7k
}
679
680
64.6k
Status MemTable::_to_block(std::unique_ptr<vectorized::Block>* res) {
681
64.6k
    size_t same_keys_num = _sort();
682
64.6k
    if (_keys_type == KeysType::DUP_KEYS || same_keys_num == 0) {
683
62.7k
        if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) {
684
17
            _output_mutable_block.swap(_input_mutable_block);
685
62.7k
        } else {
686
62.7k
            vectorized::Block in_block = _input_mutable_block.to_block();
687
62.7k
            RETURN_IF_ERROR(_put_into_output(in_block));
688
62.7k
        }
689
62.7k
    } else {
690
1.93k
        (_skip_bitmap_col_idx == -1) ? _aggregate<true, false>() : _aggregate<true, true>();
691
1.93k
    }
692
64.6k
    if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow &&
693
64.6k
        !_tablet_schema->cluster_key_uids().empty()) {
694
1.55k
        if (_partial_update_mode != UniqueKeyUpdateModePB::UPSERT) {
695
0
            return Status::InternalError(
696
0
                    "Partial update for mow with cluster keys is not supported");
697
0
        }
698
1.55k
        RETURN_IF_ERROR(_sort_by_cluster_keys());
699
1.55k
    }
700
64.6k
    _input_mutable_block.clear();
701
64.6k
    *res = vectorized::Block::create_unique(_output_mutable_block.to_block());
702
64.6k
    return Status::OK();
703
64.6k
}
704
705
64.6k
Status MemTable::to_block(std::unique_ptr<vectorized::Block>* res) {
706
64.6k
    RETURN_IF_ERROR_OR_CATCH_EXCEPTION(_to_block(res));
707
64.7k
    return Status::OK();
708
64.6k
}
709
710
#include "common/compile_check_end.h"
711
} // namespace doris