Skip to content

Commit e275f85

Browse files
committed
[fix](be) Leave shared scan LIMIT EOS to the scan operator
### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: Scanner::get_block_after_projects() and Scanner::get_block() turned an exhausted shared scan LIMIT into EOS: a scanner scheduled after its peers had produced the whole LIMIT, or whose own rows exhausted it, reported EOS although its reader still had data. EOS means the underlying read is exhausted, while the LIMIT belongs to the scan operator, which cuts the extra rows and stops the scanners once it holds enough. Deriving EOS from the LIMIT bookkeeping means any mistake there ends a scan early and returns too few rows, instead of, at worst, letting a scanner run until the operator stops it. The scanner now stops reading when the shared LIMIT is exhausted but never reports EOS for it: get_block_after_projects() returns an empty block on entry, the padding loop stops reading and returns the rows it already holds, and the tail only charges the produced rows. The two pre-existing counter checks in get_block() are removed so that the shared LIMIT is handled in one place. The query still finishes as before: ScannerContext::get_block_from_queue() completes the context once the counter is exhausted with nothing in flight, and ScanOperatorX::get_block_impl() stops the scanners after reached_limit(). ### Release note None ### Check List (For Author) - Test: Unit Test: ScannerProjectionTest cases updated to expect no EOS from the shared LIMIT, plus two new cases (own rows exhaust the LIMIT, a peer exhausts it mid-read); ran the scan/workload-group/row-ID UT filter. Regression test: query_p0/limit and correctness_p0/test_shared_scan_limit_pending_tasks on a local cluster with the thread pool scheduler default. - Behavior changed: Yes (scanners no longer report EOS for an exhausted shared scan LIMIT; the scan operator stops them) - Does this need documentation: No
1 parent 1b47b99 commit e275f85

5 files changed

Lines changed: 108 additions & 43 deletions

File tree

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
9494
Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) {
9595
SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block);
9696
// Peer scanners may have exhausted the shared LIMIT while this scanner waited to be
97-
// scheduled. Admission does not look at the LIMIT, so the scanner reports EOS itself with an
98-
// empty block instead of reading on; the operator already has enough rows and cuts the rest.
99-
if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) {
100-
*eos = true;
97+
// scheduled. Admission does not look at the LIMIT, so the scanner returns an empty block
98+
// instead of reading on. It does not report EOS: the operator already holds enough rows and
99+
// stops the scanners after cutting the LIMIT itself.
100+
if (_shared_scan_limit_exhausted()) {
101101
return Status::OK();
102102
}
103103
const auto& row_descriptor = _local_state->_parent->operator_row_desc_before_projection();
@@ -119,8 +119,11 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool
119119
_shared_scan_limit->load(std::memory_order_acquire), 0, min_batch_size));
120120
}
121121
const auto block_max_bytes = state->preferred_block_size_bytes();
122+
// Stop reading as soon as peers exhaust the shared LIMIT; the rows already held are
123+
// still returned below.
122124
while (_padding_block.rows() < min_batch_size &&
123-
_padding_block.bytes() < block_max_bytes && !*eos) {
125+
_padding_block.bytes() < block_max_bytes && !*eos &&
126+
!_shared_scan_limit_exhausted()) {
124127
RETURN_IF_ERROR(get_block(state, &_origin_block, eos));
125128
if (*eos) {
126129
// For the final block, merge any padding directly and return eos in this call.
@@ -157,12 +160,10 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool
157160

158161
// Only rows actually produced by this call consume the shared LIMIT. In particular, padding
159162
// kept for a later call must not make the context finish before that call can return it.
160-
if (_shared_scan_limit) {
161-
if (block->rows() > 0) {
162-
_shared_scan_limit->fetch_sub(cast_set<int64_t>(block->rows()),
163-
std::memory_order_acq_rel);
164-
}
165-
*eos = *eos || _shared_scan_limit->load(std::memory_order_acquire) <= 0;
163+
// Exhausting the counter is not EOS either: the next call returns an empty block, and the
164+
// scan operator stops this scanner once it has cut the LIMIT.
165+
if (_shared_scan_limit != nullptr && block->rows() > 0) {
166+
_shared_scan_limit->fetch_sub(cast_set<int64_t>(block->rows()), std::memory_order_acq_rel);
166167
}
167168
return Status::OK();
168169
}
@@ -171,14 +172,6 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) {
171172
// only empty block should be here
172173
DCHECK(block->rows() == 0);
173174

174-
// Stop early if other scanners have already collected enough rows
175-
// for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn
176-
// path or no LIMIT).
177-
if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) {
178-
*eof = true;
179-
return Status::OK();
180-
}
181-
182175
// scanner running time
183176
SCOPED_RAW_TIMER(&_per_scanner_timer);
184177
int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num;
@@ -224,9 +217,6 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) {
224217
// set eof to true if per scanner limit is reached
225218
// currently for query: ORDER BY key LIMIT n
226219
*eof = *eof || (_limit > 0 && _num_rows_return >= _limit);
227-
// A peer may have returned enough rows while this scanner was reading. The caller can still
228-
// project the current block, and the scan operator makes the final LIMIT cut.
229-
*eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0);
230220

231221
return Status::OK();
232222
}

‎be/src/exec/scan/scanner.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ class Scanner {
140140
return Status::OK();
141141
}
142142

143+
// True once scanners of this operator have produced the whole shared LIMIT. The scanner then
144+
// stops reading but does not report EOS for it: EOS means the underlying read is exhausted,
145+
// while the LIMIT is applied by the scan operator, which stops the scanners once it holds
146+
// enough rows.
147+
bool _shared_scan_limit_exhausted() const {
148+
return _shared_scan_limit != nullptr &&
149+
_shared_scan_limit->load(std::memory_order_acquire) <= 0;
150+
}
151+
143152
// Update the counters before closing this scanner
144153
virtual void _collect_profile_before_close();
145154

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ std::shared_ptr<ScanTask> ScannerContext::_pull_next_scan_task(
866866
if (!_pending_tasks.empty()) {
867867
// Do not submit more pending scanners after the shared LIMIT is exhausted while
868868
// completed or in-flight tasks can still make progress. If neither exists, allow pending
869-
// scanners to be submitted so they can report EOS and wake the pipeline task.
869+
// scanners to be submitted so they can complete without reading and wake the pipeline
870+
// task, which finishes the context once nothing is in flight.
870871
if (_is_shared_scan_limit_exhausted() &&
871872
(_in_flight_tasks_num != 0 || !_completed_tasks.empty())) {
872873
return nullptr;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,10 @@ TEST_F(ScannerContextTest, thread_pool_admission_ignores_shared_limit) {
748748

749749
// The shared LIMIT is exhausted while a task is in flight and a completed result waits for the
750750
// operator. Admission only applies the concurrency limit: the pending scanner is still
751-
// admitted and reports EOS itself in Scanner::get_block_after_projects(). Holding it back
752-
// based on the in-flight or completed tasks is what can leave a context with nothing to
753-
// wake the operator.
751+
// admitted, and Scanner::get_block_after_projects() returns an empty block without reading
752+
// (and without EOS, which the scan operator decides from the LIMIT). Holding it back based on
753+
// the in-flight or completed tasks is what can leave a context with nothing to wake the
754+
// operator.
754755
shared_limit.store(0);
755756
scanner_context->_in_flight_tasks_num = 1;
756757
auto completed_task = std::make_shared<ScanTask>(std::make_shared<ScannerDelegate>(scanner));

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

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <gtest/gtest.h>
2020

2121
#include <atomic>
22+
#include <functional>
2223
#include <latch>
2324
#include <list>
2425
#include <thread>
@@ -63,6 +64,8 @@ class TestScanner final : public Scanner {
6364

6465
void add_block(Block block) { _blocks.push_back(std::move(block)); }
6566
size_t unread_blocks() const { return _blocks.size(); }
67+
// Runs after each source block is handed out, to model a peer scanner acting mid-read.
68+
void set_after_read_hook(std::function<void()> hook) { _after_read_hook = std::move(hook); }
6669

6770
protected:
6871
bool _can_merge_padding_blocks(const Block& /*left*/, const Block& /*right*/) const override {
@@ -77,12 +80,16 @@ class TestScanner final : public Scanner {
7780
*eof = false;
7881
block->swap(_blocks.front());
7982
_blocks.pop_front();
83+
if (_after_read_hook) {
84+
_after_read_hook();
85+
}
8086
return Status::OK();
8187
}
8288

8389
private:
8490
bool _allow_padding = true;
8591
std::list<Block> _blocks;
92+
std::function<void()> _after_read_hook;
8693
};
8794

8895
class FakeTablet final : public BaseTablet {
@@ -741,9 +748,9 @@ TEST_F(ScannerLateArrivalRfTest, olap_scanner_releases_resources_before_open) {
741748
}
742749

743750
// Admission does not look at the shared LIMIT, so a pending scanner is still scheduled after its
744-
// peers have exhausted it. The scanner reports EOS with an empty block on that call instead of
745-
// reading its range.
746-
TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_without_reading) {
751+
// peers have exhausted it. The scanner returns an empty block on that call instead of reading its
752+
// range, and does not report EOS: the scan operator decides that from the LIMIT.
753+
TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_returns_empty_block_without_eos) {
747754
ObjectPool pool;
748755
auto data_type = std::make_shared<DataTypeInt32>();
749756
auto row_descriptor = MockRowDescriptor({data_type}, &pool);
@@ -773,7 +780,7 @@ TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_witho
773780
Block output;
774781
bool eos = false;
775782
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
776-
EXPECT_TRUE(eos);
783+
EXPECT_FALSE(eos);
777784
EXPECT_EQ(output.rows(), 0);
778785
EXPECT_EQ(scanner.unread_blocks(), 2);
779786
EXPECT_EQ(op->_shared_scan_limit.load(), 0);
@@ -943,17 +950,19 @@ TEST(ScannerProjectionTest, shared_limit_counts_only_emitted_rows) {
943950
EXPECT_EQ(peer_output.rows(), 4);
944951
EXPECT_EQ(op->_shared_scan_limit.load(), 1);
945952

946-
// The held row already satisfies the one remaining row, so flush it without reading on.
953+
// The held row already satisfies the one remaining row, so flush it without reading on. The
954+
// reader still has no EOS to report; the operator stops the scanner after cutting the LIMIT.
947955
Block final_output;
948956
ASSERT_TRUE(scanner.get_block_after_projects(&state, &final_output, &eos).ok());
949-
EXPECT_TRUE(eos);
957+
EXPECT_FALSE(eos);
950958
ASSERT_EQ(final_output.rows(), 1);
951959
EXPECT_EQ(final_output.get_by_position(0).column->get_int(0), 0);
952960
EXPECT_EQ(op->_shared_scan_limit.load(), 0);
953961
}
954962

955-
// Padding can be discarded when a peer has already produced the whole LIMIT. It never consumed
956-
// shared credit, so skipping the scanner cannot leave the operator short of rows.
963+
// Padding stays unreturned when a peer has already produced the whole LIMIT. It never consumed
964+
// shared credit, so skipping it cannot leave the operator short of rows, and the scanner leaves
965+
// EOS to the operator's LIMIT cut.
957966
TEST(ScannerProjectionTest, shared_limit_drops_padding_after_peer_satisfies_limit) {
958967
ObjectPool pool;
959968
auto data_type = std::make_shared<DataTypeInt32>();
@@ -994,7 +1003,7 @@ TEST(ScannerProjectionTest, shared_limit_drops_padding_after_peer_satisfies_limi
9941003

9951004
Block skipped_output;
9961005
ASSERT_TRUE(scanner.get_block_after_projects(&state, &skipped_output, &eos).ok());
997-
EXPECT_TRUE(eos);
1006+
EXPECT_FALSE(eos);
9981007
EXPECT_EQ(skipped_output.rows(), 0);
9991008
EXPECT_EQ(op->_shared_scan_limit.load(), 0);
10001009
}
@@ -1028,7 +1037,7 @@ TEST(ScannerProjectionTest, shared_limit_flushes_padding_before_reading_more) {
10281037
Block output;
10291038
bool eos = false;
10301039
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1031-
EXPECT_TRUE(eos);
1040+
EXPECT_FALSE(eos);
10321041
ASSERT_EQ(output.rows(), 1);
10331042
EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0);
10341043
EXPECT_EQ(scanner.unread_blocks(), 1);
@@ -1065,7 +1074,7 @@ TEST(ScannerProjectionTest, shared_limit_flushes_merged_padding_at_limit) {
10651074
Block output;
10661075
bool eos = false;
10671076
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1068-
EXPECT_TRUE(eos);
1077+
EXPECT_FALSE(eos);
10691078
ASSERT_EQ(output.rows(), 2);
10701079
EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0);
10711080
EXPECT_EQ(output.get_by_position(0).column->get_int(1), 1);
@@ -1095,7 +1104,7 @@ TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_projection
10951104
Block output;
10961105
bool eos = false;
10971106
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1098-
EXPECT_TRUE(eos);
1107+
EXPECT_FALSE(eos);
10991108
EXPECT_EQ(output.rows(), 3);
11001109
EXPECT_EQ(scanner.unread_blocks(), 1);
11011110
EXPECT_EQ(op->_shared_scan_limit.load(), -1);
@@ -1127,7 +1136,7 @@ TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_padding) {
11271136
Block output;
11281137
bool eos = false;
11291138
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1130-
EXPECT_TRUE(eos);
1139+
EXPECT_FALSE(eos);
11311140
EXPECT_EQ(output.rows(), 3);
11321141
EXPECT_EQ(scanner.unread_blocks(), 1);
11331142
EXPECT_EQ(op->_shared_scan_limit.load(), -1);
@@ -1212,7 +1221,7 @@ TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_n
12121221
Block output;
12131222
bool eos = false;
12141223
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1215-
EXPECT_TRUE(eos);
1224+
EXPECT_FALSE(eos);
12161225
EXPECT_EQ(output.rows(), 0);
12171226
EXPECT_EQ(op->_shared_scan_limit.load(), -1);
12181227
EXPECT_EQ(scanner.unread_blocks(), 1);
@@ -1242,8 +1251,10 @@ TEST(ScannerProjectionTest, no_shared_limit_without_sql_limit) {
12421251
EXPECT_EQ(scanner._shared_scan_limit, nullptr);
12431252
}
12441253

1245-
// The scanner stops once its rows exhaust the shared LIMIT, even though its reader still has data.
1246-
TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_it) {
1254+
// Once its own rows exhaust the shared LIMIT the scanner stops reading, even though its reader
1255+
// still has data. It does not report EOS for that: the next call returns an empty block and the
1256+
// scan operator stops the scanner after cutting the LIMIT.
1257+
TEST(ScannerProjectionTest, shared_limit_exhausted_by_own_rows_stops_reading_without_eos) {
12471258
ObjectPool pool;
12481259
auto data_type = std::make_shared<DataTypeInt32>();
12491260
auto row_descriptor = MockRowDescriptor({data_type}, &pool);
@@ -1270,9 +1281,62 @@ TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_i
12701281
Block output;
12711282
bool eos = false;
12721283
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1273-
EXPECT_TRUE(eos);
1284+
EXPECT_FALSE(eos);
12741285
EXPECT_EQ(output.rows(), 5);
1286+
EXPECT_EQ(scanner.unread_blocks(), 1);
1287+
EXPECT_EQ(op->_shared_scan_limit.load(), -2);
1288+
1289+
Block next_output;
1290+
ASSERT_TRUE(scanner.get_block_after_projects(&state, &next_output, &eos).ok());
1291+
EXPECT_FALSE(eos);
1292+
EXPECT_EQ(next_output.rows(), 0);
1293+
EXPECT_EQ(scanner.unread_blocks(), 1);
12751294
EXPECT_EQ(op->_shared_scan_limit.load(), -2);
12761295
}
12771296

1297+
// A peer may exhaust the shared LIMIT while this scanner is still padding small blocks. The read
1298+
// loop stops at once, the rows already held are returned, and EOS is again left to the operator.
1299+
TEST(ScannerProjectionTest, shared_limit_exhausted_by_peer_mid_read_returns_held_rows_without_eos) {
1300+
ObjectPool pool;
1301+
auto data_type = std::make_shared<DataTypeInt32>();
1302+
auto row_descriptor = MockRowDescriptor({data_type}, &pool);
1303+
1304+
MockRuntimeState state;
1305+
state._batch_size = 8;
1306+
1307+
auto op = std::make_shared<MockScanOperatorX>();
1308+
op->_row_descriptor = row_descriptor;
1309+
auto& projection = op->set_projection_for_test(
1310+
MockRowDescriptor(std::vector<DataTypePtr> {data_type}, &pool));
1311+
op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0];
1312+
op->set_limit_for_test(10);
1313+
1314+
auto local_state = std::make_shared<MockScanLocalState>(&state, op.get());
1315+
local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type);
1316+
1317+
RuntimeProfile profile("scanner");
1318+
TestScanner scanner(&state, local_state.get(), -1, &profile);
1319+
ASSERT_TRUE(scanner.init(&state, {}).ok());
1320+
for (int i = 0; i < 5; ++i) {
1321+
scanner.add_block(ColumnHelper::create_block<DataTypeInt32>({i}));
1322+
}
1323+
// A peer produces the whole LIMIT right after this scanner reads its first block.
1324+
bool peer_done = false;
1325+
scanner.set_after_read_hook([&] {
1326+
if (!peer_done) {
1327+
peer_done = true;
1328+
op->_shared_scan_limit.fetch_sub(10);
1329+
}
1330+
});
1331+
1332+
Block output;
1333+
bool eos = false;
1334+
ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok());
1335+
EXPECT_FALSE(eos);
1336+
ASSERT_EQ(output.rows(), 1);
1337+
EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0);
1338+
EXPECT_EQ(scanner.unread_blocks(), 4);
1339+
EXPECT_EQ(op->_shared_scan_limit.load(), -1);
1340+
}
1341+
12781342
} // namespace doris

0 commit comments

Comments
 (0)