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