Skip to content

Commit

Permalink
Merge pull request #156 from ssi-dk/fix/db_joins-join_by
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte authored Oct 16, 2024
2 parents 659f091 + 12e9d06 commit b4561d7
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 114 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* `update_snapshot()` has been optimized and now runs faster on all the supported backends (#137).

* `*_joins()` can now take `dplyr::join_by()` as `by` argument when no `na_by` argument is given (#156).

## Documentation

* A vignette including benchmarks of `update_snapshot()` across various backends is added (#138).
Expand Down
108 changes: 49 additions & 59 deletions R/db_joins.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,19 @@ join_warn_experimental <- function() {
#' @seealso [dplyr::show_query]
#' @exportS3Method dplyr::inner_join
inner_join.tbl_sql <- function(x, y, by = NULL, ...) {

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)

.dots <- list(...)

if (!"na_by" %in% names(.dots)) {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()
join_warn()
return(NextMethod("inner_join"))
}

# Check arguments
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

join_warn_experimental()

args <- as.list(rlang::current_env()) |>
Expand All @@ -205,20 +205,19 @@ inner_join.tbl_sql <- function(x, y, by = NULL, ...) {
#' @rdname joins
#' @exportS3Method dplyr::left_join
left_join.tbl_sql <- function(x, y, by = NULL, ...) {

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)

.dots <- list(...)

if (!"na_by" %in% names(.dots)) {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()

join_warn()
return(NextMethod("left_join"))
}

# Check arguments
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

join_warn_experimental()

args <- as.list(rlang::current_env()) |>
Expand All @@ -240,20 +239,19 @@ left_join.tbl_sql <- function(x, y, by = NULL, ...) {
#' @rdname joins
#' @exportS3Method dplyr::right_join
right_join.tbl_sql <- function(x, y, by = NULL, ...) {

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)

.dots <- list(...)

if (!"na_by" %in% names(.dots)) {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()

join_warn()
return(NextMethod("right_join"))
}

# Check arguments
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

join_warn_experimental()

args <- as.list(rlang::current_env()) |>
Expand All @@ -276,62 +274,54 @@ right_join.tbl_sql <- function(x, y, by = NULL, ...) {
#' @rdname joins
#' @exportS3Method dplyr::full_join
full_join.tbl_sql <- function(x, y, by = NULL, ...) {
.dots <- list(...)

if (!"na_by" %in% names(.dots)) {
join_warn()
return(NextMethod("full_join"))
}

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)
join_warn_experimental()

if ("na_by" %in% names(.dots)) {
join_warn_experimental()
# Full joins are hard...
out <- dplyr::union(dplyr::left_join(x, y, by = by, na_by = .dots$na_by),
dplyr::right_join(x, y, by = by, na_by = .dots$na_by))
return(out)
} else {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()
return(dplyr::full_join(x, y, by = by, ...))
}
# Full joins are hard...
out <- dplyr::union(
dplyr::left_join(x, y, by = by, na_by = .dots$na_by),
dplyr::right_join(x, y, by = by, na_by = .dots$na_by)
)

return(out)
}


#' @rdname joins
#' @exportS3Method dplyr::semi_join
semi_join.tbl_sql <- function(x, y, by = NULL, ...) {

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)

.dots <- list(...)

if ("na_by" %in% names(.dots)) {
stop("Not implemented")
} else {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()
return(dplyr::semi_join(x, y, by = by, ...))
if (!"na_by" %in% names(.dots)) {
join_warn()
return(NextMethod("semi_join"))
}

stop("Not implemented")
}


#' @rdname joins
#' @exportS3Method dplyr::anti_join
anti_join.tbl_sql <- function(x, y, by = NULL, ...) {

# Check arguments
assert_data_like(x)
assert_data_like(y)
checkmate::assert_character(by, null.ok = TRUE)

.dots <- list(...)

if ("na_by" %in% names(.dots)) {
stop("Not implemented")
} else {
if (inherits(x, "tbl_dbi") || inherits(y, "tbl_dbi")) join_warn()
return(dplyr::anti_join(x, y, by = by, ...))
if (!"na_by" %in% names(.dots)) {
join_warn()
return(NextMethod("anti_join"))
}

stop("Not implemented")
}
64 changes: 32 additions & 32 deletions pak.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"ref": "askpass",
"package": "askpass",
"version": "1.2.0",
"version": "1.2.1",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -19,10 +19,10 @@
"RemoteRef": "askpass",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "1.2.0"
"RemoteSha": "1.2.1"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/askpass_1.2.0.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/askpass_1.2.0.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/askpass_1.2.1.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/askpass_1.2.1.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -534,7 +534,7 @@
{
"ref": "commonmark",
"package": "commonmark",
"version": "1.9.1",
"version": "1.9.2",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -546,10 +546,10 @@
"RemoteRef": "commonmark",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "1.9.1"
"RemoteSha": "1.9.2"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/commonmark_1.9.1.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/commonmark_1.9.1.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/commonmark_1.9.2.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/commonmark_1.9.2.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -702,7 +702,7 @@
{
"ref": "data.table",
"package": "data.table",
"version": "1.16.0",
"version": "1.16.2",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -714,10 +714,10 @@
"RemoteRef": "data.table",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "1.16.0"
"RemoteSha": "1.16.2"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/data.table_1.16.0.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/data.table_1.16.0.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/data.table_1.16.2.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/data.table_1.16.2.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -1012,7 +1012,7 @@
{
"ref": "evaluate",
"package": "evaluate",
"version": "1.0.0",
"version": "1.0.1",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -1024,10 +1024,10 @@
"RemoteRef": "evaluate",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "1.0.0"
"RemoteSha": "1.0.1"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/evaluate_1.0.0.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/evaluate_1.0.0.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/evaluate_1.0.1.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/evaluate_1.0.1.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -1683,7 +1683,7 @@
{
"ref": "hunspell",
"package": "hunspell",
"version": "3.0.4",
"version": "3.0.5",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -1695,10 +1695,10 @@
"RemoteRef": "hunspell",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "3.0.4"
"RemoteSha": "3.0.5"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/hunspell_3.0.4.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/hunspell_3.0.4.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/hunspell_3.0.5.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/hunspell_3.0.5.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -3619,7 +3619,7 @@
{
"ref": "spelling",
"package": "spelling",
"version": "2.3.0",
"version": "2.3.1",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -3631,10 +3631,10 @@
"RemoteRef": "spelling",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "2.3.0"
"RemoteSha": "2.3.1"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/spelling_2.3.0.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/spelling_2.3.0.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/spelling_2.3.1.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/spelling_2.3.1.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -3719,7 +3719,7 @@
{
"ref": "sys",
"package": "sys",
"version": "3.4.2",
"version": "3.4.3",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -3731,10 +3731,10 @@
"RemoteRef": "sys",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "3.4.2"
"RemoteSha": "3.4.3"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/sys_3.4.2.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/sys_3.4.2.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/sys_3.4.3.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/sys_3.4.3.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down Expand Up @@ -4340,7 +4340,7 @@
{
"ref": "xfun",
"package": "xfun",
"version": "0.47",
"version": "0.48",
"type": "standard",
"direct": false,
"binary": true,
Expand All @@ -4352,10 +4352,10 @@
"RemoteRef": "xfun",
"RemoteRepos": "https://packagemanager.posit.co/cran/__linux__/jammy/latest",
"RemotePkgPlatform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"RemoteSha": "0.47"
"RemoteSha": "0.48"
},
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/xfun_0.47.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/xfun_0.47.tar.gz",
"sources": "https://packagemanager.posit.co/cran/__linux__/jammy/latest/src/contrib/xfun_0.48.tar.gz",
"target": "src/contrib/x86_64-pc-linux-gnu-ubuntu-22.04/4.4/xfun_0.48.tar.gz",
"platform": "x86_64-pc-linux-gnu-ubuntu-22.04",
"rversion": "4.4",
"directpkg": false,
Expand Down
Loading

0 comments on commit b4561d7

Please sign in to comment.