diff --git a/R/bmerge.R b/R/bmerge.R index 848b45739..b661cd47a 100644 --- a/R/bmerge.R +++ b/R/bmerge.R @@ -48,7 +48,7 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos stop("Attempting roll join on factor column when joining x.",names(x)[xc]," to i.",names(i)[ic],". Only integer, double or character columns may be roll joined.") if (xclass=="factor" && iclass=="factor") { if (verbose) cat("Matching i.",names(i)[ic]," factor levels to x.",names(x)[xc]," factor levels.\n",sep="") - set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]]) + set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]]) # nomatch=0L otherwise a level that is missing would match to NA values next } else { if (xclass=="character") { @@ -57,11 +57,13 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos next } else if (iclass=="character") { if (verbose) cat("Matching character column i.",names(i)[ic]," to factor levels in x.",names(x)[xc],".\n",sep="") - set(i, j=ic, value=chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L)) + newvalue = chmatch(i[[ic]], levels(x[[xc]]), nomatch=0L) + if (anyNA(i[[ic]])) newvalue[is.na(i[[ic]])] = NA_integer_ # NA_character_ should match to NA in factor, #3809 + set(i, j=ic, value=newvalue) next } - # else incompatible join type error below (factors can only join to factors or character) } + stop("Incompatible join types: x.", names(x)[xc], " (",xclass,") and i.", names(i)[ic], " (",iclass,"). Factor columns must join to factor or character columns.") } if (xclass == iclass) { if (verbose) cat("i.",names(i)[ic]," has same type (",xclass,") as x.",names(x)[xc],". No coercion needed.\n", sep="") diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 4582e25a6..78514cae0 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15864,6 +15864,12 @@ test(2095, any(grepl('override', capture.output(dcast(DT, a~b, fun.aggregate=len # gmean intermediate can overflow integers without warning, #986 test(2096, data.table(a=c(1L,1L), v=c(2e9L, 2e9L))[, mean(v), a], data.table(a=1L, V1=2e9)) +# NA_character joining to NA factor, #3809 +DT = data.table(fac = as.factor(c("a",NA,"b")), v=1:3, key="fac") +test(2097.1, DT[.(NA_character_)], data.table(fac=NA_character_, v=2L, key="fac")) +test(2097.2, DT[J(NA)], error="Factor columns must join to factor or character columns") +test(2097.3, DT[J(NA_integer_)], error="Factor columns must join to factor or character columns") + ################################### # Add new tests above this line #