Skip to content

Commit 69dbe63

Browse files
committed
changes + updates from previous PR (date_build, difftime)
1 parent 403be02 commit 69dbe63

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

R/backend-dbplyr__duckdb_connection.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,14 @@ sql_translation.duckdb_connection <- function(con) {
308308

309309
},
310310
date_build = function(year, month = 1L, day = 1L, ..., invalid = NULL) {
311-
dbplyr:::check_unsupported_arg(invalid, allow_null = TRUE)
311+
check_unsupported_arg(invalid, allow_null = TRUE)
312312
rlang::check_dots_empty()
313-
build_sql("MAKE_DATE(CAST(", year, " AS INTEGER), CAST(", month, " AS INTEGER), CAST(", day, " AS INTEGER))")
314-
313+
dbplyr::sql_expr(MAKE_DATE(!!year, !!month, !!day))
315314
},
316315
difftime = function(time1, time2, tz, units = "days") {
317-
dbplyr:::check_unsupported_arg(tz)
318-
dbplyr:::check_unsupported_arg(units, allowed = "days")
319-
build_sql("DATEDIFF('day', ", !!time2, ", " ,!!time1, ")")
316+
check_unsupported_arg(tz)
317+
check_unsupported_arg(units, allowed = "days")
318+
dbplyr::sql_expr(DATEDIFF("day", !!time2, !!time1))
320319
},
321320

322321
# stringr functions

R/check_unsupported_arg.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#' Check for unsupported argument values
2+
#'
3+
#' The function checks whether a given argument is unsupported
4+
#' It throws an error message if the argument is not among the allowed values.
5+
#' Function is taken from dplyr.
6+
#'
7+
#' @param x The argument to check.
8+
#' @param allowed A value or vector of values that are allowed for `x`. If `NULL`, no value is allowed.
9+
#' @param allow_null Logical. If `TRUE`, allows `x` to be `NULL`.
10+
#' @param backend Optional character string indicating the back-end.
11+
#' @param arg The name of the argument being checked (automatically inferred via `rlang::caller_arg()`).
12+
#' @param call The calling environment used for error reporting (automatically inferred via `rlang::caller_env()`).
13+
#'
14+
#' @noRd
15+
check_unsupported_arg <- function (x, allowed = NULL, allow_null = FALSE, backend = NULL,
16+
arg = caller_arg(x), call = caller_env())
17+
{
18+
if (rlang::is_missing(x)) {
19+
return()
20+
}
21+
if (allow_null && rlang::is_null(x)) {
22+
return()
23+
}
24+
if (identical(x, allowed)) {
25+
return()
26+
}
27+
if (rlang::is_null(allowed)) {
28+
msg <- "Argument {.arg {arg}} isn't supported"
29+
}
30+
else {
31+
msg <- "{.code {arg} = {.val {x}}} isn't supported"
32+
}
33+
if (is.null(backend)) {
34+
msg <- paste0(msg, " on database backends.")
35+
}
36+
else {
37+
msg <- paste0(msg, " in {backend} translation.")
38+
}
39+
if (!rlang::is_null(allowed)) {
40+
if (allow_null) {
41+
allow_msg <- "It must be {.val {allowed}} or {.code NULL} instead."
42+
}
43+
else {
44+
allow_msg <- "It must be {.val {allowed}} instead."
45+
}
46+
msg <- c(msg, i = allow_msg)
47+
}
48+
cli_abort(msg, call = call)
49+
}
50+

src/duckdb/ub_src_function_table_system-6d8b1e5a.o.tmp

Whitespace-only changes.

tests/testthat/test-backend-dbplyr__duckdb_connection.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ test_that("custom clock functions translated correctly", {
152152
precision = "day")),
153153
sql(r"{DATEDIFF('day', start, end)}"))
154154

155-
expect_equal(translate(difftime(time1 = "time1", time2 = "time2", units = "days")), sql(r"{DATEDIFF('day', time2, time1)}"))
156-
expect_equal(translate(date_build(year = 2000L, month = 8L, day = 8L)), sql(r"{MAKE_DATE(CAST(2000.0 AS INTEGER), CAST(8.0 AS INTEGER), CAST(8.0 AS INTEGER))}"))
157-
expect_equal(translate(date_build(year = 2000)), sql(r"{MAKE_DATE(CAST(2000.0 AS INTEGER), CAST(1 AS INTEGER), CAST(1 AS INTEGER))}"))
155+
expect_equal(translate(difftime(time1 = "time1", time2 = "time2", units = "days")), sql(r"{DATEDIFF('day', 'time2', 'time1')}"))
156+
expect_equal(translate(date_build(year = 2000L, month = 8L, day = 8L)), sql(r"{MAKE_DATE(2000, 8, 8)}"))
157+
expect_equal(translate(date_build(year = 2000)), sql(r"{MAKE_DATE(2000.0, 1, 1)}"))
158158

159159
test_data <- data.frame(
160160
person = 1L,

0 commit comments

Comments
 (0)