forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate-sql-base.r
185 lines (167 loc) · 5.09 KB
/
translate-sql-base.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
183
184
185
#' @include translate-sql-helpers.r
#' @include sql-escape.r
NULL
#' @export
#' @rdname sql_variant
#' @format NULL
base_scalar <- sql_translator(
`+` = sql_infix("+"),
`*` = sql_infix("*"),
`/` = sql_infix("/"),
`%%` = sql_infix("%"),
`^` = sql_prefix("power", 2),
`-` = function(x, y = NULL) {
if (is.null(y)) {
build_sql(sql(" - "), x)
} else {
build_sql(x, sql(" - "), y)
}
},
`!=` = sql_infix("!="),
`==` = sql_infix("="),
`<` = sql_infix("<"),
`<=` = sql_infix("<="),
`>` = sql_infix(">"),
`>=` = sql_infix(">="),
`!` = sql_prefix("not"),
`&` = sql_infix("and"),
`&&` = sql_infix("and"),
`|` = sql_infix("or"),
`||` = sql_infix("or"),
xor = function(x, y) {
sql(sprintf("%1$s OR %2$s AND NOT (%1$s AND %2$s)", escape(x), escape(y)))
},
abs = sql_prefix("abs", 1),
acos = sql_prefix("acos", 1),
acosh = sql_prefix("acosh", 1),
asin = sql_prefix("asin", 1),
asinh = sql_prefix("asinh", 1),
atan = sql_prefix("atan", 1),
atan2 = sql_prefix("atan2", 2),
atanh = sql_prefix("atanh", 1),
ceil = sql_prefix("ceil", 1),
ceiling = sql_prefix("ceil", 1),
cos = sql_prefix("cos", 1),
cosh = sql_prefix("cosh", 1),
cot = sql_prefix("cot", 1),
coth = sql_prefix("coth", 1),
exp = sql_prefix("exp", 1),
floor = sql_prefix("floor", 1),
log = sql_prefix("log", 2),
log10 = sql_prefix("log10", 1),
round = sql_prefix("round", 2),
sign = sql_prefix("sign", 1),
sin = sql_prefix("sin", 1),
sinh = sql_prefix("sinh", 1),
sqrt = sql_prefix("sqrt", 1),
tan = sql_prefix("tan", 1),
tolower = sql_prefix("lower", 1),
toupper = sql_prefix("upper", 1),
nchar = sql_prefix("length", 1),
`if` = function(cond, if_true, if_false = NULL) {
build_sql("CASE WHEN ", cond, " THEN ", if_true,
if (!is.null(if_false)) build_sql(" ELSE "), if_false, " ",
"END")
},
sql = function(...) sql(...),
`(` = function(x) {
build_sql("(", x, ")")
},
`{` = function(x) {
build_sql("(", x, ")")
},
desc = function(x) {
build_sql(x, sql(" DESC"))
},
is.null = function(x) {
build_sql(x, " IS NULL")
},
is.na = function(x) {
build_sql(x, "IS NULL")
},
as.numeric = function(x) build_sql("CAST(", x, " AS NUMERIC)"),
as.integer = function(x) build_sql("CAST(", x, " AS INTEGER)"),
as.character = function(x) build_sql("CAST(", x, " AS TEXT)"),
c = function(...) escape(c(...)),
`:` = function(from, to) escape(from:to)
)
base_symbols <- sql_translator(
pi = sql("PI()"),
`*` = sql("*"),
`NULL` = sql("NULL")
)
#' @export
#' @rdname sql_variant
#' @format NULL
base_agg <- sql_translator(
# SQL-92 aggregates
# http://db.apache.org/derby/docs/10.7/ref/rrefsqlj33923.html
n = sql_prefix("count"),
mean = sql_prefix("avg", 1),
var = sql_prefix("variance", 1),
sum = sql_prefix("sum", 1),
min = sql_prefix("min", 1),
max = sql_prefix("max", 1)
)
#' @export
#' @rdname sql_variant
#' @format NULL
base_win <- sql_translator(
# rank functions have a single order argument that overrides the default
row_number = win_rank("row_number"),
min_rank = win_rank("rank"),
rank = win_rank("rank"),
dense_rank = win_rank("dense_rank"),
percent_rank = win_rank("percent_rank"),
cume_dist = win_rank("cume_dist"),
ntile = function(order_by, n) {
over(
build_sql("NTILE", list(n)),
partition_group(),
order_by %||% partition_order()
)
},
# Recycled aggregate fuctions take single argument, don't need order and
# include entire partition in frame.
mean = win_recycled("avg"),
sum = win_recycled("sum"),
min = win_recycled("min"),
max = win_recycled("max"),
n = function() {
over(sql("COUNT(*)"), partition_group(), frame = c(-Inf, Inf))
},
# Cumulative function are like recycled aggregates except that R names
# have cum prefix, order_by is inherited and frame goes from -Inf to 0.
cummean = win_cumulative("mean"),
cumsum = win_cumulative("sum"),
cummin = win_cumulative("min"),
cummax = win_cumulative("max"),
# Finally there are a few miscellaenous functions that don't follow any
# particular pattern
nth = function(x, order = NULL) {
over(build_sql("NTH_VALUE", list(x)), partition_group(), order %||% partition$order())
},
first = function(x, order = NULL) {
over(sql("FIRST_VALUE()"), partition_group(), order %||% partition_order())
},
last = function(x, order = NULL) {
over(sql("LAST_VALUE()"), partition_group(), order %||% partition_order())
},
lead = function(x, n = 1L, default = NA, order = NULL) {
over(
build_sql("LEAD", list(x, n, default)),
partition_group(),
order %||% partition_order()
)
},
lag = function(x, n = 1L, default = NA, order = NULL) {
over(
build_sql("LAG", list(x, n, default)),
partition_group(),
order %||% partition_order()
)
},
order_by = function(order_by, expr) {
over(expr, partition_group(), order_by)
}
)