Skip to content

Commit

Permalink
Merge pull request #223 from ThomasSoeiro/main
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Oct 14, 2024
2 parents f8af88b + dd3ef2c commit a60fe7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
26 changes: 21 additions & 5 deletions R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#'
#' @inheritParams duckdb_register
#' @param files One or more CSV file names, should all have the same structure though
#' @param ... Reserved for future extensions, must be empty.
#' @param header Whether or not the CSV files have a separate header in the first line
#' @param na.strings Which strings in the CSV files should be considered to be NULL
#' @param nrow.check How many rows should be read from the CSV file to figure out data types
Expand All @@ -14,7 +15,7 @@
#' @param lower.case.names Transform column names to lower case
#' @param sep Alias for delim for compatibility
#' @param transaction Should a transaction be used for the entire operation
#' @param ... Passed on to [read.csv()]
#' @param temporary Set to `TRUE` to create a temporary table
#' @return The number of rows in the resulted table, invisibly.
#' @export
#' @examplesIf duckdb:::TEST_RE2
Expand All @@ -29,9 +30,24 @@
#' dbReadTable(con, "data")
#'
#' dbDisconnect(con)
duckdb_read_csv <- function(conn, name, files, header = TRUE, na.strings = "", nrow.check = 500,
delim = ",", quote = "\"", col.names = NULL, lower.case.names = FALSE, sep = delim, transaction = TRUE, ...) {
#
duckdb_read_csv <- function(
conn,
name,
files,
...,
header = TRUE,
na.strings = "",
nrow.check = 500,
delim = ",",
quote = "\"",
col.names = NULL,
lower.case.names = FALSE,
sep = delim,
transaction = TRUE,
temporary = FALSE
) {
# FIXME: Warning as of duckdb 1.1.1, turn this into an error later
if (...length() > 0) warning("Arguments passed to ... are currently not used")
if (length(na.strings) > 1) stop("na.strings must be of length 1")
if (!missing(sep)) delim <- sep

Expand Down Expand Up @@ -67,7 +83,7 @@ duckdb_read_csv <- function(conn, name, files, header = TRUE, na.strings = "", n
}
names(headers[[1]]) <- col.names
}
dbWriteTable(conn, tablename, headers[[1]][FALSE, , drop = FALSE])
dbCreateTable(conn, tablename, headers[[1]], temporary = temporary)
}

for (i in seq_along(files)) {
Expand Down
7 changes: 5 additions & 2 deletions man/duckdb_read_csv.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,17 @@ test_that("duckdb_read_csv() works as expected", {
read.csv(tf3, na.strings = "-")
)

# temporary table
# see https://github.com/duckdb/duckdb-r/issues/142
db <- tempfile()
con2 <- dbConnect(duckdb(), dbdir = db)
duckdb_read_csv(con2, "iris", tf, temporary = TRUE)
expect_true(length(dbListTables(con2)) == 1)
dbDisconnect(con2)

con2 <- dbConnect(duckdb(), dbdir = db)
expect_true(length(dbListTables(con2)) == 0)
dbDisconnect(con2)

dbDisconnect(con, shutdown = TRUE)
})

0 comments on commit a60fe7a

Please sign in to comment.