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
6770protected:
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
8389private:
8490 bool _allow_padding = true ;
8591 std::list<Block> _blocks;
92+ std::function<void ()> _after_read_hook;
8693};
8794
8895class 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.
957966TEST (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