This article describes the translations provided by duckplyr for
different data types, verbs, and functions within verbs. If a
translation is not provided, duckplyr falls back to dplyr, see
vignette("fallback")
for details. The translation layer can
be bypassed, see vignette("duckdb")
for details.
library(conflicted)
library(dplyr)
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")
duckplyr supports the following data types:
is.logical()
is.integer()
is.numeric()
is.character()
is.Date()
is.POSIXct()
(with UTC time zone)is.difftime()
duckplyr::duckdb_tibble(
logical = TRUE,
integer = 1L,
numeric = 1.1,
character = "a",
Date = as.Date("2025-01-11"),
POSIXct = as.POSIXct("2025-01-11 19:23:00", tz = "UTC"),
difftime = as.difftime(1, units = "secs"),
) |>
compute()
Generally, zero-column tibbles are not supported by duckplyr, neither as input nor as a result.
Support for more data types, and passthrough of unknown data types, is planned. Let’s discuss any additional data types you would like to see supported.
Not all dplyr verbs are implemented within duckplyr. For unsupported
verbs, duckplyr automatically falls back to dplyr. See
?unsupported
for a list of verbs for which duckplyr does
not provide a method.
See the reference index for a list of verbs with corresponding duckplyr methods.
Let’s discuss any additional verbs you would like to see supported.
For all functions used in dplyr verbs, translations must be provided. If an expression contains a function for which no translation is provided, duckplyr falls back to dplyr. With some exceptions, only positional matching is implemented.
As of now, here are the translations provided:
Implemented: (
.
Reference: ?Paren
.
Implemented: >
, >=
,
<
, <=
, ==
,
!=
.
Reference: ?Comparison
.
Implemented: +
, -
, *
,
/
.
Reference: ?Arithmetic
.
Implemented: log()
, log10()
,
abs()
.
Reference: ?Math
.
Implemented: !
, &
, |
.
Reference: ?Logic
.
Implemented:
is.na()
, as.integer()
dplyr::if_else()
, dplyr::coalesce()
strftime(x, format)
Implemented: grepl()
, substr()
,
sub()
, gsub()
.
Implemented: lubridate::hour()
,
lubridate::minute()
, lubridate::second()
,
lubridate::wday()
.
Implemented:
sum(x, na.rm)
, dplyr::n()
,
dplyr::n_distinct()
mean(x, na.rm)
, median(x, na.rm)
,
sd(x, na.rm)
min()
, max()
, any()
,
all()
duckplyr::duckdb_tibble(a = 1:3, b = c(1, 2, 2), .prudence = "stingy") |>
summarize(
sum(a),
n(),
n_distinct(b),
)
duckplyr::duckdb_tibble(a = 1:3, b = c(1, 2, NA), .prudence = "stingy") |>
summarize(
mean(b, na.rm = TRUE),
median(a),
sd(b),
)
duckplyr::duckdb_tibble(a = 1:3, .prudence = "stingy") |>
summarize(
min(a),
max(a),
any(a > 1),
all(a > 1),
)
All optional arguments to dplyr::lag()
and
dplyr::lead()
are supported.
duckplyr::duckdb_tibble(a = 1:3, .prudence = "stingy") |>
mutate(lag(a), lead(a))
duckplyr::duckdb_tibble(a = 1:3, .prudence = "stingy") |>
mutate(lag(a, 2), lead(a, n = 2))
duckplyr::duckdb_tibble(a = 1:3, .prudence = "stingy") |>
mutate(lag(a, default = 0), lead(a, default = 4))
duckplyr::duckdb_tibble(a = 1:3, b = c(2, 3, 1), .prudence = "stingy") |>
mutate(lag(a, order_by = b), lead(a, order_by = b))
Ranking in DuckDB is very different from dplyr. Most functions in DuckDB rank only by the current row number, whereas in dplyr, ranking is done by a column. It will be difficult to provide translations for the following ranking functions.
rank()
, dplyr::min_rank()
,
dplyr::dense_rank()
dplyr::percent_rank()
,
dplyr::cume_dist()
Implementing dplyr::ntile()
is feasible for the
n
argument. The only ranking function currently implemented
is dplyr::row_number()
.
$
(?Extract
) is implemented if the LHS is
.data
or .env
:
b <- 4
duckplyr::duckdb_tibble(a = 1, b = 2, .prudence = "stingy") |>
mutate(.data$a + .data$b, .env$b)
%in%
(?match
) is implemented if the RHS is
a constant with up to 100 values:
duckplyr::duckdb_tibble(a = 1:3, .prudence = "stingy") |>
mutate(a %in% c(1, 3)) |>
collect()
duckplyr::last_rel()
dplyr::desc()
is only implemented in the context of
dplyr::arrange()
:
suppressWarnings()
is a no-op:
Refer to our contributing guide to learn how to contribute new translations to the package. Ideally, duckplyr will also support adding custom translations for functions for the duration of the current R session.
This section tracks known incompatibilities between dplyr and duckplyr. Changing these is likely to require substantial effort, and might be best addressed by providing new functions with consistent behavior in both dplyr and DuckDB.
DuckDB does not guarantee order stability for the output. For performance reasons, duckplyr does not enable output order stability by default.
duckplyr::flights_df() |>
duckplyr::as_duckdb_tibble() |>
distinct(day) |>
summarize(paste(day, collapse = " ")) # fallback
duckplyr::flights_df() |>
distinct(day) |>
summarize(paste(day, collapse = " "))
This can be changed globally with the
DUCKPLYR_OUTPUT_ORDER
environment variable, see
?config
for details. With this setting, the output order is
stable, but the plans are more complicated, and DuckDB needs to do more
work.
sum()
In duckplyr, this function returns a numeric value also for integers, due to DuckDB’s type stability requirement.
At the time of writing, empty vectors only occur when summarizing an
empty table without grouping. In all cases, duckplyr returns
NA
, and the behavior of dplyr is different:
sum()
for an empty vector returns 0
any()
and all()
return
FALSE
min()
and max()
return infinity values
(with a warning)min()
and max()
for logical inputFor completeness, duckplyr returns a logical for min()
and max()
when the input is logical, while dplyr returns an
integer.
n_distinct()
and multiple argumentsThis function needs exactly one argument besides the optional
na.rm
. Multiple arguments is not supported.
is.na()
and NaN
valuesThis function returns FALSE
for NaN
values
in duckplyr, while it returns TRUE
in dplyr.
Does the same pipeline give different results with
tibble()
and duckdb_tibble()
? We would love to
hear about it, please file an issue.