fix(arrow): drop-row at batch boundary in RowsToRecord#685
Merged
Conversation
The loop condition `rows.Next() && count < batchSize` advanced the underlying cursor once after the final scan; that row was silently dropped when the caller asked for the next batch. Unbounded SELECTs crossing the 1024-row batch boundary lost one row per transition (deterministic, ORDER BY-stable). COUNT(*) returned the parquet metadata count, hiding the discrepancy from aggregation queries. Reported by Marce Coll on dbt_marce.credit_purchase_events (COUNT(*) = 12617, SELECT = 12605) and dbt.usage_allocation (2,689,942 vs 2,687,758). WHERE filters still found the rows. Swap the order: `count < batchSize && rows.Next()` so Next() is not called when the batch is already full. Add regression test covering the production case.
mcoll-posthog
approved these changes
Jun 5, 2026
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.
Summary
RowsToRecordlost one row at every Arrow batch transition (batchSize=1024) for unbounded SELECTs.rows.Next() && count < batchSizeadvanced the cursor once after the last scan; that row was neverScan'd and silently skipped by the next batch call.count < batchSize && rows.Next()soNext()is not called when the batch is already full. One-line fix.Repro (prod)
Reported by Marce Coll. On
dbt_marce.credit_purchase_events:Probing with
LIMIT:Pattern: for every batch transition after the first, lose row at index
k * batchSize. The loop body never executed for that row becausecount < batchSizefailed afterrows.Next()already advanced the driver cursor.Why nobody noticed earlier
COUNT(*)reads parquet/DuckLake metadata row counts → unaffected.WHERE col = Xreturns a single row that fits in one batch → no transition → no loss.SELECT(not aggregations) of large tables may have silently lost rows for as long as this code has been live.Test
New
TestRowsToRecordNoRowsLostAtBatchBoundarycovers:Walks the seen-set, reports first dropped id. On the broken code:
delivered 12605 rows, expected 12617 (first dropped id = 1024). On the fix: all cases pass.Test plan
duckdbservicepackage tests pass.RowsToRecorddepend on the old (buggy) cursor-advance behavior.SELECTof a >1024-row table needs a full rebuild.