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


##########
be/src/storage/segment/segment_iterator.cpp:
##########
@@ -1170,6 +1172,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(
+                        &predicate, nullptr, &ranges));
+        remaining &= RowRanges::ranges_to_roaring(ranges);
+        filter.applied_version = snapshot.version;
+        filter.last_update_rows = _topn_rows_read;
+    }
+    const auto filtered = before - remaining.cardinality();
+    if (filtered != 0) {
+        // Roaring iterators borrow bitmap containers. Destroy the old cursor 
before replacing
+        // its bitmap, then bind a new cursor to the remaining rows in the 
same read direction.
+        _range_iter.reset();
+        _row_bitmap = std::move(remaining);
+        _reset_range_iterator();
+        _opts.stats->rows_stats_filtered += filtered;
+        _opts.stats->rows_stats_rp_filtered += filtered;

Review Comment:
   [P3] Include late zone-map pruning in the aggregate condition count
   
   The new refresh increments `rows_stats_filtered` and 
`rows_stats_rp_filtered` but not `rows_conditions_filtered`. The latter is 
documented in `olap_common.h` as rows filtered by column indexes, and the 
initial page-pruning path in `_apply_scan_restrictions()` (line 876) increments 
it by the bitmap reduction. In the new late-arrival test with 131072 INT rows 
and a bound of 65535, the refresh can skip 65536 rows while 
`RowsConditionsFiltered` remains zero even though 
`RowsZoneMapRuntimePredicateFiltered` is 65536. This makes the aggregate scan 
profile misleading. Please also add `filtered` to `rows_conditions_filtered` 
here (it already excludes consumed rows and earlier restrictions), and check 
the aggregate in the late-arrival test.



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