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

feat: Point to the native CSV reader if encountering data frames read with readr #469

Merged
merged 2 commits into from
Jan 17, 2025
Merged
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Suggests:
palmerpenguins,
prettycode,
purrr,
readr,
rmarkdown,
testthat (>= 3.1.5),
usethis,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ S3method(as_duckdb_tibble,default)
S3method(as_duckdb_tibble,duckplyr_df)
S3method(as_duckdb_tibble,grouped_df)
S3method(as_duckdb_tibble,rowwise_df)
S3method(as_duckdb_tibble,spec_tbl_df)
S3method(as_duckdb_tibble,tbl_duckdb_connection)
S3method(as_tibble,duckplyr_df)
S3method(auto_copy,duckplyr_df)
Expand Down
11 changes: 11 additions & 0 deletions R/ducktbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ as_duckdb_tibble.rowwise_df <- function(x, ...) {
))
}

#' @export
as_duckdb_tibble.spec_tbl_df <- function(x, ...) {
check_dots_empty()

cli::cli_abort(c(
"The input is data read by {.pkg readr}, and {.pkg duckplyr} supports reading CSV files directly.",
i = "Use {.code read_csv_duckdb()} to use the built-in reader.",
i = "To proceed with the data as read by {.pkg readr}, use {.code as_tibble()} before {.code as_duckdb_tibble()}."
))
}

#' is_duckdb_tibble
#'
#' `is_duckdb_tibble()` returns `TRUE` if `x` is a duckplyr data frame.
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/ducktbl.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@
! duckplyr does not support `rowwise()`.
i To proceed with dplyr, use `as_tibble()` or `as.data.frame()`.

# as_duckdb_tibble() and readr data

Code
as_duckdb_tibble(readr::read_csv(path, show_col_types = FALSE))
Condition
Error in `as_duckdb_tibble()`:
! The input is data read by readr, and duckplyr supports reading CSV files directly.
i Use `read_csv_duckdb()` to use the built-in reader.
i To proceed with the data as read by readr, use `as_tibble()` before `as_duckdb_tibble()`.

16 changes: 16 additions & 0 deletions tests/testthat/test-ducktbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ test_that("as_duckdb_tibble() and rowwise df", {
})
})

test_that("as_duckdb_tibble() and readr data", {
skip_if_not_installed("readr")

path <- withr::local_tempfile(fileext = ".csv")
readr::write_csv(data.frame(a = 1), path)

expect_snapshot(error = TRUE, {
as_duckdb_tibble(readr::read_csv(path, show_col_types = FALSE))
})

expect_equal(
as_duckdb_tibble(as_tibble(readr::read_csv(path, show_col_types = FALSE))),
duckdb_tibble(a = 1)
)
})

test_that("as_duckdb_tibble() and dbplyr tables", {
skip_if_not_installed("dbplyr")
con <- withr::local_db_connection(DBI::dbConnect(duckdb::duckdb()))
Expand Down
Loading