Skip to content

Commit

Permalink
check_bigint() and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tau31 authored and krlmlr committed Mar 24, 2024
1 parent bb0308d commit 472f1ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
28 changes: 15 additions & 13 deletions R/Driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ driver_registry <- new.env(parent = emptyenv())
#' @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 @@ -165,7 +154,6 @@ check_tz <- function(timezone) {

timezone
}

path_normalize <- function(path) {
if (path == "" || path == DBDIR_MEMORY) {
return(DBDIR_MEMORY)
Expand All @@ -181,3 +169,17 @@ path_normalize <- function(path) {
}
out
}

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))
)
}
7 changes: 5 additions & 2 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 Down Expand Up @@ -65,6 +67,8 @@ dbConnect__duckdb_driver <- function(

if (missing(bigint)) {
bigint <- drv@bigint
} else {
check_bigint(bigint)
}

config <- utils::modifyList(drv@config, config)
Expand All @@ -79,7 +83,6 @@ dbConnect__duckdb_driver <- function(

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

on.exit(NULL)

rs_on_connection_opened(conn)
Expand Down
4 changes: 3 additions & 1 deletion 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)
})

0 comments on commit 472f1ef

Please sign in to comment.