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 ca6af09
Browse filesBrowse the repository at this point in the historyBrowse files
Functions defined inside [ capture its frame, so they block moving the code that uses them.
Move them to internal top-level functions with class "dt_query_helper", so that conditions they raise still report the [.data.table call.
No behaviour change.
if (length(x)!=1L) internal_error("drop_dot passed %d items", length(x)) # nocov
1360
-
if (startsWith(x<-as.character(x), ".") &&x %chin% c(".N", ".I", ".GRP", ".NGRP", ".BY"))
1361
-
substr(x, 2L, nchar(x))
1362
-
else
1363
-
x
1364
-
}
1365
-
# handle auto-naming of last item of j (e.g. within {} or if/else, #2478)
1366
-
# e.g. DT[, .(a=sum(v), v, .N), by=] should create columns named a, v, N
1367
-
do_j_names=function(q) {
1368
-
if (!is.call(q) ||!is.name(q[[1L]])) return(q)
1369
-
if (q[[1L]] %chin% c('list', '.')) {
1370
-
q[[1L]] = quote(list)
1371
-
qlen= length(q)
1372
-
if (qlen>1L) {
1373
-
nm= names(q[-1L]) # check list(a=sum(v),v)
1374
-
if (is.null(nm)) nm= rep.int("", qlen-1L)
1375
-
# attempt to auto-name unnamed columns
1376
-
for (jjin which(!nzchar(nm))) {
1377
-
thisq=q[[jj+1L]]
1378
-
if (missing(thisq)) stopf("Item %d of the .() or list() passed to j is missing", jj) #3507
1379
-
if (is.name(thisq)) nm[jj] = drop_dot(thisq)
1380
-
# TO DO: if call to a[1] for example, then call it 'a' too
1381
-
}
1382
-
if (!is.null(jvnames)) {
1383
-
if (length(nm) != length(jvnames))
1384
-
warningf("j may not evaluate to the same number of columns for each group; if you're sure this warning is in error, please put the branching logic outside of [ for efficiency")
1385
-
elseif (any(idx<-nm!=jvnames))
1386
-
warningf('Different branches of j expression produced different auto-named columns: %s; using the most "last" names. If this was intentional (e.g., you know only one branch will ever be used in a given query because the branch is controlled by a function argument), please (1) pull this branch out of the call; (2) explicitly provide missing defaults for each branch in all cases; or (3) use the same name for each branch and re-name it in a follow-up call.', brackify(sprintf('%s!=%s', nm[idx], jvnames[idx])))
1387
-
}
1388
-
# nolint next: undesirable_operator_linter. Workaround is clunkier, though a bigger refactor could be considered.
1389
-
jvnames<<-nm# TODO: handle if() list(a, b) else list(b, a) better
1390
-
setattr(q, "names", NULL) # drops the names from the list so it's faster to eval the j for each group; reinstated at the end on the result.
1391
-
}
1392
-
return(q) # else empty list is needed for test 468: adding an empty list column
1393
-
}
1394
-
if (q[[1L]] =='{') {
1395
-
if (!is.null(q[[qlen<-length(q)]])) q[[qlen]] = do_j_names(q[[qlen]])
1396
-
return(q)
1397
-
}
1398
-
if (q[[1L]] =='if') {
1399
-
#explicit NULL would return NULL, assigning NULL would delete that from the expression
1400
-
if (!is.null(q[[3L]])) q[[3L]] = do_j_names(q[[3L]])
1401
-
if (length(q) ==4L&&!is.null(q[[4L]])) q[[4L]] = do_j_names(q[[4L]])
1402
-
return(q)
1403
-
}
1404
-
q
1405
-
}
1406
1356
if (is.name(jsub)) {
1407
1357
# j is a single unquoted column name
1408
-
if (jsub!=".SD") jvnames= drop_dot(jsub)
1358
+
if (jsub!=".SD") jvnames=.drop_dot(jsub)
1409
1359
# jsub is list()ed after it's eval'd inside dogroups.
1410
-
} elsejsub= do_j_names(jsub) # else maybe a call to transform or something which returns a list.
1360
+
} else { # else maybe a call to transform or something which returns a list.
1361
+
jsub_names= .do_j_names(jsub, jvnames)
1362
+
jsub=jsub_names$jsub
1363
+
jvnames=jsub_names$jvnames
1364
+
}
1411
1365
av= all.vars(jsub,TRUE) # TRUE fixes bug #1294 which didn't see b in j=fns[[b]](c)
1412
1366
use.I=".I" %chin% av
1413
1367
if (any(c(".SD","eval","get","mget") %chin% av)) {
1414
1368
if (missing_SDcols) {
1415
-
# here we need to use 'dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
1369
+
# here we need to use '.dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
1416
1370
# slight memory efficiency boost if we only store sdvars if it differs from ansvars, but a bit tricky to examine all the uses here
1417
-
ansvars=sdvars= dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
1371
+
ansvars=sdvars=.dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
1418
1372
# just using .SD in j triggers all non-by columns in the subset even if some of
1419
1373
# those columns are not used. It would be tricky to detect whether the j expression
1420
1374
# really does use all of the .SD columns or not, hence .SDcols for grouping
if (.Call(C_islocked, x)) stopf(".SD is locked. Using := in .SD's j is reserved for possible future use; a tortuously flexible way to modify by group. Use := in j directly to modify by group by reference.")
1537
-
suppPrint=function(x) { .global$print=address(x); x }
1491
+
suppPrint=.suppPrint
1538
1492
# Suppress print when returns ok not on error, bug #2376. Thanks to: http://stackoverflow.com/a/13606880/403310
1539
1493
# All appropriate returns following this point are wrapped; i.e. return(suppPrint(x)).
setalloccol(ans) # TODO: overallocate in dogroups in the first place and remove this line
2228
2141
}
2229
2142
2143
+
# setdiff removes duplicate entries, which'll create issues with duplicated names. Use %chin% instead.
2144
+
.dupdiff=function(x, y) x[!x %chin% y]
2145
+
class(.dupdiff) ="dt_query_helper"
2146
+
2147
+
# auto-name a symbol in j: special symbols .N, .I, .GRP, .NGRP and .BY lose their leading dot
2148
+
.drop_dot=function(x) {
2149
+
if (length(x)!=1L) internal_error("drop_dot passed %d items", length(x)) # nocov
2150
+
if (startsWith(x<-as.character(x), ".") &&x %chin% c(".N", ".I", ".GRP", ".NGRP", ".BY"))
2151
+
substr(x, 2L, nchar(x))
2152
+
else
2153
+
x
2154
+
}
2155
+
class(.drop_dot) ="dt_query_helper"
2156
+
2157
+
# handle auto-naming of last item of j (e.g. within {} or if/else, #2478)
2158
+
# e.g. DT[, .(a=sum(v), v, .N), by=] should create columns named a, v, N
2159
+
# returns list(jsub=, jvnames=); jvnames is threaded through the recursion so that the names of different branches can be compared
2160
+
.do_j_names=function(q, jvnames) {
2161
+
if (!is.call(q) ||!is.name(q[[1L]])) return(list(jsub=q, jvnames=jvnames))
2162
+
if (q[[1L]] %chin% c('list', '.')) {
2163
+
q[[1L]] = quote(list)
2164
+
qlen= length(q)
2165
+
if (qlen>1L) {
2166
+
nm= names(q[-1L]) # check list(a=sum(v),v)
2167
+
if (is.null(nm)) nm= rep.int("", qlen-1L)
2168
+
# attempt to auto-name unnamed columns
2169
+
for (jjin which(!nzchar(nm))) {
2170
+
thisq=q[[jj+1L]]
2171
+
if (missing(thisq)) stopf("Item %d of the .() or list() passed to j is missing", jj) #3507
2172
+
if (is.name(thisq)) nm[jj] = .drop_dot(thisq)
2173
+
# TO DO: if call to a[1] for example, then call it 'a' too
2174
+
}
2175
+
if (!is.null(jvnames)) {
2176
+
if (length(nm) != length(jvnames))
2177
+
warningf("j may not evaluate to the same number of columns for each group; if you're sure this warning is in error, please put the branching logic outside of [ for efficiency")
2178
+
elseif (any(idx<-nm!=jvnames))
2179
+
warningf('Different branches of j expression produced different auto-named columns: %s; using the most "last" names. If this was intentional (e.g., you know only one branch will ever be used in a given query because the branch is controlled by a function argument), please (1) pull this branch out of the call; (2) explicitly provide missing defaults for each branch in all cases; or (3) use the same name for each branch and re-name it in a follow-up call.', brackify(sprintf('%s!=%s', nm[idx], jvnames[idx])))
2180
+
}
2181
+
jvnames=nm# TODO: handle if() list(a, b) else list(b, a) better
2182
+
setattr(q, "names", NULL) # drops the names from the list so it's faster to eval the j for each group; reinstated at the end on the result.
2183
+
}
2184
+
return(list(jsub=q, jvnames=jvnames)) # else empty list is needed for test 468: adding an empty list column
2185
+
}
2186
+
if (q[[1L]] =='{') {
2187
+
if (!is.null(q[[qlen<-length(q)]])) {
2188
+
ans= .do_j_names(q[[qlen]], jvnames)
2189
+
q[[qlen]] =ans$jsub
2190
+
jvnames=ans$jvnames
2191
+
}
2192
+
return(list(jsub=q, jvnames=jvnames))
2193
+
}
2194
+
if (q[[1L]] =='if') {
2195
+
#explicit NULL would return NULL, assigning NULL would delete that from the expression
2196
+
if (!is.null(q[[3L]])) {
2197
+
ans= .do_j_names(q[[3L]], jvnames)
2198
+
q[[3L]] =ans$jsub
2199
+
jvnames=ans$jvnames
2200
+
}
2201
+
if (length(q) ==4L&&!is.null(q[[4L]])) {
2202
+
ans= .do_j_names(q[[4L]], jvnames)
2203
+
q[[4L]] =ans$jsub
2204
+
jvnames=ans$jvnames
2205
+
}
2206
+
return(list(jsub=q, jvnames=jvnames))
2207
+
}
2208
+
list(jsub=q, jvnames=jvnames)
2209
+
}
2210
+
class(.do_j_names) ="dt_query_helper"
2211
+
2212
+
.suppPrint=function(x) { .global$print=address(x); x }
2213
+
class(.suppPrint) ="dt_query_helper"
2214
+
2215
+
# can jval be sorted by the same key as x? improved for #4498
0 commit comments