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

   ### Background
   
   `SegmentIterator` already uses lazy materialization for scans: it reads the 
predicate columns first, filters the rows, and only then reads the remaining 
expression and output columns. Its first batch currently merges vectorized 
predicate columns, short-circuit predicate columns, and delete-condition 
columns into one `_predicate_ordinals` read. Vectorized predicates run first, 
so short-circuit columns are decoded for rows that the vectorized predicates 
discard right away.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: None
   PR Type: Performance
   
   Problem Summary:
   
   A query that combines a selective vectorized predicate with a short-circuit 
predicate pays to decode the short-circuit column on rows that vectorized 
filtering removes. This PR splits short-circuit-only columns into a second, 
rowid-based read after vectorized filtering and evaluates their predicates on 
the resulting dense columns. Delete-condition columns stay in the first read. 
Query results and predicate semantics are unchanged; the benefit is limited to 
batches where vectorized filtering removes enough rows to amortize the 
random-access read.
   
   ### Details
   
   - **Why it was slow:** The first read in `_next_batch_internal` uses 
`_predicate_ordinals`, which before this change held every column with a scan 
or delete predicate, so short-circuit columns were fully decoded before 
`_evaluate_vectorization_predicate` ran. The source carried a TODO 
acknowledging the wasted read, and the cost follows directly from the read 
order.
   - **Optimization:** Lazy materialization now distinguishes three read stages 
for predicate columns: vectorized and delete columns first, short-circuit-only 
columns by surviving rowids after vectorized filtering, then expression and 
output columns. Short-circuit predicates whose column also feeds a vectorized 
or delete predicate stay on the original sparse path, so their selector 
semantics do not change. When the surviving rowids form a contiguous run, the 
deferred read falls back to a single seek + `next_batch` instead of gathering 
by rowid, so a low-selectivity filter does not pay a per-page re-seek on the 
deferred columns.
   - **Results:** A 100K-row table with a 200-byte string column, median 
`BlockLoadTime` over 5 rounds on a Debug BE:
   
     | Workload | Before | After |
     |---|---:|---:|
     | `id > 99900` (~99.9% filtered) | 1.44 ms | 0.37 ms (~3.9x) |
     | `id > 0` (no filter) | 24.65 ms | 26.40 ms (~7% slower) |
   
     The gain scales with the short-circuit column's decoding cost: a cheap 
dictionary-encoded short-string column showed no gain.
   - **Applicability and cost:** Applies only when a short-circuit predicate 
column is not also used by a vectorized predicate or delete condition. It adds 
a rowid-gather read and a small dense-selector translation, so it may not help 
when vectorized predicates filter few rows or when the short-circuit column is 
already hot in cache. Delete-condition handling is deliberately unchanged. 
Because dense short-circuit predicates now run after delete filtering, 
`short_circuit_cond_input_rows` is not directly comparable with the old value 
when both sparse and dense short-circuit predicates exist.
   
   ### Testing
   
   - Two white-box cases appended to 
`be/test/storage/segment/segment_iterator_lazy_pruned_test.cpp`: 
`splitsShortCircuitOnlyColumnsFromFirstRead` asserts the split of predicate, 
short-circuit, delete, and output ordinals; 
`densePredicateFiltersWithIdentitySelector` asserts dense short-circuit 
filtering with an identity selector.
   - The author reports `./run-be-ut.sh --run 
--filter='SegmentIteratorLazyPrunedTest.*'` passes all 4 tests in the suite on 
commit `38149b7`.
   - A differential SQL comparison between the candidate BE and the baseline BE 
(`b73ed53f`) over 10 queries — vectorized `id` range plus short-circuit `v 
IN/NOT IN/empty/NULL`, over data with NULL and empty values — returned 
byte-identical result sets.
   
   ### Notes for Reviewers
   
   The second read produces dense columns, while the first read produces 
batch-indexed columns. `_evaluate_short_circuit_predicate` keeps using original 
row positions for sparse columns and delete conditions; the new dense path 
evaluates with an identity selector, then maps surviving dense positions back 
to original row positions. Dict-code conversion for dense short-circuit 
predicates is deliberately performed after the second read so it sees the 
gathered dictionary codes.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Ready — 4 BE unit tests passed, the differential 
SQL comparison returned identical results, and the performance comparison shows 
~3.9x on the target workload (the ~7% no-filter regression is disclosed in 
Details)
   
   ### AI Disclosure
   
   - Tool: CodeBuddy (Claude Opus 4.6)
   - Generated: implementation in `segment_iterator.cpp`/`segment_iterator.h`, 
the two test cases, and this description
   - Human review: not stated
   - Prompts: The author supplied the target optimization, the source 
locations, and the constraint that delete columns stay in the first read, then 
asked to continue per project memory and Doris skills to a pre-submission state.
   


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