scr_fetch: Fetch a table with reproducible server-side sampling

View source: R/db.R

scr_fetchR Documentation

Fetch a table with reproducible server-side sampling

Description

Sampling happens on the server, not in R: pulling a million rows to discard ninety percent of them pays the network cost twice. max_rows is a memory guard: when it binds, the requested fraction is reduced on the server and the reduction is reported. The random expression follows the connection class (rand(seed) on Spark/Databricks/MySQL, random() on PostgreSQL/DuckDB, an integer-modulo expression on SQLite, which has no seedable random()); pass sample_expr to override it.

Usage

scr_fetch(
  con,
  table,
  sample_frac = 1,
  seed = NULL,
  max_rows = NULL,
  sample_expr = NULL,
  verbose = NULL
)

Arguments

con

A DBI connection, from scr_connect().

table

Qualified table name.

sample_frac

Fraction of rows to fetch, in (0, 1].

seed

Seed of the server-side random function, where supported.

max_rows

Row cap. NULL switches it off.

sample_expr

Optional SQL expression yielding a uniform number in ⁠[0, 1)⁠, used as ⁠WHERE <sample_expr> <= sample_frac⁠.

verbose

TRUE/FALSE to echo (or not) the query for this call; NULL follows scr_verbose().

Value

A data.table with the fetched table.

See Also

Other database: scr_connect()

Examples


con <- scr_connect(driver = RSQLite::SQLite(), dbname = ":memory:")
d <- scr_demo; d$ref_date <- as.character(d$ref_date)
DBI::dbWriteTable(con, "dtm", d)
nrow(scr_fetch(con, "dtm", sample_frac = 0.5, seed = 42))
nrow(scr_fetch(con, "dtm", max_rows = 1000))
DBI::dbDisconnect(con)


scorecraft documentation built on Dec. 6, 2026, 1:09 a.m.