Skip to content

Commit

Permalink
Fix bug in rows_patch.tbl_lazy() throwing an error when multiple co…
Browse files Browse the repository at this point in the history
…lumns need to be patched (#1444)

* Fix bug in `rows_patch.tbl_lazy()` throwing an error when multiple columns need to be patched
  • Loading branch information
gorcha authored Feb 1, 2024
1 parent 781d630 commit 4714a47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* `rows_patch(in_place = FALSE)` now works when more than one column should be
patched (@gorcha, #1443).

* Namespaced dplyr calls now error if the function doesn't exist, or
a translation is not available (#1426).

Expand Down
6 changes: 5 additions & 1 deletion R/rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ rows_patch.tbl_lazy <- function(x,
)

patch_columns_y <- paste0(new_columns, "...y")
patch_quos <- lapply(new_columns, function(.x) quo(coalesce(!!sym(.x), !!sym(patch_columns_y)))) %>%
patch_quos <-
lapply(
seq_along(new_columns),
function(.x) quo(coalesce(!!sym(new_columns[.x]), !!sym(patch_columns_y[.x])))
) %>%
rlang::set_names(new_columns)
if (is_empty(new_columns)) {
patched <- to_patch
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/_snaps/rows.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,31 @@
ON (`df_x`.`x` = `df_y`.`x`)
) AS `q01`

# `rows_patch()` works with multiple columns to update

Code
rows_patch(lazy_frame(x = 1:3, y = c(11, 12, NA), z = c(31, NA, 33), .name = "df_x"),
lazy_frame(x = 2:3, y = 22:23, z = 42:43, .name = "df_y"), by = "x", unmatched = "ignore",
in_place = FALSE)
Output
<SQL>
SELECT `df_x`.*
FROM `df_x`
WHERE NOT EXISTS (
SELECT 1 FROM `df_y`
WHERE (`df_x`.`x` = `df_y`.`x`)
)
UNION ALL
SELECT `x`, COALESCE(`y`, `y...y`) AS `y`, COALESCE(`z`, `z...y`) AS `z`
FROM (
SELECT `df_x`.*, `df_y`.`y` AS `y...y`, `df_y`.`z` AS `z...y`
FROM `df_x`
INNER JOIN `df_y`
ON (`df_x`.`x` = `df_y`.`x`)
) AS `q01`

# `rows_patch()` works with `in_place = TRUE`

Code
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,32 @@ test_that("`rows_patch()` works with `in_place = FALSE`", {
expect_equal(df %>% collect(), tibble(x = 1:3, y = c(11, 12, NA)))
})

test_that("`rows_patch()` works with multiple columns to update", {
expect_snapshot(
rows_patch(
lazy_frame(x = 1:3, y = c(11, 12, NA), z = c(31, NA, 33), .name = "df_x"),
lazy_frame(x = 2:3, y = 22:23, z = 42:43, .name = "df_y"),
by = "x",
unmatched = "ignore",
in_place = FALSE
)
)

df <- memdb_frame(x = 1:3, y = c(11, 12, NA), z = c(31, NA, 33))
expect_equal(
rows_patch(
df, memdb_frame(x = 2:3, y = 22:23, z = 42:43),
by = "x",
unmatched = "ignore",
in_place = FALSE
) %>%
collect(),
tibble(x = 1:3, y = c(11, 12, 23), z = c(31, 42, 33))
)

expect_equal(df %>% collect(), tibble(x = 1:3, y = c(11, 12, NA), z = c(31, NA, 33)))
})

test_that("`rows_patch()` works with `in_place = FALSE` and `returning`", {
expect_equal(
rows_patch(
Expand Down

0 comments on commit 4714a47

Please sign in to comment.