Skip to content

Commit 391abec

Browse files
committed
[.data.table: capture missing() of arguments once as flags (#852)
missing() works in the calling frame so it blocks moving code out of [.data.table. Capture it once at the top as missing_<arg> flags and keep them in sync where an argument is reassigned. No behaviour change.
1 parent 08f3c81 commit 391abec

2 files changed

Lines changed: 53 additions & 41 deletions

File tree

‎R/data.table.R‎

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -526,64 +526,76 @@ replace_dot_alias = function(e) {
526526
# ..selfcount <<- ..selfcount+1 # in dev, we check no self calls, each of which doubles overhead, or could
527527
# test explicitly if the caller is [.data.table (even stronger test. TO DO.)
528528
# the drop=NULL is to sink drop argument when dispatching to [.data.frame; using '...' stops test 147
529+
# missing() is only valid in this frame, so it is captured once here and code moved into helpers uses these flags, #852.
530+
# Keep them in sync when an argument is reassigned below: assigning a value clears missingness, `x = substitute()` restores it.
531+
missing_i = missing(i)
532+
missing_j = missing(j)
533+
missing_by = missing(by)
534+
missing_keyby = missing(keyby)
535+
missing_on = missing(on)
536+
missing_nomatch = missing(nomatch)
537+
missing_SDcols = missing(.SDcols)
538+
missing_drop = missing(drop)
539+
missing_verbose = missing(verbose)
529540
if (!cedta()) {
530541
# Fix for #500 (to do)
531542
if (substitute(j) %iscall% c(":=", "let")) {
532543
# Throw a specific error message
533544
stopf("[ was called on a data.table in an environment that is not data.table-aware (i.e. cedta()), but '%s' was used, implying the owner of this call really intended for data.table methods to be called. See vignette('datatable-importing') for details on properly importing data.table.", as.character(substitute(j)[[1L]]))
534545
}
535-
Nargs = nargs() - (!missing(drop))
546+
Nargs = nargs() - (!missing_drop)
536547
ans = if (Nargs<3L) { `[.data.frame`(x,i) } # drop ignored anyway by DF[i]
537-
else if (missing(drop)) `[.data.frame`(x,i,j)
548+
else if (missing_drop) `[.data.frame`(x,i,j)
538549
else `[.data.frame`(x,i,j,drop)
539550
# added is.data.table(ans) check to fix bug #81
540-
if (!missing(i) && is.data.table(ans)) setkey(ans, NULL) # drops index too; tested by plyr::arrange test in other.Rraw
551+
if (!missing_i && is.data.table(ans)) setkey(ans, NULL) # drops index too; tested by plyr::arrange test in other.Rraw
541552
return(ans)
542553
}
543-
if (!missing(verbose)) {
554+
if (!missing_verbose) {
544555
if (!is.integer(verbose) && !is.logical(verbose)) stopf("verbose must be logical or integer")
545556
if (length(verbose)!=1L || anyNA(verbose)) stopf("verbose must be length 1 non-NA")
546557
# set the global verbose option because that is fetched from C code without having to pass it through
547558
oldverbose = options(datatable.verbose=verbose)
548559
on.exit(options(oldverbose))
549560
}
550561
.global$print=""
551-
missingby = missing(by) && missing(keyby) # for tests 359 & 590 where passing by=NULL results in data.table not vector
552-
if (missingby || missing(j)) {
562+
missingby = missing_by && missing_keyby # for tests 359 & 590 where passing by=NULL results in data.table not vector
563+
if (missingby || missing_j) {
553564
if (!missingby) warningf("Ignoring by/keyby because 'j' is not supplied")
554565
by = bysub = NULL
555566
keyby = FALSE
556567
} else {
557-
if (missing(by)) {
568+
if (missing_by) {
558569
by = bysub = if (is.null(env)) substitute(keyby)
559570
else eval(substitute(substitute2(.keyby, env), list(.keyby = substitute(keyby))))
560571
keyby = TRUE
561572
} else {
562573
by = bysub = if (is.null(env)) substitute(by)
563574
else eval(substitute(substitute2(.by, env), list(.by = substitute(by))))
564-
if (missing(keyby))
575+
if (missing_keyby)
565576
keyby = FALSE
566577
else if (!isTRUEorFALSE(keyby))
567578
stopf("When by and keyby are both provided, keyby must be TRUE or FALSE")
568579
}
569-
if (missing(by)) { missingby=TRUE; by=bysub=NULL } # possible when env is used, PR#4304
580+
if (missing(bysub)) { missingby=TRUE; by=bysub=NULL } # possible when env is used, PR#4304
570581
else if (verbose && !is.null(env)) catf("Argument '%s' after substitute: %s\n", "by", deparse1(bysub, collapse="\n"))
571582
}
572583
bynull = !missingby && is.null(by) #3530
573584
byjoin = !is.null(by) && is.symbol(bysub) && bysub==".EACHI"
574585
naturaljoin = FALSE
575586
names_x = names(x)
576-
if (missing(i) && !missing(on)) {
587+
if (missing_i && !missing_on) {
577588
on_tmp = eval.parent(.massagei(substitute(on)))
578589
if (!is.list(on_tmp) || !length(names(on_tmp))) {
579590
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))
580591
on = NULL
581592
} else {
582593
i = on_tmp
594+
missing_i = FALSE
583595
naturaljoin = TRUE
584596
}
585597
}
586-
if (missing(i) && missing(j)) {
598+
if (missing_i && missing_j) {
587599
tt_isub = substitute(i)
588600
tt_jsub = substitute(j)
589601
if (!is.null(names(sys.call())) && # not relying on nargs() as it considers DT[,] to have 3 arguments, #3163
@@ -594,7 +606,6 @@ replace_dot_alias = function(e) {
594606
return(x)
595607
}
596608
if (!mult %chin% c("first", "last", "all", "error")) stopf("mult argument can only be 'first', 'last', 'all' or 'error'")
597-
missingroll = missing(roll)
598609
if (length(roll)!=1L || is.na(roll)) stopf("roll must be a single TRUE, FALSE, positive/negative integer/double including +Inf and -Inf or 'nearest'")
599610
if (is.character(roll)) {
600611
if (roll!="nearest") stopf("roll is '%s' (type character). Only valid character value is 'nearest'.", roll)
@@ -607,35 +618,34 @@ replace_dot_alias = function(e) {
607618
if (length(rollends)==1L) rollends=rep.int(rollends,2L)
608619
# TO DO (document/faq/example). Removed for now ... if ((roll || rolltolast) && missing(mult)) mult="last" # for when there is exact match to mult. This does not control cases where the roll is mult, that is always the last one.
609620
.unsafe.opt() #3585
610-
missingnomatch = missing(nomatch)
611621
nomatch0 = identical(nomatch,0.0) || identical(nomatch,0L) || identical(nomatch, FALSE) # for warning with row-numbers in i; #4353
612622
if (nomatch0) nomatch=NULL # retain nomatch=0|FALSE backwards compatibility, #857 #5214
613623
if (!is.null(nomatch)) {
614624
if (!(length(nomatch)==1L && is.na(nomatch))) stopf("nomatch= must be either NA or NULL (or 0 for backwards compatibility which is the same as NULL but please use NULL)")
615625
nomatch=NA # convert NA_character_ to NA-logical, PR#5216
616626
}
617627
if (!is.logical(which) || length(which)>1L) stopf("which= must be a logical vector length 1. Either FALSE, TRUE or NA.")
618-
if ((isTRUE(which)||is.na(which)) && !missing(j)) stopf("which==%s (meaning return row numbers) but j is also supplied. Either you need row numbers or the result of j, but only one type of result can be returned.", which)
628+
if ((isTRUE(which)||is.na(which)) && !missing_j) stopf("which==%s (meaning return row numbers) but j is also supplied. Either you need row numbers or the result of j, but only one type of result can be returned.", which)
619629
if (is.null(nomatch) && is.na(which)) stopf("which=NA with nomatch=0|NULL would always return an empty vector. Please change or remove either which or nomatch.")
620-
if (!with && missing(j)) stopf("j must be provided when with=FALSE")
621-
if (!missing(by) && !(isTRUEorFALSE(showProgress) || (is.numeric(showProgress) && length(showProgress)==1L && showProgress >= 0))) stopf("showProgress must be TRUE, FALSE, or a single non-negative number") # nocov
630+
if (!with && missing_j) stopf("j must be provided when with=FALSE")
631+
if (!(isTRUEorFALSE(showProgress) || (is.numeric(showProgress) && length(showProgress)==1L && showProgress >= 0))) stopf("showProgress must be TRUE, FALSE, or a single non-negative number") # nocov
622632
irows = NULL # Meaning all rows. We avoid creating 1:nrow(x) for efficiency.
623633
notjoin = FALSE
624634
rightcols = leftcols = integer()
625635
optimizedSubset = FALSE ## flag: tells whether a normal query was optimized into a join.
626636
..syms = NULL
627637
av = NULL
628638
jsub = NULL
629-
if (!missing(j)) {
639+
if (!missing_j) {
630640
if (is.null(env)) jsub = substitute(j) else {
631641
jsub = eval(substitute(
632642
substitute2(.j, env),
633643
list(.j = substitute(j))
634644
))
635-
if (missing(jsub)) {j = substitute(); jsub=NULL} else if (verbose && !is.null(env)) catf("Argument '%s' after substitute: %s\n", "j", deparse1(jsub, collapse="\n"))
645+
if (missing(jsub)) {j = substitute(); jsub=NULL; missing_j=TRUE} else if (verbose && !is.null(env)) catf("Argument '%s' after substitute: %s\n", "j", deparse1(jsub, collapse="\n"))
636646
}
637647
}
638-
if (!missing(j)) {
648+
if (!missing_j) {
639649
jsub = replace_dot_alias(jsub)
640650
root = root_name(jsub)
641651
# exclude '..1' etc. for #5460
@@ -704,9 +714,9 @@ replace_dot_alias = function(e) {
704714
if (root == "let")
705715
jsub[[1L]] = as.symbol(":=")
706716
allow.cartesian=TRUE # (see #800)
707-
if (!missing(i) && keyby)
717+
if (!missing_i && keyby)
708718
stopf(":= with keyby is only possible when i is not supplied since you can't setkey on a subset of rows. Either change keyby to by or remove i")
709-
if (!missingnomatch) {
719+
if (!missing_nomatch) {
710720
warningf("nomatch isn't relevant together with :=, ignoring nomatch")
711721
nomatch=NULL
712722
}
@@ -716,16 +726,16 @@ replace_dot_alias = function(e) {
716726
# setdiff removes duplicate entries, which'll create issues with duplicated names. Use %chin% instead.
717727
dupdiff = function(x, y) x[!x %chin% y]
718728
isub = NULL
719-
if (!missing(i)) {
729+
if (!missing_i) {
720730
if (is.null(env)) isub = substitute(i) else {
721731
isub = eval(substitute(
722732
substitute2(.i, env),
723733
list(.i = substitute(i))
724734
))
725-
if (missing(isub)) {i = substitute(); isub=NULL} else if (verbose && !is.null(env)) catf("Argument '%s' after substitute: %s\n", "i", deparse1(isub, collapse="\n"))
735+
if (missing(isub)) {i = substitute(); isub=NULL; missing_i=TRUE} else if (verbose && !is.null(env)) catf("Argument '%s' after substitute: %s\n", "i", deparse1(isub, collapse="\n"))
726736
}
727737
}
728-
if (!missing(i)) {
738+
if (!missing_i) {
729739
xo = NULL
730740
if (identical(isub, NA)) {
731741
# only possibility *isub* can be NA (logical) is the symbol NA itself; i.e. DT[NA]
@@ -763,7 +773,7 @@ replace_dot_alias = function(e) {
763773
}
764774
if (isub %iscall% "!") {
765775
notjoin = TRUE
766-
if (!missingnomatch) stopf("not-join '!' prefix is present on i but nomatch is provided. Please remove nomatch.");
776+
if (!missing_nomatch) stopf("not-join '!' prefix is present on i but nomatch is provided. Please remove nomatch.");
767777
nomatch = NULL
768778
isub = isub[[2L]]
769779
# #932 related so that !(v1 == 1) becomes v1 == 1 instead of (v1 == 1) after removing "!"
@@ -782,6 +792,7 @@ replace_dot_alias = function(e) {
782792
notjoin = o$notjoin
783793
i = o$i
784794
on = o$on
795+
missing_on = FALSE
785796
## the following two are ignored if i is not a data.table.
786797
## Since we are converting i to data.table, it is important to set them properly.
787798
nomatch = NULL
@@ -840,7 +851,7 @@ replace_dot_alias = function(e) {
840851
}
841852

842853
if (is.data.frame(i)) {
843-
if (missing(on)) {
854+
if (missing_on) {
844855
if (haskey(x)) check_duplicate_key(x)
845856
else stopf("When i is a data.table (or character vector), the columns to join by must be specified using the 'on=' argument (see ?data.table); by keying x (i.e., x is sorted and marked as such, see ?setkey); or by using 'on = .NATURAL' to indicate using the shared column names between x and i (i.e., a natural join). Keyed joins might have further speed benefits on very large data due to x being sorted in RAM.")
846857

@@ -860,8 +871,9 @@ replace_dot_alias = function(e) {
860871
}
861872
}
862873
on = common_names
874+
missing_on = FALSE
863875
}
864-
if (!missing(on)) {
876+
if (!missing_on) {
865877
# on = .() is now possible, #1257
866878
on_ops = .parse_on(substitute(on), isnull_inames)
867879
on = on_ops[[1L]]
@@ -942,7 +954,7 @@ replace_dot_alias = function(e) {
942954
# unnecessary construction of logical vectors
943955
if (is.null(nomatch) && allLen1) irows = irows[irows != 0L]
944956
} else {
945-
if (length(xo) && missing(on))
957+
if (length(xo) && missing_on)
946958
internal_error("Cannot by=.EACHI when joining to an index, yet") # nocov
947959
# since f__ refers to xo later in grouping, so xo needs to be passed through to dogroups too.
948960
if (length(irows))
@@ -1050,7 +1062,7 @@ replace_dot_alias = function(e) {
10501062
# column when irows contains negatives.
10511063
}
10521064
if (which) return(irows %||% seq_len(nrow(x)))
1053-
} else { # missing(i)
1065+
} else { # missing_i
10541066
i = NULL
10551067
}
10561068
names_i = names(i) # value is now stable
@@ -1063,7 +1075,7 @@ replace_dot_alias = function(e) {
10631075
# except in cases like DT[ , lapply(.SD, function(x) x/V1), .SDcols = !'V1'],
10641076
# see e.g. #484, #495, #1744, #1965.
10651077
non_sdvars = character(0L)
1066-
if (missing(j)) {
1078+
if (missing_j) {
10671079
# missingby was already checked above before dealing with i
10681080
if (!length(x)) return(null.data.table())
10691081
if (!length(leftcols)) {
@@ -1396,7 +1408,7 @@ replace_dot_alias = function(e) {
13961408
av = all.vars(jsub,TRUE) # TRUE fixes bug #1294 which didn't see b in j=fns[[b]](c)
13971409
use.I = ".I" %chin% av
13981410
if (any(c(".SD","eval","get","mget") %chin% av)) {
1399-
if (missing(.SDcols)) {
1411+
if (missing_SDcols) {
14001412
# here we need to use 'dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
14011413
# slight memory efficiency boost if we only store sdvars if it differs from ansvars, but a bit tricky to examine all the uses here
14021414
ansvars = sdvars = dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
@@ -1485,7 +1497,7 @@ replace_dot_alias = function(e) {
14851497
}
14861498
}
14871499

1488-
if (missing(.SDcols)) {
1500+
if (missing_SDcols) {
14891501
ansvars = setdiff(allcols, bynames) # fix for bug #34
14901502
} else {
14911503
# fixes #4089 - if .SDcols was already evaluated, we do not want the order of the columns to change.
@@ -1502,7 +1514,7 @@ replace_dot_alias = function(e) {
15021514
}
15031515
# .SDcols might include grouping columns if users wants that, but normally we expect user not to include them in .SDcols
15041516
} else {
1505-
if (!missing(.SDcols)) warningf("This j doesn't use .SD but .SDcols has been supplied. Ignoring .SDcols. See ?data.table.")
1517+
if (!missing_SDcols) warningf("This j doesn't use .SD but .SDcols has been supplied. Ignoring .SDcols. See ?data.table.")
15061518
allcols = c(names_x, xdotprefix, names_i, idotprefix)
15071519
ansvars = sdvars = setdiff(intersect(av, allcols), bynames)
15081520
if (verbose) catf("Detected that j uses these columns: %s\n",if (!length(ansvars)) "<none>" else brackify(ansvars))
@@ -1695,7 +1707,7 @@ replace_dot_alias = function(e) {
16951707
icolsAns = which(wna)
16961708
}
16971709
}
1698-
} # end of if !missing(j)
1710+
} # end of if !missing_j
16991711

17001712
SDenv = new.env(parent=parent.frame())
17011713

@@ -1751,12 +1763,12 @@ replace_dot_alias = function(e) {
17511763
ans[xcolsAns] = .Call(CsubsetDT, x, irows, xcols)
17521764
setattr(ans, "names", ansvars)
17531765
# NB: could be NULL
1754-
setattr(ans, "sorted", .join_result_key(x, i, ans, if (!missing(on)) names(on), ansvars, leftcols, rightcols, names_i, irows, roll))
1766+
setattr(ans, "sorted", .join_result_key(x, i, ans, if (!missing_on) names(on), ansvars, leftcols, rightcols, names_i, irows, roll))
17551767
setattr(ans, "class", class(x)) # retain class that inherits from data.table, #64
17561768
setattr(ans, "row.names", .set_row_names(length(ans[[1L]])))
17571769
setalloccol(ans)
17581770
}
1759-
if (!with || missing(j)) return(ans)
1771+
if (!with || missing_j) return(ans)
17601772
if (!is.data.table(ans)) setattr(ans, "class", c("data.table","data.frame")) # DF |> DT(,.SD[...]) .SD should be data.table, test 2212.013
17611773
SDenv$.SDall = ans
17621774
SDenv$.SD = if (length(non_sdvars)) shallow(SDenv$.SDall, sdvars) else SDenv$.SDall
@@ -1770,7 +1782,7 @@ replace_dot_alias = function(e) {
17701782
# Binary search can return all 0 irows when none of the input matches. Instead of doing all(irows==0L) (previous method), which has to allocate a logical vector the size of irows, we can make use of 'max'. If max is 0, we return 0. The condition where only some irows > 0 won't occur.
17711783
}
17721784
# Temp fix for #921. Allocate `.I` only if j-expression uses it.
1773-
SDenv$.I = if (!missing(j) && use.I) seq_len(SDenv$.N) else 0L
1785+
SDenv$.I = if (!missing_j && use.I) seq_len(SDenv$.N) else 0L
17741786
SDenv$.GRP = 1L
17751787
SDenv$.NGRP = 1L
17761788
.Call(C_lock, SDenv$.SD) # used to stop := modifying .SD via j=f(.SD), bug#1727. The more common case of j=.SD[,subcol:=1] was already caught when jsub is inspected for :=.
@@ -1930,7 +1942,7 @@ replace_dot_alias = function(e) {
19301942
# Much faster for a few known groups vs a 'by' for all followed by a subset
19311943
if (!is.data.table(i)) stopf("logical error. i is not a data.table, but mult='all' and 'by'=.EACHI")
19321944
byval = i
1933-
bynames = if (missing(on)) head(key(x),length(leftcols)) else names(on)
1945+
bynames = if (missing_on) head(key(x),length(leftcols)) else names(on)
19341946
allbyvars = NULL
19351947
bysameorder = (haskey(i) && identical(leftcols, chmatch(head(key(i),length(leftcols)), names(i)))) || # leftcols leading subset of key(i); see #4917
19361948
(roll==FALSE && is.sorted(f__)) # roll==FALSE is fix for #1010
@@ -2050,7 +2062,7 @@ replace_dot_alias = function(e) {
20502062
jiscols = chmatch(jisvars, names_i) # integer() if there are no jisvars (usually there aren't, advanced feature)
20512063
xjiscols = chmatch(xjisvars, names_x)
20522064
SDenv$.xSD = .Call(CcopyAsGrowable, x[min(nrow(i), 1L), xjisvars, with=FALSE])
2053-
if (!missing(on)) o__ = xo else o__ = integer(0L)
2065+
if (!missing_on) o__ = xo else o__ = integer(0L)
20542066
} else {
20552067
groups = byval
20562068
grpcols = seq_along(byval)
@@ -2124,7 +2136,7 @@ replace_dot_alias = function(e) {
21242136
}
21252137
ans = c(g, ans)
21262138
} else {
2127-
ans = .Call(Cdogroups, x, xcols, groups, grpcols, jiscols, xjiscols, grporder, o__, f__, len__, jsub, SDenv, cols, newnames, !missing(on), verbose, as.integer(showProgress))
2139+
ans = .Call(Cdogroups, x, xcols, groups, grpcols, jiscols, xjiscols, grporder, o__, f__, len__, jsub, SDenv, cols, newnames, !missing_on, verbose, as.integer(showProgress))
21282140
}
21292141
# unlock any locked data.table components of the answer, #4159
21302142
# MAX_DEPTH prevents possible infinite recursion from truly recursive object, #4173

‎inst/tests/bracket.Rraw‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test(1.51, DT[on="b"], DT, warning=c("When on= is provided but not i=", "i and j
9191
# forms of i
9292
test(1.61, DT[matrix(1:2, ncol=1L)], DT[1:2,])
9393
test(1.62, DT[list("b"), on="b"], DT[3:4,])
94-
test(1.63, DT[list(b="c"), on="b"], DT[5:6,])
94+
test(1.63, DT[on=list(b="c")], DT[5:6,])
9595
K = copy(DT); setkey(K, b)
9696
test(1.64, K["b"], K[3:4,])
9797
test(1.65, K[factor("c")], K[5:6,])

0 commit comments

Comments
 (0)