Skip to content

fix: Keep an Arrow chunk whose conversion warning is turned into an error - #962

Draft
krlmlr wants to merge 7 commits into
claude/rb-arrow-coercion-reportsfrom
claude/rb-arrow-kept-chunk
Draft

krlmlr wants to merge 7 commits into
claude/rb-arrow-coercion-reportsfrom
claude/rb-arrow-kept-chunk

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for DBI methods: Arrow fetching is still internal. For the internal Arrow fetch, a chunk whose conversion warnings don't return is no longer lost. #961 reads an Arrow chunk and then raises its conversion warnings from R. With options(warn = 2), an exiting handler (tryCatch(warning = )), or a failure while building the warnings, the chunk was dropped while its rows already counted as fetched, so dbGetRowCount() and dbHasCompleted() described rows the caller never received.

The result now keeps the chunk until the warnings have returned. result_fetch_arrow_chunk() builds the chunk into the result and then hands it out to R; if the warnings unwind, fetch_arrow_chunk_warn() hands the chunk back with the new internal result_keep_arrow_chunk() from on.exit(). The next fetch returns the kept chunk whole and ignores its n: a fetch of one row after a kept chunk of two returns both. It doesn't warn again: the warnings belong to the fetch that read the rows, and warning again would make the chunk unreachable under options(warn = 2). The entry point takes the chunk out of the result before it allocates anything in R, keeping the result alive with its own share, so a finalizer that clears or fetches from the result meanwhile can't pull the chunk away; a failed allocation hands the chunk back, with its warnings still to come. Until the chunk is handed out, dbGetRowCount() doesn't count its rows and dbHasCompleted() is FALSE. Kept chunks form a queue in the order of their rows: when a calling handler's own fetch is kept and then the interrupted fetch unwinds too, the interrupted chunk comes first. Rows can come out of order when a calling handler's own fetch is handed out: if the handler of chunk A fetches chunk B and gets it, and A's warning then unwinds, A is kept and the caller sees B's rows before A's. The alternative would be to drop A and lose its rows; the open question is #962 (comment). A chunk handed back after dbBind() (from a handler) belongs to an earlier execution and is dropped; dbBind(), dbClearResult() and the finalizer free kept chunks. A calling handler that clears the result still gets its chunk, as in #961.

Tests: the new test-arrow-kept-chunk.R covers warn = 2 followed by a refetch that returns the same rows in order (also for a chunk of all rows), an exiting handler, warnings that fail to build (mocked warn_coercions()), nested handlers that keep one or two chunks, a calling handler that clears the result before an exit or under warn = 2, a rebind outside and inside a handler, and dbClearResult() of a kept chunk, with dbGetRowCount() and dbHasCompleted() at each step. Two expectations in test-arrow-coercion.R that encoded the loss are removed. The Arrow validation of a kept chunk is a test of its own, so that a skip for nanoarrow < 0.6.0 skips nothing else. Red on #961's head: 6 of the 8 other new tests fail (the two release tests pass there, as nothing is kept); green with the fix. Full suite with NOT_CRAN=true: FAIL 0. The new test file and a script that lets the garbage collector free a kept chunk with its connection, and runs keep/hand-out/clear under gctorture(), are clean under valgrind (0 errors, 0 bytes definitely or indirectly lost).

Layer F4b of the rebuilt chain, planned in #880; based on #961. Ported from KeptArrowChunk in the old chain's #862 (claude/arrow-coercion-reports), adapted to warnings raised from R instead of C++; nothing for mixed data-frame/Arrow fetching.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

…rror

An Arrow fetch reads its chunk and then raises the warnings about its values from R.
A warning made an error, as with `options(warn = 2)`, a handler that exits,
or a failure to build the warnings lost the chunk, while its rows counted as fetched.
The result now keeps the chunk until the warnings have returned:
the next fetch returns it whole, without warning again.
Until then, `dbGetRowCount()` doesn't count its rows and `dbHasCompleted()` is `FALSE`.
A chunk kept from a handler's own fetch comes after the chunk that the handler interrupted.
`dbBind()`, `dbClearResult()` and the garbage collector free a kept chunk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 07:36

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: which order should a kept Arrow chunk come back in, when a warning handler fetched more meanwhile?

This pull request keeps an Arrow chunk whose conversion warnings didn't finish, for example under options(warn = 2) or with an exiting handler, and returns it on the next fetch. One corner remains. A calling handler of those warnings can fetch the next chunk itself before the interrupted fetch exits. The interrupted chunk then comes back to the result after rows the handler has already received, see keep_arrow_chunk(). No row is lost or duplicated, but the user sees them out of order. A handler that fetches from the result whose warning it handles is rare, and the earlier layer #961 tests it only to make sure nothing breaks.

  • Return the kept chunk on the next fetch, even if rows the handler fetched came earlier: complete, possibly out of order, as implemented (recommended)
  • Refuse a fetch from the same result inside a handler of its warnings with a clear error: order and completeness kept, but a pattern that works in feat: Warn when an Arrow fetch converts or drops a value #961 starts to fail
  • Drop the interrupted chunk in that case: order kept, its rows lost

Kirill decides. Until then the first option stays as implemented.


Generated by Claude Code

claude and others added 3 commits December 1, 2026 08:09
The entry point took the chunk from the result after R allocated
the external pointers and the list of warnings,
so a finalizer run by the garbage collector in between could clear the result
or fetch from it.
It now takes the chunk first, into its own owner, and holds a share of the result.
A failed allocation hands the chunk back with its warnings still to come.
A chunk joins the queue once its coercions are complete.
The validation of a kept chunk is a test of its own,
so that a skip for an old nanoarrow skips nothing else.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants