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
*Followed TODO: by mattdowle from resolution to '2-space indentation #2420'
*Added tests for jsub that modify DT by-reference
*Added test case for interger vector indexing
Copy file name to clipboardExpand all lines: NEWS.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -133,6 +133,8 @@ rowwiseDT(
133
133
134
134
19. An integer overflow in `fread()` with lines longer than `2^(31/2)` bytes is prevented, [#6729](https://github.com/Rdatatable/data.table/issues/6729). The typical impact was no worse than a wrong initial allocation size, corrected later. Thanks to @TaikiSan21 for the report and @aitap for the fix.
135
135
136
+
20. By reference assignments (':=') with functions that modify the data.table by reference e.g. (`foo=function(DT){DT[,b:=1L];return(2L)}`, `DT[,a:=foo(DT)]`) returned a mallformed data.table due to the the modification of the targeted named column ("a") index before and after the j expression evaluation. Thanks @AntonNM for the the report and fix.
137
+
136
138
## NOTES
137
139
138
140
1. There is a new vignette on joins! See `vignette("datatable-joins")`. Thanks to Angel Feliz for authoring it! Feedback welcome. This vignette has been highly requested since 2017: [#2181](https://github.com/Rdatatable/data.table/issues/2181).
# Adding new column(s). TO DO: move after the first eval in case the jsub has an error.
1181
+
# Adding new column(s). Allocation for columns and recalculation of target cols moved after the jval = eval(jsub)
1182
+
# in case of error or by-reference modifications to the DT
1183
1183
newnames=setdiff(lhs, names_x)
1184
1184
m[is.na(m)] = ncol(x)+seq_len(length(newnames))
1185
1185
cols= as.integer(m)
1186
1186
# don't pass verbose to selfrefok here -- only activated when
1187
-
# ok=-1 which will trigger setalloccol with verbose in the next
1188
-
#branch, which again calls _selfrefok and returns the message then
1187
+
# ok=-1 which will trigger setalloccol with verbose after
1188
+
#the jval = eval(jsub, ...)
1189
1189
if ((ok<-selfrefok(x, verbose=FALSE))==0L) # ok==0 so no warning when loaded from disk (-1) [-1 considered TRUE by R]
1190
1190
if (is.data.table(x)) warningf("A shallow copy of this data.table was taken so that := can add or remove %d columns by reference. At an earlier point, this data.table was copied by R (or was created manually using structure() or similar). Avoid names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. It's also not unusual for data.table-agnostic packages to produce tables affected by this issue. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.", length(newnames))
1191
-
# !is.data.table for DF |> DT(,:=) tests 2212.16-19 (#5113) where a shallow copy is routine for data.frame
1192
-
if ((ok<1L) || (truelength(x) < ncol(x)+length(newnames))) {
1193
-
DT=x# in case getOption contains "ncol(DT)" as it used to. TODO: warn and then remove
1194
-
n= length(newnames) + eval(getOption("datatable.alloccol")) # TODO: warn about expressions and then drop the eval()
1195
-
# i.e. reallocate at the size as if the new columns were added followed by setalloccol().
1196
-
name= substitute(x)
1197
-
if (is.name(name) &&ok&&verbose) { # && NAMED(x)>0 (TO DO) # ok here includes -1 (loaded from disk)
1198
-
catf("Growing vector of column pointers from truelength %d to %d. A shallow copy has been taken, see ?setalloccol. Only a potential issue if two variables point to the same data (we can't yet detect that well) and if not you can safely ignore this. To avoid this message you could setalloccol() first, deep copy first using copy(), wrap with suppressWarnings() or increase the 'datatable.alloccol' option.\n", truelength(x), n)
1199
-
# #1729 -- copying to the wrong environment here can cause some confusion
1200
-
if (ok==-1L) catf("Note that the shallow copy will assign to the environment from which := was called. That means for example that if := was called within a function, the original table may be unaffected.\n")
1201
-
1202
-
# Verbosity should not issue warnings, so cat rather than warning.
1203
-
# TO DO: Add option 'datatable.pedantic' to turn on warnings like this.
1204
-
1205
-
# TO DO ... comments moved up from C ...
1206
-
# Note that the NAMED(dt)>1 doesn't work because .Call
1207
-
# always sets to 2 (see R-ints), it seems. Work around
1208
-
# may be possible but not yet working. When the NAMED test works, we can drop allocwarn argument too
1209
-
# because that's just passed in as FALSE from [<- where we know `*tmp*` isn't really NAMED=2.
1210
-
# Note also that this growing will happen for missing columns assigned NULL, too. But so rare, we
1211
-
# don't mind.
1212
-
}
1213
-
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
if (length(j)!=1L) stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
1222
-
j= match(j, names(k))
1223
-
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
# Re-matches characters names in the lhs after jval to account for jsub's that modify the columns of the data.table (#6768)
1364
+
# Replaces numerical lhs with respective names_x
1365
+
if(is.character(lhs)){
1366
+
m= chmatch(lhs, names_x)
1367
+
if(!anyNA(m)){
1368
+
# updates by reference to existing columns
1369
+
cols= as.integer(m)
1370
+
newnames=NULL
1371
+
if (identical(irows, integer())) {
1372
+
# Empty integer() means no rows e.g. logical i with only FALSE and NA
1373
+
# got converted to empty integer() by the which() above
1374
+
# Short circuit and do-nothing since columns already exist. If some don't
1375
+
# exist then for consistency with cases where irows is non-empty, we need to create
1376
+
# them of the right type and populate with NA. Which will happen via the regular
1377
+
# alternative branches below, to cover #759.
1378
+
# We need this short circuit at all just for convenience. Otherwise users may need to
1379
+
# fix errors in their RHS when called on empty edge cases, even when the result won't be
1380
+
# used anyway (so it would be annoying to have to fix it.)
1381
+
if (verbose) {
1382
+
catf("No rows match i. No new columns to add so not evaluating RHS of :=\nAssigning to 0 row subset of %d rows\n", nrow(x))
1383
+
}
1384
+
.Call(Cassign, x, irows, NULL, NULL, NULL) # only purpose is to write 0 to .Last.updated
1385
+
.global$print= address(x)
1386
+
return(invisible(x))
1387
+
}
1388
+
}else{
1389
+
# Adding new column(s).
1390
+
newnames=setdiff(lhs, names_x)
1391
+
m[is.na(m)] = ncol(x)+seq_len(length(newnames))
1392
+
cols= as.integer(m)
1393
+
# ok <- selfrefok above called without verbose -- only activated when
1394
+
# ok=-1 which will trigger setalloccol with verbose in the next
1395
+
# branch, which again calls _selfrefok and returns the message then
1396
+
# !is.data.table for DF |> DT(,:=) tests 2212.16-19 (#5113) where a shallow copy is routine for data.frame
1397
+
if ((ok<1L) || (truelength(x) < ncol(x)+length(newnames))) {
1398
+
DT=x# in case getOption contains "ncol(DT)" as it used to. TODO: warn and then remove
1399
+
n= length(newnames) + eval(getOption("datatable.alloccol")) # TODO: warn about expressions and then drop the eval()
1400
+
# i.e. reallocate at the size as if the new columns were added followed by setalloccol().
1401
+
name= substitute(x)
1402
+
if (is.name(name) &&ok&&verbose) { # && NAMED(x)>0 (TO DO) # ok here includes -1 (loaded from disk)
1403
+
catf("Growing vector of column pointers from truelength %d to %d. A shallow copy has been taken, see ?setalloccol. Only a potential issue if two variables point to the same data (we can't yet detect that well) and if not you can safely ignore this. To avoid this message you could setalloccol() first, deep copy first using copy(), wrap with suppressWarnings() or increase the 'datatable.alloccol' option.\n", truelength(x), n)
1404
+
# #1729 -- copying to the wrong environment here can cause some confusion
1405
+
if (ok==-1L) catf("Note that the shallow copy will assign to the environment from which := was called. That means for example that if := was called within a function, the original table may be unaffected.\n")
1406
+
1407
+
# Verbosity should not issue warnings, so cat rather than warning.
1408
+
# TO DO: Add option 'datatable.pedantic' to turn on warnings like this.
1409
+
1410
+
# TO DO ... comments moved up from C ...
1411
+
# Note that the NAMED(dt)>1 doesn't work because .Call
1412
+
# always sets to 2 (see R-ints), it seems. Work around
1413
+
# may be possible but not yet working. When the NAMED test works, we can drop allocwarn argument too
1414
+
# because that's just passed in as FALSE from [<- where we know `*tmp*` isn't really NAMED=2.
1415
+
# Note also that this growing will happen for missing columns assigned NULL, too. But so rare, we
1416
+
# don't mind.
1417
+
}
1418
+
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
if (length(j)!=1L) stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
1427
+
j= match(j, names(k))
1428
+
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
0 commit comments