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

   …
   ### Background
   
   `SegmentIterator` reads rows from a single storage segment into a 
caller-supplied `Block`. During normal batch reads, `_init_current_block` moves 
the non-predicate columns out of the caller's `Block` into `_current_columns`, 
fills them, and moves them back into `Block` before returning (or inside 
`_process_eof` when zero rows survive after `_init_current_block`). When 
`StorageReadOptions::read_limit` is set, each `SegmentIterator` stops after 
returning `read_limit` rows.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #62222 (introduced the bug), #68788 (backported it to branch-4.2)
   PR Type: Bugfix
   
   Problem Summary:
   
   Polling `SegmentIterator::next_batch(Block*)` again after it has already 
returned `_opts.read_limit` rows overwrites every non-predicate column pointer 
in the caller's `Block` with `nullptr`, causing a `SIGSEGV` in 
`IColumn::mutate()` when a multi-segment or multi-rowset iterator 
(`VUnionIterator`, `DirectIterator`, or `VCollectIterator`) reuses that `Block` 
on the next segment. Replace the pre-`_init_current_block` 
`_process_eof(block)` call at the `read_limit` exit with in-place 
`block->clear_column_data()` and iterator teardown, matching 
`_emit_count_shortcut_batch`. Most of the diff is BE unit tests covering 
single-segment EOF polling, pushed predicates, and multi-segment 
`VUnionIterator` `Block` reuse.
   
   For example, when `VUnionIterator` chains two 20-row `(INT, Nullable<INT>)` 
segments with `opts.read_limit = 5` and `opts.block_row_max = 4` 
(`SegmentIteratorLimitOptTest.read_limit_eof_preserves_block_columns` and 
`union_iterator_reuses_block_after_read_limit_eof`):
   
   | Case | Without the fix | With the fix |
   |---|---|---|
   | `read_limit_eof_preserves_block_columns` | `Expected: (nullptr) != 
(block.get_by_position(0).column.get()), actual: (nullptr) vs NULL` | 
`END_OF_FILE`, non-null empty columns | | 
`union_iterator_reuses_block_after_read_limit_eof` | `Segmentation fault` 
(`SIGSEGV` in `IColumn::mutate()`) | Keys `{0, 1, 2, 3, 4, 100, 101, 102, 103, 
104}` |
   
   ### Details
   
   - **Frequency:** Always when `SegmentIterator::next_batch` is polled again 
after `_rows_returned >= _opts.read_limit` (any multi-segment or multi-rowset 
scan with `read_limit > 0` that continues past the first exhausted segment).
   - **Root cause:** In `SegmentIterator::_next_batch_internal`, `if 
(_opts.read_limit > 0 && _rows_returned >= _opts.read_limit)` runs before 
`_init_current_block(block, _current_columns, ...)`, when `_current_columns[i]` 
is `nullptr` (already moved back into `block` at the end of the previous 
batch). Calling `_process_eof(block)` there executes 
`block->replace_by_position(i, std::move(_current_columns[i]))` and replaces 
valid non-predicate `Block` columns with `nullptr`.
   - **Why existing tests missed it:** Unit tests: 
`SegmentIteratorLimitOptTest` 
(`be/test/storage/segment/segment_iterator_limit_opt_test.cpp`) only called 
`_can_opt_limit_reads()` and `_apply_read_limit_to_selected_rows()` on a 
null-segment stub without calling `next_batch()`, while 
`SegmentIteratorCountEmitShortcutTest` tested `_emit_count_shortcut_batch`, 
which already avoided `_process_eof`. Regression: 
`test_general_limit_pushdown.groovy` and `test_unified_limit_pushdown.groovy` 
(`regression-test/suites/query_p0/limit/`) run via `OlapScanner`, whose 
scanner-level `_limit` stops calling `BlockReader` as soon as the batch hits 
`N` rows, before the exhausted `SegmentIterator` is polled again.
   - **Fix:** Clear `block` column data in place and release 
`_column_iterators` and `_index_iterators` directly in the 
pre-`_init_current_block` `read_limit` branch of 
`SegmentIterator::_next_batch_internal`, and assert `_current_columns[i].get() 
!= nullptr` with `DCHECK` in `_process_eof`. The fix sits in `SegmentIterator` 
because `RowwiseIterator::next_batch(Block*)` owns the invariant that `block` 
columns stay valid across `EndOfFile`.
   - **Other paths and branches:** Checked all `EndOfFile` and `_process_eof` 
paths in `SegmentIterator`: `_emit_count_shortcut_batch` already clears `block` 
in place (not affected), and the `_selected_size == 0` path calls 
`_process_eof` after `_init_current_block` has populated `_current_columns` 
(not affected). `branch-4.2` has the bug (via `#68788`); `branch-3.1`, 
`branch-4.0`, and `branch-4.1` lack `_opts.read_limit` and are not affected.
   
   ### Testing
   
   - New BE unit tests in 
`be/test/storage/segment/segment_iterator_limit_opt_test.cpp` 
(`read_limit_eof_preserves_block_columns`, 
`union_iterator_reuses_block_after_read_limit_eof`, 
`read_limit_eof_with_pushed_predicate_preserves_columns`): fail on `master` 
without the fix (null column assertion failures and `SIGSEGV` when 
`VUnionIterator` advances to the second segment) and pass (`11/11` in 
`SegmentIteratorLimitOptTest.*`) on both RELEASE and ASAN BE unit-test builds 
run on a Linux x86_64 dev machine.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Ready
   
   ### AI Disclosure
   
   - Tool: Antigravity (Gemini)
   - Generated: the fix, the unit tests, and this description
   - Human review: not stated
   - Prompts: Asked what pre-existing master bug the BlockIterator benchmark 
uncovered and whether PR #70741 still hit it, then asked to open a separate 
small PR fixing that SegmentIterator bug.
   
   <!-- Recommended: develop the change with the develop-with-evidence skill 
and write this description with the doris-pr-description skill, both from 
https://github.com/apache/doris-skills (the doris-skills submodule of this 
repository). -->
   
   ### Background
   
   <!-- How the feature or module this PR touches works today, in two to four 
plain sentences, for a reviewer who doesn't know it. Link the user doc if there 
is one. Leave the problem and the fix to Problem Summary. -->
   
   ### What problem does this PR solve?
   
   <!-- Issue Number: "close #N" if this PR resolves the issue, "#N" if it only 
relates to it, "None" if there is none. A Feature needs an issue. -->
   <!-- Related PR: "#N (relation)" for each PR a reviewer should know about, 
such as "#123 (introduced the bug)", separated by commas. Write "None" if no 
related PR is known. -->
   <!-- PR Type: Bugfix, Performance, Feature, Enhancement, Refactor, Test, 
Build/CI/Chore, Revert, Release/Dependency, or Other. -->
   
   Issue Number:
   Related PR:
   PR Type:
   
   Problem Summary:
   
   <!-- Two to five sentences: the problem or need and who is affected; the 
approach in one sentence; when the diff is larger than that suggests, what else 
it had to handle, or that most of it is tests; and what existing users see 
after an upgrade. -->
   
   ### Details
   
   <!--
   For a diff of more than about 300 changed lines, start with "- **Change 
map:**": how many lines are production code, tests, generated files, and docs, 
then the code in parts, each with the problem it handles and its size.
   Then answer each field of your PR type on its own line, as "- **Field:** 
answer", in this order:
     Bugfix: Frequency, Root cause, Why existing tests missed it, Fix, Other 
paths and branches
     Performance: Why it was slow, Optimization, Results, Applicability and cost
     Feature: Scope, Design, Usage, Default behavior, Compatibility
     Enhancement: Before and after, Configs and defaults, Impact on existing 
users
     Refactor: Moves vs. logic changes, Behavior change, Compatibility, 
Performance impact, Rollback
     Test, adding tests: Scenarios, What existing tests miss, Fails when broken
     Test, fixing a test: Failure, Cause, Stability evidence
     Build/CI/Chore: Affected, Developer and release impact, License and 
third-party, Rollback
     Revert: Why not fix forward, Original author notified, Follow-up
     Release/Dependency: Versions, Upstream changes, Compatibility, Rollback
     Other: Why Other
   "None" is a valid answer when it is true. Don't restate the diff.
   -->
   
   ### Testing
   
   <!-- The tests, suites, or commands that ran, what each shows, and where it 
ran (locally or in CI). For a fix, the test that fails before it and passes 
after. -->
   
   ### Notes for Reviewers
   
   <!-- Optional; delete this section if there is nothing to say. Why each 
non-obvious edit was made, what you are unsure about, and where to start 
reading if Details has no Change map. -->
   
   ### Release note
   
   <!-- One paragraph on what users will notice, or None. -->
   
   ### Check List (For Author)
   
   <!-- Behavior changed: "No", or "Yes (see Details)" with a Behavior change 
field in Details. -->
   <!-- Does this need documentation: "No", or "Yes" with the 
apache/doris-website PR or the plan. -->
   <!-- Pre-submission status: "Ready"; "Partially verified: " and what is not 
verified; or "Not ready: " and why, for a Draft PR. -->
   
   - Behavior changed:
   - Does this need documentation:
   - Pre-submission status:
   
   ### AI Disclosure
   
   <!-- Tool: the agent and model, or None. Generated: the parts it wrote 
(code, tests, this description), or None. Human review: what you reviewed of 
the generated parts, or "Not applicable" without AI tools. You may add "- 
Prompts:" to summarize how you directed the agent. -->
   
   - Tool:
   - Generated:
   - Human review:
   


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