Skip to content

Commit

Permalink
fix(db_joins): Allow dplyr::join_by as by argument
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Oct 11, 2024
1 parent 659f091 commit 24b67c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
31 changes: 24 additions & 7 deletions R/db_joins.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ 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)

checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)
.dots <- list(...)

if (!"na_by" %in% names(.dots)) {
Expand Down Expand Up @@ -209,7 +211,10 @@ 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)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)

Expand Down Expand Up @@ -244,7 +249,10 @@ 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)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)

Expand Down Expand Up @@ -280,7 +288,10 @@ full_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)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)

Expand All @@ -304,7 +315,10 @@ 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)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)

Expand All @@ -324,7 +338,10 @@ 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)
checkmate::assert(
checkmate::check_character(by, null.ok = TRUE),
checkmate::check_class(by, "dplyr_join_by", null.ok = TRUE)
)

.dots <- list(...)

Expand Down
31 changes: 30 additions & 1 deletion tests/testthat/test-db_joins.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("*_join() works", {
test_that("*_join() works with character `by` and `na_by`", {
for (conn in get_test_conns()) {

# Define two test datasets
Expand Down Expand Up @@ -115,3 +115,32 @@ test_that("*_join() works", {
connection_clean_up(conn)
}
})


test_that("*_join() works with `dplyr::join_by()`", {
for (conn in get_test_conns()) {

# Define two test datasets
x <- get_table(conn, "__mtcars") |>
dplyr::select(name, mpg, cyl, hp, vs, am, gear, carb)

y <- get_table(conn, "__mtcars") |>
dplyr::select(name, drat, wt, qsec)


# Test the implemented joins
q <- dplyr::left_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
qr <- dplyr::left_join(dplyr::collect(x), dplyr::collect(y), by = dplyr::join_by(x$name == y$name))
expect_equal(q, qr)

q <- dplyr::right_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
qr <- dplyr::right_join(dplyr::collect(x), dplyr::collect(y), by = dplyr::join_by(x$name == y$name))
expect_equal(q, qr)

q <- dplyr::inner_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
qr <- dplyr::inner_join(dplyr::collect(x), dplyr::collect(y), by = dplyr::join_by(x$name == y$name))
expect_equal(q, qr)

connection_clean_up(conn)
}
})

0 comments on commit 24b67c8

Please sign in to comment.