Skip to content

Commit 40762c7

Browse files
morningmanclaude
andcommitted
[fix](binlog) Fix BE crash when APPEND_ONLY row-binlog scan omits key columns
Any row-binlog scan used to force a key-ordered merged read with read_orderby_key_num_prefix_columns == 0, which makes the merge comparators (VMergeIteratorContext::compare and VCollectIterator's LevelIteratorComparator) fall back to comparing the first num_key_columns block positions, where num_key_columns counts the key columns of the whole tablet schema. MIN_DELTA / DETAIL scans satisfy that contract because they widen the storage projection with the full key prefix. APPEND_ONLY, however, takes the direct_mode branch and keeps the raw SQL projection, which may omit some or all key columns. Block::compare_at() only has DCHECK bounds checks, so in a release build the first heap comparison reads past the block's column array and crashes the BE with SIGSEGV inside VMergeIterator::init / std::push_heap. Fix: - olap_scanner: only MIN_DELTA / DETAIL force the key-ordered merged read. APPEND_ONLY does no key grouping, so it now reads unordered like a plain scan (ORDER BY / TopN pushdowns stay disabled for all binlog scan types as before). - VMergeIteratorContext::init and VCollectIterator::Level1Iterator::init now validate the merge compare contract (compare positions exist in the block, and the projection starts with the full ordered key prefix for the default comparison) and return a diagnosable error instead of performing an out-of-bounds or silently misordered comparison. - Add BE unit tests for the rejected projections and a regression suite covering APPEND_ONLY / DETAIL / MIN_DELTA with narrow projections. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 53d6fa3 commit 40762c7

7 files changed

Lines changed: 379 additions & 12 deletions

File tree

‎be/src/exec/scan/olap_scanner.cpp‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,14 @@ Status OlapScanner::_init_tablet_reader_params(
476476
}
477477
};
478478

479-
// For row-binlog scans that emit BEFORE/AFTER pairs (MIN_DELTA / DETAIL), we must read
480-
// every key column, every requested value column, the binlog meta columns (tso / op)
481-
// and their __BEFORE__ mirrors, so the BlockReader can reconstruct change rows.
482-
const bool need_before_columns =
479+
// MIN_DELTA / DETAIL row-binlog scans reconstruct change rows in BlockReader through a
480+
// key-ordered merge. They must read every key column, every requested value column, the
481+
// binlog meta columns (tso / op) and their __BEFORE__ mirrors. APPEND_ONLY streams rows
482+
// as-is and stays on the plain projection paths below.
483+
const bool is_binlog_merge_scan =
483484
_tablet_reader_params.binlog_scan_type == TBinlogScanType::MIN_DELTA ||
484485
_tablet_reader_params.binlog_scan_type == TBinlogScanType::DETAIL;
485-
if (need_before_columns) {
486+
if (is_binlog_merge_scan) {
486487
for (size_t i = 0; i < tablet_schema->num_key_columns(); ++i) {
487488
add_return_column_if_absent(static_cast<uint32_t>(i));
488489
}
@@ -565,16 +566,26 @@ Status OlapScanner::_init_tablet_reader_params(
565566

566567
RETURN_IF_ERROR(_init_tso_pushdown());
567568

568-
// For any row-binlog scan, force the storage layer to deliver rows strictly in primary-key
569-
// order so the BlockReader can group consecutive same-key changes (MIN_DELTA) or emit
570-
// BEFORE/AFTER pairs in deterministic order (DETAIL). Disable ORDER BY / TopN pushdowns
571-
// and reset their related params, since they would otherwise re-order the stream.
569+
// Row-binlog scans must not be re-ordered or truncated by ORDER BY / TopN pushdowns,
570+
// so reset every reorder-related param for all binlog scan types.
571+
//
572+
// Only MIN_DELTA / DETAIL additionally force the storage layer to deliver rows strictly
573+
// in primary-key order, so the BlockReader can group consecutive same-key changes
574+
// (MIN_DELTA) or emit BEFORE/AFTER pairs in deterministic order (DETAIL). Their storage
575+
// projection is widened above with the full key prefix, which the key-ordered merge
576+
// comparator relies on: with read_orderby_key_num_prefix_columns == 0 the comparator
577+
// falls back to comparing the first num_key_columns block positions.
578+
//
579+
// APPEND_ONLY does no key grouping and keeps the raw SQL projection, which may omit
580+
// some or even all key columns. Forcing a key-ordered merge would make the fallback
581+
// comparator read key positions that do not exist in the projected blocks and crash
582+
// the BE (issue #66390), so it reads unordered like a plain scan.
572583
if (_tablet_reader_params.binlog_scan_type != TBinlogScanType::NONE) {
573-
_tablet_reader_params.read_orderby_key = true;
584+
_tablet_reader_params.read_orderby_key = is_binlog_merge_scan;
585+
_tablet_reader_params.force_key_ordered_read = is_binlog_merge_scan;
574586
_tablet_reader_params.read_orderby_key_reverse = false;
575587
_tablet_reader_params.read_orderby_key_num_prefix_columns = 0;
576588
_tablet_reader_params.read_orderby_key_limit = 0;
577-
_tablet_reader_params.force_key_ordered_read = true;
578589
_tablet_reader_params.topn_filter_source_node_ids.clear();
579590
}
580591

‎be/src/storage/iterator/vcollect_iterator.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ Status VCollectIterator::Level1Iterator::init(bool get_data_by_ref) {
709709
}
710710
}
711711

712+
RETURN_IF_ERROR(_validate_merge_compare_contract(sequence_loc));
713+
712714
_heap = std::make_unique<MergeHeap>(LevelIteratorComparator(
713715
sequence_loc, _is_reverse, _reader->_reader_context.use_insert_order_when_same,
714716
tso_col_id >= 0));
@@ -766,6 +768,61 @@ void VCollectIterator::Level1Iterator::init_level0_iterators_for_union() {
766768
}
767769
}
768770

771+
// LevelIteratorComparator reads block positions that carry no bounds checks in release
772+
// builds: either the explicit compare-column positions, or positions
773+
// [0, tablet_schema().num_key_columns()) plus the sequence tie-break position. A read
774+
// projection that omits or reorders the leading key columns would turn the first heap
775+
// comparison into an out-of-bounds or semantically wrong positional access (issue #66390).
776+
// Validate the contract against every child's first block before anything enters _heap.
777+
Status VCollectIterator::Level1Iterator::_validate_merge_compare_contract(int sequence_loc) const {
778+
const auto& return_columns = _reader->_return_columns;
779+
const size_t num_key_columns = _schema.num_key_columns();
780+
for (const auto& child : _children) {
781+
const IteratorRowRef* ref = child->current_row_ref();
782+
if (ref->block == nullptr) {
783+
continue;
784+
}
785+
const size_t block_columns = ref->block->columns();
786+
auto contract_error = [&](const std::string& detail) {
787+
std::string projected_ids;
788+
for (auto cid : return_columns) {
789+
if (!projected_ids.empty()) {
790+
projected_ids += ',';
791+
}
792+
projected_ids += std::to_string(cid);
793+
}
794+
return Status::InternalError(
795+
"merge heap compare contract violated: {}, tablet_id={}, block_columns={}, "
796+
"num_key_columns={}, sequence_loc={}, return_columns=[{}]",
797+
detail, _reader->_tablet->tablet_id(), block_columns, num_key_columns,
798+
sequence_loc, projected_ids);
799+
};
800+
if (_compare_columns != nullptr) {
801+
for (uint32_t pos : *_compare_columns) {
802+
if (pos >= block_columns) {
803+
return contract_error(
804+
fmt::format("compare column position {} out of range", pos));
805+
}
806+
}
807+
} else {
808+
if (num_key_columns > return_columns.size() || num_key_columns > block_columns) {
809+
return contract_error("projection has fewer columns than the key prefix");
810+
}
811+
for (size_t i = 0; i < num_key_columns; ++i) {
812+
if (return_columns[i] != i) {
813+
return contract_error(
814+
fmt::format("position {} holds column id {} instead of key column {}",
815+
i, return_columns[i], i));
816+
}
817+
}
818+
}
819+
if (sequence_loc != -1 && static_cast<size_t>(sequence_loc) >= block_columns) {
820+
return contract_error("sequence column position out of range");
821+
}
822+
}
823+
return Status::OK();
824+
}
825+
769826
Status VCollectIterator::Level1Iterator::_merge_next(IteratorRowRef* ref) {
770827
auto res = _cur_child->next(ref);
771828
if (LIKELY(res.ok())) {

‎be/src/storage/iterator/vcollect_iterator.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ class VCollectIterator {
324324
bool collected_enough_rows(const MutableColumns& columns, int rows_to_merge) const;
325325

326326
private:
327+
// Validate that every block position LevelIteratorComparator may touch exists in
328+
// each child's current block and, for the default key-prefix comparison, that the
329+
// read projection really starts with the full ordered key prefix. Called before
330+
// any child is pushed into _heap, so a broken projection surfaces as an error
331+
// instead of an out-of-bounds positional access (issue #66390).
332+
Status _validate_merge_compare_contract(int sequence_loc) const;
333+
327334
Status _merge_next(IteratorRowRef* ref);
328335

329336
Status _normal_next(IteratorRowRef* ref);

‎be/src/storage/iterator/vgeneric_iterators.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,66 @@ Status VMergeIteratorContext::init(const StorageReadOptions& opts) {
296296
_record_rowids = opts.record_rowids;
297297
RETURN_IF_ERROR(_load_next_block());
298298
if (valid()) {
299+
RETURN_IF_ERROR(_validate_compare_contract(opts));
299300
RETURN_IF_ERROR(advance());
300301
}
301302
_pre_ctx_same_bit.reserve(_block_row_max);
302303
_pre_ctx_same_bit.assign(_block_row_max, false);
303304
return Status::OK();
304305
}
305306

307+
// compare() reads block positions that are only DCHECK-bounds-checked in Block::compare_at(),
308+
// so in a release build a projection violating the merge contract turns into an out-of-bounds
309+
// read inside std::push_heap and kills the BE (issue #66390). Verify the contract once the
310+
// first block is loaded and surface a diagnosable error instead:
311+
// - explicit compare columns (_compare_columns) must all point inside the block;
312+
// - otherwise the default comparison touches positions [0, _num_key_columns), where
313+
// _num_key_columns counts the key columns of the WHOLE tablet schema. Key columns always
314+
// occupy column ids [0, num_key_columns) of the tablet schema, so the projection must
315+
// start with exactly those ids, in order, for the positional comparison to be key order;
316+
// - the sequence tie-break column, when present, must point inside the block as well.
317+
Status VMergeIteratorContext::_validate_compare_contract(const StorageReadOptions& opts) const {
318+
const size_t block_columns = _block->columns();
319+
auto contract_error = [&](const std::string& detail) {
320+
std::string projected_ids;
321+
for (auto cid : _output_schema->column_ids()) {
322+
if (!projected_ids.empty()) {
323+
projected_ids += ',';
324+
}
325+
projected_ids += std::to_string(cid);
326+
}
327+
return Status::InternalError(
328+
"merge iterator compare contract violated: {}, tablet_id={}, rowset_id={}, "
329+
"version={}, block_columns={}, num_key_columns={}, sequence_id_idx={}, "
330+
"projected_column_ids=[{}]",
331+
detail, opts.tablet_id, opts.rowset_id.to_string(), opts.version.to_string(),
332+
block_columns, _num_key_columns, _sequence_id_idx, projected_ids);
333+
};
334+
if (_compare_columns != nullptr) {
335+
for (uint32_t pos : *_compare_columns) {
336+
if (pos >= block_columns) {
337+
return contract_error(fmt::format("compare column position {} out of range", pos));
338+
}
339+
}
340+
} else {
341+
const auto num_key_columns = static_cast<size_t>(_num_key_columns);
342+
if (num_key_columns > _output_schema->num_column_ids() || num_key_columns > block_columns) {
343+
return contract_error("projection has fewer columns than the key prefix");
344+
}
345+
for (size_t i = 0; i < num_key_columns; ++i) {
346+
if (_output_schema->column_ids()[i] != static_cast<ColumnId>(i)) {
347+
return contract_error(
348+
fmt::format("position {} holds column id {} instead of key column {}", i,
349+
_output_schema->column_ids()[i], i));
350+
}
351+
}
352+
}
353+
if (_sequence_id_idx != -1 && static_cast<size_t>(_sequence_id_idx) >= block_columns) {
354+
return contract_error("sequence column position out of range");
355+
}
356+
return Status::OK();
357+
}
358+
306359
Status VMergeIteratorContext::advance() {
307360
_skip = false;
308361
_same = false;

‎be/src/storage/iterator/vgeneric_iterators.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ class VMergeIteratorContext {
185185
// Load next block into _block
186186
Status _load_next_block();
187187

188+
// Validate that every block position compare() may touch actually exists in _block
189+
// and, for the default key-prefix comparison, that the projection really starts with
190+
// the full ordered key prefix. Returns an error instead of letting compare() perform
191+
// an out-of-bounds or semantically wrong positional access (issue #66390).
192+
Status _validate_compare_contract(const StorageReadOptions& opts) const;
193+
188194
RowwiseIteratorUPtr _iter;
189195

190196
int _sequence_id_idx = -1;

‎be/test/exec/scan/vgeneric_iterators_test.cpp‎

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class VGenericIteratorsTest : public testing::Test {
4242
virtual ~VGenericIteratorsTest() {}
4343
};
4444

45-
static Schema create_schema() {
45+
static std::vector<TabletColumnPtr> create_col_schemas() {
4646
std::vector<TabletColumnPtr> col_schemas;
4747
auto c1 = std::make_shared<TabletColumn>(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE,
4848
FieldType::OLAP_FIELD_TYPE_SMALLINT, true);
@@ -57,6 +57,11 @@ static Schema create_schema() {
5757
col_schemas.emplace_back(
5858
std::make_shared<TabletColumn>(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM,
5959
FieldType::OLAP_FIELD_TYPE_BIGINT, true));
60+
return col_schemas;
61+
}
62+
63+
static Schema create_schema() {
64+
std::vector<TabletColumnPtr> col_schemas = create_col_schemas();
6065

6166
std::vector<ColumnId> column_ids(col_schemas.size());
6267
for (uint32_t cid = 0; cid < column_ids.size(); ++cid) {
@@ -402,4 +407,100 @@ TEST(VGenericIteratorsTest, MergeWithSeqColumnSmallSeqFirst) {
402407
EXPECT_EQ(0, actual_value);
403408
}
404409

410+
// Emits num_rows rows for a schema whose projection (col_ids) may be narrower than the
411+
// full tablet schema. The filled block layout follows the projection, matching how
412+
// VMergeIteratorContext::block_reset builds its block from the output schema.
413+
class ProjectedColumnsUtIterator : public RowwiseIterator {
414+
public:
415+
ProjectedColumnsUtIterator(Schema schema, size_t num_rows)
416+
: _schema(std::move(schema)), _num_rows(num_rows) {}
417+
~ProjectedColumnsUtIterator() override = default;
418+
419+
Status init(const StorageReadOptions& opts) override { return Status::OK(); }
420+
421+
Status next_batch(Block* block) override {
422+
if (_rows_returned >= _num_rows) {
423+
return Status::EndOfFile("End of ProjectedColumnsUtIterator");
424+
}
425+
while (_rows_returned < _num_rows) {
426+
for (size_t j = 0; j < _schema.num_column_ids(); ++j) {
427+
ColumnWithTypeAndName& vc = block->get_by_position(j);
428+
IColumn& vi = (IColumn&)(*vc.column);
429+
430+
char data[16] = {};
431+
size_t data_len = 0;
432+
const auto* col_schema = _schema.column(_schema.column_id(j));
433+
switch (col_schema->type()) {
434+
case FieldType::OLAP_FIELD_TYPE_SMALLINT:
435+
*(int16_t*)data = static_cast<int16_t>(_rows_returned);
436+
data_len = sizeof(int16_t);
437+
break;
438+
case FieldType::OLAP_FIELD_TYPE_INT:
439+
*(int32_t*)data = static_cast<int32_t>(_rows_returned);
440+
data_len = sizeof(int32_t);
441+
break;
442+
case FieldType::OLAP_FIELD_TYPE_BIGINT:
443+
*(int64_t*)data = static_cast<int64_t>(_rows_returned);
444+
data_len = sizeof(int64_t);
445+
break;
446+
default:
447+
break;
448+
}
449+
450+
vi.insert_data(data, data_len);
451+
}
452+
++_rows_returned;
453+
}
454+
return Status::OK();
455+
}
456+
457+
const Schema& schema() const override { return _schema; }
458+
459+
private:
460+
Schema _schema;
461+
size_t _num_rows;
462+
size_t _rows_returned = 0;
463+
};
464+
465+
// The merge-heap comparator compares the first num_key_columns block positions when no
466+
// explicit compare columns are given, and num_key_columns counts the key columns of the
467+
// WHOLE tablet schema. A projection narrower than the key prefix must be rejected at
468+
// init time with an error instead of crashing inside std::push_heap (issue #66390: a
469+
// ROW-binlog APPEND_ONLY scan projected value columns only).
470+
TEST(VGenericIteratorsTest, MergeRejectsProjectionMissingKeyPrefix) {
471+
// Full tablet schema: k0(smallint), k1(int), v2(bigint); project only v2.
472+
Schema projected(create_col_schemas(), std::vector<ColumnId> {2});
473+
auto output_schema = std::make_shared<Schema>(projected);
474+
475+
std::vector<RowwiseIteratorUPtr> inputs;
476+
inputs.push_back(std::make_unique<ProjectedColumnsUtIterator>(projected, 10));
477+
inputs.push_back(std::make_unique<ProjectedColumnsUtIterator>(projected, 10));
478+
479+
auto iter = new_merge_iterator(std::move(inputs), -1, false, false, nullptr, output_schema);
480+
StorageReadOptions opts;
481+
auto st = iter->init(opts);
482+
EXPECT_FALSE(st.ok());
483+
EXPECT_TRUE(st.to_string().find("compare contract violated") != std::string::npos)
484+
<< st.to_string();
485+
}
486+
487+
// Same as above, but the projection has enough columns while not starting with the full
488+
// ordered key prefix (k0 is missing): position 0 would be compared as if it were k0.
489+
TEST(VGenericIteratorsTest, MergeRejectsProjectionWithoutLeadingKey) {
490+
// Full tablet schema: k0(smallint), k1(int), v2(bigint); project {k1, v2}.
491+
Schema projected(create_col_schemas(), std::vector<ColumnId> {1, 2});
492+
auto output_schema = std::make_shared<Schema>(projected);
493+
494+
std::vector<RowwiseIteratorUPtr> inputs;
495+
inputs.push_back(std::make_unique<ProjectedColumnsUtIterator>(projected, 10));
496+
inputs.push_back(std::make_unique<ProjectedColumnsUtIterator>(projected, 10));
497+
498+
auto iter = new_merge_iterator(std::move(inputs), -1, false, false, nullptr, output_schema);
499+
StorageReadOptions opts;
500+
auto st = iter->init(opts);
501+
EXPECT_FALSE(st.ok());
502+
EXPECT_TRUE(st.to_string().find("compare contract violated") != std::string::npos)
503+
<< st.to_string();
504+
}
505+
405506
} // namespace doris

0 commit comments

Comments
 (0)