Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add union all #1045

Closed
tomasgreif opened this issue Mar 27, 2015 · 3 comments
Closed

Add union all #1045

tomasgreif opened this issue Mar 27, 2015 · 3 comments
Labels
Milestone

Comments

@tomasgreif
Copy link

@tomasgreif tomasgreif commented Mar 27, 2015

union all keeps rows even they are equal. Adding it would be pretty easy:

sets.r

#' @export
union <- function(x, y, ...) UseMethod("union_all")

tbl-sql.r

#' @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))
}
@hadley hadley added this to the 0.5 milestone May 19, 2015
@hadley hadley added the feature label Oct 22, 2015
@hadley
Copy link
Member

@hadley hadley commented Oct 22, 2015

Could fall back to bind_rows() for local data.

@hadley hadley added the SQL label Oct 22, 2015
@hadley hadley closed this in e5f2952 Mar 7, 2016
@iangow
Copy link

@iangow iangow commented Jun 7, 2016

Note difference between UNION ALL and bind_rows evident below. As discussed at #1890, UNION CORRESPONDING is the equivalent to bind_rows (but not commonly implemented). May not be a good idea to have same verb with different behaviour:

library(dplyr)
pg <- src_postgres()

sql <- "
    WITH data AS (SELECT 1 AS a, 2 AS b)

    SELECT a, b
    FROM data 
    UNION ALL
    SELECT b, a
    FROM data"

tbl(pg, sql(sql))

df <-
    tbl(pg, sql("SELECT 1 AS a, 2 AS b")) %>%
    collect()

df %>% 
    select(b, a) %>% 
    bind_rows(df %>% select(a, b))

SQL output:

> tbl(pg, sql(sql))
Source: postgres 9.6.0 [igow@localhost:5432/crsp]
From: <derived table> [?? x 2]

       a     b
   (int) (int)
1      1     2
2      2     1

R output:

Source: local data frame [2 x 2]

      b     a
  (int) (int)
1     2     1
2     2     1

@iangow
Copy link

@iangow iangow commented Jun 7, 2016

I guess one could "fix" UNION ALL, but it's well established.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants