-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathset_indicator.R
More file actions
270 lines (246 loc) · 8.22 KB
/
set_indicator.R
File metadata and controls
270 lines (246 loc) · 8.22 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#' Make a set indicator node.
#'
#' Create a new column indicating the membership of another column in a given set.
#'
#'
#' @param source source to select from.
#' @param rescol name of column to land indicator in.
#' @param testcol name of column to check.
#' @param testvalues values to check for.
#' @param ... force later arguments to bind by name
#' @param translate_quotes logical if TRUE translate quotes to SQL choice (simple replacement, no escaping).
#' @param env environment to look to.
#' @return set_indicator node.
#'
#' @examples
#'
#' if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) {
#' my_db <- DBI::dbConnect(RSQLite::SQLite(),
#' ":memory:")
#'
#' d <- rq_copy_to(my_db, 'd',
#' data.frame(a = c("1", "2", "1", "3"),
#' b = c("1", "1", "3", "2"),
#' q = 1,
#' stringsAsFactors = FALSE),
#' temporary = TRUE,
#' overwrite = TRUE)
#' # example
#' set <- c("1", "2")
#' op_tree <- d %.>%
#' set_indicator(., "one_two", "a", set) %.>%
#' set_indicator(., "z", "a", c())
#' print(column_names(op_tree))
#' print(columns_used(op_tree))
#' cat(format(op_tree))
#' sql <- to_sql(op_tree, my_db)
#' cat(sql)
#' print(DBI::dbGetQuery(my_db, sql))
#'
#' op_tree2 <- d %.>%
#' set_indicator(., "one_two", "a", set) %.>%
#' set_indicator(., "z", "b", c()) %.>%
#' select_columns(., c("z", "one_two"))
#' print(column_names(op_tree2))
#' print(columns_used(op_tree2))
#'
#' # cleanup
#' DBI::dbDisconnect(my_db)
#' }
#'
#' @export
#'
set_indicator <- function(source,
rescol,
testcol,
testvalues,
...,
translate_quotes = FALSE,
env = parent.frame()) {
force(env)
wrapr::stop_if_dot_args(substitute(list(...)), "set_indicator")
UseMethod("set_indicator", source)
}
#' @export
set_indicator.relop <- function(source,
rescol,
testcol,
testvalues,
...,
translate_quotes = FALSE,
env = parent.frame()) {
force(env)
wrapr::stop_if_dot_args(substitute(list(...)), "set_indicator")
testvname <- rquery_deparse(substitute(testvalues))
cols <- column_names(source)
if(rescol %in% cols) {
stop("rquery::set_indicator.relop rescol must not be a column name of source data")
}
if(!(testcol %in% cols)) {
stop("rquery::set_indicator.relop testcol must be a column name of source data")
}
display_form <- paste0("set_indicator(., ",
rescol,
" = ",
testcol,
" IN ",
testvname,
")")
if(length(testvalues)>0) {
terms = list(as.name(testcol), " IN ( ")
for(i in seq_len(length(testvalues))) {
if(i>1) {
terms <- c(terms, " , ")
}
terms <- c(terms, list(list(testvalues[[i]])))
}
terms <- c(terms, list(" ) "))
terms <- list(terms)
names(terms) <- rescol
} else {
terms <- rescol %:=% 0
}
r <- list(source = list(source),
table_name = NULL,
parsed = NULL,
rescol = rescol,
testcol = testcol,
testvalues = testvalues,
display_form = display_form,
terms = terms,
translate_quotes = translate_quotes)
r <- relop_decorate("relop_set_indicator", r)
r
}
#' @export
set_indicator.data.frame <- function(source,
rescol,
testcol,
testvalues,
...,
translate_quotes = FALSE,
env = parent.frame()) {
force(env)
wrapr::stop_if_dot_args(substitute(list(...)), "set_indicator")
tmp_name <- mk_tmp_name_source("rquery_tmp")()
dnode <- mk_td(tmp_name, colnames(source))
enode <- set_indicator(source = dnode,
rescol = rescol,
testcol = testcol,
testvalues = testvalues,
env = env)
rquery_apply_to_data_frame(source, enode, env = env)
}
#' @export
format_node.relop_set_indicator <- function(node) {
paste0(node$display_form,
"\n")
}
calc_used_relop_set_indicator <- function(x,
using = NULL) {
cols <- column_names(x)
if(length(using)>0) {
cols <- using
}
if(!(x$testcol %in% cols)) {
cols <- c(cols, x$testcol)
}
cols
}
#' @export
columns_used.relop_set_indicator <- function (x, ...,
using = NULL) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery::columns_used.relop_set_indicator")
cols <- calc_used_relop_set_indicator(x,
using = using)
cols <- setdiff(cols, x$rescol)
columns_used(x$source[[1]],
using = cols)
}
#' @export
column_names.relop_set_indicator <- function (x, ...) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery::column_names.relop_set_indicator")
cols <- column_names(x$source[[1]])
if(!(x$testcol %in% cols)) {
cols <- c(cols, x$testcol)
}
if(!(x$rescol %in% cols)) {
cols <- c(cols, x$rescol)
}
cols
}
#' @export
to_sql.relop_set_indicator <- function (x,
db,
...,
limit = NULL,
source_limit = NULL,
indent_level = 0,
tnum = mk_tmp_name_source('tsql'),
append_cr = TRUE,
using = NULL) {
wrapr::stop_if_dot_args(substitute(list(...)), "rquery::to_sql.relop_set_indicator")
dispatch_to_sql_method(
method_name = "to_sql.relop_set_indicator",
x = x,
db = db,
limit = limit,
source_limit = source_limit,
indent_level = indent_level,
tnum = tnum,
append_cr = append_cr,
using = using)
}
to_sql_relop_set_indicator <- function(
x,
db,
...,
limit = NULL,
source_limit = NULL,
indent_level = 0,
tnum = mk_tmp_name_source('tsql'),
append_cr = TRUE,
using = NULL) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery::to_sql.relop_set_indicator")
cols1 <- column_names(x$source[[1]])
qexample = quote_string(db, "a")
qlen = as.numeric(regexec("a", qexample, fixed = TRUE)) - 1
qsym = substr(qexample, 1, qlen)
sqlexprs <- vapply(x$terms,
function(ei) {
prep_sql_toks(db, ei,
translate_quotes = x$translate_quotes,
qsym = qsym)
}, character(1))
if(length(sqlexprs)!=1) {
stop("rquery::to_sql.relop_set_indicator expected indicator calculation to be length 1")
}
subsql_list <- to_sql(x$source[[1]],
db = db,
limit = limit,
source_limit = source_limit,
indent_level = indent_level + 1,
tnum = tnum,
append_cr = FALSE,
using = cols1) # TODO: double check using calculation
subsql <- subsql_list[[length(subsql_list)]]
tab <- tnum()
prefix <- paste(rep(' ', indent_level), collapse = '')
q <- paste0(prefix, "SELECT *, ",
sqlexprs[[1]], " AS ", quote_identifier(db, names(sqlexprs)),
" FROM (\n",
subsql, "\n",
prefix, ") ",
tab)
if(!is.null(limit)) {
q <- paste(q, "LIMIT",
format(ceiling(limit), scientific = FALSE))
}
if(append_cr) {
q <- paste0(q, "\n")
}
c(subsql_list[-length(subsql_list)], q)
}