github-actions[bot] commented on code in PR #62222:
URL: https://github.com/apache/doris/pull/62222#discussion_r3128005491


##########
be/src/exec/scan/scanner_context.cpp:
##########
@@ -421,12 +400,7 @@ Status ScannerContext::get_block_from_queue(RuntimeState* 
state, Block* block, b
         }
     }
 
-    // Mark finished when either:
-    // (1) all scanners completed normally, or
-    // (2) shared limit exhausted and no scanners are still running.
-    if (_completed_tasks.empty() &&

Review Comment:
   This is now a real liveness bug for `LIMIT 0`, not just a missed fast path. 
`_pull_next_scan_task()` below returns `nullptr` whenever `_shared_scan_limit 
== 0`, but this branch only marks the context finished after every scanner has 
reached EOS. For `LIMIT 0`, no scanner is ever submitted, 
`_num_finished_scanners` stays 0, `_completed_tasks` stays empty, and 
`_scan_dependency` never becomes ready, so the pipeline can stall before 
`ScanOperatorX::get_block()` gets a chance to call `reached_limit()` / 
`stop_scanners()`. Please either keep the old `shared_limit == 0 && 
_in_flight_tasks_num == 0` finish condition or short-circuit `LIMIT 0` before 
creating the scanner context.



##########
be/src/exec/scan/olap_scanner.cpp:
##########
@@ -457,29 +456,38 @@ Status OlapScanner::_init_tablet_reader_params(
                 }
                 _tablet_reader_params.read_orderby_key_num_prefix_columns =
                         olap_scan_node.sort_info.is_asc_order.size();
-                _tablet_reader_params.read_orderby_key_limit = _limit;
 
-                if (_tablet_reader_params.read_orderby_key_limit > 0 &&
-                    olap_scan_local_state->_storage_no_merge()) {
-                    _tablet_reader_params.filter_block_conjuncts = _conjuncts;
-                    _conjuncts.clear();
+                if (_limit > 0 && olap_scan_local_state->_storage_no_merge()) {
+                    _tablet_reader_params.read_orderby_key_limit = _limit;
                 }
             } else if (_limit > 0 && 
olap_scan_local_state->_storage_no_merge()) {
-                // General limit pushdown for DUP_KEYS and UNIQUE_KEYS with MOW
-                // (non-merge path). Only when topn optimization is NOT active.
-                // NOTE: _limit is the global query limit (TPlanNode.limit), 
not a
-                // per-scanner budget. With N scanners each scanner may read 
up to
-                // _limit rows, so up to N * _limit rows are read in total 
before
-                // the _shared_scan_limit coordinator stops them. This is
-                // acceptable because _shared_scan_limit guarantees 
correctness,
-                // and the over-read is bounded by (N-1) * _limit which is 
small
-                // for typical LIMIT values.
-                _tablet_reader_params.general_read_limit = _limit;
+                // GENERAL limit pushdown is only safe when there is no filter
+                // sitting between SegmentIterator's output and the operator's
+                // limit accounting. If we move _conjuncts into the storage
+                // layer below, those filters become "post output" with respect
+                // to SegmentIterator and would shrink the result, so the
+                // per-segment budget would be reached prematurely.
+                if (_conjuncts.empty()) {
+                    _tablet_reader_params.general_read_limit = _limit;
+                }
+            }
+
+            if (olap_scan_local_state->_storage_no_merge() && 
!_conjuncts.empty()) {
                 _tablet_reader_params.filter_block_conjuncts = _conjuncts;
                 _conjuncts.clear();

Review Comment:
   This is still unsafe for the `ORDER BY key LIMIT` path. A few lines above we 
may set `read_orderby_key_limit = _limit`, and then this branch moves the 
remaining `_conjuncts` into `filter_block_conjuncts`. That means 
`SegmentIterator` enforces a hard raw-row cap, while `_topn_next()` only counts 
rows after `filter_block_conjuncts` runs. On a MOW table with deleted head rows 
(the hidden `__DORIS_DELETE_SIGN__ = 0` filter survives here) or any session 
with `enable_common_expr_pushdown = false`, a rowset can hit EOF after its 
first K raw rows but contribute fewer than K surviving candidates, so the final 
merge never sees later valid keys from that same rowset. This is different from 
the shared-scan-limit issue already discussed: even with `_shared_scan_limit = 
nullptr`, the per-rowset `read_orderby_key_limit` is still wrong when 
post-`SegmentIterator` filters remain. Please gate the top-N read-limit path on 
`_conjuncts.empty()` as well, or keep reading in `_topn_next()` until each row
 set has produced K post-filter rows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to