From 21e68c02e90927c2d7ca9095bd70aa5c24a54213 Mon Sep 17 00:00:00 2001 From: Cole Miller <57992489+ColeMiller1@users.noreply.github.com> Date: Wed, 27 May 2020 21:31:45 -0400 Subject: [PATCH] Allow .SD; allow original is.sorted portion to escape if irows is.null --- R/data.table.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/data.table.R b/R/data.table.R index b20c6a5106..63029f3659 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -1337,21 +1337,21 @@ replace_dot_alias = function(e) { if (is.data.table(jval)) { setattr(jval, 'class', class(x)) # fix for #64 # can jval be sorted by the same key as x? improved for #4498 - get_shared_keys = function(jsub, jvnames, key) { + get_shared_keys = function(jsub, jvnames, sdvars, key) { if (is.null(key)) return(NULL) - if (!jsub %iscall% "list") return(NULL) - jnames = as.character(Filter(is.name, jsub)[-1L]) + if (!((SD_only <- jsub == quote(.SD))|| jsub %iscall% "list")) return(NULL) + jnames = if (SD_only) sdvars else as.character(Filter(is.name, jsub)[-1L]) key_idx = chmatch(key, jnames) missing_keys = which(is.na(key_idx)) if (length(missing_keys) && missing_keys[1L] == 1L) return(NULL) if (!length(missing_keys)) return(jvnames[key_idx]) return(jvnames[head(key_idx, missing_keys[1L] - 1L)]) } - shared_keys = get_shared_keys(jsub, jvnames, key(x)) + shared_keys = get_shared_keys(jsub, jvnames, sdvars = sdvars, key(x)) if (is.null(irows) && !is.null(shared_keys)) { setattr(jval, 'sorted', shared_keys) # potentially inefficient backup -- check if jval is sorted by key(x) - } else if (haskey(x) && all(key(x) %chin% names(jval)) && suppressWarnings(is.sorted(jval, by=key(x)))) { # TO DO: perhaps this usage of is.sorted should be allowed internally then (tidy up and make efficient) + } else if (haskey(x) && all(key(x) %chin% names(jval)) && (is.null(irows) || (!roll && .Call(CisOrderedSubset, irows, nrow(x))) || suppressWarnings(is.sorted(jval, by=key(x))))) { # TO DO: perhaps this usage of is.sorted should be allowed internally then (tidy up and make efficient) setattr(jval, 'sorted', key(x)) } if (any(vapply_1b(jval, is.null))) stop("Internal error: j has created a data.table result containing a NULL column") # nocov