forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate-sql-helpers.r
182 lines (160 loc) · 5.41 KB
/
translate-sql-helpers.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#' Create an sql translator
#'
#' When creating a package that maps to a new SQL based src, you'll often
#' want to provide some additional mappings from common R commands to the
#' commands that your tbl provides. These three functions make that
#' easy.
#'
#' @section Helper functions:
#'
#' \code{sql_infix} and \code{sql_prefix} create default SQL infix and prefix
#' functions given the name of the SQL function. They don't perform any input
#' checking, but do correctly escape their input, and are useful for
#' quickly providing default wrappers for a new SQL variant.
#'
#' @keywords internal
#' @param scalar,aggregate,window The three families of functions than an
#' SQL variant can supply.
#' @param ...,.funs named functions, used to add custom converters from standard
#' R functions to sql functions. Specify individually in \code{...}, or
#' provide a list of \code{.funs}
#' @param .parent the sql variant that this variant should inherit from.
#' Defaults to \code{base_sql} which provides a standard set of
#' mappings for the most common operators and functions.
#' @param f the name of the sql function as a string
#' @param n for \code{sql_infix}, an optional number of arguments to expect.
#' Will signal error if not correct.
#' @seealso \code{\link{sql}} for an example of a more customised sql
#' conversion function.
#' @export
#' @examples
#' # An example of adding some mappings for the statistical functions that
#' # postgresql provides: http://bit.ly/K5EdTn
#'
#' postgres_agg <- sql_translator(.parent = base_agg,
#' cor = sql_prefix("corr"),
#' cov = sql_prefix("covar_samp"),
#' sd = sql_prefix("stddev_samp"),
#' var = sql_prefix("var_samp")
#' )
#' postgres_var <- sql_variant(
#' base_scalar,
#' postgres_agg
#' )
#'
#' translate_sql(cor(x, y), variant = postgres_var)
#' translate_sql(sd(income / years), variant = postgres_var)
#'
#' # Any functions not explicitly listed in the converter will be translated
#' # to sql as is, so you don't need to convert all functions.
#' translate_sql(regr_intercept(y, x), variant = postgres_var)
sql_variant <- function(scalar = sql_translator(),
aggregate = sql_translator(),
window = sql_translator()) {
stopifnot(is.environment(scalar))
stopifnot(is.environment(aggregate))
stopifnot(is.environment(window))
structure(list(scalar = scalar, aggregate = aggregate, window = window),
class = "sql_variant")
}
is.sql_variant <- function(x) inherits(x, "sql_variant")
#' @export
print.sql_variant <- function(x, ...) {
wrap_ls <- function(x, ...) {
vars <- sort(ls(envir = x))
wrapped <- strwrap(paste0(vars, collapse = ", "), ...)
if (identical(wrapped, "")) return()
paste0(wrapped, "\n", collapse = "")
}
cat("<sql_variant>\n")
cat(wrap_ls(x$scalar, prefix = "scalar: "))
cat(wrap_ls(x$aggregate, prefix = "aggregate: "))
cat(wrap_ls(x$window, prefix = "window: "))
}
#' @export
names.sql_variant <- function(x) {
c(ls(envir = x$scalar), ls(envir = x$aggregate), ls(envir = x$window))
}
#' @export
#' @rdname sql_variant
sql_translator <- function(..., .funs = list(),
.parent = new.env(parent = emptyenv())) {
funs <- c(list(...), .funs)
if (length(funs) == 0) return(.parent)
list2env(funs, copy_env(.parent))
}
copy_env <- function(from, to = NULL, parent = parent.env(from)) {
list2env(as.list(from), envir = to, parent = parent)
}
#' @rdname sql_variant
#' @export
sql_infix <- function(f) {
assert_that(is.string(f))
f <- toupper(f)
function(x, y) {
build_sql(x, " ", sql(f), " ", y)
}
}
#' @rdname sql_variant
#' @export
sql_prefix <- function(f, n = NULL) {
assert_that(is.string(f))
f <- toupper(f)
function(..., na.rm) {
if (!missing(na.rm)) {
stop("na.rm not needed in SQL: NULL are always dropped", call. = FALSE)
}
args <- list(...)
if (!is.null(n) && length(args) != n) {
stop("Invalid number of args to SQL ", f, ". Expecting ", n,
call. = FALSE)
}
if (any(names2(args) != "")) {
warning("Named arguments ignored for SQL ", f, call. = FALSE)
}
build_sql(sql(f), args)
}
}
#' @rdname sql_variant
#' @export
sql_not_supported <- function(f) {
assert_that(is.string(f))
f <- toupper(f)
function(...) {
stop(f, " is not available in this SQL variant", call. = FALSE)
}
}
win_rank <- function(f) {
force(f)
function(order = NULL) {
over(build_sql(sql(f), list()), partition_group(), order %||% partition_order())
}
}
win_recycled <- function(f) {
force(f)
function(x) {
over(build_sql(sql(f), list(x)), partition_group(), NULL, frame = c(-Inf, Inf))
}
}
win_cumulative <- function(f) {
force(f)
function(x) {
over(build_sql(sql(f), list(x)), partition_group(), partition_order(), frame = c(-Inf, 0))
}
}
# Use a global variable to communicate state of partitioning between
# tbl and sql translator. This isn't the most amazing design, but it keeps
# things loosely coupled and is straightforward to understand.
partition <- new.env(parent = emptyenv())
set_partition <- function(group_by, order_by) {
old <- list(partition$group_by, partition$order_by)
if (is.list(group_by)) {
order_by <- group_by[[2]]
group_by <- group_by[[1]]
}
partition$group_by <- group_by
partition$order_by <- order_by
invisible(old)
}
partition_group <- function() partition$group_by
partition_order <- function() partition$order_by