Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes usage of bigint parameter when using dbConnect. #59

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions R/Driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,7 @@ drv_to_string <- function(drv) {
#' @export
duckdb <- function(dbdir = DBDIR_MEMORY, read_only = FALSE, bigint = "numeric", config = list()) {
check_flag(read_only)

switch(bigint,
numeric = {
# fine
},
integer64 = {
if (!is_installed("bit64")) {
stop("bit64 package is required for integer64 support")
}
},
stop(paste("Unsupported bigint configuration", bigint))
)
check_bigint(bigint)

# R packages are not allowed to write extensions into home directory, so use R_user_dir instead
if (!("extension_directory" %in% names(config))) {
Expand Down Expand Up @@ -136,3 +125,17 @@ check_tz <- function(timezone) {

timezone
}

check_bigint <- function(bigint_type) {
switch(bigint_type,
numeric = {
# fine
},
integer64 = {
if (!is_installed("bit64")) {
stop("bit64 package is required for integer64 support")
}
},
stop(paste("Unsupported bigint configuration", bigint_type))
)
}
16 changes: 12 additions & 4 deletions R/dbConnect__duckdb_driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#' If `"force"` is chosen, the timestamp will have the same clock
#' time as the timestamp in the database, but with the new time zone.
#' @param config Named list with DuckDB configuration flags
#' @param bigint How 64-bit integers should be returned, default is double/numeric. Set to integer64 for bit64 encoding.
#' @param bigint How 64-bit integers should be returned. There are two options: `"numeric`" and `"integer64`".
#' If `"numeric`" is selected, bigint integers will be treated as double/numeric.
#' If `"integer64`" is selected, bigint integers will be set to bit64 encoding.
#'
#' @return `dbConnect()` returns an object of class
#' \linkS4class{duckdb_connection}.
Expand All @@ -43,25 +45,31 @@ dbConnect__duckdb_driver <- function(drv, dbdir = DBDIR_MEMORY, ...,
debug = getOption("duckdb.debug", FALSE),
read_only = FALSE,
timezone_out = "UTC",
tz_out_convert = c("with", "force"), config = list(), bigint = "numeric") {
tz_out_convert = c("with", "force"), config = list(), bigint = NULL) {
check_flag(debug)
timezone_out <- check_tz(timezone_out)
tz_out_convert <- match.arg(tz_out_convert)

missing_dbdir <- missing(dbdir)
dbdir <- path.expand(as.character(dbdir))

bigint_type_provided <- !is.null(bigint)
if (bigint_type_provided) {
check_bigint(bigint)
drv@bigint <- bigint
} else {
bigint <- drv@bigint
}

# aha, a late comer. let's make a new instance.
if (!missing_dbdir && dbdir != drv@dbdir) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick question, since dbdir is set by default to DBDIR_MEMORY in the function signature, is there a situation in which it can be missing? Thanks

drv <- duckdb(dbdir, read_only, bigint, config)
}

conn <- duckdb_connection(drv, debug = debug)
on.exit(dbDisconnect(conn))

conn@timezone_out <- timezone_out
conn@tz_out_convert <- tz_out_convert

on.exit(NULL)

rs_on_connection_opened(conn)
Expand Down
8 changes: 5 additions & 3 deletions man/duckdb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions tests/testthat/test_integer64.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# this tests both retrieval and scans
test_that("we can roundtrip an integer64", {
test_that("we can roundtrip an integer64 via driver", {
skip_if_not_installed("bit64")

con <- dbConnect(duckdb(bigint = "integer64"))
on.exit(dbDisconnect(con, shutdown = TRUE))
df <- data.frame(a = bit64::as.integer64(42), b = bit64::as.integer64(-42), c = bit64::as.integer64(NA))
Expand All @@ -11,3 +10,15 @@ test_that("we can roundtrip an integer64", {
res <- dbReadTable(con, "df")
expect_identical(df, res)
})

test_that("we can roundtrip an integer64 via dbConnect", {
skip_if_not_installed("bit64")
con <- dbConnect(duckdb(), bigint = "integer64")
on.exit(dbDisconnect(con, shutdown = TRUE))
df <- data.frame(a = bit64::as.integer64(42), b = bit64::as.integer64(-42), c = bit64::as.integer64(NA))

duckdb_register(con, "df", df)

res <- dbReadTable(con, "df")
expect_identical(df, res)
})