-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrename.R
More file actions
215 lines (198 loc) · 6.67 KB
/
rename.R
File metadata and controls
215 lines (198 loc) · 6.67 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
#' Make a rename columns node (copies columns not renamed).
#'
#' @param source source to rename from.
#' @param cmap map written as new column names as keys and old column names as values.
#' @param env environment to look to.
#' @return rename columns 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(AUC = 0.6, R2 = 0.2, z = 3))
#' op_tree <- rename_columns(d, c('R2' %:=% 'AUC', 'AUC' %:=% 'R2'))
#' cat(format(op_tree))
#' sql <- to_sql(op_tree, my_db)
#' cat(sql)
#' print(DBI::dbGetQuery(my_db, sql))
#' DBI::dbDisconnect(my_db)
#' }
#'
#' @export
#'
rename_columns <- function(source, cmap,
env = parent.frame()) {
force(env)
UseMethod("rename_columns", source)
}
#' @export
rename_columns.relop <- function(source, cmap,
env = parent.frame()) {
force(env)
if(length(cmap)<=0) {
return(source)
}
if(length(cmap)!=length(unique(as.character(cmap)))) {
stop("rquery::rename_columns map values must be unique")
}
if(length(cmap)!=length(unique(names(cmap)))) {
stop("rquery::rename_columns map keys must be unique")
}
have <- column_names(source)
check_have_cols(have, as.character(cmap),
"rquery::rename_columns cmap")
collisions <- intersect(names(cmap),
setdiff(have, as.character(cmap)))
if(length(collisions)>0) {
stop(paste("rquery::rename_columns rename collisions",
paste(collisions, collapse = ", ")))
}
cmap <- cmap[names(cmap)!=cmap]
if(length(cmap)<=0) {
return(source)
}
r <- list(source = list(source),
table_name = NULL,
parsed = NULL,
cmap = cmap)
r <- relop_decorate("relop_rename_columns", r)
r
}
#' @export
rename_columns.data.frame <- function(source, cmap,
env = parent.frame()) {
force(env)
if(length(cmap)<=0) {
stop("rquery::rename_columns must rename at least 1 column")
}
if(length(cmap)!=length(unique(as.character(cmap)))) {
stop("rquery::rename_columns map values must be unique")
}
if(length(cmap)!=length(unique(names(cmap)))) {
stop("rquery::rename_columns map keys must be unique")
}
tmp_name <- mk_tmp_name_source("rquery_tmp")()
dnode <- mk_td(tmp_name, colnames(source))
enode <- rename_columns(dnode, cmap, env = env)
rquery_apply_to_data_frame(source, enode, env = env)
}
#' @export
column_names.relop_rename_columns <- function (x, ...) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery::column_names.relop_rename_columns")
sc <- column_names(x$source[[1]])
rmap <- names(x$cmap)
names(rmap) <- as.character(x$cmap)
sc[sc %in% names(rmap)] <- rmap[sc[sc %in% names(rmap)]]
sc
}
#' @export
format_node.relop_rename_columns <- function(node) {
paste0("rename_columns(.,\n",
" ", gsub("\n", "\n ",
wrapr::map_to_char(node$cmap, sep = "\n "),
fixed = TRUE), ")",
"\n")
}
calc_used_relop_rename_columns <- function (x,
...,
using = NULL) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery:::calc_used_relop_rename_columns")
cols <- column_names(x)
if(length(using)>0) {
missing <- setdiff(using, cols)
if(length(missing)>0) {
stop(paste("rquery::calc_used_relop_rename_columns unknown columns",
paste(missing, collapse = ", ")))
}
cols <- intersect(cols, using)
}
# map back prior to rename
rmap <- x$cmap
sc <- cols
sc[sc %in% names(rmap)] <- rmap[sc[sc %in% names(rmap)]]
names(sc) <- cols
sc
}
#' @export
columns_used.relop_rename_columns <- function (x,
...,
using = NULL) {
wrapr::stop_if_dot_args(substitute(list(...)),
"rquery::columns_used.relop_rename_columns")
qmap <- calc_used_relop_rename_columns(x, using=using)
return(columns_used(x$source[[1]],
using = as.character(qmap)))
}
#' @export
to_sql.relop_rename_columns <- 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_rename_columns")
dispatch_to_sql_method(
method_name = "to_sql.relop_rename_columns",
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_rename_columns <- 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_rename_columns")
qmap <- calc_used_relop_rename_columns(x, using=using)
colsV <- vapply(as.character(qmap),
function(ci) {
quote_identifier(db, ci)
}, character(1))
colsA <- vapply(names(qmap),
function(ci) {
quote_identifier(db, ci)
}, character(1))
cols <- paste(colsV, "AS", colsA)
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 = as.character(qmap))
subsql <- subsql_list[[length(subsql_list)]]
tab <- tnum()
prefix <- paste(rep(' ', indent_level), collapse = '')
q <- paste0(prefix, "SELECT\n",
prefix, " ", paste(cols, collapse = paste0(",\n", prefix, " ")), "\n",
prefix, "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)
}