Skip to content

Commit 977eaa7

Browse files
Userwhitegavinchou
andauthored
[fix](row binlog): op should be insert but update when insert after delete (#66046)
### What problem does this PR solve? Issue Number: close #65808 The main reason for the OP error is that the data has already been deleted, but in Doris, deletion may only set the delete sign to 1, so the data can still be queried when retrieving historical data. Therefore, we need to proactively detect this situation. --------- Co-authored-by: Gavin Chou <[email protected]>
1 parent e10a674 commit 977eaa7

11 files changed

Lines changed: 314 additions & 44 deletions

‎be/src/storage/partial_update_info.cpp‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,36 @@ Status FixedReadPlan::read_columns_by_plan(
384384
return Status::OK();
385385
}
386386

387+
Status FixedReadPlan::fill_old_delete_signs(const Block& old_value_block,
388+
const std::map<uint32_t, uint32_t>& read_index,
389+
size_t num_rows,
390+
std::vector<signed char>* old_delete_signs) const {
391+
if (old_delete_signs == nullptr) {
392+
return Status::OK();
393+
}
394+
const auto* old_delete_sign_column_data =
395+
BaseTablet::get_delete_sign_column_data(old_value_block);
396+
if (old_delete_sign_column_data == nullptr) {
397+
return Status::InternalError("old delete signs column not found, block: {}",
398+
old_value_block.dump_structure());
399+
}
400+
old_delete_signs->assign(num_rows, 0);
401+
for (size_t idx = 0; idx < num_rows; ++idx) {
402+
auto it = read_index.find(cast_set<uint32_t>(idx));
403+
if (it != read_index.end()) {
404+
(*old_delete_signs)[idx] = old_delete_sign_column_data[it->second];
405+
}
406+
}
407+
return Status::OK();
408+
}
409+
387410
Status FixedReadPlan::fill_missing_columns(
388411
const segment_v2::HistoricalRowRetrieverContext& historical_context,
389412
const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset,
390413
const TabletSchema& tablet_schema, Block& full_block,
391414
const std::vector<bool>& use_default_or_null_flag, bool has_default_or_nullable,
392-
uint32_t segment_start_pos, const Block* block) const {
415+
uint32_t segment_start_pos, const Block* block,
416+
std::vector<signed char>* old_delete_signs) const {
393417
auto mutable_full_columns_guard = full_block.mutate_columns_scoped();
394418
auto& mutable_full_columns = mutable_full_columns_guard.mutable_columns();
395419
// create old value columns
@@ -413,11 +437,14 @@ Status FixedReadPlan::fill_missing_columns(
413437
RETURN_IF_ERROR(read_columns_by_plan(tablet_schema, missing_cids, rsid_to_rowset,
414438
old_value_block, &read_index, true, nullptr));
415439

416-
const auto* old_delete_signs = BaseTablet::get_delete_sign_column_data(old_value_block);
417-
if (old_delete_signs == nullptr) {
440+
const auto* old_delete_sign_column_data =
441+
BaseTablet::get_delete_sign_column_data(old_value_block);
442+
if (old_delete_sign_column_data == nullptr) {
418443
return Status::InternalError("old delete signs column not found, block: {}",
419444
old_value_block.dump_structure());
420445
}
446+
RETURN_IF_ERROR(fill_old_delete_signs(old_value_block, read_index,
447+
use_default_or_null_flag.size(), old_delete_signs));
421448
// build default value columns
422449
auto default_value_block = old_value_block.clone_empty();
423450
RETURN_IF_ERROR(BaseTablet::generate_default_value_block(tablet_schema, missing_cids,
@@ -439,8 +466,7 @@ Status FixedReadPlan::fill_missing_columns(
439466

440467
bool should_use_default = use_default_or_null_flag[idx];
441468
if (!should_use_default) {
442-
bool old_row_delete_sign =
443-
(old_delete_signs != nullptr && old_delete_signs[pos_in_old_block] != 0);
469+
bool old_row_delete_sign = old_delete_sign_column_data[pos_in_old_block] != 0;
444470
if (old_row_delete_sign) {
445471
if (!tablet_schema.has_sequence_col()) {
446472
should_use_default = true;

‎be/src/storage/partial_update_info.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ class FixedReadPlan {
135135
const TabletSchema& tablet_schema, Block& full_block,
136136
const std::vector<bool>& use_default_or_null_flag,
137137
bool has_default_or_nullable, uint32_t segment_start_pos,
138-
const Block* block) const;
138+
const Block* block,
139+
std::vector<signed char>* old_delete_signs = nullptr) const;
140+
Status fill_old_delete_signs(const Block& old_value_block,
141+
const std::map<uint32_t, uint32_t>& read_index, size_t num_rows,
142+
std::vector<signed char>* old_delete_signs) const;
139143

140144
private:
141145
std::map<RowsetId, std::map<uint32_t /* segment_id */, std::vector<RidAndPos>>> plan;

‎be/src/storage/segment/historical_row_retriever.cpp‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ Status PrimaryKeyModelRowRetriever::retrieve_historical_row(const Int8* delete_s
8686
auto* tablet = static_cast<Tablet*>(_context.tablet.get());
8787
auto& tablet_schema = _context.tablet_schema;
8888

89-
DCHECK(_context.partial_update_info);
90-
9189
std::vector<RowsetSharedPtr> specified_rowsets;
9290
{
9391
std::shared_lock rlock(_context.tablet->get_header_lock());
@@ -169,7 +167,7 @@ Status PrimaryKeyModelRowRetriever::build_after_block(Block* block, size_t row_p
169167
}
170168
return _rssid_to_rid.fill_missing_columns(
171169
_context, _rsid_to_rowset, *_context.tablet_schema, *block, _use_default_or_null_flag,
172-
_has_default_or_nullable, cast_set<uint32_t>(row_pos), block);
170+
_has_default_or_nullable, cast_set<uint32_t>(row_pos), block, &_old_delete_signs);
173171
}
174172

175173
Status PrimaryKeyModelRowRetriever::build_before_block(Block* before_block,
@@ -193,17 +191,18 @@ Status PrimaryKeyModelRowRetriever::build_before_block(Block* before_block,
193191
// key: logical row index in current batch; value: index in old_value_block
194192
std::map<uint32_t, uint32_t> read_index;
195193
RETURN_IF_ERROR(_rssid_to_rid.read_columns_by_plan(*tablet_schema, value_cids, _rsid_to_rowset,
196-
old_value_block, &read_index, false,
194+
old_value_block, &read_index, true,
197195
nullptr));
196+
RETURN_IF_ERROR(_fill_old_delete_signs(old_value_block, read_index, num_rows));
198197

199198
{
200199
auto mutable_before_columns_guard = before_block->mutate_columns_scoped();
201200
auto& mutable_before_columns = mutable_before_columns_guard.mutable_columns();
202201
// Fill each row in before_block.
203202
for (uint32_t idx = 0; idx < num_rows; ++idx) {
204203
auto it = read_index.find(idx);
205-
if (it == read_index.end()) {
206-
// No historical row, fill BEFORE with NULL.
204+
if (it == read_index.end() || _old_delete_signs[idx] != 0) {
205+
// No live historical row, fill BEFORE with NULL.
207206
for (size_t i = 0; i < value_cids.size(); ++i) {
208207
auto* nullable_column =
209208
assert_cast<ColumnNullable*>(mutable_before_columns[i].get());
@@ -223,6 +222,38 @@ Status PrimaryKeyModelRowRetriever::build_before_block(Block* before_block,
223222
return Status::OK();
224223
}
225224

225+
Status PrimaryKeyModelRowRetriever::revise_operators_by_old_delete_sign(size_t num_rows) {
226+
if (_operators.empty() || _rssid_to_rid.empty()) {
227+
return Status::OK();
228+
}
229+
DCHECK_EQ(_operators.size(), num_rows);
230+
231+
if (_old_delete_signs.empty()) {
232+
// If no BEFORE/missing column was read, read only old delete signs here.
233+
Block old_delete_sign_block;
234+
std::map<uint32_t, uint32_t> read_index;
235+
RETURN_IF_ERROR(_rssid_to_rid.read_columns_by_plan(
236+
*_context.tablet_schema, std::vector<uint32_t> {}, _rsid_to_rowset,
237+
old_delete_sign_block, &read_index, true, nullptr));
238+
RETURN_IF_ERROR(_fill_old_delete_signs(old_delete_sign_block, read_index, num_rows));
239+
}
240+
DCHECK_EQ(_old_delete_signs.size(), num_rows);
241+
242+
for (size_t idx = 0; idx < num_rows; ++idx) {
243+
if (_operators[idx] == ROW_BINLOG_UPDATE && _old_delete_signs[idx] != 0) {
244+
_operators[idx] = ROW_BINLOG_APPEND;
245+
}
246+
}
247+
return Status::OK();
248+
}
249+
250+
Status PrimaryKeyModelRowRetriever::_fill_old_delete_signs(
251+
const Block& old_value_block, const std::map<uint32_t, uint32_t>& read_index,
252+
size_t num_rows) {
253+
return _rssid_to_rid.fill_old_delete_signs(old_value_block, read_index, num_rows,
254+
&_old_delete_signs);
255+
}
256+
226257
std::string PrimaryKeyModelRowRetriever::_full_encode_keys(
227258
const std::vector<IOlapColumnDataAccessor*>& key_columns, size_t pos, bool null_first) {
228259
return _full_encode_keys(_key_coders, key_columns, pos, null_first);

‎be/src/storage/segment/historical_row_retriever.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class PrimaryKeyModelRowRetriever : public HistoricalRowRetriever {
8282
Status build_before_block(Block* before_block, const std::vector<uint32_t>& value_cids,
8383
size_t /*row_pos*/, size_t num_rows) override;
8484

85+
Status revise_operators_by_old_delete_sign(size_t num_rows);
86+
8587
void clear() override {
8688
_key_columns.clear();
8789
_seq_column = nullptr;
@@ -90,11 +92,15 @@ class PrimaryKeyModelRowRetriever : public HistoricalRowRetriever {
9092
_rssid_to_rid.clear();
9193
_rsid_to_rowset.clear();
9294
_operators.clear();
95+
_old_delete_signs.clear();
9396
}
9497

9598
std::vector<int64_t>& get_operators() override { return _operators; };
9699

97100
private:
101+
Status _fill_old_delete_signs(const Block& old_value_block,
102+
const std::map<uint32_t, uint32_t>& read_index, size_t num_rows);
103+
98104
void _maybe_invalid_row_cache(const std::string& key);
99105

100106
// used for unique-key with merge on write and segment min_max key
@@ -128,6 +134,8 @@ class PrimaryKeyModelRowRetriever : public HistoricalRowRetriever {
128134

129135
// cache operator for fill_binlog_columns
130136
std::vector<int64_t> _operators;
137+
// Aligned by row position in the current append batch.
138+
std::vector<signed char> _old_delete_signs;
131139
};
132140

133141
} // namespace segment_v2

‎be/src/storage/segment/row_binlog_segment_writer.cpp‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ Status RowBinlogSegmentWriter::append_block(const Block* block, size_t row_pos,
194194
// We read historical rows only when we really need them:
195195
// 1. partial update: build the full AFTER row.
196196
// 2. write_before: fill __BEFORE__* columns.
197-
// Otherwise we do not compare with old rows here, so row binlog op only
198-
// keeps the simple meaning: append for non-delete rows, delete for delete rows.
197+
// 3. partial update / write_before need the old delete sign to identify INSERT -> DELETE
198+
// -> INSERT correctly; otherwise the last INSERT may be treated as UPDATE because lookup
199+
// hits the tombstone row.
199200
if (is_partial_update || _write_before) {
200201
auto* pk_retriever =
201202
dynamic_cast<PrimaryKeyModelRowRetriever*>(_historical_data_writer.get());
@@ -263,15 +264,22 @@ Status RowBinlogSegmentWriter::append_block(const Block* block, size_t row_pos,
263264
}
264265
}
265266

267+
if (_write_before) {
268+
RETURN_IF_ERROR(_fill_before_columns(num_rows));
269+
}
270+
271+
if (is_partial_update || _write_before) {
272+
auto* pk_retriever =
273+
dynamic_cast<PrimaryKeyModelRowRetriever*>(_historical_data_writer.get());
274+
DCHECK(pk_retriever != nullptr);
275+
RETURN_IF_ERROR(pk_retriever->revise_operators_by_old_delete_sign(num_rows));
276+
}
277+
266278
RETURN_IF_ERROR(_fill_binlog_columns(num_rows, operators));
267279

268280
// row-binlog key don't need seq column
269281
RETURN_IF_ERROR(build_key_index(_converted_key_columns, nullptr, num_rows));
270282

271-
if (_write_before) {
272-
RETURN_IF_ERROR(_fill_before_columns(num_rows));
273-
}
274-
275283
_num_rows_written += num_rows;
276284
// need to clean olap_data_convertor that be used when fill binlog columns and build key index
277285
_olap_data_convertor->clear_source_content();
@@ -334,7 +342,6 @@ Status RowBinlogSegmentWriter::_fill_binlog_columns(size_t num_rows,
334342
assert_cast<ColumnInt64*>(lsn_col_ptr)->insert_value(_lsn_ids->at(i));
335343
}
336344

337-
// wrong op only happens when partial-update, it will be fixed by delete bitmap when publish
338345
const FieldType op_col_type = _tablet_schema->column(binlog_cids[2]).type();
339346
IColumn* op_col_ptr = binlog_prefix_columns[2].get();
340347
auto* op_nullable_column = check_and_get_column<ColumnNullable>(op_col_ptr);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 "storage/segment/historical_row_retriever.h"
19+
20+
#include <gtest/gtest.h>
21+
22+
#include <map>
23+
#include <memory>
24+
#include <vector>
25+
26+
#include "core/column/column_vector.h"
27+
#include "core/data_type/data_type_number.h"
28+
#include "storage/binlog.h"
29+
#include "storage/olap_common.h"
30+
#include "storage/utils.h"
31+
32+
namespace doris::segment_v2 {
33+
34+
TEST(HistoricalRowRetrieverTest, ReviseOperatorWithOldDeleteSign) {
35+
auto delete_sign_column = ColumnInt8::create();
36+
delete_sign_column->insert_value(1);
37+
delete_sign_column->insert_value(0);
38+
delete_sign_column->insert_value(1);
39+
40+
Block old_value_block;
41+
old_value_block.insert(
42+
{std::move(delete_sign_column), std::make_shared<DataTypeInt8>(), DELETE_SIGN});
43+
44+
std::map<uint32_t, uint32_t> read_index {
45+
{0, 0},
46+
{1, 1},
47+
{3, 2},
48+
};
49+
50+
PrimaryKeyModelRowRetriever retriever;
51+
ASSERT_TRUE(retriever._fill_old_delete_signs(old_value_block, read_index, 4).ok());
52+
ASSERT_EQ((std::vector<signed char> {1, 0, 0, 1}), retriever._old_delete_signs);
53+
54+
retriever._operators = {ROW_BINLOG_UPDATE, ROW_BINLOG_UPDATE, ROW_BINLOG_UPDATE,
55+
ROW_BINLOG_DELETE};
56+
RowsetId rowset_id;
57+
rowset_id.init(1);
58+
retriever._rssid_to_rid.prepare_to_read(RowLocation(rowset_id, 0, 0), 0);
59+
ASSERT_TRUE(retriever.revise_operators_by_old_delete_sign(4).ok());
60+
61+
EXPECT_EQ(ROW_BINLOG_APPEND, retriever._operators[0]);
62+
EXPECT_EQ(ROW_BINLOG_UPDATE, retriever._operators[1]);
63+
EXPECT_EQ(ROW_BINLOG_UPDATE, retriever._operators[2]);
64+
EXPECT_EQ(ROW_BINLOG_DELETE, retriever._operators[3]);
65+
}
66+
67+
} // namespace doris::segment_v2

‎regression-test/data/row_binlog_p0/test_binlog_changes_syntax.out‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
-- !mow_append_range --
6767
2 20 b 0
68+
2 21 b1 0
6869
4 40 d 0
6970
5 50 e 0
7071

@@ -82,10 +83,9 @@
8283
1 12 a2 2
8384
1 12 a2 3
8485
1 13 a3 3
85-
2 \N \N 2
8686
2 20 b 0
8787
2 20 b 1
88-
2 21 b1 3
88+
2 21 b1 0
8989
3 30 c 1
9090
4 40 d 0
9191
5 50 e 0
@@ -98,10 +98,9 @@
9898
1 12 2
9999
1 12 3
100100
1 13 3
101-
2 \N 2
102101
2 20 0
103102
2 20 1
104-
2 21 3
103+
2 21 0
105104
3 30 1
106105
4 40 0
107106
5 50 0
@@ -116,10 +115,9 @@
116115
1 12 2
117116
1 12 3
118117
1 13 3
119-
2 \N 2
120118
2 20 0
121119
2 20 1
122-
2 21 3
120+
2 21 0
123121
3 30 0
124122
3 30 1
125123
4 40 0
@@ -134,10 +132,9 @@
134132
1 12 2
135133
1 12 3
136134
1 13 3
137-
2 \N 2
138135
2 20 0
139136
2 20 1
140-
2 21 3
137+
2 21 0
141138
3 30 0
142139
3 30 1
143140
4 40 0
@@ -167,10 +164,9 @@
167164
1 12 2
168165
1 12 3
169166
1 13 3
170-
2 \N 2
171167
2 20 0
172168
2 20 1
173-
2 21 3
169+
2 21 0
174170
3 30 0
175171
3 30 1
176172
4 40 0

‎regression-test/data/row_binlog_p0/test_row_binlog_basic.out‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
1 2 2 2 2000 201 2000 200
4040
2 1 1 1 10 10 10 10
4141

42+
-- !mow_reinsert_before_binlog --
43+
0 1 10 \N
44+
2 1 10 10
45+
0 1 10 \N
46+
47+
-- !mow_reinsert_binlog --
48+
0 2 20 partial
49+
2 2 \N default
50+
0 2 20 default
51+
4252
-- !mow_seq_raw --
4353

4454
-- !mow_seq_binlog --
@@ -55,4 +65,5 @@
5565
1 1 1 1 300 350 300 300
5666
1 1 1 1 400 350 300 350
5767
1 1 1 1 400 360 400 350
58-
2 1 1 1 400 350 400 350
68+
2 1 1 1 400 350 400 350
69+

0 commit comments

Comments
 (0)