A mlr3::DataBackend for duckdb.
Can be easily constructed with as_duckdb_backend()
.
Super class
mlr3::DataBackend
-> DataBackendDuckDB
Public fields
levels
(named
list()
)
List (named with column names) of factor levels ascharacter()
. Used to auto-convert character columns to factor variables.connector
(
function()
)
Function which is called to re-connect in case the connection became invalid.table
(
character(1)
)
Data base table or view to operate on.
Active bindings
table_info
(
data.frame()
)
Data frame as returned by pragmatable_info()
.rownames
(
integer()
)
Returns vector of all distinct row identifiers, i.e. the contents of the primary key column.colnames
(
character()
)
Returns vector of all column names, including the primary key column.nrow
(
integer(1)
)
Number of rows (observations).ncol
(
integer(1)
)
Number of columns (variables), including the primary key column.valid
(
logical(1)
)
ReturnsNA
if the data does not inherits from"tbl_sql"
(i.e., it is not a real SQL data base). Returns the result ofDBI::dbIsValid()
otherwise.
Methods
Inherited methods
Method new()
Creates a backend for a duckdb::duckdb()
database.
Usage
DataBackendDuckDB$new(
data,
table,
primary_key,
strings_as_factors = TRUE,
connector = NULL
)
Arguments
data
(connection)
A connection created withDBI::dbConnect()
. If constructed manually (and not via the helper functionas_duckdb_backend()
, make sure that there exists an (unique) index for the key column.table
(
character(1)
)
Table or view to operate on.primary_key
(
character(1)
)
Name of the primary key column.strings_as_factors
(
logical(1)
||character()
)
Either a character vector of column names to convert to factors, or a single logical flag: ifFALSE
, no column will be converted, ifTRUE
all string columns (except the primary key). For conversion, the backend is queried for distinct values of the respective columns on construction and their levels are stored in$levels
.connector
(function()
)\cr If not
NULL`, a function which re-connects to the database in case the connection has become invalid. Database connections can become invalid due to timeouts or if the backend is serialized to the file system and then de-serialized again. This round trip is often performed for parallelization, e.g. to send the objects to remote workers.DBI::dbIsValid()
is called to validate the connection. The function must return just the connection, not adplyr::tbl()
object! Note that this this function is serialized together with the backend, including possible sensitive information such as login credentials. These can be retrieved from the stored mlr3::DataBackend/mlr3::Task. To protect your credentials, it is recommended to use the secret package.
Method finalize()
Finalizer which disconnects from the database. This is called during garbage collection of the instance.
Returns
logical(1)
, the return value of DBI::dbDisconnect()
.
Method data()
Returns a slice of the data.
The rows must be addressed as vector of primary key values, columns must be referred to via column names.
Queries for rows with no matching row id and queries for columns with no matching
column name are silently ignored.
Rows are guaranteed to be returned in the same order as rows
, columns may be returned in an arbitrary order.
Duplicated row ids result in duplicated rows, duplicated column names lead to an exception.
Arguments
rows
integer()
Row indices.cols
character()
Column names.data_format
(
character(1)
)
Desired data format, e.g."data.table"
or"Matrix"
.
Method distinct()
Returns a named list of vectors of distinct values for each column
specified. If na_rm
is TRUE
, missing values are removed from the
returned vectors of distinct values. Non-existing rows and columns are
silently ignored.
Arguments
rows
integer()
Row indices.cols
character()
Column names.na_rm
logical(1)
Whether to remove NAs or not.
Returns
Named list()
of distinct values.
Method missings()
Returns the number of missing values per column in the specified slice of data. Non-existing rows and columns are silently ignored.
Arguments
rows
integer()
Row indices.cols
character()
Column names.
Returns
Total of missing values per column (named numeric()
).