Skip to content

Commit 64e9ec1

Browse files
authored
set call setalloccol for non-selfrefok tables when removing columns (#7500)
* add fix to set * add assign and tests * update tests
1 parent 3ac805b commit 64e9ec1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

R/data.table.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,14 @@ setcolorder = function(x, neworder=key(x), before=NULL, after=NULL, skip_absent=
28482848

28492849
set = function(x,i=NULL,j,value) # low overhead, loopable
28502850
{
2851+
# If removing columns from a table that's not selfrefok, need to call setalloccol first, #7488
2852+
if ((is.null(value) || (is.list(value) && any(vapply_1b(value, is.null)))) && selfrefok(x, verbose=FALSE) < 1L) {
2853+
name = substitute(x)
2854+
setalloccol(x, verbose=FALSE)
2855+
if (is.name(name)) {
2856+
assign(as.character(name), x, parent.frame(), inherits=TRUE)
2857+
}
2858+
}
28512859
.Call(Cassign,x,i,j,NULL,value)
28522860
invisible(x)
28532861
}

inst/tests/tests.Rraw

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21898,7 +21898,14 @@ rm(DT, strings)
2189821898

2189921899
# do remove columns in freshly unserialized data.tables, #7488
2190021900
DT = unserialize(serialize(as.data.table(mtcars), NULL))
21901-
test(2351, DT[,carb:=NULL], as.data.table(mtcars)[,carb:=NULL])
21901+
test(2351.1, DT[,carb:=NULL], as.data.table(mtcars)[,carb:=NULL])
21902+
DT = unserialize(serialize(as.data.table(mtcars), NULL))
21903+
test(2351.2, set(DT, j="carb", value=NULL), as.data.table(mtcars)[,carb:=NULL])
21904+
DT = unserialize(serialize(as.data.table(mtcars), NULL))
21905+
null_in_value <- NULL
21906+
test(2351.3, "cyl" %notin% names(DT[, cyl := null_in_value]))
21907+
DT = unserialize(serialize(as.data.table(mtcars), NULL))
21908+
test(2351.4, ncol(DT[, c("cyl", "mpg") := .(null_in_value, null_in_value)]), ncol(mtcars) - 2L)
2190221909
rm(DT)
2190321910

2190421911
# rbindlist did not protect the temporary UTF-8 strings, #7452

0 commit comments

Comments
 (0)