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: Rename duck_tbl() to duckdb_tibble(), and as_duck_tbl() to as_duckdb_tibble() #457

Merged
merged 2 commits into from
Jan 11, 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
18 changes: 9 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ S3method(anti_join,duckplyr_df)
S3method(arrange,duckplyr_df)
S3method(as.data.frame,duckplyr_df)
S3method(as.data.frame,lazy_duckplyr_df)
S3method(as_duck_tbl,data.frame)
S3method(as_duck_tbl,default)
S3method(as_duck_tbl,duckplyr_df)
S3method(as_duck_tbl,grouped_df)
S3method(as_duck_tbl,rowwise_df)
S3method(as_duck_tbl,tbl_duckdb_connection)
S3method(as_duckdb_tibble,data.frame)
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,tbl_duckdb_connection)
S3method(as_tibble,duckplyr_df)
S3method(auto_copy,duckplyr_df)
S3method(collect,duckplyr_df)
Expand Down Expand Up @@ -110,7 +110,7 @@ S3method(ungroup,duckplyr_df)
S3method(union,duckplyr_df)
S3method(union_all,duckplyr_df)
export("%>%")
export(as_duck_tbl)
export(as_duckdb_tibble)
export(as_duckplyr_df)
export(as_duckplyr_tibble)
export(compute_csv)
Expand All @@ -125,7 +125,7 @@ export(duck_file)
export(duck_json)
export(duck_parquet)
export(duck_sql)
export(duck_tbl)
export(duckdb_tibble)
export(duckplyr_df_from_csv)
export(duckplyr_df_from_file)
export(duckplyr_df_from_parquet)
Expand All @@ -136,7 +136,7 @@ export(fallback_review)
export(fallback_sitrep)
export(fallback_upload)
export(flights_df)
export(is_duck_tbl)
export(is_duckdb_tibble)
export(is_duckplyr_df)
export(last_rel)
export(methods_overwrite)
Expand Down
6 changes: 3 additions & 3 deletions R/as_duckplyr_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
#' as_duckplyr_df() %>%
#' mutate(b = a + 1)
as_duckplyr_df <- function(.data) {
lifecycle::deprecate_soft("1.0.0", "as_duckplyr_df()", "as_duck_tbl()")
lifecycle::deprecate_soft("1.0.0", "as_duckplyr_df()", "as_duckdb_tibble()")

as_duckplyr_df_impl(.data)
}

as_duckplyr_df_impl <- function(x, error_call = caller_env()) {
# FIXME: Move to as_duck_tbl()
# FIXME: Move to as_duckdb_tibble()
if (!identical(class(x), "data.frame") && !identical(class(x), c("tbl_df", "tbl", "data.frame"))) {
cli::cli_abort(call = error_call, c(
"Must pass a plain data frame or a tibble, not {.obj_type_friendly {x}}.",
i = "Convert it with {.fun as.data.frame} or {.fun tibble::as_tibble}."
))
}

new_duck_tbl(x, class = class(x), error_call = error_call)
new_duckdb_tibble(x, class = class(x), error_call = error_call)
}
2 changes: 1 addition & 1 deletion R/as_duckplyr_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @rdname as_duckplyr_df
#' @export
as_duckplyr_tibble <- function(.data) {
lifecycle::deprecate_soft("1.0.0", "as_duckplyr_tibble()", "as_duck_tbl()")
lifecycle::deprecate_soft("1.0.0", "as_duckplyr_tibble()", "as_duckdb_tibble()")

if (inherits(.data, "tbl_duckdb_connection")) {
con <- dbplyr::remote_con(.data)
Expand Down
2 changes: 1 addition & 1 deletion R/collect-rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @inheritParams dplyr::collect
#' @examples
#' library("duckplyr")
#' df <- duck_tbl(x = c(1, 2), .lazy = TRUE)
#' df <- duckdb_tibble(x = c(1, 2), .lazy = TRUE)
#' df
#' try(print(df$x))
#' df <- collect(df)
Expand Down
4 changes: 2 additions & 2 deletions R/compute-rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#'
#' @inheritParams dplyr::compute
#' @param lazy Set to `TRUE` to return a lazy or `FALSE` to return an eager data frame,
#' see the "Eager and lazy" section in [duck_tbl()].
#' see the "Eager and lazy" section in [duckdb_tibble()].
#' The default is to inherit the lazyness of the input.
#' @param name The name of the table to store the result in.
#' @param schema_name The schema to store the result in, defaults to the current schema.
#' @param temporary Set to `FALSE` to store the result in a permanent table.
#' @examples
#' library("duckplyr")
#' df <- duck_tbl(x = c(1, 2))
#' df <- duckdb_tibble(x = c(1, 2))
#' df <- mutate(df, y = 2)
#' df <- compute(df)
#' explain(df)
Expand Down
2 changes: 1 addition & 1 deletion R/compute.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ compute.duckplyr_df <- function(
out <- duckplyr_reconstruct(out_rel, x)

if (is_lazy_duckplyr_df(out) != lazy) {
out <- as_duck_tbl(out, .lazy = lazy)
out <- as_duckdb_tibble(out, .lazy = lazy)
}

return(out)
Expand Down
66 changes: 33 additions & 33 deletions R/ducktbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' For such objects,
#' dplyr verbs such as [mutate()], [select()] or [filter()] will use DuckDB.
#'
#' `duck_tbl()` works like [tibble()], returning an "eager" duckplyr data frame by default.
#' `duckdb_tibble()` works like [tibble()], returning an "eager" duckplyr data frame by default.
#' See the "Eager and lazy" section below.
#'
#' @section Eager and lazy:
Expand All @@ -24,7 +24,7 @@
#' Once the results are computed, they are cached and subsequent requests are fast.
#' This is a good choice for small to medium-sized data,
#' where DuckDB can provide a nice speedup but materializing the data is affordable.
#' This is the default for `duck_tbl()` and `as_duck_tbl()`.
#' This is the default for `duckdb_tibble()` and `as_duckdb_tibble()`.
#' Use `.lazy = TRUE` for these functions to check
#' that all operations are supported by DuckDB.
#'
Expand All @@ -37,7 +37,7 @@
#' It is safe to use `duck_parquet(lazy = FALSE)`
#' if the data is small enough to be materialized at any stage.
#'
#' A lazy duckplyr frame can be converted to an eager one with `as_duck_tbl(.lazy = FALSE)`.
#' A lazy duckplyr frame can be converted to an eager one with `as_duckdb_tibble(.lazy = FALSE)`.
#' The [collect.duckplyr_df()] method converts to a plain tibble.
#' Other useful methods include [compute_file()] for storing results in a file,
#' and [compute.duckplyr_df()] for storing results in temporary storage on disk.
Expand All @@ -54,18 +54,18 @@
#' but "eager" tables are not available there at the time of writing,
#' and the data must always be brought into R memory through [collect()].
#'
#' @param ... For `duck_tbl()`, passed on to [tibble()].
#' For `as_duck_tbl()`, passed on to methods.
#' @param ... For `duckdb_tibble()`, passed on to [tibble()].
#' For `as_duckdb_tibble()`, passed on to methods.
#' @param .lazy Logical, whether to create a lazy duckplyr frame.
#' See the section "Eager and lazy" for details.
#'
#' @return For `duck_tbl()` and `as_duck_tbl()`, an object with the following classes:
#' @return For `duckdb_tibble()` and `as_duckdb_tibble()`, an object with the following classes:
#' - `"lazy_duckplyr_df"` if `.lazy` is `TRUE`
#' - `"duckplyr_df"`
#' - Classes of a [tibble]
#'
#' @examples
#' x <- duck_tbl(a = 1)
#' x <- duckdb_tibble(a = 1)
#' x
#'
#' library(dplyr)
Expand All @@ -74,26 +74,26 @@
#'
#' x$a
#'
#' y <- duck_tbl(a = 1, .lazy = TRUE)
#' y <- duckdb_tibble(a = 1, .lazy = TRUE)
#' y
#' try(length(y$a))
#' length(collect(y)$a)
#' @export
duck_tbl <- function(..., .lazy = FALSE) {
duckdb_tibble <- function(..., .lazy = FALSE) {
out <- tibble::tibble(...)
as_duck_tbl(out, .lazy = .lazy)
as_duckdb_tibble(out, .lazy = .lazy)
}

#' as_duck_tbl
#' as_duckdb_tibble
#'
#' `as_duck_tbl()` converts a data frame or a dplyr lazy table to a duckplyr data frame.
#' `as_duckdb_tibble()` converts a data frame or a dplyr lazy table to a duckplyr data frame.
#' This is a generic function that can be overridden for custom classes.
#'
#' @param x The object to convert or to test.
#' @rdname duck_tbl
#' @rdname duckdb_tibble
#' @export
as_duck_tbl <- function(x, ..., .lazy = FALSE) {
out <- as_duck_tbl_dispatch(x, ...)
as_duckdb_tibble <- function(x, ..., .lazy = FALSE) {
out <- as_duckdb_tibble_dispatch(x, ...)

if (isTRUE(.lazy)) {
out <- as_lazy_duckplyr_df(out)
Expand All @@ -102,14 +102,14 @@ as_duck_tbl <- function(x, ..., .lazy = FALSE) {
}

return(out)
UseMethod("as_duck_tbl")
UseMethod("as_duckdb_tibble")
}
as_duck_tbl_dispatch <- function(x, ...) {
UseMethod("as_duck_tbl")
as_duckdb_tibble_dispatch <- function(x, ...) {
UseMethod("as_duckdb_tibble")
}

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

con <- dbplyr::remote_con(x)
Expand All @@ -119,33 +119,33 @@ as_duck_tbl.tbl_duckdb_connection <- function(x, ...) {
}

#' @export
as_duck_tbl.duckplyr_df <- function(x, ...) {
as_duckdb_tibble.duckplyr_df <- function(x, ...) {
check_dots_empty()
x
}

#' @export
as_duck_tbl.data.frame <- function(x, ...) {
as_duckdb_tibble.data.frame <- function(x, ...) {
check_dots_empty()

tbl <- as_tibble(x)

# - as_tibble() to remove row names
new_duck_tbl(tbl)
new_duckdb_tibble(tbl)
}

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

# - as.data.frame() call for good measure and perhaps https://github.com/tidyverse/tibble/issues/1556
# - as_tibble() to remove row names
# Could call as_duck_tbl(as.data.frame(x)) here, but that would be slower
new_duck_tbl(as_tibble(as.data.frame(x)))
# Could call as_duckdb_tibble(as.data.frame(x)) here, but that would be slower
new_duckdb_tibble(as_tibble(as.data.frame(x)))
}

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

cli::cli_abort(c(
Expand All @@ -156,7 +156,7 @@ as_duck_tbl.grouped_df <- function(x, ...) {
}

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

cli::cli_abort(c(
Expand All @@ -165,21 +165,21 @@ as_duck_tbl.rowwise_df <- function(x, ...) {
))
}

#' is_duck_tbl
#' is_duckdb_tibble
#'
#' `is_duck_tbl()` returns `TRUE` if `x` is a duckplyr data frame.
#' `is_duckdb_tibble()` returns `TRUE` if `x` is a duckplyr data frame.
#'
#' @return For `is_duck_tbl()`, a scalar logical.
#' @rdname duck_tbl
#' @return For `is_duckdb_tibble()`, a scalar logical.
#' @rdname duckdb_tibble
#' @export
is_duck_tbl <- function(x) {
is_duckdb_tibble <- function(x) {
inherits(x, "duckplyr_df")
}


#' @param lazy Only adds the class, does not recreate the relation object!
#' @noRd
new_duck_tbl <- function(x, class = NULL, lazy = FALSE, error_call = caller_env()) {
new_duckdb_tibble <- function(x, class = NULL, lazy = FALSE, error_call = caller_env()) {
if (is.null(class)) {
class <- c("tbl_df", "tbl", "data.frame")
}
Expand Down
2 changes: 1 addition & 1 deletion R/flights.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Provides a copy of `nycflights13::flights` that is compatible with duckplyr,
#' as a tibble.
#' Call [as_duck_tbl()] to enable duckplyr operations.
#' Call [as_duckdb_tibble()] to enable duckplyr operations.
#'
#' @export
#' @examplesIf requireNamespace("nycflights13", quietly = TRUE)
Expand Down
8 changes: 4 additions & 4 deletions R/io2.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ duck_json <- function(path, ..., lazy = TRUE, options = list()) {
#' `"read_csv"`, `"read_csv_auto"` or `"read_json"`.
#' @param lazy Logical, whether to create a lazy duckplyr frame.
#' By default, a lazy duckplyr frame is created.
#' See the "Eager and lazy" section in [duck_tbl()] for details.
#' See the "Eager and lazy" section in [duckdb_tibble()] for details.
#' @param options Arguments to the DuckDB function
#' indicated by `table_function`.
#'
#' @return A duckplyr frame, see [as_duck_tbl()] for details.
#' @return A duckplyr frame, see [as_duckdb_tibble()] for details.
#'
#' @rdname duck_file
#' @export
Expand Down Expand Up @@ -154,10 +154,10 @@ duckfun <- function(table_function, args, ..., lazy = TRUE) {

# Start with lazy, to avoid unwanted materialization
df <- duckdb$rel_to_altrep(rel, allow_materialization = FALSE)
out <- new_duck_tbl(df, lazy = TRUE)
out <- new_duckdb_tibble(df, lazy = TRUE)

if (!lazy) {
out <- as_duck_tbl(out, .lazy = lazy)
out <- as_duckdb_tibble(out, .lazy = lazy)
}

out
Expand Down
2 changes: 1 addition & 1 deletion R/is_duckplyr_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' as_duckplyr_df() %>%
#' is_duckplyr_df()
is_duckplyr_df <- function(.data) {
lifecycle::deprecate_soft("1.0.0", "is_duckplyr_df()", "is_duck_tbl()")
lifecycle::deprecate_soft("1.0.0", "is_duckplyr_df()", "is_duckdb_tibble()")

inherits(.data, "duckplyr_df")
}
2 changes: 1 addition & 1 deletion R/relational-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ duckdb_rel_from_df <- function(df) {
return(rel)
}

if (!is_duck_tbl(df)) {
if (!is_duckdb_tibble(df)) {
df <- as_duckplyr_df_impl(df)
}

Expand Down
2 changes: 1 addition & 1 deletion R/relational.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ check_lazy <- function(x, duckplyr_error, call = caller_env()) {
"This operation cannot be carried out by DuckDB, and the input is a lazy duckplyr frame.",
"*" = duckplyr_error_msg,
"*" = "Use {.code compute(lazy = FALSE)} to materialize to temporary storage and continue with {.pkg duckplyr}.",
"*" = 'See the "Eager and lazy" section in {.help duck_tbl} for other options.'
"*" = 'See the "Eager and lazy" section in {.help duckdb_tibble} for other options.'
))
}
}
4 changes: 2 additions & 2 deletions R/sql.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Return SQL query as duck_tbl
#' Return SQL query as duckdb_tibble
#'
#' @description
#' `r lifecycle::badge("experimental")`
Expand Down Expand Up @@ -33,5 +33,5 @@ duck_sql <- function(sql, ..., lazy = TRUE, con = NULL) {
meta_rel_register(rel, expr(duckdb$rel_from_sql(con, !!sql)))

out <- duckdb$rel_to_altrep(rel)
as_duck_tbl(out, .lazy = lazy)
as_duckdb_tibble(out, .lazy = lazy)
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Fallback is silent by default, but can be made verbose.
Sys.setenv(DUCKPLYR_FALLBACK_INFO = TRUE)
out <-
nycflights13::flights %>%
duckplyr::as_duck_tbl() %>%
duckplyr::as_duckdb_tibble() %>%
mutate(inflight_delay = arr_delay - dep_delay)
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ Fallback is silent by default, but can be made verbose.
Sys.setenv(DUCKPLYR_FALLBACK_INFO = TRUE)
out <-
nycflights13::flights %>%
duckplyr::as_duck_tbl() %>%
duckplyr::as_duckdb_tibble() %>%
mutate(inflight_delay = arr_delay - dep_delay)
#> Error processing duckplyr query with DuckDB, falling back to dplyr.
#> Caused by error in `check_df_for_rel()` at duckplyr/R/relational-duckdb.R:100:3:
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ template:
reference:
- title: Using duckplyr
contents:
- duck_tbl
- duckdb_tibble
- duck_file
- duck_sql

Expand Down
Loading
Loading