-
Notifications
You must be signed in to change notification settings - Fork 185
add support for DISTINCT ON
#1620
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
04d2822
09f6aa1
4fdaa08
e7db82b
7713bca
9b75eb0
b2b7146
f1a235d
d6c04a1
2f0d8da
8b31696
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,10 @@ lazy_select_query <- function( | |
| # stopifnot(is.character(group_by)) | ||
| stopifnot(is_lazy_sql_part(order_by)) | ||
| check_number_whole(limit, allow_infinite = TRUE, allow_null = TRUE) | ||
| check_bool(distinct) | ||
| # distinct = FALSE -> no distinct | ||
| # distinct = TRUE -> distinct over all columns | ||
| # distinct = columns -> DISTINCT ON (...) | ||
| stopifnot(is.logical(distinct) || is_lazy_sql_part(distinct)) | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| select <- select %||% syms(set_names(op_vars(x))) | ||
| select_operation <- arg_match0( | ||
|
|
@@ -45,6 +48,9 @@ lazy_select_query <- function( | |
| ) | ||
| } else { | ||
| select <- new_lazy_select(select) | ||
| if (!is.logical(distinct)) { | ||
| distinct <- new_lazy_select(distinct) | ||
|
||
| } | ||
| } | ||
|
|
||
| lazy_query( | ||
|
|
@@ -152,7 +158,12 @@ is_select_identity <- function(select, vars_prev) { | |
|
|
||
| #' @export | ||
| print.lazy_select_query <- function(x, ...) { | ||
| cat_line("<SQL SELECT", if (x$distinct) " DISTINCT", ">") | ||
| cat_line( | ||
| "<SQL SELECT", | ||
| if (!isFALSE(x$distinct)) " DISTINCT", | ||
| if (!is.logical(x$distinct)) " ON", | ||
| ">" | ||
| ) | ||
| cat_line("From:") | ||
| cat_line(indent_print(sql_build(x$x, simulate_dbi()))) | ||
|
|
||
|
|
@@ -195,8 +206,24 @@ sql_build.lazy_select_query <- function(op, con, ..., sql_options = NULL) { | |
|
|
||
| alias <- remote_name(op$x, null_if_local = FALSE) %||% unique_subquery_name() | ||
| from <- sql_build(op$x, con, sql_options = sql_options) | ||
|
|
||
| selects <- op$select | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!is.logical(op$distinct)) { | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| distinct <- get_select_sql( | ||
| select = op$distinct, | ||
| select_operation = op$select_operation, | ||
| in_vars = op_vars(op$x), | ||
| table_alias = alias, | ||
| con = con, | ||
| use_star = sql_options$use_star | ||
| )$select_sql | ||
| } else { | ||
| distinct <- op$distinct | ||
| } | ||
|
|
||
| select_sql_list <- get_select_sql( | ||
| select = op$select, | ||
| select = selects, | ||
| select_operation = op$select_operation, | ||
| in_vars = op_vars(op$x), | ||
| table_alias = alias, | ||
|
|
@@ -217,7 +244,7 @@ sql_build.lazy_select_query <- function(op, con, ..., sql_options = NULL) { | |
| having = translate_sql_(op$having, con = con, window = FALSE), | ||
| window = select_sql_list$window_sql, | ||
| order_by = translate_sql_(op$order_by, con = con), | ||
| distinct = op$distinct, | ||
| distinct = distinct, | ||
| limit = op$limit, | ||
| from_alias = alias | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,10 +53,19 @@ sql_clause_select <- function( | |
| top <- as.integer(top) | ||
| } | ||
|
|
||
| sql_distinct <- | ||
| if (isTRUE(distinct)) { | ||
| " DISTINCT" | ||
|
||
| } else if (isFALSE(distinct)) { | ||
| "" | ||
| } else { | ||
| glue_sql2(con, " DISTINCT ON ({.col distinct})") | ||
| } | ||
|
|
||
| clause <- glue_sql2( | ||
| con, | ||
| "SELECT", | ||
| if (distinct) " DISTINCT", | ||
| sql_distinct, | ||
| if (!is.null(top)) " TOP {.val top}" | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,31 @@ | |
| distinct.tbl_lazy <- function(.data, ..., .keep_all = FALSE) { | ||
| grps <- syms(op_grps(.data)) | ||
| empty_dots <- dots_n(...) == 0 | ||
| can_use_distinct <- !.keep_all || (empty_dots && is_empty(grps)) | ||
| if (!can_use_distinct) { | ||
| can_use_distinct <- | ||
| !.keep_all || | ||
| (empty_dots && is_empty(grps)) || | ||
| supports_distinct_on(.data$src$con) | ||
|
|
||
| if (can_use_distinct) { | ||
| if (empty_dots) { | ||
| dots <- quos(!!!syms(colnames(.data))) | ||
| } else { | ||
| dots <- partial_eval_dots(.data, ..., .named = FALSE) | ||
| dots <- quos(!!!dots) | ||
| } | ||
| prep <- distinct_prepare_compat(.data, dots, group_vars = group_vars(.data)) | ||
|
|
||
| if (!.keep_all) { | ||
| out <- dplyr::select(prep$data, prep$keep) | ||
| out$lazy_query <- add_distinct(out, distinct = TRUE) | ||
| } else { | ||
| out <- prep$data | ||
| out$lazy_query <- add_distinct( | ||
| out, | ||
| distinct = set_names(names(quos_auto_name(dots))) | ||
| ) | ||
| } | ||
| } else { | ||
| needs_dummy_order <- is.null(op_sort(.data)) | ||
|
|
||
| if (needs_dummy_order) { | ||
|
|
@@ -37,17 +60,6 @@ distinct.tbl_lazy <- function(.data, ..., .keep_all = FALSE) { | |
|
|
||
| return(.data) | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (empty_dots) { | ||
| dots <- quos(!!!syms(colnames(.data))) | ||
| } else { | ||
| dots <- partial_eval_dots(.data, ..., .named = FALSE) | ||
| dots <- quos(!!!dots) | ||
| } | ||
| prep <- distinct_prepare_compat(.data, dots, group_vars = group_vars(.data)) | ||
| out <- dplyr::select(prep$data, prep$keep) | ||
|
|
||
| out$lazy_query <- add_distinct(out) | ||
| out | ||
| } | ||
|
|
||
|
|
@@ -148,12 +160,25 @@ quo_is_variable_reference <- function(quo) { | |
| } | ||
|
|
||
|
|
||
| add_distinct <- function(.data) { | ||
| add_distinct <- function(.data, distinct) { | ||
| lazy_query <- .data$lazy_query | ||
|
|
||
| distinct <- | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (is.logical(distinct)) { | ||
| distinct | ||
| } else { | ||
| # if the DISTINCT ON clause contains all columns | ||
| # we can fall back to a normal DISTINCT | ||
| if (is_identity(syms(distinct), names(distinct), colnames(.data))) { | ||
| TRUE | ||
| } else { | ||
| syms(distinct) | ||
|
||
| } | ||
| } | ||
|
|
||
| out <- lazy_select_query( | ||
| x = lazy_query, | ||
| distinct = TRUE | ||
| distinct = distinct | ||
| ) | ||
| # TODO this could also work for joins | ||
| if (!is_lazy_select_query(lazy_query)) { | ||
|
|
@@ -173,6 +198,11 @@ add_distinct <- function(.data) { | |
| return(out) | ||
| } | ||
|
|
||
| lazy_query$distinct <- TRUE | ||
| if (!is.logical(distinct)) { | ||
| lazy_query$distinct <- new_lazy_select(distinct) | ||
lschneiderbauer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| lazy_query$distinct <- distinct | ||
| } | ||
|
|
||
| lazy_query | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great comment that helps me understand the intent of the rest of the code 😄
But it does illustrates a problem with
lazy_select_query()— it isn't properly documented and thus it's hard to tell what the types of the inputs are. You don't need to fix it here, but it might be a useful follow up PR.