diff --git a/NEWS.md b/NEWS.md index 4236e8b07..1a50ca096 100644 --- a/NEWS.md +++ b/NEWS.md @@ -115,6 +115,8 @@ rowwiseDT( 15. `DT[1, on=NULL]` now works for returning the first row, [#6579](https://github.com/Rdatatable/data.table/issues/6579). Thanks to @Kodiologist for the report and @tdhock for the PR. +16. Joining with incompatible column times (e.g., `Date` with `POSIXt`) now provides a clear warning, [#6605](https://github.com/Rdatatable/data.table/issues/6605). Thanks to @al-obrien for the report and @r2evans for the PR. + ## NOTES 1. Tests run again when some Suggests packages are missing, [#6411](https://github.com/Rdatatable/data.table/issues/6411). Thanks @aadler for the note and @MichaelChirico for the fix. diff --git a/R/bmerge.R b/R/bmerge.R index aa7ea4103..c5f9a3f34 100644 --- a/R/bmerge.R +++ b/R/bmerge.R @@ -70,6 +70,14 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos } stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, xclass, iname, iclass) } + # data.table::as.ITime, chron::times, nanotime::nanotime + timeclasses = c("Date", "POSIXt", "ITime", "times", "nanotime") + xclass_time = intersect(class(x[[xc]]), timeclasses) + iclass_time = intersect(class(i[[ic]]), timeclasses) + if (length(xclass_time) > 0L && length(iclass_time) > 0L && !identical(sort(xclass_time), sort(iclass_time))) { + warningf("Attempting to join column %s (%s) with column %s (%s). They are likely to be incompatible numbers, suggest you convert one to the other's class.", + xname, toString(xclass_time), iname, toString(iclass_time)) + } if (xclass == iclass) { if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, xclass, xname) next diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d68130255..8636d764f 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -20596,3 +20596,6 @@ test(2295.3, is.data.table(d2)) # #6588: .checkTypos used to give arbitrary strings to stopf as the first argument test(2296, d2[x %no such operator% 1], error = '%no such operator%') + +# #6605: Joins do not warn user when using POSc and Date comparisons +test(2297.1, data.table(a = Sys.time(), v1 = 1)[data.table(a = Sys.Date(), v2 = 2), on = "a"], output = ".+", warning = "incompatible")