Skip to contents

[Experimental]

Signal that a chunk returned by dbFetchArrowChunk() is no longer needed, so that the backend can free the memory it holds. Calling this function is optional: a chunk that is not cleared is released by the garbage collector. Use it when memory is tight and chunks are large.

Usage

dbClearArrowChunk(res, chunk, ...)

Arguments

res

An object inheriting from DBI::DBIResultArrow, created by dbSendQueryArrow().

chunk

A chunk returned by dbFetchArrowChunk() for res.

...

Other arguments passed on to methods.

Value

dbClearArrowChunk() returns TRUE, invisibly.

Details

The chunk returned by dbFetchArrowChunk() is owned by the caller. It remains valid until it is garbage collected or passed to dbClearArrowChunk(), even after further chunks have been fetched and after the result set has been cleared with dbClearResult().

After dbClearArrowChunk() returns, the chunk must not be used again: its buffers may have been released. A chunk handed to another library must not be cleared before that library is done with it. The result set is unaffected, further chunks can be fetched with dbFetchArrowChunk(). Clearing a chunk that has already been cleared is harmless. dbClearArrowChunk() can also be called after dbClearResult().

The default method releases chunks that are nanoarrow arrays and does nothing for other objects, so that backends are not required to implement it. A data frame created with as.data.frame() from a chunk returned by dbFetchArrowChunk() remains valid after the chunk is cleared. Vectors converted from a chunk that is not a struct array may still refer to its buffers and must not be used after clearing. Backends that hand out memory they manage themselves can override the method to release that memory.

Examples

con <- dbConnect(RSQLite::SQLite(), ":memory:")

dbWriteTable(con, "mtcars", mtcars)

rs <- dbSendQueryArrow(con, "SELECT * FROM mtcars WHERE cyl = 4")
while (!dbHasCompleted(rs)) {
  chunk <- dbFetchArrowChunk(rs)
  print(nrow(as.data.frame(chunk)))
  dbClearArrowChunk(rs, chunk)
}
#> [1] 11
dbClearResult(rs)

dbDisconnect(con)