-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmssql-helper.R
462 lines (399 loc) · 15.2 KB
/
mssql-helper.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# library(dplyr)
# library(dbplyr)
# library(odbc)
# library(DBI)
# library(rlist)
connect_mssql <- function(...) {
dbConnect(odbc(),
UID=.get_db_usr(),
PWD = .get_db_pwd(),
driver = "{ODBC Driver 17 for SQL Server}",
server=.get_db_svr(),
database=.get_db_nme(),
encoding="latin1",
...
)
}
disconnect_mssql <- function(con) {
dbDisconnect(con)
}
# seems like line breaks at the end of a cell can cause an error in the conversion string.
find_problematic_row <- function(df, table_name, find_cell = TRUE, find_character = TRUE) {
n_splits = nrow(df)
n <- nrow(df)
max_rows <- floor(n / (n_splits))
r <- rep(1:ceiling(n/max_rows),each=max_rows)[1:n]
d <- split(df,r)
target <- NULL
found_row <- FALSE
message("connecting")
con <- connect_mssql()
tryCatchLog({
purrr::map(.x = d, function(y) {
message("Checking a row out of ", length(d), " rows. ")
target <<- y
copy_to_mssql(con = con, df = y, tbl_name = table_name)
})
},
error = function(error) {
message("Error triggered", error)
found_row <<- TRUE
},
finally = {
message("disconnecting")
disconnect_mssql(con)
if (found_row) {
message("Found problematic row")
if (find_cell) {
target_cell <- find_problematic_cell(prob_df = target, table_name = table_name, find_character)
if (find_character) {
return(list(target = target, target_cell = target_cell$cell, target_char = target_cell$char))
}
return(list(target = target, target_cell = target_cell))
}
return(target)
}
}
)
return(NULL)
}
find_problematic_cell <- function(prob_df, table_name, find_character = TRUE) {
found_cell <- FALSE
for (i in 1:ncol(prob_df))
{
tryCatchLog({
message("connecting")
con <- connect_mssql()
copy_to_mssql(con = con, df = prob_df[,1:i], tbl_name = table_name)
}, error = function(error) {
message("Error triggered on cell", error)
found_cell <<- TRUE
},
finally = {
message("disconnecting")
disconnect_mssql(con)
if (found_cell) {
message("Found problematic cell")
if (find_character) {
target_character <- find_problematic_character(prob_df = prob_df[,1:i], table_name = table_name)
return(list(cell = prob_df[,i], char = target_character))
}
return(prob_df[,i])
}
})
}
return(NULL)
}
find_problematic_character <- function(prob_df, table_name) {
test_seq <- function(x) {
prob_df[1,ncol(prob_df)] <<- x
message("Checking sequence: ", prob_df[1,ncol(prob_df)])
found_seq <- FALSE
tryCatchLog({
message("connecting")
con <- connect_mssql()
copy_to_mssql(con = con, df = prob_df, tbl_name = table_name)
}, error = function(error) {
message("Error triggered on sequence ", error)
found_seq <<- TRUE
},
finally = {
message("disconnecting")
disconnect_mssql(con)
if (found_seq) {
message("Found problematic sequence: ", x)
return(TRUE)
}
return(FALSE)
})
}
found_char <- FALSE
found_seq <- FALSE
prob_seq <- NULL
string <- prob_df[[1,ncol(prob_df)]]
message("Checking string ", string)
while(!found_char&&!is.null(string)&&nchar(string)>1) {
part1 = substr(string, 1,floor(nchar(string)/2))
if (test_seq(part1)) {
prob_seq <- part1
string <- part1
} else {
part2 = substr(string, floor(nchar(string)/2)+1, nchar(string))
if (test_seq(part2)) {
prob_seq <- part2
string <- part2
} else {
string <- NULL
}
}
if (!is.null(string)&&nchar(string)==1) {
found_char <- TRUE
message("Found problematic character")
}
}
return(list(char = string, seq = prob_seq))
}
unwanted_array = list( 'Š'='S', 'š'='s', 'Ž'='Z', 'ž'='z', 'À'='A', 'Á'='A', 'Â'='A', 'Ã'='A', 'Ä'='A', 'Å'='A', 'Æ'='A', 'Ç'='C', 'È'='E', 'É'='E',
'Ê'='E', 'Ë'='E', 'Ì'='I', 'Í'='I', 'Î'='I', 'Ï'='I', 'Ñ'='N', 'Ò'='O', 'Ó'='O', 'Ô'='O', 'Õ'='O', 'Ö'='O', 'Ø'='O', 'Ù'='U',
'Ú'='U', 'Û'='U', 'Ü'='U', 'Ý'='Y', 'Þ'='B', 'ß'='Ss', 'à'='a', 'á'='a', 'â'='a', 'ã'='a', 'ä'='a', 'å'='a', 'æ'='a', 'ç'='c',
'è'='e', 'é'='e', 'ê'='e', 'ë'='e', 'ì'='i', 'í'='i', 'î'='i', 'ï'='i', 'ð'='o', 'ñ'='n', 'ò'='o', 'ó'='o', 'ô'='o', 'õ'='o',
'ö'='o', 'ø'='o', 'ù'='u', 'ú'='u', 'û'='u', 'ü'='u', 'ý'='y', 'ý'='y', 'þ'='b', 'ÿ'='y' )
# Spent a lot of time because of character in a name of a column causing on a right truncation very seldomly so it seemed that the names were ok but actually they were causing HUGE problems in MSSQL
# https://stackoverflow.com/questions/20495598/replace-accented-characters-in-r-with-non-accented-counterpart-utf-8-encoding
fix_column_names <- function(df, drastic = FALSE) {
x <- names(df)
x <- str_replace_all(x, regex("\\W+"), " ")
x <- chartr(paste(names(unwanted_array), collapse=''),
paste(unwanted_array, collapse=''),
x)
#x <- iconv(x, to='ASCII//TRANSLIT')
names(df) <- sapply(x, function(y) {
#y <- iconv(iconv(enc2utf8(y), from="UTF-8", to="latin1", toRaw = TRUE), from="latin1", to="UTF-8")
substring(y, 1, 128) #128 normally
})
names(df) <- chartr(".","_",names(as_tibble(df, .name_repair = "unique")))
df
}
# Thanks to https://stackoverflow.com/questions/48105277/writing-unicode-from-r-to-sql-server
# Slightly modified / improved
copy_to_mssql <- function(con, df, tbl_name) {
DBI::dbWriteTable(conn = con,
name = SQL(tbl_name),
value = convert_to_UTF16_df(df),
overwrite = TRUE,
row.names = FALSE,
field.types = mssql_field_types(df)
)
}
insert_to_mssql <- function(con, df, tbl_name) {
DBI::dbAppendTable(conn = con,
name = SQL(tbl_name),
value = convert_to_UTF16_df(df)
)
}
# End of copied code
wait_approx <- function(seconds) {
date_time<-Sys.time()
while((as.numeric(Sys.time()) - as.numeric(date_time))<seconds){
Sys.sleep(0.1)
}
}
render_view <- function(view, view_name) {
view_select <- view %>% sql_render() %>% as.character()
message("Creating view: ", view_select)
create_view(view_name, view_select)
}
# slicker would be to use a create statement directly from the select but not sure how for now as it may be database specific...
render_tbl <- function(tbl_reference, tbl_name) {
tbl_data <- tbl_reference %>% collect()
connect_repeat_query(query = function(con) {
copy_to_mssql(con, tbl_data, tbl_name)
})
}
# need to suppress the error message on success.
create_view <- function(view_name, view_select) {
connect_repeat_query(query = function(con) {
tryCatchLog({
tbl(con, view_name)
DBI::dbExecute(conn = con, paste0("ALTER VIEW dbo.", view_name," AS ", view_select))
message("Successfully altered view: ", view_name)
}, error = function(e) {
DBI::dbExecute(conn = con, paste0("CREATE VIEW dbo.", view_name," AS ", view_select))
tbl(con, view_name)
message("Successfully created new view: ", view_name)
})
})
}
connect_repeat_query <- function(query, max_repeats = 3, exponential_backoff = TRUE, seconds = 1, max_seconds = 120) {
counter <- 0
wait_time <- 0
result = NULL
while(is.null(result) & counter < max_repeats) {
if (counter>0) {
if (exponential_backoff) {
# full jitter
wait_time <- 2^(counter) * seconds * runif(1, 0, 1)
# decorrelated jitter + full jitter - modification of the following
# not yet tested
# https://www.awsarchitectureblog.com/2015/03/backoff.html
# wait_time <- min(2^(counter) * runif(1, seconds, max(seconds, wait_time) * 3), max_seconds)
} else {
wait_time <- seconds
}
message(paste0("Waiting ", wait_time," seconds before retry number ",counter,"."))
wait_approx(wait_time)
}
tryLog({
con <- connect_mssql()
result = query(con)
disconnect_mssql(con)
return(result)
}, include.full.call.stack = FALSE, include.compact.call.stack = FALSE)
counter <- counter + 1
}
stop(paste0("Query did not complete within ", max_repeats," tries."))
}
copy_to_mssql_large <- function(df, tbl_name, ...) {
connect_repeat_query(query = function(con) {
copy_to_mssql(con, df = df[0,], tbl_name = tbl_name)
})
insert_to_mssql_large(df, tbl_name, ...)
}
insert_to_mssql_large <- function(df, tbl_name, mem_limit = 10000000, ...) {
message(paste0("mem_limit = ", mem_limit))
# get size of data frame
# split into safe amount - based on one test assuming less than 3000000 bytes is ok (3840880 worked)
# determine the number of splits
n_splits <- as.numeric(ceiling(object.size(df)/mem_limit))+1
message(paste0("n_splits = ", n_splits))
n <- nrow(df)
message(paste0("The large insert includes ", n, "rows"))
max_rows <- floor(n / (n_splits)) + 1
message(paste0("The maximum number of rows per insert is ", max_rows, "rows"))
if (max_rows == 0) {
stop("A single row exceeds the memory limit used. You can try to set a different memory limit with the argument mem_limit.",
call. = FALSE)
}
# number of rows in final part - as a check
n_last <- n %% max_rows
r <- rep(1:ceiling(n/max_rows),each=max_rows)[1:n]
d <- split(df,r)
#### DEBIAN ONLY - need a system agnostic version in used
# memfree <- as.numeric(system("awk '/MemFree/ {print $2}' /proc/meminfo",
# intern=TRUE))
# message(paste0("Memory available: ", memfree))
purrr::map(.x = d, function(x) {
if (object.size(x) > mem_limit) {
message("Nesting inserts.")
insert_to_mssql_large(x, tbl_name, mem_limit = mem_limit, ...)
} else {
message(paste0("Inserting ",nrow(x)," rows with ", object.size(x)," size."))
connect_repeat_query(query = function(con) {
insert_to_mssql(con, x, tbl_name)
}, ...)
}
})
}
round_trip_encoding <- function(text, encoding = "UTF-16LE") {
if (Encoding(text)=="unknown") {
text16 <- iconv(
enc2utf8(text),
from = "UTF-8",
to = encoding,
toRaw = TRUE
)
} else {
text16 <- iconv(
text,
from = Encoding(text),
to = encoding,
toRaw = TRUE
)
}
iconv(
text16,
from = encoding,
to = "UTF-8"
)
}
#################### Encoding functions
# Below are functions from online sources that are very helpful to overcome the encoding issues
# Thanks to https://stackoverflow.com/questions/48105277/writing-unicode-from-r-to-sql-server and the prior response: https://stackoverflow.com/a/61363450/7406873
# Using these helper functions was required to be able to write data to the SQL server due to the problem of different encoding issues around the odbc driver, R, and mssql. Required to convert UTF-8 character string into raw UTF-16LE bytes in tables to avoid truncation errors on mssql and nul byte errors on R.
# I made modification to an answer to address non-UTF8 encodings in R character vectors and posted the updated on stackoverflow
# see about changing this to not require the rlist package for list.cbind
convert_to_UTF16_df <- function(df){
output <- cbind(df[sapply(df, typeof) != "character"]
, list.cbind(apply(df[sapply(df, typeof) == "character"], 2, function(x){
return(lapply(x, function(y) {
#e <- Encoding(y)
#print(e)
if (Encoding(y)=="unknown") {
unlist(iconv(enc2utf8(y), from = "UTF-8", to = "UTF-16LE", toRaw = TRUE))
} else {
unlist(iconv(y, from = Encoding(y), to = "UTF-16LE", toRaw = TRUE))
}
}))
}))
)[colnames(df)]
return(output)
}
# this is a dummy function to see if forcing text to latin1 will get rid of problematic sequences of characters
# convert_to_UTF16_df <- function(df) {
# output <- cbind(df[sapply(df, typeof) != "character"]
# , list.cbind(apply(df[sapply(df, typeof) == "character"], 2, function(x){
# return(lapply(x, function(y) {
# # First send to latin1 and then back to hopefully get rid of bad characters for the driver/mssql on linux
#
# rawbom <- raw(2)
# rawbom[1] <- as.raw(255)
# rawbom[2] <- as.raw(254)
#
#
# # y <- round_trip_encoding(y, encoding = "latin1")
# # y2 <- unlist(iconv(y, from = "UTF-8", to = "UTF-16LE", toRaw = TRUE))
#
# #y <- iconv(iconv(enc2utf8(y), from="UTF-8", to="latin1", toRaw = TRUE), from="latin1", to="UTF-8")
# # if(nchar(y)>100) {
# # message(y)
# # }
#
# #y <- paste0("AAAAAA ")
#
# if (Encoding(y)=="unknown") {
# y2 <- unlist(iconv(enc2utf8(y), from = "UTF-8", to = "UTF-16LE", toRaw = TRUE))
# } else {
# y2 <- unlist(iconv(y, from = Encoding(y), to = "UTF-16LE", toRaw = TRUE))
# }
#
# c(rawbom, y2)
#
# }))
# }))
# )[colnames(df)]
# return(output)
# }
field_details <- function(x) {
details <-
list(x=x,encoding=Encoding(x),bytes=nchar(x,"b"),chars=nchar(x,"c"),
width=nchar(x,"w"),raw=paste(charToRaw(x),collapse=":"))
print(t(as.matrix(details)))
}
convert_raw_UTF16_to_UTF8 <- function(raw_bytes) {
# https://github.com/tidyverse/readr/issues/306
# This is the text.
#text <- "1\t2\n"
# Converted to UTF-16LE
#text_utf16 <- iconv(text,from="UTF-8",to="UTF-16LE", toRaw = TRUE)
# This is the Byte Order Mark:
rawbom <- raw(2)
rawbom[1] <- as.raw(255)
rawbom[2] <- as.raw(254)
tmp_file_name <- tempfile()
fd <- file(tmp_file_name, "wb")
# write BOM to top of file
writeBin(rawbom, fd)
lapply(raw_bytes, function(rb) {
if (!is.null(rb)) {
writeBin(unlist(rb), fd)
} else {
writeBin(unlist(iconv("-- NULL --",from="UTF-8",to="UTF-16LE", toRaw = TRUE)), fd)
}
writeBin(unlist(iconv("\n",from="UTF-8",to="UTF-16LE", toRaw = TRUE)), fd)
})
read.delim(tmp_file_name, header=FALSE, fileEncoding = "UTF-16LE", stringsAsFactors = FALSE)
}
mssql_field_types <- function(df){
output <- list()
output[colnames(df)[sapply(df, typeof) == "character"]] <- "nvarchar(max)"
return(output)
}
show_query_and_collect <- function(dft){
dft %>% show_query() %>% collect()
}
all_equal_example <- function(dft, df) {
dft %>% collect() %>% all_equal(df)
}
list_tables <- function(con) {
DBI::dbListTables(con)
}