You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 391abec
Browse filesBrowse the repository at this point in the historyBrowse files
[.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.
# ..selfcount <<- ..selfcount+1 # in dev, we check no self calls, each of which doubles overhead, or could
527
527
# test explicitly if the caller is [.data.table (even stronger test. TO DO.)
528
528
# 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)
529
540
if (!cedta()) {
530
541
# Fix for #500 (to do)
531
542
if (substitute(j) %iscall% c(":=", "let")) {
532
543
# Throw a specific error message
533
544
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]]))
534
545
}
535
-
Nargs= nargs() - (!missing(drop))
546
+
Nargs= nargs() - (!missing_drop)
536
547
ans=if (Nargs<3L) { `[.data.frame`(x,i) } # drop ignored anyway by DF[i]
537
-
elseif (missing(drop)) `[.data.frame`(x,i,j)
548
+
elseif (missing_drop) `[.data.frame`(x,i,j)
538
549
else `[.data.frame`(x,i,j,drop)
539
550
# 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
541
552
return(ans)
542
553
}
543
-
if (!missing(verbose)) {
554
+
if (!missing_verbose) {
544
555
if (!is.integer(verbose) &&!is.logical(verbose)) stopf("verbose must be logical or integer")
545
556
if (length(verbose)!=1L|| anyNA(verbose)) stopf("verbose must be length 1 non-NA")
546
557
# set the global verbose option because that is fetched from C code without having to pass it through
547
558
oldverbose= options(datatable.verbose=verbose)
548
559
on.exit(options(oldverbose))
549
560
}
550
561
.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) {
553
564
if (!missingby) warningf("Ignoring by/keyby because 'j' is not supplied")
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))
580
591
on=NULL
581
592
} else {
582
593
i=on_tmp
594
+
missing_i=FALSE
583
595
naturaljoin=TRUE
584
596
}
585
597
}
586
-
if (missing(i)&&missing(j)) {
598
+
if (missing_i&&missing_j) {
587
599
tt_isub= substitute(i)
588
600
tt_jsub= substitute(j)
589
601
if (!is.null(names(sys.call())) &&# not relying on nargs() as it considers DT[,] to have 3 arguments, #3163
if (length(rollends)==1L) rollends=rep.int(rollends,2L)
608
619
# 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.
609
620
.unsafe.opt() #3585
610
-
missingnomatch= missing(nomatch)
611
621
nomatch0= identical(nomatch,0.0) || identical(nomatch,0L) || identical(nomatch, FALSE) # for warning with row-numbers in i; #4353
612
622
if (nomatch0) nomatch=NULL# retain nomatch=0|FALSE backwards compatibility, #857 #5214
613
623
if (!is.null(nomatch)) {
614
624
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)")
615
625
nomatch=NA# convert NA_character_ to NA-logical, PR#5216
616
626
}
617
627
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)
619
629
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
622
632
irows=NULL# Meaning all rows. We avoid creating 1:nrow(x) for efficiency.
623
633
notjoin=FALSE
624
634
rightcols=leftcols= integer()
625
635
optimizedSubset=FALSE## flag: tells whether a normal query was optimized into a join.
626
636
..syms=NULL
627
637
av=NULL
628
638
jsub=NULL
629
-
if (!missing(j)) {
639
+
if (!missing_j) {
630
640
if (is.null(env)) jsub= substitute(j) else {
631
641
jsub= eval(substitute(
632
642
substitute2(.j, env),
633
643
list(.j= substitute(j))
634
644
))
635
-
if (missing(jsub)) {j= substitute(); jsub=NULL} elseif (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} elseif (verbose&&!is.null(env)) catf("Argument '%s' after substitute: %s\n", "j", deparse1(jsub, collapse="\n"))
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.")
# 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.
1771
1783
}
1772
1784
# Temp fix for #921. Allocate `.I` only if j-expression uses it.
.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 :=.
0 commit comments