hello-stephen commented on code in PR #70384:
URL: https://github.com/apache/doris/pull/70384#discussion_r4768921604


##########
be/src/storage/segment/segment_iterator.cpp:
##########
@@ -1166,6 +1168,75 @@ Status SegmentIterator::_apply_ann_topn_predicate() {
     return Status::OK();
 }
 
+void SegmentIterator::_reset_range_iterator() {
+    // TopN pruning can replace the bitmap between batches. Refresh the cached 
cardinality with
+    // the cursor so batch sizing does not use the larger range from before 
the bound arrived.
+    _row_bitmap_cardinality = _row_bitmap.cardinality();
+    if (_opts.read_orderby_key_reverse) {
+        _range_iter = 
std::make_unique<BackwardBitmapRangeIterator>(_row_bitmap);
+    } else {
+        _range_iter = std::make_unique<BitmapRangeIterator>(_row_bitmap);
+    }
+}
+
+Status SegmentIterator::_update_topn_runtime_filter_ranges() {
+    // Apply the first bound immediately and throttle later changes by raw 
rows read.
+    // A 40960-row delta is not enough; it must exceed the limit.
+    constexpr uint64_t update_threshold = 4096 * 10;
+    auto needs_update = [&](const TopNRuntimeFilter& filter) {
+        return filter.predicate->version() > filter.applied_version &&
+               (filter.applied_version == 0 ||
+                _topn_rows_read - filter.last_update_rows > update_threshold);
+    };
+    if (std::ranges::none_of(_topn_runtime_filters, needs_update)) {
+        return Status::OK();
+    }
+
+    SCOPED_RAW_TIMER(&_opts.stats->generate_row_ranges_by_zonemap_ns);
+    roaring::Roaring remaining = _row_bitmap;
+    // Exclude already consumed rows BEFORE intersecting page ranges. For 
example, after reading
+    // [0,100), a surviving page [50,150) may only add [100,150) to the 
rebuilt forward iterator.
+    if (_opts.read_orderby_key_reverse) {
+        remaining.removeRange(_topn_read_position, num_rows());
+    } else {
+        remaining.removeRange(0, _topn_read_position);
+    }
+    const auto before = remaining.cardinality();
+    // The last returned batch may have exhausted the cursor before the 
producer publishes a
+    // tighter bound. There are no unread rows to test against page metadata 
in that case.
+    if (before == 0) {
+        return Status::OK();
+    }
+    for (auto& filter : _topn_runtime_filters) {
+        if (!needs_update(filter)) {
+            continue;
+        }
+        const auto snapshot = filter.predicate->snapshot();
+        DCHECK(snapshot.predicate != nullptr);
+        AndBlockColumnPredicate predicate;
+        predicate.add_column_predicate(
+                SingleColumnBlockPredicate::create_unique(snapshot.predicate));
+        RowRanges ranges = RowRanges::create_single(num_rows());
+        RETURN_IF_ERROR(
+                
_column_iterators[filter.predicate->column_id()]->get_row_ranges_by_zone_map(

Review Comment:
   [P1] Exclude substituted version columns from the TopN refresh
   
   For a fresh single-version MoW rowset, `__DORIS_VERSION_COL__` is `0` on 
disk; `_replace_version_col_if_needed()` substitutes the positive rowset 
version only after reading. With `show_hidden_columns=true`, consider `ORDER BY 
__DORIS_VERSION_COL__ DESC, k DESC LIMIT 20` over a large `UNIQUE KEY(k)` 
rowset scanned in ascending `k` order. If the first bound arrives mid-scan, 
this new call tests `version >= V` against page zone maps `[0,0]` and drops 
every unread page, although those rows tie on version and can win on `k`. The 
base did not reapply page metadata after the bound arrived. Please apply the 
single-version `version_col_idx()` exclusion already used by 
`_apply_expr_zonemap_to_row_ranges()`, or evaluate its substituted value, and 
cover a late descending bound with a secondary key.



-- 
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