mrhhsg opened a new pull request, #71435:
URL: https://github.com/apache/doris/pull/71435

   ### Background
   
   Doris scan keys describe ranges over a table's sort-key prefixes. Rowset 
metadata can also contain per-segment full-key bounds, but existing zone-map 
and key-range filtering happens after a segment is opened. A segment-cache miss 
can therefore require file/footer access even for a segment that cannot match.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: None
   PR Type: Performance
   
   Problem Summary:
   
   Selective prefix scans can open segments whose existing metadata already 
proves they cannot match. This PR checks comparable bounds before creating lazy 
iterators, skipping a segment only when it is disjoint from every scan-key 
range. It applies only to eligible merge-on-write queries; non-merge-on-write 
rowsets and unsupported or uncertain comparisons retain the existing path. The 
mutable BE setting `enable_segment_key_range_pruning` defaults to `true`; query 
results and persisted formats are unchanged. Most added lines are tests.
   
   ### Details
   
   - **Change map:** Production code: +276/-2 across 12 files; tests: +1063/-1 
across 5 files; generated test outputs: +92/-0 across 3 files. About 81% of 
added lines are tests or expected outputs.
     1. Pre-open eligibility, endpoint encoding and interval comparison: 238 
changed lines in `beta_rowset_reader.cpp`.
     2. Reuse the existing integer codec with allocator-aware output: 6 added 
lines in `key_coder.h`.
     3. The configuration switch and profile/open attribution, including 
parallel preload and BM25 preparation: 34 changed lines across the remaining 
production files.
   - **Why it was slow:** On a segment-cache miss, the lazy iterator loads the 
segment before downstream filtering can reject it. The reader did not compare 
its scan-key ranges against available per-segment bounds before creating that 
iterator.
   - **Optimization:** Encode at most 64 existing ranges once per rowset-reader 
invocation, using query-tracked endpoint buffers and a query-tracked range 
container. Compare complete key prefixes lexicographically, retaining NULL 
markers and open/closed endpoints. Skip only when every OR range is provably 
disjoint; retain unknown truncated comparisons and fall back for the entire 
range union if an endpoint is unsupported.
   - **Results:** Local ASAN checks used the same build/data, toggling only 
pruning and clearing SegmentCache before each cold-reader check. The fixture 
has three two-row segments. A separate 24,576-row, three-segment SQL fixture 
reports skips of 0 versus 2. These are structural checks, not latency/CPU/GET 
benchmarks; other cache layers were not controlled.
   
     | Cold-reader workload | Rows OFF / ON | Opens OFF / ON | Skips OFF / ON |
     |---|---:|---:|---:|
     | Prefix equality | 2 / 2 | 3 / 1 | 0 / 2 |
     | All-match control | 6 / 6 | 3 / 3 | 0 / 0 |
   
   - **Applicability and cost:** Require merge-on-write, lexical ordering, 
compatible schemas and position-aligned nonaggregated bounds. Support 
signed-integer prefixes and final physical VARCHAR keys after supported 
integers, excluding sequence columns for VARCHAR. Non-merge-on-write linked 
changes can retain older encoded bounds, so those readers are excluded 
entirely. Unsupported layouts/readers, missing metadata and over 64 ranges 
retain the existing path. Encoding and bounded comparisons add work even when 
nothing is skipped. Disable the setting to restore the original candidate path; 
formats are unchanged.
   
   ### Testing
   
   Verified locally on the final source with an ASAN build:
   
   - `./run-be-ut.sh --run -j 48 
--filter='SegmentKeyPruningTest.*:BetaRowsetTest.*:RowsetMetaTest.*:SegmentsKeyBoundsTruncationTest.*:KeyCoderTest.*:CollectionStatisticsTest.*:CollectionStatisticsDetailedTest.*:TabletCollectionStatisticsTest.*'`:
 144 tests across 8 suites passed, including cold-reader OFF/ON equivalence, 
metadata fallbacks, physical IDs/split ranges/delete bitmaps, endpoint 
allocation limits and scoring-open attribution.
   - `./run-regression-test.sh --conf <isolated-local-conf> --run -d 
fault_injection_p0 -s <suite>`: `test_segment_prefix_key_pruning`, 
`test_segment_key_pruning_schema_change` and 
`test_segment_open_profile_scoring` each passed normal replay.
   - The same regression entry point passed `-d data_model_p0/duplicate -s 
test_non_mow_key_bounds_aggregation` and `-d unique_with_mow_p0/key_bounds -s 
test_key_bounds_truncation_read_scenarios`.
   - All five SQL runs reported zero failed suites, fatal scripts and skipped 
scripts. Expected outputs were runner-generated; 30 deterministic result tags 
were independently checked. Serial and parallel scoring scans reported 3 cold 
opens and 0 warm opens, with shared collection charged only once.
   
   ### Notes for Reviewers
   
   `NumSegmentPrunedByKeyBounds` counts skipped candidate segment splits, not 
distinct files. `NumSegmentOpened` counts successful `Segment::open` calls, 
excludes SegmentCache hits, and includes row-count preload and scoring 
preparation without duplicating shared collection opens. Preload, caching and 
early termination can limit savings. Segment order, physical IDs, row ranges 
and delete-bitmap mappings are preserved.
   
   ### Release note
   
   Eligible merge-on-write queries with supported sort-key prefix ranges can 
avoid opening provably disjoint segments using existing metadata. Query 
profiles expose pre-open skips and successful segment opens, including scoring 
preparation. Other readers retain the existing scan path.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: Yes; document the BE setting, eligibility 
and counter definitions in the configuration and query-profile guides.
   - Pre-submission status: Partially verified: RELEASE latency/CPU, remote 
object-store I/O, multi-backend and cloud integration, and rolling upgrades 
have not been validated.
   
   ### AI Disclosure
   
   - Tool: Codex (model not recorded).
   - Generated: Implementation, tests and PR description.
   - Human review: No human review of the AI-generated changes has been 
reported.


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