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 94d7529
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, named .dt_* 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
1359
-
if (startsWith(x<-as.character(x), ".") &&x %chin% c(".N", ".I", ".GRP", ".NGRP", ".BY"))
1360
-
substr(x, 2L, nchar(x))
1361
-
else
1362
-
x
1363
-
}
1364
-
# handle auto-naming of last item of j (e.g. within {} or if/else, #2478)
1365
-
# e.g. DT[, .(a=sum(v), v, .N), by=] should create columns named a, v, N
1366
-
do_j_names=function(q) {
1367
-
if (!is.call(q) ||!is.name(q[[1L]])) return(q)
1368
-
if (q[[1L]] %chin% c('list', '.')) {
1369
-
q[[1L]] = quote(list)
1370
-
qlen= length(q)
1371
-
if (qlen>1L) {
1372
-
nm= names(q[-1L]) # check list(a=sum(v),v)
1373
-
if (is.null(nm)) nm= rep.int("", qlen-1L)
1374
-
# attempt to auto-name unnamed columns
1375
-
for (jjin which(!nzchar(nm))) {
1376
-
thisq=q[[jj+1L]]
1377
-
if (missing(thisq)) stopf("Item %d of the .() or list() passed to j is missing", jj) #3507
1378
-
if (is.name(thisq)) nm[jj] = drop_dot(thisq)
1379
-
# TO DO: if call to a[1] for example, then call it 'a' too
1380
-
}
1381
-
if (!is.null(jvnames)) {
1382
-
if (length(nm) != length(jvnames))
1383
-
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")
1384
-
elseif (any(idx<-nm!=jvnames))
1385
-
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])))
1386
-
}
1387
-
# nolint next: undesirable_operator_linter. Workaround is clunkier, though a bigger refactor could be considered.
1388
-
jvnames<<-nm# TODO: handle if() list(a, b) else list(b, a) better
1389
-
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.
1390
-
}
1391
-
return(q) # else empty list is needed for test 468: adding an empty list column
1392
-
}
1393
-
if (q[[1L]] =='{') {
1394
-
if (!is.null(q[[qlen<-length(q)]])) q[[qlen]] = do_j_names(q[[qlen]])
1395
-
return(q)
1396
-
}
1397
-
if (q[[1L]] =='if') {
1398
-
#explicit NULL would return NULL, assigning NULL would delete that from the expression
1399
-
if (!is.null(q[[3L]])) q[[3L]] = do_j_names(q[[3L]])
1400
-
if (length(q) ==4L&&!is.null(q[[4L]])) q[[4L]] = do_j_names(q[[4L]])
1401
-
return(q)
1402
-
}
1403
-
q
1404
-
}
1405
1355
if (is.name(jsub)) {
1406
1356
# j is a single unquoted column name
1407
-
if (jsub!=".SD") jvnames=drop_dot(jsub)
1357
+
if (jsub!=".SD") jvnames=.dt_drop_dot(jsub)
1408
1358
# jsub is list()ed after it's eval'd inside dogroups.
1409
-
} elsejsub= do_j_names(jsub) # else maybe a call to transform or something which returns a list.
1359
+
} else { # else maybe a call to transform or something which returns a list.
1360
+
jsub_names= .dt_do_j_names(jsub, jvnames)
1361
+
jsub=jsub_names$jsub
1362
+
jvnames=jsub_names$jvnames
1363
+
}
1410
1364
av= all.vars(jsub,TRUE) # TRUE fixes bug #1294 which didn't see b in j=fns[[b]](c)
1411
1365
use.I=".I" %chin% av
1412
1366
if (any(c(".SD","eval","get","mget") %chin% av)) {
1413
1367
if (missing_SDcols) {
1414
-
# here we need to use 'dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
1368
+
# here we need to use '.dt_dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
1415
1369
# slight memory efficiency boost if we only store sdvars if it differs from ansvars, but a bit tricky to examine all the uses here
1416
-
ansvars=sdvars=dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
1370
+
ansvars=sdvars=.dt_dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
1417
1371
# just using .SD in j triggers all non-by columns in the subset even if some of
1418
1372
# those columns are not used. It would be tricky to detect whether the j expression
1419
1373
# 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.")
1536
-
suppPrint=function(x) { .global$print=address(x); x }
1490
+
suppPrint=.dt_suppPrint
1537
1491
# Suppress print when returns ok not on error, bug #2376. Thanks to: http://stackoverflow.com/a/13606880/403310
1538
1492
# 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
2227
2140
}
2228
2141
2142
+
# setdiff removes duplicate entries, which'll create issues with duplicated names. Use %chin% instead.
2143
+
.dt_dupdiff=function(x, y) x[!x %chin% y]
2144
+
2145
+
# auto-name a symbol in j: special symbols .N, .I, .GRP, .NGRP and .BY lose their leading dot
2146
+
.dt_drop_dot=function(x) {
2147
+
if (length(x)!=1L) internal_error("drop_dot passed %d items", length(x)) # nocov
2148
+
if (startsWith(x<-as.character(x), ".") &&x %chin% c(".N", ".I", ".GRP", ".NGRP", ".BY"))
2149
+
substr(x, 2L, nchar(x))
2150
+
else
2151
+
x
2152
+
}
2153
+
2154
+
# handle auto-naming of last item of j (e.g. within {} or if/else, #2478)
2155
+
# e.g. DT[, .(a=sum(v), v, .N), by=] should create columns named a, v, N
2156
+
# returns list(jsub=, jvnames=); jvnames is threaded through the recursion so that the names of different branches can be compared
2157
+
.dt_do_j_names=function(q, jvnames) {
2158
+
if (!is.call(q) ||!is.name(q[[1L]])) return(list(jsub=q, jvnames=jvnames))
2159
+
if (q[[1L]] %chin% c('list', '.')) {
2160
+
q[[1L]] = quote(list)
2161
+
qlen= length(q)
2162
+
if (qlen>1L) {
2163
+
nm= names(q[-1L]) # check list(a=sum(v),v)
2164
+
if (is.null(nm)) nm= rep.int("", qlen-1L)
2165
+
# attempt to auto-name unnamed columns
2166
+
for (jjin which(!nzchar(nm))) {
2167
+
thisq=q[[jj+1L]]
2168
+
if (missing(thisq)) stopf("Item %d of the .() or list() passed to j is missing", jj) #3507
2169
+
if (is.name(thisq)) nm[jj] = .dt_drop_dot(thisq)
2170
+
# TO DO: if call to a[1] for example, then call it 'a' too
2171
+
}
2172
+
if (!is.null(jvnames)) {
2173
+
if (length(nm) != length(jvnames))
2174
+
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")
2175
+
elseif (any(idx<-nm!=jvnames))
2176
+
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])))
2177
+
}
2178
+
jvnames=nm# TODO: handle if() list(a, b) else list(b, a) better
2179
+
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.
2180
+
}
2181
+
return(list(jsub=q, jvnames=jvnames)) # else empty list is needed for test 468: adding an empty list column
2182
+
}
2183
+
if (q[[1L]] =='{') {
2184
+
if (!is.null(q[[qlen<-length(q)]])) {
2185
+
ans= .dt_do_j_names(q[[qlen]], jvnames)
2186
+
q[[qlen]] =ans$jsub
2187
+
jvnames=ans$jvnames
2188
+
}
2189
+
return(list(jsub=q, jvnames=jvnames))
2190
+
}
2191
+
if (q[[1L]] =='if') {
2192
+
#explicit NULL would return NULL, assigning NULL would delete that from the expression
2193
+
if (!is.null(q[[3L]])) {
2194
+
ans= .dt_do_j_names(q[[3L]], jvnames)
2195
+
q[[3L]] =ans$jsub
2196
+
jvnames=ans$jvnames
2197
+
}
2198
+
if (length(q) ==4L&&!is.null(q[[4L]])) {
2199
+
ans= .dt_do_j_names(q[[4L]], jvnames)
2200
+
q[[4L]] =ans$jsub
2201
+
jvnames=ans$jvnames
2202
+
}
2203
+
return(list(jsub=q, jvnames=jvnames))
2204
+
}
2205
+
list(jsub=q, jvnames=jvnames)
2206
+
}
2207
+
2208
+
.dt_suppPrint=function(x) { .global$print=address(x); x }
2209
+
2210
+
# can jval be sorted by the same key as x? improved for #4498
0 commit comments