Skip to content

Commit

Permalink
maybe_reset_index() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Jul 12, 2024
1 parent 8b3a80a commit 497a08f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 1 addition & 4 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
Expand Down
12 changes: 8 additions & 4 deletions R/setkey.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 497a08f

Please sign in to comment.