fix(SelectQuery): process the last partial chunk in runChunks()#255
Merged
Conversation
The loop guard `$offset + $limit <= $count` stopped as soon as fewer than $limit rows remained, so the trailing partial page (count % limit rows) was never passed to the callback — silent data loss in any batch/export/migration built on runChunks(). Changing the guard to `$offset < $count` lets the final iteration return the remaining rows via LIMIT/OFFSET. Fixes #254 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Covers cases left untested around #254: chunk size larger than the total row count (previously processed zero rows under the old guard), an empty result set, and the $offset/$count arguments passed to the callback. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes a data-loss bug in SelectQuery::runChunks() where the final partial chunk (and the limit > count case) could be skipped, and adds regression tests to ensure all rows are processed across chunk boundaries.
Changes:
- Update
SelectQuery::runChunks()loop guard to process the trailing partial chunk ($offset < $count). - Add functional regression tests covering partial tail chunks,
limit > count, empty results, and callback offset/count arguments.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Query/SelectQuery.php | Adjusts chunk iteration condition so the final partial page is not skipped. |
| tests/Database/Functional/Driver/Common/Driver/StatementTest.php | Adds new test cases validating chunk traversal behavior and callback arguments. |
Comments suppressed due to low confidence (1)
src/Query/SelectQuery.php:312
runChunks()can enter an infinite loop when$limit <= 0(because$offsetnever increases meaningfully, and the loop guard only checks$offset < $count). SincerunChunks()accepts anyint, it should validate that the chunk size is a positive integer and fail fast with an exception to avoid hanging processes.
while ($offset < $count) {
$result = $callback(
$select->offset($offset)->getIterator(),
$offset,
$count,
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MSSQL returns the paginated id column as a string and its OFFSET/FETCH ordering is not guaranteed without an explicit ORDER BY. Add orderBy('id') for deterministic iteration and use assertEquals (matching the existing testChunks convention) so the row-value assertions don't depend on the driver's scalar type.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 2.x #255 +/- ##
=========================================
Coverage 95.68% 95.68%
Complexity 2038 2038
=========================================
Files 137 137
Lines 5775 5775
=========================================
Hits 5526 5526
Misses 249 249 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔍 What was changed
SelectQuery::runChunks()no longer skips the last partial chunk.The loop guard was
$offset + $limit <= $count, which required a full remaining chunk and stopped as soon as fewer than$limitrows were left. The trailing partial page ($count % $limitrows) was silently dropped — no error, no warning. The guard is changed to$offset < $count, so the final iteration issuesLIMIT N OFFSET kand naturally returns the remaining< $limitrows.Note
The same guard also broke the
limit > countcase entirely: a table with fewer rows than the chunk size processed zero rows. This is fixed by the same change and covered by a dedicated test.🤔 Why?
Silent data loss in any batch/export/migration built on
runChunks(). Full coverage previously happened only when$countwas an exact multiple of$limit.Example — 2500 rows,
runChunks(1000, …):📝 Checklist
Closes [Bug]: SelectQuery::runChunks() silently skips the last partial chunk (data loss) #254
How was this tested:
Added regression and edge-case tests to
StatementTest(common across all drivers):testChunksProcessLastPartialChunk— trailing partial page is visited (count % limit != 0)testChunksWithLimitGreaterThanCount— chunk size larger than the row count still yields every rowtestChunksPassOffsetAndCountToCallback—$offset/$countcallback argumentstestChunksOnEmptyResultNeverInvokesCallback— empty result set never invokes the callback📃 Documentation
Not needed — behavioral bugfix, public API unchanged.