Repository navigation
fread from 1.11: self healing regression for fill = T when unmatched quote occurs #2859
Description
Activity
Thanks Christian, this is an interesting example.
I notice that the file is read correctly under the default setting
fill=FALSE. There is even a warning:Warning message: In fread("/Users/pasha/Downloads/dt_2859.csv", verbose = T) : Found and resolved improper quoting out-of-sample. First healed line 20001: <<20000,"20000,20000,20000,20000,20000,20000,20000,20000,20000>>. If the fields are not quoted (e.g. field separator does not appear within any field), try quote="" to avoid this warning.But if you do need
fill=TRUE, the problems start to appear.- In the verbose mode, a message is printed
Column 2 ("V2") bumped from 'int32' to 'string' due to <<"20000,20000,20000,20000 ..., and that...contains almost the entire file. Lucky it's just a megabyte, not gigabyte. So the problem is that the error message"Column %d (\"%.*s\") bumped from '%s' to '%s' due to <<%.*s>> on row %llu\n"does not sanitize the field throughstrlim(). - That entire field, starting from
"20000, ...until the end of file becomes cell [20000, 1] of the resulting DT, and the rest of the elements on row 20000 become NAs (because of fill=TRUE). The problem here is that if there are no closing quote anywhere in the file, the field should not have parsed under QR=0; and under QR=3 it wouldn't have gobbled any newlines. - Presumably, printing such a large string element crashes RStudio (this could actually be RStudio's problem). It also looks quite horribly in R console (truncate string fields that are too long when printing a DT?).
Lastly, I'd like to point out that using
fill=TRUEwith a file which contains invalid quotes is quite dangerous. If there was another quote anywhere in the file (and frankly I'm surprised there is only one!), then everything between the two quotes would be converted into a single field (because that's what the CSV standard demands). You'd need to specify explicitlyquoteRule=3to parse such a file, and that's only after #2768 is implemented...- In the verbose mode, a message is printed
Regarding 3.: It's not only printing that crashes RStudio. In my original files that resulted in the issue I
freadmultiple files in apurrr::mapiteration and that causes R to crash, I actually don't execute the script in RStudio by default. The resulting exception is:*** caught segfault *** address (nil), cause 'memory not mapped' Traceback: 1: fread(.x, fill = T, skip = 1, integer64 = "character") 2: cbind(fread(.x, fill = T, skip = 1, integer64 = "character"), foo = "bar") 3: .f(.x[[i]], ...) 4: .Call(map_impl, environment(), ".x", ".f", "list") 5: purrr::map(file_list$filename, function(.x) { if (file.size(.x)== 0) { cat(paste0("Skipping empty file ", .x), sep = "\n") return(NULL) } cat(paste0("Loading file ", .x), sep = "\n") cbind(fread(.x, fill = T, skip = 1, integer64 = "character"), foo = "bar")}) 6: eval(lhs, parent, parent) 7: eval(lhs, parent, parent) 8: purrr::map(file_list$filename, function(.x) { if (file.size(.x)== 0) { cat(paste0("Skipping empty file ", .x), sep = "\n") return(NULL) } cat(paste0("Loading file ", .x), sep = "\n") cbind(fread(.x, fill = T, skip = 1, integer64 = "character"), foo = "bar")}) %>% rbindlist(fill = T) An irrecoverable exception occurred. R is aborting now ...I agree that unmatched quotes are extremely dangerous and could crash any csv reader by default. In my actual files there are multiple quotes but there are a few thousand lines between them. Which is obviously terrible but I never knew because it applies to a small subset of files. Also, in the past I was on a data.table version which was not 1.10.4 or 1.11 which read the files just fine even without manually setting
quote = ""(I guess it must have guessed) but I don't know anymore which version that could have been (probably 1.10.X).Eventually, I'd expect
freadto automatically setquote = ""by scanning the file, just as humans would.This appears to be a similar case, though it crashes whether
fillis TRUE or FALSE. The file is naturally unquoted, but has"within fields. Settingquote=''avoids the crash.library(data.table) system('wget -qO jstor.txt http://www.jstor.org/kbart/collections/all-archive-titles?contentType=journals')) jstor<-fread('jstor.txt',quote='') # works as expected jstor<-fread('jstor.txt',fill=F) # crashes R session jstor<-fread('jstor.txt',fill=T) # crashes R session
> devtools::session_info() Session info ---------------------------------------------------------------- setting value version R version 3.4.4 (2018-03-15) system x86_64, linux-gnu ui RStudio (1.1.419) language (EN) collate en_US.UTF-8 tz Etc/UTC date 2018-08-29 Packages -------------------------------------------------------------------- package * version date source base * 3.4.4 2018-04-21 local compiler 3.4.4 2018-04-21 local data.table * 1.11.5 2018-08-29 Github (Rdatatable/data.table@8d4ce7a) datasets * 3.4.4 2018-04-21 local devtools 1.13.4 2017-11-09 CRAN (R 3.4.4) digest 0.6.16 2018-08-22 cran (@0.6.16) graphics * 3.4.4 2018-04-21 local grDevices * 3.4.4 2018-04-21 local memoise 1.1.0 2017-04-21 CRAN (R 3.4.4) methods * 3.4.4 2018-04-21 local stats * 3.4.4 2018-04-21 local tools 3.4.4 2018-04-21 local utils * 3.4.4 2018-04-21 local withr 2.1.2 2018-03-15 CRAN (R 3.4.4) yaml 2.2.0 2018-07-25 CRAN (R 3.4.4)@dhanashreedeshpande indeed, jstor is not related to json. Please read
freadmanual for more details: https://rdatatable.gitlab.io/data.table/library/data.table/html/fread.html@dhanashreedeshpande it means that documentation has been written, and maintained for the purposed of answering such a basic questions, and even more complex. Please do not continue "json" topic in this issue as it is unrelated to json, so it is polluting original topic.
freaddoes not support reading json format, currently there are no plans to support it, there were no feature requests for it.@dhanashreedeshpande please see tools specialized for JSON format, e.g.
jsonlite.
For the attached file, the following code will not throw a warning in
data.table1.11 whereas we get an expected warning with 1.10.4. Background: the file contains an unmatched quote in line 20000:This results in warning with following sessionInfo():
whereas `fread` stays quiet with following sessionInfo():
In version 1.11 only the first 20000 rows are read and at least for me rstudio crashes when trying to print the resulting
dt_2859. With version 1.10.4 however we get a warning and know to setquote = "".