From 6f6bd5747fa3ee7f7b868b3079f6001304edb312 Mon Sep 17 00:00:00 2001 From: Nicolas Bennett <3158446+nbenn@users.noreply.github.com> Date: Thu, 9 Nov 2023 09:35:21 +0100 Subject: [PATCH] add arrow example --- R/ResultArrow.R | 2 +- R/dbBind_Result.R | 7 ++- README.Rmd | 30 ++++++++- README.md | 155 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 187 insertions(+), 7 deletions(-) diff --git a/R/ResultArrow.R b/R/ResultArrow.R index afa5a93..06e1446 100644 --- a/R/ResultArrow.R +++ b/R/ResultArrow.R @@ -11,7 +11,7 @@ AdbiResultArrow <- function(connection, statement, immediate = NULL, #' @export setClass( "AdbiResultArrow", - contains = "DBIResult", + contains = "DBIResultArrow", slots = list( statement = "ANY", metadata = "environment" diff --git a/R/dbBind_Result.R b/R/dbBind_Result.R index 2b66c6c..e4eda0b 100644 --- a/R/dbBind_Result.R +++ b/R/dbBind_Result.R @@ -110,8 +110,11 @@ dbBind_AdbiResult <- function(res, params, ...) { if (!is.null(meta(res, "data"))) { if (!isTRUE(meta(res, "has_completed"))) { - # only triggers if fetched past end - warning("Not all data has been fetched.", call. = FALSE) + # trigger can only be disabled if fetched past end + warning( + "It is possible that not all data has been fetched.", + call. = FALSE + ) } else { meta(res, "has_completed") <- FALSE } diff --git a/README.Rmd b/README.Rmd index c94b2d0..3575f59 100644 --- a/README.Rmd +++ b/README.Rmd @@ -33,9 +33,9 @@ devtools::install_github("r-dbi/adbi") ## Example -This is a basic example which shows you how to solve a common problem: +The `data.frame` API of DBI is supported. -```{r example} +```{r df} library(DBI) # Create an SQLite connection using the adbcsqlite backend @@ -58,6 +58,30 @@ dbFetch(res) # Cleanup dbClearResult(res) -dbDisconnect(con) ``` +More interestingly, the recent arrow-extension API of DBI is supported as well. + +```{r arrow} +# Queries +dbGetQueryArrow(con, "SELECT * from swiss WHERE Agriculture < 40") + +# Prepared statements +res <- dbSendQueryArrow(con, "SELECT * from swiss WHERE Agriculture < ?") + +dbBind(res, 30) + +while (!dbHasCompleted(res)) { + print(dbFetchArrow(res)) +} + +dbBind(res, 20) + +while(!dbHasCompleted(res)) { + print(dbFetchArrow(res)) +} + +# Cleanup +dbClearResult(res) +dbDisconnect(con) +``` diff --git a/README.md b/README.md index df23a79..ce83f29 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ devtools::install_github("r-dbi/adbi") ## Example -This is a basic example which shows you how to solve a common problem: +The `data.frame` API of DBI is supported. ``` r library(DBI) @@ -86,6 +86,159 @@ dbFetch(res) #> 7 67.6 18.7 25 7 8.65 19.5 #> 8 35.0 1.2 37 53 42.34 18.0 +# Cleanup +dbClearResult(res) +``` + +More interestingly, the recent arrow-extension API of DBI is supported +as well. + +``` r +# Queries +dbGetQueryArrow(con, "SELECT * from swiss WHERE Agriculture < 40") +#> +#> $ length : int 16 +#> $ null_count: int 0 +#> $ offset : int 0 +#> $ buffers :List of 1 +#> ..$ :[0][0 b]> `` +#> $ children :List of 6 +#> ..$ Fertility : +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `80.2 92.5 85.8 76.1...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Agriculture : +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `17.0 39.7 36.5 35.3...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Examination : +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `15 5 12 9 17 26 31 2...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Education : +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `12 5 7 7 8 28 20 19 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Catholic : +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `9.96 93.40 33.77 90...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Infant.Mortality: +#> .. ..$ length : int 16 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[16][128 b]> `22.2 20.2 20.3 26.6...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> $ dictionary: NULL + +# Prepared statements +res <- dbSendQueryArrow(con, "SELECT * from swiss WHERE Agriculture < ?") + +dbBind(res, 30) + +while (!dbHasCompleted(res)) { + print(dbFetchArrow(res)) +} +#> +#> $ length : int 10 +#> $ null_count: int 0 +#> $ offset : int 0 +#> $ buffers :List of 1 +#> ..$ :[0][0 b]> `` +#> $ children :List of 6 +#> ..$ Fertility : +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `80.2 55.7 54.3 58.3 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Agriculture : +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `17.0 19.4 15.2 26.8 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Examination : +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `15 26 31 25 29 22 35 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Education : +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `12 28 20 19 11 13 32 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Catholic : +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `9.96 12.11 2.15 18.4...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> ..$ Infant.Mortality: +#> .. ..$ length : int 10 +#> .. ..$ null_count: int -1 +#> .. ..$ offset : int 0 +#> .. ..$ buffers :List of 2 +#> .. .. ..$ :[16][2 b]> `TRUE TRUE TRUE TRUE...` +#> .. .. ..$ :[10][80 b]> `22.2 20.2 10.8 20.9 ...` +#> .. ..$ dictionary: NULL +#> .. ..$ children : list() +#> $ dictionary: NULL +#> [1] Fertility Agriculture Examination Education +#> [5] Catholic Infant.Mortality +#> <0 rows> (or 0-length row.names) + +dbBind(res, 20) + +while(!dbHasCompleted(res)) { + print(dbFetchArrow(res)) +} + # Cleanup dbClearResult(res) dbDisconnect(con)