Skip to content

Commit

Permalink
Implement union_all.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 7, 2016
1 parent 5503d91 commit e5f2952
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ S3method(ungroup,tbl_sql)
S3method(union,data.frame)
S3method(union,default)
S3method(union,tbl_sql)
S3method(union_all,data.frame)
S3method(union_all,default)
S3method(union_all,tbl_sql)
S3method(unique,sql)
S3method(update,tbl_sql)
export("%>%")
Expand Down Expand Up @@ -510,6 +513,7 @@ export(trunc_mat)
export(type_sum)
export(ungroup)
export(union)
export(union_all)
export(with_order)
import(DBI)
import(assertthat)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr 0.4.3.9000

* New `union_all()` method. Maps to `UNION ALL` for SQL sources, `bind_rows()`
for data frames/tbl\_dfs, and `combine()` for vectors (#1045).

* `src_sqlite()` throws errors if you try and use it with window functions
(#907).

Expand Down
3 changes: 3 additions & 0 deletions R/dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ intersect.data.frame <- function(x, y, ...) intersect_data_frame(x, y)
#' @export
union.data.frame <- function(x, y, ...) union_data_frame(x, y)

#' @export
union_all.data.frame <- function(x, y, ...) bind_rows(x, y)

#' @export
setdiff.data.frame <- function(x, y, ...) setdiff_data_frame(x, y)

Expand Down
6 changes: 6 additions & 0 deletions R/sets.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#' setdiff(first, second)
#' setdiff(second, first)
#'
#' union_all(first, second)
#' setequal(mtcars, mtcars[32:1, ])
NULL

Expand All @@ -28,6 +29,9 @@ intersect <- function(x, y, ...) UseMethod("intersect")
union <- function(x, y, ...) UseMethod("union")
#' @rdname setops
#' @export
union_all <- function(x, y, ...) UseMethod("union_all")
#' @rdname setops
#' @export
setdiff <- function(x, y, ...) UseMethod("setdiff")
#' @rdname setops
#' @export
Expand All @@ -38,6 +42,8 @@ intersect.default <- function(x, y, ...) base::intersect(x, y, ...)
#' @export
union.default <- function(x, y, ...) base::union(x, y, ...)
#' @export
union_all.default <- function(x, y, ...) combine(x, y, ...)
#' @export
setdiff.default <- function(x, y, ...) base::setdiff(x, y, ...)
#' @export
setequal.default <- function(x, y, ...) base::setequal(x, y, ...)
Expand Down
6 changes: 6 additions & 0 deletions R/tbl-sql.r
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ union.tbl_sql <- function(x, y, copy = FALSE, ...) {
update(tbl(x$src, sql), group_by = groups(x))
}
#' @export
union_all.tbl_sql <- function(x, y, copy = FALSE, ...) {
y <- auto_copy(x, y, copy)
sql <- sql_set_op(x$src$con, x, y, "UNION ALL")
update(tbl(x$src, sql), group_by = groups(x))
}
#' @export
setdiff.tbl_sql <- function(x, y, copy = FALSE, ...) {
y <- auto_copy(x, y, copy)
sql <- sql_set_op(x$src$con, x, y, "EXCEPT")
Expand Down
4 changes: 4 additions & 0 deletions man/setops.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/testthat/test-union-all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
context("union_all")

test_that("union all on vectors concatenates", {
expect_equal(union_all(1:3, 4:6), 1:6)
})

test_that("union all on data frames calls bind rows", {
df1 <- data_frame(x = 1:2)
df2 <- data_frame(y = 1:2)

expect_equal(union_all(df1, df2), bind_rows(df1, df2))
})

test_that("union on database uses UNION ALL", {
db <- src_sqlite(":memory:", TRUE)

df1 <- copy_to(db, data_frame(x = 1:2), "df1")
df2 <- copy_to(db, data_frame(x = 1:2), "df2")

res <- collect(union_all(df1, df2))
expect_equal(res$x, rep(1:2, 2))
})

0 comments on commit e5f2952

Please sign in to comment.