@@ -18819,110 +18819,99 @@ LNNA = function(x) { # Last Not NA
1881918819 if (length(nna)) x[nna[length(nna)]] else x[0L]
1882018820}
1882118821test_no = 2239.0
18822- for (col in setdiff(names(DT), "grp")) {
18823- # test vector input to first/last outside of DT[] query
18824- test(test_no+.01, first(DT[[col]], na.rm=TRUE), ans<-FNNA(DT[[col]]))
18825- test(test_no+.02, first(DT[[col]], na.rm="row"), ans) # "row" same as TRUE for vectors
18826- test(test_no+.03, last(DT[[col]], na.rm=TRUE), ans<-LNNA(DT[[col]]))
18827- test(test_no+.04, last(DT[[col]], na.rm="row"), ans)
18828-
18829- # one function by group with and without .() wrapper
18830- test(test_no+.05, EVAL("DT[, .(first(",col,",na.rm=TRUE)), by=grp, verbose=TRUE]"),
18831- ans<-EVAL("DT[, .(FNNA(",col,")), by=grp]"), output="GForce optimized.*gfirst")
18832- if (col!="E") # subsets of list columns need to be done within .() otherwise j's result looks like multiple columns
18833- test(test_no+.06, EVAL("DT[, first(",col,",na.rm=TRUE), by=grp, verbose=TRUE]"),
18834- ans, output="GForce optimized.*gfirst")
18835- test(test_no+.07, EVAL("DT[, .(last(",col,",na.rm=TRUE)), by=grp, verbose=TRUE]"),
18836- ans<-EVAL("DT[, .(LNNA(",col,")), by=grp]"), output="GForce optimized.*glast")
18837- if (col!="E") # see comment above why !="E"
18838- test(test_no+.08, EVAL("DT[, last(",col,",na.rm=TRUE), by=grp, verbose=TRUE]"),
18839- ans, output="GForce optimized.*glast")
18840- test_no = test_no+.1
18822+ for (opt in c(0, Inf)) {
18823+ options(datatable.optimize=opt)
18824+ out = if (opt) "GForce optimized" else "GForce FALSE"
18825+ for (col in setdiff(names(DT), "grp")) {
18826+ # test vector input to first/last outside of DT[] query
18827+ test(test_no+.01, first(DT[[col]], na.rm=TRUE), ans<-FNNA(DT[[col]]))
18828+ test(test_no+.02, first(DT[[col]], na.rm="row"), ans) # "row" same as TRUE for vectors
18829+ test(test_no+.03, last(DT[[col]], na.rm=TRUE), ans<-LNNA(DT[[col]]))
18830+ test(test_no+.04, last(DT[[col]], na.rm="row"), ans)
18831+
18832+ # one function by group with and without .() wrapper
18833+ test(test_no+.05, EVAL("DT[, .(first(",col,",na.rm=TRUE)), by=grp, verbose=TRUE]"),
18834+ ans<-EVAL("DT[, .(FNNA(",col,")), by=grp]"), output=out)
18835+ if (col!="E") # subsets of list columns need to be done within .() otherwise j's result looks like multiple columns
18836+ test(test_no+.06, EVAL("DT[, first(",col,",na.rm=TRUE), by=grp, verbose=TRUE]"),
18837+ ans, output=out)
18838+ test(test_no+.07, EVAL("DT[, .(last(",col,",na.rm=TRUE)), by=grp, verbose=TRUE]"),
18839+ ans<-EVAL("DT[, .(LNNA(",col,")), by=grp]"), output=out)
18840+ if (col!="E") # see comment above why !="E"
18841+ test(test_no+.08, EVAL("DT[, last(",col,",na.rm=TRUE), by=grp, verbose=TRUE]"),
18842+ ans, output=out)
18843+ test_no = test_no+.1
18844+ }
18845+ test_no = trunc(test_no)+1
18846+ test(test_no+.01, first(DT), DT[1,])
18847+ test(test_no+.02, first(DT, na.rm=TRUE), data.table(grp=1L, A=1L, B=1, C="a", D=1i, E=list(c("a","b")))) # first non-NA in each column
18848+ test(test_no+.03, first(DT, na.rm="row"), DT[4]) # first row with no NA
18849+ test(test_no+.04, last(DT), DT[.N,])
18850+ test(test_no+.05, last(DT, na.rm=TRUE), data.table(grp=4L, A=3L, B=pi, C="d", D=4i, E=list(3:4)))
18851+ test(test_no+.06, last(DT, na.rm="row"), DT[7])
18852+ test(test_no+.07, first(DT[0]), DT[0])
18853+ test(test_no+.08, last(DT[0]), DT[0])
18854+ DF = as.data.frame(DT)
18855+ test(test_no+.11, first(DF), DF[1,])
18856+ test(test_no+.12, first(DF, na.rm=TRUE), {x=data.frame(grp=1L, A=1L, B=1, C="a", D=1i); x$E=list(c("a","b")); x}) # with E inside data.frame() it recycles to 2 rows
18857+ test(test_no+.13, first(DF, na.rm="row"), DF[4,])
18858+ test(test_no+.14, last(DF), DF[8,])
18859+ test(test_no+.15, last(DF, na.rm=TRUE), {x=data.frame(grp=4L, A=3L, B=pi, C="d", D=4i); x$E=list(3:4); x})
18860+ test(test_no+.16, last(DF, na.rm="row"), DF[7,])
18861+ test(test_no+.17, first(list(), na.rm=TRUE), list())
18862+ test(test_no+.18, last(list(), na.rm=TRUE), list())
18863+ test(test_no+.21, DT[, first(.SD, na.rm=TRUE), by=grp, verbose=TRUE],
18864+ ans<-data.table(grp=1:4, A=c(1L,2L,NA,3L), B=c(1,pi,3,pi), C=c("a","b","c","d"), D=c(1i,2i,NA,4i), E=list(NA,c("a","b"),list(1:2),3:4)),
18865+ output=out)
18866+ test(test_no+.22, DT[, lapply(.SD, first, na.rm=TRUE), by=grp, verbose=TRUE], ans, output=out)
18867+ test(test_no+.23, DT[, first(.SD, na.rm='row'), by=grp, verbose=TRUE],
18868+ data.table(grp=1:4, A=c(NA,2L,NA,3L), B=c(NA,3,NA,pi), C=c(NA,"b",NA,"d"), D=c(NA,3i,NA,4i), E=list(NA,c("a","b"),NA,3:4)),
18869+ notOutput="GForce optimized") # TODO: could try to implement gforce optimized na.rm='row' in future
18870+ test(test_no+.24, DT[, last(.SD, na.rm=TRUE), by=grp, verbose=TRUE],
18871+ ans<-data.table(grp=1:4, A=c(1L,2L,NA,3L), B=c(1,3,3,pi), C=c("a","b","c","d"), D=c(1i,3i,NA,4i), E=list(NA,c("a","b"),list(1:2),3:4)),
18872+ output=out)
18873+ test(test_no+.25, DT[, lapply(.SD, last, na.rm=TRUE), by=grp, verbose=TRUE], ans, output=out)
18874+ test(test_no+.26, DT[, last(.SD, na.rm='row'), by=grp, verbose=TRUE],
18875+ data.table(grp=1:4, A=c(NA,2L,NA,3L), B=c(NA,3,NA,pi), C=c(NA,"b",NA,"d"), D=c(NA,3i,NA,4i), E=list(NA,c("a","b"),NA,3:4)),
18876+ output="GForce FALSE")
18877+ test(test_no+.27, DT[, .(last(A,na.rm=TRUE), first(B, na.rm=TRUE), last(C)), by=grp, verbose=TRUE],
18878+ data.table(grp=1:4, V1=c(1L, 2L, NA, 3L), V2=c(1,pi,3,pi), V3=c(NA,NA,"c",NA)),
18879+ output=out)
18880+ test(test_no+.31, DT[, last(D,na.rm=TRUE,n=2)], c(3i,4i))
18881+ test(test_no+.32, DT[, last(B,na.rm=TRUE,n=2), by=grp, verbose=TRUE], data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi)), output=out)
18882+ test(test_no+.33, DT[, first(D,na.rm=TRUE,n=2)], c(1i,2i))
18883+ test(test_no+.34, DT[, first(B,na.rm=TRUE,n=2), by=grp, verbose=TRUE], data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi)), output=out)
18884+ test(test_no+.35, DT[, last(D,na.rm=TRUE,n=1), by=grp, verbose=TRUE], data.table(grp=INT(1,2,4), V1=c(1i,3i,4i)), output=out)
18885+ test(test_no+.41, DT[, last(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE],
18886+ data.table(grp=INT(1,2,2,3,4,4), A=INT(1,NA,2,NA,3,3), B=c(1,pi,3,3,NA,pi), C=c("a",NA,"b","c",NA,"d"), D=c(1i,2i,3i,NA,NA,4i), E=list(NA,NA,c("a","b"),list(1:2),NA,3:4)),
18887+ output=out)
18888+ test(test_no+.42, DT[, last(.SD, na.rm="row", n=2), by=grp, verbose=TRUE],
18889+ data.table(grp=INT(1,1,2,2,3,4,4), A=c(NA,NA,NA,2L,NA,NA,3L), B=c(NA,NA,NA,3,NA,NA,pi), C=c(NA,NA,NA,"b",NA,NA,"d"), D=c(NA,NA,NA,3i,NA,NA,4i), E=list(NA,NA,NA,c("a","b"),NA,NA,3:4)),
18890+ output="GForce FALSE")
18891+ test(test_no+.43, DT[, first(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE],
18892+ data.table(grp=INT(1,2,2,3,4,4), A=INT(1,2,NA,NA,3,3), B=c(1,pi,3,3,pi,NA), C=c("a","b",NA,"c","d",NA), D=c(1i,2i,3i,NA,4i,NA), E=list(NA,c("a","b"),NA,list(1:2),3:4,NA)),
18893+ output=out)
18894+ test(test_no+.44, DT[, first(.SD, na.rm="row", n=2), by=grp, verbose=TRUE],
18895+ data.table(grp=INT(1,1,2,2,3,4,4), A=c(NA,NA,2L,NA,NA,3L,NA), B=c(NA,NA,3,NA,NA,pi,NA), C=c(NA,NA,"b",NA,NA,"d",NA), D=c(NA,NA,3i,NA,NA,4i,NA), E=list(NA,NA,c("a","b"),NA,NA,3:4,NA)),
18896+ output="GForce FALSE")
18897+ test(test_no+.51, DT[, first(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18898+ test(test_no+.52, DT[, last(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18899+ test(test_no+.53, DT[, head(.SD, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18900+ test(test_no+.54, DT[, tail(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18901+ test(test_no+.61, first(DT$A, n=-1), error="n.*is not TRUE")
18902+ test(test_no+.62, DT[,first(A, n=-1)], error="n.*is not TRUE")
18903+ test(test_no+.63, DT[,first(A, n=-1),by=grp,verbose=TRUE], error="n.*is not TRUE", output="GForce FALSE")
18904+ test(test_no+.64, last(DT$A, n=-1), error="n.*is not TRUE")
18905+ test(test_no+.65, DT[,last(A, n=-1)], error="n.*is not TRUE")
18906+ test(test_no+.66, DT[,last(A, n=-1),by=grp,verbose=TRUE], error="n.*is not TRUE", output="GForce FALSE")
18907+ test(test_no+.67, first(matrix(1:12,nrow=3), na.rm=TRUE), error="na.rm.*not currently supported for.*matrix")
18908+ test(test_no+.68, first(matrix(1:12,nrow=3), na.rm='row'), error="na.rm.*not currently supported for.*matrix")
18909+ test(test_no+.69, first(matrix(1:12,nrow=3)), matrix(INT(1,4,7,10),nrow=1))
18910+ test(test_no+.71, first(DT$A, na.rm=NA), error="na.rm")
18911+ test(test_no+.72, DT[,first(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output=out)
18912+ test(test_no+.73, last(DT$A, na.rm=NA), error="na.rm")
18913+ test(test_no+.74, DT[,last(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output=out)
18914+ test_no = trunc(test_no)+1
1884118915}
18842- test(2240.01, first(DT), DT[1,])
18843- test(2240.02, first(DT, na.rm=TRUE), data.table(grp=1L, A=1L, B=1, C="a", D=1i, E=list(c("a","b")))) # first non-NA in each column
18844- test(2240.03, first(DT, na.rm="row"), DT[4]) # first row with no NA
18845- test(2240.04, last(DT), DT[.N,])
18846- test(2240.05, last(DT, na.rm=TRUE), data.table(grp=4L, A=3L, B=pi, C="d", D=4i, E=list(3:4)))
18847- test(2240.06, last(DT, na.rm="row"), DT[7])
18848- test(2240.07, first(DT[0]), DT[0])
18849- test(2240.08, last(DT[0]), DT[0])
18850- DF = as.data.frame(DT)
18851- test(2240.11, first(DF), DF[1,])
18852- test(2240.12, first(DF, na.rm=TRUE), {x=data.frame(grp=1L, A=1L, B=1, C="a", D=1i); x$E=list(c("a","b")); x}) # with E inside data.frame() it recycles to 2 rows
18853- test(2240.13, first(DF, na.rm="row"), DF[4,])
18854- test(2240.14, last(DF), DF[8,])
18855- test(2240.15, last(DF, na.rm=TRUE), {x=data.frame(grp=4L, A=3L, B=pi, C="d", D=4i); x$E=list(3:4); x})
18856- test(2240.16, last(DF, na.rm="row"), DF[7,])
18857- test(2240.21, first(list(), na.rm=TRUE), list())
18858- test(2240.22, last(list(), na.rm=TRUE), list())
18859- test(2240.31, DT[, first(.SD, na.rm=TRUE), by=grp, verbose=TRUE],
18860- ans<-data.table(grp=1:4, A=c(1L,2L,NA,3L), B=c(1,pi,3,pi), C=c("a","b","c","d"), D=c(1i,2i,NA,4i), E=list(NA,c("a","b"),list(1:2),3:4)),
18861- output="GForce optimized.*gfirst")
18862- test(2240.32, DT[, lapply(.SD, first, na.rm=TRUE), by=grp, verbose=TRUE], ans, output="GForce optimized.*gfirst")
18863- old = options(datatable.optimize=0)
18864- test(2240.33, DT[, first(.SD, na.rm=TRUE), by=grp, verbose=TRUE], ans, notOutput="gfirst")
18865- test(2240.34, DT[, lapply(.SD, first, na.rm=TRUE), by=grp, verbose=TRUE], ans, notOutput="gfirst")
18866- options(old)
18867- test(2240.35, DT[, first(.SD, na.rm='row'), by=grp, verbose=TRUE],
18868- ans<-data.table(grp=1:4, A=c(NA,2L,NA,3L), B=c(NA,3,NA,pi), C=c(NA,"b",NA,"d"), D=c(NA,3i,NA,4i), E=list(NA,c("a","b"),NA,3:4)),
18869- notOutput="GForce optimized") # TODO: could try and gforce optimize in future
18870- test(2240.41, DT[, last(.SD, na.rm=TRUE), by=grp, verbose=TRUE],
18871- ans<-data.table(grp=1:4, A=c(1L,2L,NA,3L), B=c(1,3,3,pi), C=c("a","b","c","d"), D=c(1i,3i,NA,4i), E=list(NA,c("a","b"),list(1:2),3:4)),
18872- output="GForce optimized.*glast")
18873- test(2240.42, DT[, lapply(.SD, last, na.rm=TRUE), by=grp, verbose=TRUE], ans, output="GForce optimized.*glast")
18874- old = options(datatable.optimize=0)
18875- test(2240.43, DT[, last(.SD, na.rm=TRUE), by=grp, verbose=TRUE], ans, notOutput="glast")
18876- test(2240.44, DT[, lapply(.SD, last, na.rm=TRUE), by=grp, verbose=TRUE], ans, notOutput="glast")
18877- options(old)
18878- test(2240.45, DT[, last(.SD, na.rm='row'), by=grp, verbose=TRUE],
18879- ans<-data.table(grp=1:4, A=c(NA,2L,NA,3L), B=c(NA,3,NA,pi), C=c(NA,"b",NA,"d"), D=c(NA,3i,NA,4i), E=list(NA,c("a","b"),NA,3:4)),
18880- notOutput="GForce optimized") # TODO: could try and gforce optimize in future
18881- test(2240.51, DT[, .(last(A,na.rm=TRUE), first(B, na.rm=TRUE), last(C)), by=grp, verbose=TRUE],
18882- data.table(grp=1:4, V1=c(1L, 2L, NA, 3L), V2=c(1,pi,3,pi), V3=c(NA,NA,"c",NA)),
18883- output="GForce optimized.*glast.*gfirst.*glast")
18884- test(2240.61, DT[, last(D,na.rm=TRUE,n=2)], c(3i,4i))
18885- test(2240.62, DT[, last(B,na.rm=TRUE,n=2), by=grp, verbose=TRUE], data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi)), output="GForce.*glast")
18886- test(2240.63, DT[, first(D,na.rm=TRUE,n=2)], c(1i,2i))
18887- test(2240.64, DT[, first(B,na.rm=TRUE,n=2), by=grp, verbose=TRUE], data.table(grp=INT(1,2,2,3,4), V1=c(1,pi,3,3,pi)), output="GForce.*gfirst")
18888- test(2240.65, DT[, last(D,na.rm=TRUE,n=1), by=grp, verbose=TRUE], data.table(grp=INT(1,2,4), V1=c(1i,3i,4i)), output="GForce.*glast")
18889- test(2240.71, DT[, last(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE],
18890- ans<-data.table(grp=INT(1,2,2,3,4,4), A=INT(1,NA,2,NA,3,3), B=c(1,pi,3,3,NA,pi), C=c("a",NA,"b","c",NA,"d"), D=c(1i,2i,3i,NA,NA,4i), E=list(NA,NA,c("a","b"),list(1:2),NA,3:4)),
18891- output="GForce.*glast")
18892- old = options(datatable.optimize=0)
18893- test(2240.72, DT[, last(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE], ans, output="GForce FALSE")
18894- options(old)
18895- test(2240.73, DT[, last(.SD, na.rm="row", n=2), by=grp, verbose=TRUE],
18896- data.table(grp=INT(1,1,2,2,3,4,4), A=c(NA,NA,NA,2L,NA,NA,3L), B=c(NA,NA,NA,3,NA,NA,pi), C=c(NA,NA,NA,"b",NA,NA,"d"), D=c(NA,NA,NA,3i,NA,NA,4i), E=list(NA,NA,NA,c("a","b"),NA,NA,3:4)),
18897- output="GForce FALSE")
18898- test(2240.74, DT[, first(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE],
18899- ans<-data.table(grp=INT(1,2,2,3,4,4), A=INT(1,2,NA,NA,3,3), B=c(1,pi,3,3,pi,NA), C=c("a","b",NA,"c","d",NA), D=c(1i,2i,3i,NA,4i,NA), E=list(NA,c("a","b"),NA,list(1:2),3:4,NA)),
18900- output="GForce.*gfirst")
18901- old = options(datatable.optimize=0)
18902- test(2240.75, DT[, first(.SD, na.rm=TRUE, n=2), by=grp, verbose=TRUE], ans, output="GForce FALSE")
18903- options(old)
18904- test(2240.76, DT[, first(.SD, na.rm="row", n=2), by=grp, verbose=TRUE],
18905- data.table(grp=INT(1,1,2,2,3,4,4), A=c(NA,NA,2L,NA,NA,3L,NA), B=c(NA,NA,3,NA,NA,pi,NA), C=c(NA,NA,"b",NA,NA,"d",NA), D=c(NA,NA,3i,NA,NA,4i,NA), E=list(NA,NA,c("a","b"),NA,NA,3:4,NA)),
18906- output="GForce FALSE")
18907- test(2240.771, DT[, first(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18908- test(2240.772, DT[, last(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18909- test(2240.773, DT[, head(.SD, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18910- test(2240.774, DT[, tail(.SD, na.rm=TRUE, n=0), by=grp, verbose=TRUE], DT[0], output="GForce FALSE")
18911- test(2240.81, first(DT$A, n=-1), error="n.*is not TRUE")
18912- test(2240.82, DT[,first(A, n=-1)], error="n.*is not TRUE")
18913- test(2240.83, DT[,first(A, n=-1),by=grp,verbose=TRUE], error="n.*is not TRUE", output="GForce FALSE")
18914- test(2240.84, last(DT$A, n=-1), error="n.*is not TRUE")
18915- test(2240.85, DT[,last(A, n=-1)], error="n.*is not TRUE")
18916- test(2240.86, DT[,last(A, n=-1),by=grp,verbose=TRUE], error="n.*is not TRUE", output="GForce FALSE")
18917- test(2240.87, first(matrix(1:12,nrow=3), na.rm=TRUE), error="na.rm.*not currently supported for.*matrix")
18918- test(2240.88, first(matrix(1:12,nrow=3), na.rm='row'), error="na.rm.*not currently supported for.*matrix")
18919- test(2240.89, first(matrix(1:12,nrow=3)), matrix(INT(1,4,7,10),nrow=1))
18920- test(2240.91, first(DT$A, na.rm=NA), error="na.rm")
18921- test(2240.92, DT[,first(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output="GForce.*gfirst")
18922- test(2240.93, last(DT$A, na.rm=NA), error="na.rm")
18923- test(2240.94, DT[,last(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output="GForce.*glast")
18924- old=options(datatable.optimize=0)
18925- test(2240.95, DT[,first(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output="GForce FALSE")
18926- test(2240.96, DT[,last(A, na.rm=NA),by=grp,verbose=TRUE], error="na.rm", output="GForce FALSE")
18927- options(old)
1892818916
18917+ # next test 2243
0 commit comments