diff --git a/R/data.table.R b/R/data.table.R index 75d7f55fb..a0d781978 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -3238,10 +3238,7 @@ is_constantish = function(q, check_singleton=FALSE) { if (verbose) {catf("Creating new index '%s'\n", paste(idxCols, collapse = "__"));flush.console()} if (verbose) {last.started.at=proc.time();catf("Creating index %s done in ...", paste(idxCols, collapse = "__"));flush.console()} idx = forderv(x, idxCols, sort=TRUE, retGrp=FALSE, lazy=TRUE) - if (!isTRUE(getOption("datatable.forder.auto.index"))) { ## forder can write index, but disabled for now, see #4386 - if (is.null(attr(x, "index", exact=TRUE))) setattr(x, "index", integer()) - setattr(attr(x, "index", exact=TRUE), paste0("__", idxCols, collapse=""), idx) - } + maybe_reset_index(x, idxCols, idx) ## forder can write index, but disabled for now, see #4386 if (verbose) {cat(timetaken(last.started.at),"\n");flush.console()} if (verbose) {catf("Optimized subsetting with index '%s'\n", paste(idxCols, collapse = "__"));flush.console()} } diff --git a/R/setkey.R b/R/setkey.R index d4ec9e3b7..c7ee8b0bf 100644 --- a/R/setkey.R +++ b/R/setkey.R @@ -72,10 +72,7 @@ setkeyv = function(x, cols, verbose=getOption("datatable.verbose"), physical=TRU o = forderv(x, cols, sort=TRUE, retGrp=!physical, lazy=TRUE) } if (!physical) { # index COULD BE saved from C forderMaybePresorted already, but disabled for now - if (!isTRUE(getOption("datatable.forder.auto.index"))) { - if (is.null(attr(x, "index", exact=TRUE))) setattr(x, "index", integer()) - setattr(attr(x, "index", exact=TRUE), paste0("__", cols, collapse=""), o) - } + maybe_reset_index(x, cols, o) return(invisible(x)) } if (length(o)) { @@ -143,6 +140,13 @@ is.sorted = function(x, by=NULL) { # Return value of TRUE/FALSE is relied on in [.data.table quite a bit on vectors. Simple. Stick with that (rather than -1/0/+1) } +maybe_reset_index = function(x, idx, cols) { + if (isTRUE(getOption("datatable.forder.auto.index"))) return(invisible()) + if (is.null(attr(x, "index", exact=TRUE))) setattr(x, "index", integer()) + setattr(attr(x, "index", exact=TRUE), paste0("__", cols, collapse=""), idx) + invisible(x) +} + ORDERING_TYPES = c('logical', 'integer', 'double', 'complex', 'character') forderv = function(x, by=seq_along(x), retGrp=FALSE, retStats=retGrp, sort=TRUE, order=1L, na.last=FALSE, lazy=getOption("datatable.forder.lazy",NA)) { if (is.atomic(x) || is.null(x)) { # including forderv(NULL) which returns error consistent with base::order(NULL),