Skip to content

Commit 389acda

Browse files
committed
capture the calling scope once as callenv in [
Capture parent.frame() once at the top as callenv and use that for every evaluation in the caller's scope (i, j, by, .SDcols, on, :=, ..var, the .N swap, SDenv's parent, ...). No behaviour change.
1 parent 72a5526 commit 389acda

1 file changed

Lines changed: 42 additions & 39 deletions

File tree

‎R/data.table.R‎

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ replace_dot_alias = function(e) {
537537
missing_SDcols = missing(.SDcols)
538538
missing_drop = missing(drop)
539539
missing_verbose = missing(verbose)
540+
# capture parent.frame() changes once as calling scope
541+
callenv = parent.frame()
540542
if (!cedta()) {
541543
# Fix for #500 (to do)
542544
if (substitute(j) %iscall% c(":=", "let")) {
@@ -585,7 +587,7 @@ replace_dot_alias = function(e) {
585587
naturaljoin = FALSE
586588
names_x = names(x)
587589
if (missing_i && !missing_on) {
588-
on_tmp = eval.parent(.massagei(substitute(on)))
590+
on_tmp = eval(.massagei(substitute(on)), callenv)
589591
if (!is.list(on_tmp) || !length(names(on_tmp))) {
590592
warningf("When on= is provided but not i=, on= must be a named list or data.table|frame, and a natural join (i.e. join on common names) is invoked. Ignoring on= which is '%s'.", class1(on_tmp))
591593
on = NULL
@@ -673,8 +675,8 @@ replace_dot_alias = function(e) {
673675
for (..name in av) {
674676
name = substr(..name, 3L, nchar(..name))
675677
if (!nzchar(name)) stopf("The symbol .. is invalid. The .. prefix must be followed by at least one character.")
676-
parent_has_..name = exists(..name, where=parent.frame())
677-
if (!exists(name, where=parent.frame())) {
678+
parent_has_..name = exists(..name, where=callenv)
679+
if (!exists(name, where=callenv)) {
678680
suggested = if (parent_has_..name)
679681
gettextf(" Variable '..%s' does exist in calling scope though, so please just removed the .. prefix from that variable name in calling scope.", name)
680682
# We have recommended 'manual' .. prefix in the past, so try to be helpful
@@ -689,7 +691,7 @@ replace_dot_alias = function(e) {
689691
}
690692
} else if (is.name(jsub)) {
691693
if (startsWith(as.character(jsub), "..")) internal_error("DT[, ..var] should be dealt with by the branch above now.") # nocov
692-
if (!with && !exists(as.character(jsub), where=parent.frame()))
694+
if (!with && !exists(as.character(jsub), where=callenv))
693695
stopf("Variable '%s' is not found in calling scope. Looking in calling scope because you set with=FALSE. Also, please use .. symbol prefix and remove with=FALSE.", as.character(jsub))
694696
}
695697
if (root=="{") {
@@ -705,7 +707,7 @@ replace_dot_alias = function(e) {
705707
# TODO: this && !any depends on data. Can we remove it?
706708
# Grab the dynamic expression from calling scope now to give the optimizer a chance to optimize it
707709
# Only when top level is eval call. Not nested like x:=eval(...) or `:=`(x=eval(...), y=eval(...))
708-
jsub = eval(jsub[[2L]], parent.frame(), parent.frame()) # this evals the symbol to return the dynamic expression
710+
jsub = eval(jsub[[2L]], callenv, callenv) # this evals the symbol to return the dynamic expression
709711
if (is.expression(jsub)) jsub = jsub[[1L]] # if expression, convert it to call
710712
# Note that the dynamic expression could now be := (new in v1.9.7)
711713
jsub = replace_dot_alias(jsub)
@@ -754,22 +756,22 @@ replace_dot_alias = function(e) {
754756
# Fixes 4994: a case where quoted expression with a "!", ex: expr = quote(!dt1); dt[eval(expr)] requires
755757
# the "eval" to be checked before `as.name("!")`. Therefore interchanged.
756758
restore.N = remove.N = FALSE
757-
old.N = get0(".N", envir=parent.frame(), inherits=FALSE)
759+
old.N = get0(".N", envir=callenv, inherits=FALSE)
758760
if (!is.null(old.N)) {
759-
locked.N = bindingIsLocked(".N", parent.frame())
760-
if (locked.N) eval(call("unlockBinding", ".N", parent.frame())) # eval call to pass R CMD check NOTE until we find cleaner way
761-
assign(".N", nrow(x), envir=parent.frame(), inherits=FALSE)
761+
locked.N = bindingIsLocked(".N", callenv)
762+
if (locked.N) eval(call("unlockBinding", ".N", callenv)) # eval call to pass R CMD check NOTE until we find cleaner way
763+
assign(".N", nrow(x), envir=callenv, inherits=FALSE)
762764
restore.N = TRUE
763765
# the comment below is invalid hereafter (due to fix for #1145)
764766
# binding locked when .SD[.N] but that's ok as that's the .N we want anyway
765767

766768
# TO DO: change isub at C level s/.N/nrow(x); changing a symbol to a constant should be ok
767769
} else {
768-
assign(".N", nrow(x), envir=parent.frame(), inherits=FALSE)
770+
assign(".N", nrow(x), envir=callenv, inherits=FALSE)
769771
remove.N = TRUE
770772
}
771773
if (isub %iscall% "eval") { # TO DO: or ..()
772-
isub = eval(.massagei(isub[[2L]]), parent.frame(), parent.frame())
774+
isub = eval(.massagei(isub[[2L]]), callenv, callenv)
773775
if (is.expression(isub)) isub=isub[[1L]]
774776
}
775777
if (isub %iscall% "!") {
@@ -785,7 +787,7 @@ replace_dot_alias = function(e) {
785787
if (is.null(isub)) return( null.data.table() )
786788

787789
if (length(o <- .prepareFastSubset(isub = isub, x = x,
788-
enclos = parent.frame(),
790+
enclos = callenv,
789791
notjoin = notjoin, verbose = verbose))){
790792
## redirect to the is.data.table(x) == TRUE branch.
791793
## Additional flag to adapt things after bmerge:
@@ -800,15 +802,15 @@ replace_dot_alias = function(e) {
800802
mult = "all"
801803
}
802804
else if (!is.name(isub)) {
803-
ienv = new.env(parent=parent.frame())
805+
ienv = new.env(parent=callenv)
804806
if (getOption("datatable.optimize") >= 1L) assign("order", forder, ienv)
805807
i = tryCatch(eval(.massagei(isub), x, ienv),
806808
dt_invalid_let_error = function(e) stopf("Operator := detected in i, the first argument inside DT[...], but is only valid in the second argument, j. Most often, this happens when forgetting the first comma (e.g. DT[newvar := 5] instead of DT[ , new_var := 5]). Please double-check the syntax. Run traceback(), and debugger() to get a line number."),
807809
error = function(e) .checkTypos(e, names_x)
808810
)
809811
} else {
810812
# isub is a single symbol name such as B in DT[B]
811-
i = try(eval(isub, parent.frame(), parent.frame()), silent=TRUE)
813+
i = try(eval(isub, callenv, callenv), silent=TRUE)
812814
if (inherits(i,"try-error") || is.function(i)) {
813815
# must be "not found" since isub is a mere symbol
814816
col = try(eval(isub, x), silent=TRUE) # is it a column name?
@@ -823,10 +825,10 @@ replace_dot_alias = function(e) {
823825
}
824826
}
825827
if (restore.N) {
826-
assign(".N", old.N, envir=parent.frame())
827-
if (locked.N) lockBinding(".N", parent.frame())
828+
assign(".N", old.N, envir=callenv)
829+
if (locked.N) lockBinding(".N", callenv)
828830
}
829-
if (remove.N) rm(list=".N", envir=parent.frame())
831+
if (remove.N) rm(list=".N", envir=callenv)
830832
if (is.matrix(i)) {
831833
if (is.numeric(i) && ncol(i)==1L) { # #826 - subset DT on single integer vector stored as matrix
832834
i = as.integer(i)
@@ -1122,10 +1124,10 @@ replace_dot_alias = function(e) {
11221124
# fix for #1216, make sure the parentheses are peeled from expr of the form (((1:4)))
11231125
while (jsub %iscall% "(") jsub = as.list(jsub)[[-1L]]
11241126
if (jsub %iscall% ":" && length(jsub)==3L) {
1125-
j = eval(jsub, setattr(as.list(seq_along(x)), 'names', names_x), parent.frame()) # else j will be evaluated for the first time on next line
1127+
j = eval(jsub, setattr(as.list(seq_along(x)), 'names', names_x), callenv) # else j will be evaluated for the first time on next line
11261128
} else {
11271129
names(..syms) = ..syms
1128-
j = eval(jsub, lapply(substr(..syms, 3L, nchar(..syms)), get, pos=parent.frame()), parent.frame())
1130+
j = eval(jsub, lapply(substr(..syms, 3L, nchar(..syms)), get, pos=callenv), callenv)
11291131
}
11301132
if (is.logical(j)) j = which(j)
11311133
if (!length(j) && !notj) return( null.data.table() )
@@ -1185,14 +1187,14 @@ replace_dot_alias = function(e) {
11851187
bysuborig = as.symbol("I")
11861188
}
11871189
if (is.name(bysub) && !(bysub %chin% names_x)) { # TO DO: names(x),names(i),and i. and x. prefixes
1188-
bysub = eval(bysub, parent.frame(), parent.frame())
1190+
bysub = eval(bysub, callenv, callenv)
11891191
# fix for # 5106 - http://stackoverflow.com/questions/19983423/why-by-on-a-vector-not-from-a-data-table-column-is-very-slow
11901192
# case where by=y where y is not a column name, and not a call/symbol/expression, but an atomic vector outside of DT.
11911193
# note that if y is a list, this'll return an error (not sure if it should).
11921194
if (is.atomic(bysub)) bysubl = list(bysuborig) else bysubl = as.list.default(bysub)
11931195
}
11941196
if (length(bysubl) && identical(bysubl[[1L]],quote(eval))) { # TO DO: or by=..()
1195-
bysub = eval(bysubl[[2L]], parent.frame(), parent.frame())
1197+
bysub = eval(bysubl[[2L]], callenv, callenv)
11961198
bysub = replace_dot_alias(bysub) # fix for #1298
11971199
if (is.expression(bysub)) bysub=bysub[[1L]]
11981200
bysubl = as.list.default(bysub)
@@ -1203,7 +1205,7 @@ replace_dot_alias = function(e) {
12031205
# tried before but since not wrapped in try() it failed on some tests
12041206
# or look for column names used in this by (since if none it wouldn't find column names anyway
12051207
# when evaled within full x[irows]). Trouble is that colA%%2L is a call and should be within frame.
1206-
tt = eval(bysub, parent.frame(), parent.frame())
1208+
tt = eval(bysub, callenv, callenv)
12071209
if (!is.character(tt)) stopf("by=c(...), key(...) or names(...) must evaluate to 'character'")
12081210
bysub=tt
12091211
} else if (is.call(bysub) && !(bysub %iscall% c("list", "as.list", "{", ".", ":"))) {
@@ -1257,9 +1259,9 @@ replace_dot_alias = function(e) {
12571259

12581260
if (is.null(irows)) {
12591261
if (bysub %iscall% ':' && length(bysub)==3L && is.name(bysub[[2L]]) && is.name(bysub[[3L]])) {
1260-
byval = eval(bysub, setattr(as.list(seq_along(x)), 'names', names_x), parent.frame())
1262+
byval = eval(bysub, setattr(as.list(seq_along(x)), 'names', names_x), callenv)
12611263
byval = as.list(x)[byval]
1262-
} else byval = eval(bysub, x, parent.frame())
1264+
} else byval = eval(bysub, x, callenv)
12631265
} else {
12641266
# length 0 when i returns no rows
12651267
if (!is.integer(irows)) internal_error("irows isn't integer") # nocov
@@ -1278,9 +1280,9 @@ replace_dot_alias = function(e) {
12781280
xss = `[.data.table`(x,irows,nomatch=nomatch,mult=mult,roll=roll,rollends=rollends)
12791281
}
12801282
if (bysub %iscall% ':' && length(bysub)==3L) {
1281-
byval = eval(bysub, setattr(as.list(seq_along(xss)), 'names', names(xss)), parent.frame())
1283+
byval = eval(bysub, setattr(as.list(seq_along(xss)), 'names', names(xss)), callenv)
12821284
byval = as.list(xss)[byval]
1283-
} else byval = eval(bysub, xss, parent.frame())
1285+
} else byval = eval(bysub, xss, callenv)
12841286
xnrow = nrow(xss)
12851287
# TO DO: pass xss (x subset) through into dogroups. Still need irows there (for :=), but more condense
12861288
# and contiguous to use xss to form .SD in dogroups than going via irows
@@ -1435,7 +1437,7 @@ replace_dot_alias = function(e) {
14351437
while(colsub %iscall% "(") colsub = as.list(colsub)[[-1L]]
14361438
if (colsub %iscall% ':' && length(colsub)==3L && !is.call(colsub[[2L]]) && !is.call(colsub[[3L]])) {
14371439
# .SDcols is of the format a:b, ensure none of : arguments is a call data.table(V1=-1L, V2=-2L, V3=-3L)[,.SD,.SDcols=-V2:-V1] #4231
1438-
.SDcols = eval(colsub, setattr(as.list(seq_along(x)), 'names', names_x), parent.frame())
1440+
.SDcols = eval(colsub, setattr(as.list(seq_along(x)), 'names', names_x), callenv)
14391441
} else {
14401442
if (colsub %iscall% 'patterns') {
14411443
patterns_list_or_vector = eval_with_cols(colsub, names_x)
@@ -1446,7 +1448,7 @@ replace_dot_alias = function(e) {
14461448
patterns_list_or_vector
14471449
}
14481450
} else {
1449-
.SDcols = eval(colsub, parent.frame(), parent.frame())
1451+
.SDcols = eval(colsub, callenv, callenv)
14501452
# allow filtering via function in .SDcols, #3950
14511453
if (is.function(.SDcols)) {
14521454
.SDcols = lapply(x, .SDcols)
@@ -1551,10 +1553,10 @@ replace_dot_alias = function(e) {
15511553
#6033 revdep. Slowly deprecate in 1.17.0. Caller has given us `dt[, substitute(names(.SD))]` which means
15521554
# jsub is actually substitute(names(.SD)) instead of just names(.SD)
15531555
if (lhs %iscall% 'substitute')
1554-
lhs = eval(lhs, parent.frame(), parent.frame())
1556+
lhs = eval(lhs, callenv, callenv)
15551557

15561558
# lhs is e.g. (MyVar) or get("MyVar") or names(.SD) || setdiff(names(.SD), cols)
1557-
lhs = eval(lhs, list(.SD = setNames(logical(length(sdvars)), sdvars)), parent.frame())
1559+
lhs = eval(lhs, list(.SD = setNames(logical(length(sdvars)), sdvars)), callenv)
15581560
}
15591561
} else {
15601562
# `:=`(c2=1L,c3=2L,...)
@@ -1663,9 +1665,9 @@ replace_dot_alias = function(e) {
16631665
}
16641666
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
16651667
if (is.name(name)) {
1666-
assign(as.character(name),x,parent.frame(),inherits=TRUE)
1668+
assign(as.character(name),x,callenv,inherits=TRUE)
16671669
} else if (.is_simple_extraction(name)) {
1668-
.reassign_extracted_table(name, x)
1670+
.reassign_extracted_table(name, x, callenv)
16691671
} # TO DO: else if env$<- or list$<-
16701672
}
16711673
}
@@ -1710,7 +1712,7 @@ replace_dot_alias = function(e) {
17101712
}
17111713
} # end of if !missing_j
17121714

1713-
SDenv = new.env(parent=parent.frame())
1715+
SDenv = new.env(parent=callenv)
17141716

17151717
syms = all.vars(jsub)
17161718
syms = syms[startsWith(syms, "..")]
@@ -1723,9 +1725,9 @@ replace_dot_alias = function(e) {
17231725
# warningf(sym," in j is looking for ",getName," in calling scope, but a column '", sym, "' exists. Column names should not start with ..")
17241726
}
17251727
getName = substr(sym, 3L, nchar(sym))
1726-
getNameVal = get0(getName, parent.frame())
1728+
getNameVal = get0(getName, callenv)
17271729
if (is.null(getNameVal)) {
1728-
if (exists(sym, parent.frame())) next # user did 'manual' prefix; i.e. variable in calling scope has .. prefix
1730+
if (exists(sym, callenv)) next # user did 'manual' prefix; i.e. variable in calling scope has .. prefix
17291731
stopf("Variable '%s' is not found in calling scope. Looking in calling scope because this symbol was prefixed with .. in the j= parameter.", getName)
17301732
}
17311733
assign(sym, getNameVal, SDenv)
@@ -1802,7 +1804,7 @@ replace_dot_alias = function(e) {
18021804
stopf("j (the 2nd argument inside [...]) is a single symbol but column name '%1$s' is not found. If you intended to select columns using a variable in calling scope, please try DT[, ..%1$s]. The .. prefix conveys one-level-up similar to a file system path.", jsubChar)
18031805
}
18041806

1805-
jval = eval(jsub, SDenv, parent.frame())
1807+
jval = eval(jsub, SDenv, callenv)
18061808
.Call(C_unlock, jval) # in case jval inherits .SD's lock, #1341 #2245. .Call directly (not via an R function like setattr or unlock) to avoid bumping jval's MAYBE_SHARED.
18071809

18081810
# copy 'jval' when required
@@ -1848,7 +1850,7 @@ replace_dot_alias = function(e) {
18481850
name = substitute(x)
18491851
setalloccol(x, verbose=FALSE)
18501852
if (is.name(name)) {
1851-
assign(as.character(name), x, parent.frame(), inherits=TRUE)
1853+
assign(as.character(name), x, callenv, inherits=TRUE)
18521854
}
18531855
}
18541856
# TODO?: use set() here now that it can add new columns. Then remove newnames and alloc logic above.
@@ -1927,6 +1929,7 @@ replace_dot_alias = function(e) {
19271929
# #3694/#761 common gotcha -- doing t1-t0 by group, but -.POSIXt uses units='auto'
19281930
# independently by group & attr mismatch among groups is ignored. The latter
19291931
# is a more general issue but the former can be fixed by forcing units='secs'
1932+
# parent.frame() in here is the frame calling `-`, i.e. where j is evaluated for a group, not callenv
19301933
SDenv$`-.POSIXt` = function(e1, e2) {
19311934
if (!inherits(e2, 'POSIXt')) {
19321935
return(base::`-.POSIXt`(e1, e2))
@@ -2053,7 +2056,7 @@ replace_dot_alias = function(e) {
20532056
lockBinding(".NGRP", SDenv)
20542057

20552058
# Determine GForce-optimized query
2056-
gforce_result = .attempt_optimize(jsub, jvnames, sdvars, SDenv, verbose, i, byjoin, f__, ansvars, use.I, lhs, names_x, parent.frame())
2059+
gforce_result = .attempt_optimize(jsub, jvnames, sdvars, SDenv, verbose, i, byjoin, f__, ansvars, use.I, lhs, names_x, callenv)
20572060
GForce = gforce_result$GForce
20582061
jsub = gforce_result$jsub
20592062
jvnames = gforce_result$jvnames
@@ -2089,7 +2092,7 @@ replace_dot_alias = function(e) {
20892092
if (!length(ansvars) && !use.I) { # j is only .N (see .optimize_gforce), so gforce() isn't needed
20902093
ans = list(len__)
20912094
} else {
2092-
thisEnv = new.env() # not parent=parent.frame() so that gsum is found
2095+
thisEnv = new.env() # not parent=callenv so that gsum is found
20932096
for (ii in ansvars) assign(ii, .subset2(x, ii), thisEnv)
20942097
assign(".N", len__, thisEnv) # For #334
20952098
# fix for #1683

0 commit comments

Comments
 (0)