Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@

27. `setnames` of whole table when original table had `NA` names skipped replacing those, [#2475](https://github.com/Rdatatable/data.table/issues/2475). Thanks to @franknarf1 and [BenoitLondon on StackOverflow](https://stackoverflow.com/questions/47228836/) for the report and @MichaelChirico for fixing.

28. CJ() works with multiple empty vectors now [#2511](https://github.com/Rdatatable/data.table/issues/2511). Thanks to @MarkusBonsch for fixing.


#### NOTES

Expand Down
12 changes: 9 additions & 3 deletions R/setkey.R
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,18 @@ CJ <- function(..., sorted = TRUE, unique = FALSE)
# Cross Join will then produce a join table with the combination of all values (cross product).
# The last vector is varied the quickest in the table, so dates should be last for roll for example
l = list(...)
if (unique) l = lapply(l, unique)
emptyList <- FALSE ## fix for #2511
if(any(sapply(l, length) == 0)){
## at least one column is empty The whole thing will be empty in the end
emptyList <- TRUE
l <- lapply(l, "[", 0)
}
if (unique && !emptyList) l = lapply(l, unique)

dups = FALSE # fix for #1513
if (length(l)==1L && sorted && length(o <- forderv(l[[1L]])))
if (length(l)==1L && !emptyList && sorted && length(o <- forderv(l[[1L]])))
l[[1L]] = l[[1L]][o]
else if (length(l) > 1L) {
else if (length(l) > 1L && !emptyList) {
# using rep.int instead of rep speeds things up considerably (but attributes are dropped).
attribs = lapply(l, attributes) # remember attributes for resetting after rep.int
n = vapply(l, length, 0L)
Expand Down
10 changes: 10 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -11187,6 +11187,16 @@ DT = setNames(data.frame(a = 1, b = 2, c = 3, d = 4), c(NA, "b", "c", NA))
setnames(DT, c('a', 'b', 'c', 'd'))
test(1853, names(DT), c('a', 'b', 'c', 'd'))

# CJ bug with multiple empty vectors (#2511)
test(1854.1, data.frame(CJ(x = integer(0))), setattr(expand.grid(x = integer(0)), "out.attrs", NULL))
test(1854.2, data.frame(CJ(x = integer(0), y = character(0))), setattr(expand.grid(x = integer(0), y = character(0)), "out.attrs", NULL))
test(1854.3, data.frame(CJ(x = integer(0), y = c("a", "b"))), setattr(expand.grid(x = integer(0), y = c("a", "b")), "out.attrs", NULL))
test(1854.4, data.frame(CJ(x = integer(0), y = character(0), z = logical(0))), setattr(expand.grid(x = integer(0), y = character(0), z = logical(0)), "out.attrs", NULL))
test(1854.5, data.frame(CJ(x = character(0), y = NA_real_)), setattr(expand.grid(x = character(0), y = NA_real_), "out.attrs", NULL))
if ("package:bit64" %in% search()) {
test(1854.6, data.frame(CJ(x = integer64(0), y = as.integer64(2))), setattr(expand.grid(x = integer64(0), y = as.integer64(2)), "out.attrs", NULL))
}

##########################

# TODO: Tests involving GForce functions needs to be run with optimisation level 1 and 2, so that both functions are tested all the time.
Expand Down