Skip to content

Commit 07544cf

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 391abec commit 07544cf

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
@@ -672,8 +674,8 @@ replace_dot_alias = function(e) {
672674
for (..name in av) {
673675
name = substr(..name, 3L, nchar(..name))
674676
if (!nzchar(name)) stopf("The symbol .. is invalid. The .. prefix must be followed by at least one character.")
675-
parent_has_..name = exists(..name, where=parent.frame())
676-
if (!exists(name, where=parent.frame())) {
677+
parent_has_..name = exists(..name, where=callenv)
678+
if (!exists(name, where=callenv)) {
677679
suggested = if (parent_has_..name)
678680
gettextf(" Variable '..%s' does exist in calling scope though, so please just removed the .. prefix from that variable name in calling scope.", name)
679681
# We have recommended 'manual' .. prefix in the past, so try to be helpful
@@ -688,7 +690,7 @@ replace_dot_alias = function(e) {
688690
}
689691
} else if (is.name(jsub)) {
690692
if (startsWith(as.character(jsub), "..")) internal_error("DT[, ..var] should be dealt with by the branch above now.") # nocov
691-
if (!with && !exists(as.character(jsub), where=parent.frame()))
693+
if (!with && !exists(as.character(jsub), where=callenv))
692694
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))
693695
}
694696
if (root=="{") {
@@ -704,7 +706,7 @@ replace_dot_alias = function(e) {
704706
# TODO: this && !any depends on data. Can we remove it?
705707
# Grab the dynamic expression from calling scope now to give the optimizer a chance to optimize it
706708
# Only when top level is eval call. Not nested like x:=eval(...) or `:=`(x=eval(...), y=eval(...))
707-
jsub = eval(jsub[[2L]], parent.frame(), parent.frame()) # this evals the symbol to return the dynamic expression
709+
jsub = eval(jsub[[2L]], callenv, callenv) # this evals the symbol to return the dynamic expression
708710
if (is.expression(jsub)) jsub = jsub[[1L]] # if expression, convert it to call
709711
# Note that the dynamic expression could now be := (new in v1.9.7)
710712
jsub = replace_dot_alias(jsub)
@@ -753,22 +755,22 @@ replace_dot_alias = function(e) {
753755
# Fixes 4994: a case where quoted expression with a "!", ex: expr = quote(!dt1); dt[eval(expr)] requires
754756
# the "eval" to be checked before `as.name("!")`. Therefore interchanged.
755757
restore.N = remove.N = FALSE
756-
old.N = get0(".N", envir=parent.frame(), inherits=FALSE)
758+
old.N = get0(".N", envir=callenv, inherits=FALSE)
757759
if (!is.null(old.N)) {
758-
locked.N = bindingIsLocked(".N", parent.frame())
759-
if (locked.N) eval(call("unlockBinding", ".N", parent.frame())) # eval call to pass R CMD check NOTE until we find cleaner way
760-
assign(".N", nrow(x), envir=parent.frame(), inherits=FALSE)
760+
locked.N = bindingIsLocked(".N", callenv)
761+
if (locked.N) eval(call("unlockBinding", ".N", callenv)) # eval call to pass R CMD check NOTE until we find cleaner way
762+
assign(".N", nrow(x), envir=callenv, inherits=FALSE)
761763
restore.N = TRUE
762764
# the comment below is invalid hereafter (due to fix for #1145)
763765
# binding locked when .SD[.N] but that's ok as that's the .N we want anyway
764766

765767
# TO DO: change isub at C level s/.N/nrow(x); changing a symbol to a constant should be ok
766768
} else {
767-
assign(".N", nrow(x), envir=parent.frame(), inherits=FALSE)
769+
assign(".N", nrow(x), envir=callenv, inherits=FALSE)
768770
remove.N = TRUE
769771
}
770772
if (isub %iscall% "eval") { # TO DO: or ..()
771-
isub = eval(.massagei(isub[[2L]]), parent.frame(), parent.frame())
773+
isub = eval(.massagei(isub[[2L]]), callenv, callenv)
772774
if (is.expression(isub)) isub=isub[[1L]]
773775
}
774776
if (isub %iscall% "!") {
@@ -784,7 +786,7 @@ replace_dot_alias = function(e) {
784786
if (is.null(isub)) return( null.data.table() )
785787

786788
if (length(o <- .prepareFastSubset(isub = isub, x = x,
787-
enclos = parent.frame(),
789+
enclos = callenv,
788790
notjoin = notjoin, verbose = verbose))){
789791
## redirect to the is.data.table(x) == TRUE branch.
790792
## Additional flag to adapt things after bmerge:
@@ -799,15 +801,15 @@ replace_dot_alias = function(e) {
799801
mult = "all"
800802
}
801803
else if (!is.name(isub)) {
802-
ienv = new.env(parent=parent.frame())
804+
ienv = new.env(parent=callenv)
803805
if (getOption("datatable.optimize") >= 1L) assign("order", forder, ienv)
804806
i = tryCatch(eval(.massagei(isub), x, ienv),
805807
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."),
806808
error = function(e) .checkTypos(e, names_x)
807809
)
808810
} else {
809811
# isub is a single symbol name such as B in DT[B]
810-
i = try(eval(isub, parent.frame(), parent.frame()), silent=TRUE)
812+
i = try(eval(isub, callenv, callenv), silent=TRUE)
811813
if (inherits(i,"try-error") || is.function(i)) {
812814
# must be "not found" since isub is a mere symbol
813815
col = try(eval(isub, x), silent=TRUE) # is it a column name?
@@ -822,10 +824,10 @@ replace_dot_alias = function(e) {
822824
}
823825
}
824826
if (restore.N) {
825-
assign(".N", old.N, envir=parent.frame())
826-
if (locked.N) lockBinding(".N", parent.frame())
827+
assign(".N", old.N, envir=callenv)
828+
if (locked.N) lockBinding(".N", callenv)
827829
}
828-
if (remove.N) rm(list=".N", envir=parent.frame())
830+
if (remove.N) rm(list=".N", envir=callenv)
829831
if (is.matrix(i)) {
830832
if (is.numeric(i) && ncol(i)==1L) { # #826 - subset DT on single integer vector stored as matrix
831833
i = as.integer(i)
@@ -1121,10 +1123,10 @@ replace_dot_alias = function(e) {
11211123
# fix for #1216, make sure the parentheses are peeled from expr of the form (((1:4)))
11221124
while (jsub %iscall% "(") jsub = as.list(jsub)[[-1L]]
11231125
if (jsub %iscall% ":" && length(jsub)==3L) {
1124-
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
1126+
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
11251127
} else {
11261128
names(..syms) = ..syms
1127-
j = eval(jsub, lapply(substr(..syms, 3L, nchar(..syms)), get, pos=parent.frame()), parent.frame())
1129+
j = eval(jsub, lapply(substr(..syms, 3L, nchar(..syms)), get, pos=callenv), callenv)
11281130
}
11291131
if (is.logical(j)) j = which(j)
11301132
if (!length(j) && !notj) return( null.data.table() )
@@ -1184,14 +1186,14 @@ replace_dot_alias = function(e) {
11841186
bysuborig = as.symbol("I")
11851187
}
11861188
if (is.name(bysub) && !(bysub %chin% names_x)) { # TO DO: names(x),names(i),and i. and x. prefixes
1187-
bysub = eval(bysub, parent.frame(), parent.frame())
1189+
bysub = eval(bysub, callenv, callenv)
11881190
# fix for # 5106 - http://stackoverflow.com/questions/19983423/why-by-on-a-vector-not-from-a-data-table-column-is-very-slow
11891191
# case where by=y where y is not a column name, and not a call/symbol/expression, but an atomic vector outside of DT.
11901192
# note that if y is a list, this'll return an error (not sure if it should).
11911193
if (is.atomic(bysub)) bysubl = list(bysuborig) else bysubl = as.list.default(bysub)
11921194
}
11931195
if (length(bysubl) && identical(bysubl[[1L]],quote(eval))) { # TO DO: or by=..()
1194-
bysub = eval(bysubl[[2L]], parent.frame(), parent.frame())
1196+
bysub = eval(bysubl[[2L]], callenv, callenv)
11951197
bysub = replace_dot_alias(bysub) # fix for #1298
11961198
if (is.expression(bysub)) bysub=bysub[[1L]]
11971199
bysubl = as.list.default(bysub)
@@ -1202,7 +1204,7 @@ replace_dot_alias = function(e) {
12021204
# tried before but since not wrapped in try() it failed on some tests
12031205
# or look for column names used in this by (since if none it wouldn't find column names anyway
12041206
# when evaled within full x[irows]). Trouble is that colA%%2L is a call and should be within frame.
1205-
tt = eval(bysub, parent.frame(), parent.frame())
1207+
tt = eval(bysub, callenv, callenv)
12061208
if (!is.character(tt)) stopf("by=c(...), key(...) or names(...) must evaluate to 'character'")
12071209
bysub=tt
12081210
} else if (is.call(bysub) && !(bysub %iscall% c("list", "as.list", "{", ".", ":"))) {
@@ -1256,9 +1258,9 @@ replace_dot_alias = function(e) {
12561258

12571259
if (is.null(irows)) {
12581260
if (bysub %iscall% ':' && length(bysub)==3L && is.name(bysub[[2L]]) && is.name(bysub[[3L]])) {
1259-
byval = eval(bysub, setattr(as.list(seq_along(x)), 'names', names_x), parent.frame())
1261+
byval = eval(bysub, setattr(as.list(seq_along(x)), 'names', names_x), callenv)
12601262
byval = as.list(x)[byval]
1261-
} else byval = eval(bysub, x, parent.frame())
1263+
} else byval = eval(bysub, x, callenv)
12621264
} else {
12631265
# length 0 when i returns no rows
12641266
if (!is.integer(irows)) internal_error("irows isn't integer") # nocov
@@ -1277,9 +1279,9 @@ replace_dot_alias = function(e) {
12771279
xss = `[.data.table`(x,irows,nomatch=nomatch,mult=mult,roll=roll,rollends=rollends)
12781280
}
12791281
if (bysub %iscall% ':' && length(bysub)==3L) {
1280-
byval = eval(bysub, setattr(as.list(seq_along(xss)), 'names', names(xss)), parent.frame())
1282+
byval = eval(bysub, setattr(as.list(seq_along(xss)), 'names', names(xss)), callenv)
12811283
byval = as.list(xss)[byval]
1282-
} else byval = eval(bysub, xss, parent.frame())
1284+
} else byval = eval(bysub, xss, callenv)
12831285
xnrow = nrow(xss)
12841286
# TO DO: pass xss (x subset) through into dogroups. Still need irows there (for :=), but more condense
12851287
# and contiguous to use xss to form .SD in dogroups than going via irows
@@ -1434,7 +1436,7 @@ replace_dot_alias = function(e) {
14341436
while(colsub %iscall% "(") colsub = as.list(colsub)[[-1L]]
14351437
if (colsub %iscall% ':' && length(colsub)==3L && !is.call(colsub[[2L]]) && !is.call(colsub[[3L]])) {
14361438
# .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
1437-
.SDcols = eval(colsub, setattr(as.list(seq_along(x)), 'names', names_x), parent.frame())
1439+
.SDcols = eval(colsub, setattr(as.list(seq_along(x)), 'names', names_x), callenv)
14381440
} else {
14391441
if (colsub %iscall% 'patterns') {
14401442
patterns_list_or_vector = eval_with_cols(colsub, names_x)
@@ -1445,7 +1447,7 @@ replace_dot_alias = function(e) {
14451447
patterns_list_or_vector
14461448
}
14471449
} else {
1448-
.SDcols = eval(colsub, parent.frame(), parent.frame())
1450+
.SDcols = eval(colsub, callenv, callenv)
14491451
# allow filtering via function in .SDcols, #3950
14501452
if (is.function(.SDcols)) {
14511453
.SDcols = lapply(x, .SDcols)
@@ -1550,10 +1552,10 @@ replace_dot_alias = function(e) {
15501552
#6033 revdep. Slowly deprecate in 1.17.0. Caller has given us `dt[, substitute(names(.SD))]` which means
15511553
# jsub is actually substitute(names(.SD)) instead of just names(.SD)
15521554
if (lhs %iscall% 'substitute')
1553-
lhs = eval(lhs, parent.frame(), parent.frame())
1555+
lhs = eval(lhs, callenv, callenv)
15541556

15551557
# lhs is e.g. (MyVar) or get("MyVar") or names(.SD) || setdiff(names(.SD), cols)
1556-
lhs = eval(lhs, list(.SD = setNames(logical(length(sdvars)), sdvars)), parent.frame())
1558+
lhs = eval(lhs, list(.SD = setNames(logical(length(sdvars)), sdvars)), callenv)
15571559
}
15581560
} else {
15591561
# `:=`(c2=1L,c3=2L,...)
@@ -1662,9 +1664,9 @@ replace_dot_alias = function(e) {
16621664
}
16631665
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
16641666
if (is.name(name)) {
1665-
assign(as.character(name),x,parent.frame(),inherits=TRUE)
1667+
assign(as.character(name),x,callenv,inherits=TRUE)
16661668
} else if (.is_simple_extraction(name)) {
1667-
.reassign_extracted_table(name, x)
1669+
.reassign_extracted_table(name, x, callenv)
16681670
} # TO DO: else if env$<- or list$<-
16691671
}
16701672
}
@@ -1709,7 +1711,7 @@ replace_dot_alias = function(e) {
17091711
}
17101712
} # end of if !missing_j
17111713

1712-
SDenv = new.env(parent=parent.frame())
1714+
SDenv = new.env(parent=callenv)
17131715

17141716
syms = all.vars(jsub)
17151717
syms = syms[startsWith(syms, "..")]
@@ -1722,9 +1724,9 @@ replace_dot_alias = function(e) {
17221724
# warningf(sym," in j is looking for ",getName," in calling scope, but a column '", sym, "' exists. Column names should not start with ..")
17231725
}
17241726
getName = substr(sym, 3L, nchar(sym))
1725-
getNameVal = get0(getName, parent.frame())
1727+
getNameVal = get0(getName, callenv)
17261728
if (is.null(getNameVal)) {
1727-
if (exists(sym, parent.frame())) next # user did 'manual' prefix; i.e. variable in calling scope has .. prefix
1729+
if (exists(sym, callenv)) next # user did 'manual' prefix; i.e. variable in calling scope has .. prefix
17281730
stopf("Variable '%s' is not found in calling scope. Looking in calling scope because this symbol was prefixed with .. in the j= parameter.", getName)
17291731
}
17301732
assign(sym, getNameVal, SDenv)
@@ -1801,7 +1803,7 @@ replace_dot_alias = function(e) {
18011803
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)
18021804
}
18031805

1804-
jval = eval(jsub, SDenv, parent.frame())
1806+
jval = eval(jsub, SDenv, callenv)
18051807
.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.
18061808

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

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

0 commit comments

Comments
 (0)