hello-stephen commented on code in PR #70384:
URL: https://github.com/apache/doris/pull/70384#discussion_r4750550049
##########
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:
[P2] Bound the cost of repeated page-range reconstruction
For `ORDER BY k ASC LIMIT 20` on a large `DUPLICATE KEY(id)` segment, a
non-key `k` that decreases with `id` keeps tightening the heap bound without
pruning unread rows. This call still reparses every page's zone map after each
>40960 raw rows, including consumed pages. Alternating long high-positive and
decreasing-negative runs makes the surviving page intervals disjoint;
`ColumnReader::_calculate_row_ranges` then calls `RowRanges::ranges_union` for
each page, copying all accumulated intervals and taking quadratic work per
refresh. The new between-batch invocation repeats that cost throughout the
segment; the base only performed this pass at initialization. Non-key ordering
prevents storage-key LIMIT pushdown, and metadata caching does not remove the
parsing/copying work. Please make ordered range accumulation linear and bound
repeated refresh work, for example with candidate-aware processing and backoff
when updates prune nothing. The fixed raw-row threshold alone does not bound
this cost as segments grow.
--
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]