diff --git a/NEWS.md b/NEWS.md index 2feb7f50fc..680bf10004 100644 --- a/NEWS.md +++ b/NEWS.md @@ -325,6 +325,8 @@ 43. `:=` could change a `data.table` creation statement in the body of the function calling it, or a variable in calling scope, [#3890](https://github.com/Rdatatable/data.table/issues/3890). Many thanks to @kirillmayantsev for the detailed reports. +44. Grouping could create a `malformed factor` and/or segfault when the factors returned by each group did not have identical levels, [#2199](https://github.com/Rdatatable/data.table/issues/2199) and [#2522](https://github.com/Rdatatable/data.table/issues/2522). Thanks to Václav Hausenblas, @franknarf1, @ben519, and @Henrik-P for reporting. + ## NOTES 1. `rbindlist`'s `use.names="check"` now emits its message for automatic column names (`"V[0-9]+"`) too, [#3484](https://github.com/Rdatatable/data.table/pull/3484). See news item 5 of v1.12.2 below. diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3e5661ac5e..b8739c9560 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -16136,6 +16136,36 @@ test(2113.3, rbindlist(list(list(a=numeric(), b=numeric()), list(a=as.factor(1L), b=as.factor(2:4)))), data.table(a=as.factor(1L), b=as.factor(2:4))) +# dogroups combine factor levels, #2199 & #2522 +DT = data.table(A=1:2) +g = function(x) { if (x==1L) factor(c("a","b888")) else factor(c("b888","c")) } # b888 to cover tl==0 in memrecycle +test(2114.1, DT[,g(.GRP),by=A], data.table(A=INT(1,1,2,2), V1=as.factor(c("a","b888","b888","c")))) +g = function(x) { if (x==1L) factor(c("a","b")) else factor(c("a","b","c")) } +test(2114.2, DT[,g(.GRP),by=A], data.table(A=INT(1,1,2,2,2), V1=as.factor(c("a","b","a","b","c")))) +# original test verbatim from the same issue #2199 +set.seed(2) +ids = sample(letters, 20) +dates = 1:40 +dt = data.table(CJ(dates, ids, ids)) +setnames(dt, c("date", "id1", "id2")) +dt[, value := rnorm(length(date))] +dt = dt[!(date == 1 & (id1 == "a" | id2 == "a"))] +dt = dt[!(date == 4 & (id1 == "e" | id2 == "e"))] +f1 = function(sdt) { + dt1 <- dcast.data.table(sdt, id1 ~ id2) + melt.data.table(dt1, id.vars = "id1") +} +res = dt[, f1(.SD), by=date] +test(2114.3, setnames(res[c(1,.N)],"variable","id2")[,id2:=as.character(id2)][], dt[c(1,.N)]) +test(2114.4, print(res), output="date.*0.433") +# and from #2522 +DT = data.table(id=1:9, grp=rep(1:3,each=3), val=c("a","b","c", "a","b","c", "a","b","c")) +test(2114.5, as.character(DT[, valfactor1 := factor(val), by = grp]$valfactor1), ans<-rep(c("a","b","c"),3)) +test(2114.6, as.character(DT[, valfactor2 := factor(val), by = id]$valfactor2), ans) +DT = data.table(x = rep(letters[c(3, 1, 2)], each = 2)) +test(2114.7, DT[, `:=`(g=.GRP, f=factor(.GRP)), by = x], + data.table(x=rep(c("c","a","b"),each=2), g=rep(1:3,each=2), f=factor(rep(as.character(1:3),each=2)))) + ################################### # Add new tests above this line # diff --git a/src/assign.c b/src/assign.c index 385176697e..2c9d6e95f2 100644 --- a/src/assign.c +++ b/src/assign.c @@ -818,6 +818,86 @@ const char *memrecycle(SEXP target, SEXP where, int start, int len, SEXP source) source = PROTECT(copyAsPlain(source)); protecti++; } } + const bool sourceIsFactor=isFactor(source), targetIsFactor=isFactor(target); + if (sourceIsFactor && targetIsFactor) { + // TODO: ^^ could be || in future; assign is already handling that so leave that for now. Then move more of assign to call here. + if (!sourceIsFactor || !targetIsFactor) { + // # nocov start + error("Internal error: memrecycle source_is_factor=%d but target_is_factor=%d. They both must be factor or both not factor.", + sourceIsFactor, targetIsFactor); + // # nocov end + } + SEXP sourceLevels = PROTECT(getAttrib(source, R_LevelsSymbol)); protecti++; + SEXP targetLevels = PROTECT(getAttrib(target, R_LevelsSymbol)); protecti++; + if (!R_compute_identical(sourceLevels, targetLevels, 0)) { + const int nTargetLevels=length(targetLevels), nSourceLevels=length(sourceLevels); + const SEXP *targetLevelsD=STRING_PTR(targetLevels), *sourceLevelsD=STRING_PTR(sourceLevels); + SEXP newSource = PROTECT(allocVector(INTSXP, length(source))); protecti++; + savetl_init(); + for (int k=0; k0) { + savetl(s); + } else if (tl<0) { + // # nocov start + for (int j=0; j=0) { + if (tl>0) savetl(s); + SET_TRUELENGTH(s, -nTargetLevels-(++nAdd)); + } + } + const int nSource = length(source); + const int *sourceD = INTEGER(source); + int *newSourceD = INTEGER(newSource); + for (int i=0; i