Skip to content

Commit ee0a462

Browse files
committed
new tests, slight change in behavior
1 parent 22240b7 commit ee0a462

File tree

2 files changed

+222
-51
lines changed

2 files changed

+222
-51
lines changed

R/data.table.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ replace_dot_alias = function(e) {
11391139
}
11401140
}
11411141
names(jsub)=""
1142-
jsub[[1L]]=as.name("list")
1142+
# dont wrap the RHS in list if it is a singular NULL and if not creating a new column
1143+
if (length(jsub[-1L]) == 1L && as.character(jsub[-1L]) == 'NULL' && all(lhs %chin% names_x)) jsub[[1L]]=as.name("identity") else jsub[[1L]]=as.name("list")
11431144
}
11441145
av = all.vars(jsub,TRUE)
11451146
if (!is.atomic(lhs)) stopf("LHS of := must be a symbol, or an atomic vector (column names or positions).")

inst/tests/tests.Rraw

Lines changed: 220 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18613,58 +18613,228 @@ test(2263.4, options=list(datatable.verbose=TRUE, datatable.optimize=Inf), dt[,
1861318613
test(2263.5, options=list(datatable.verbose=TRUE, datatable.optimize=Inf), dt[, .N, .(b,c)], data.table(b=dt$b, c=dt$c, N=1L), output="GForce optimized j to")
1861418614
test(2263.6, options=list(datatable.verbose=TRUE, datatable.optimize=Inf), names(attributes(dt[, .N, b]$b)), c("class", "att"), output="GForce optimized j to")
1861518615

18616-
# tests for replacing an element of a list column with NULL instead of deleting the column, #5558
18616+
# tests for new consistent replacement of list columns with list(NULL), #5558
18617+
# replacement of a list column with list(NULL) in a single-row data.table, using different assignment methods
1861718618
DT = data.table(L = list("A"), i = 1L)
18619+
ans = data.table(L = list(NULL), i = 1L)
18620+
# test using replacement with $ operator
1861818621
DT$L = list(NULL)
18619-
# Test replacement of a list column with list(NULL) using $ to access the column
18620-
test(2264.01, DT, data.table(L = list(NULL), i = 1L))
18622+
test(2264.01, DT, ans)
1862118623
DT = data.table(L = list("A"), i = 1L)
18622-
# Test same replacement but with LHS := RHS operator
18623-
test(2264.02, DT[, L := list(NULL)], data.table(L = list(NULL), i = 1L))
18624+
# standard form with := operator
18625+
test(2264.02, copy(DT)[, L := list(NULL)], ans)
18626+
# functional form with := operator
18627+
test(2264.03, copy(DT)[, `:=`(L = list(NULL))], ans)
18628+
# functional form with 'let' alias
18629+
test(2264.04, copy(DT)[, let(L = list(NULL))], ans)
18630+
# using set()
18631+
test(2264.05, set(copy(DT), j = "L", value = list(NULL)), ans)
18632+
# tests doing the same as above, but with multiple columns
18633+
DT = data.table(L1 = list("A"), L2 = list("B"), i = 1L)
18634+
ans = data.table(L1 = list(NULL), L2 = list(NULL), i = 1L)
18635+
DT$L1 = list(NULL)
18636+
DT$L2 = list(NULL)
18637+
test(2264.06, DT, ans)
18638+
DT = data.table(L1 = list("A"), L2 = list("B"), i = 1L)
18639+
# standard form with := operator
18640+
test(2264.07, copy(DT)[, c("L1", "L2") := list(list(NULL), list(NULL))], ans)
18641+
# functional form with := operator
18642+
test(2264.08, copy(DT)[, `:=`(L1 = list(NULL), L2 = list(NULL))], ans)
18643+
# functional form with 'let' alias
18644+
test(2264.09, copy(DT)[, let(L1 = list(NULL), L2 = list(NULL))], ans)
18645+
# using set()
18646+
test(2264.10, set(copy(DT), j = c("L1", "L2"), value = list(list(NULL), list(NULL))), ans)
18647+
18648+
# replacement of a list column with list(NULL) in a multi-row data.table, using different assignment methods
18649+
DT = data.table(L = list("A", "B"), i = 1L)
18650+
ans = data.table(L = list(NULL, NULL), i = 1L)
18651+
# test using replacement with $ operator
18652+
DT$L = list(NULL)
18653+
test(2264.11, DT, ans)
18654+
DT = data.table(L = list("A", "B"), i = 1L)
18655+
# standard form with := operator
18656+
test(2264.12, copy(DT)[, L := list(NULL)], ans)
18657+
# functional form with := operator
18658+
test(2264.13, copy(DT)[, `:=`(L = list(NULL))], ans)
18659+
# functional form with 'let' alias
18660+
test(2264.14, copy(DT)[, let(L = list(NULL))], ans)
18661+
# using set()
18662+
test(2264.15, set(copy(DT), j = "L", value = list(NULL)), ans)
18663+
# tests doing the same as above, but with multiple columns
18664+
DT = data.table(L1 = list("A", "B"), L2 = list("B", "C"), i = 1L)
18665+
ans = data.table(L1 = list(NULL, NULL), L2 = list(NULL, NULL), i = 1L)
18666+
DT$L1 = list(NULL)
18667+
DT$L2 = list(NULL)
18668+
test(2264.16, DT, ans)
18669+
DT = data.table(L1 = list("A", "B"), L2 = list("B", "C"), i = 1L)
18670+
# standard form with := operator
18671+
test(2264.17, copy(DT)[, c("L1", "L2"):= list(list(NULL), list(NULL))], ans)
18672+
# functional form with := operator
18673+
test(2264.18, copy(DT)[, `:=`(L1 = list(NULL), L2 = list(NULL))], ans)
18674+
# functional form with 'let' alias
18675+
test(2264.19, copy(DT)[, let(L1 = list(NULL), L2 = list(NULL))], ans)
18676+
# using set()
18677+
test(2264.20, set(copy(DT), j = c("L1", "L2"), value = list(list(NULL), list(NULL))), ans)
18678+
18679+
# Adding an empty list column to a single-row data.table, using different assignment methods
1862418680
DT = data.table(L = list("A"), i = 1L)
18625-
DT1 = data.table(L = list("A"), i = 1L)
18626-
# Test that := operator and functional form `:=` have the same result
18627-
test(2264.03, DT[, L := list(NULL)], DT1[, `:=`(L = list(NULL))])
18681+
ans = data.table(L = list("A"), i = 1L, D = list(NULL))
18682+
warn = "Tried to assign NULL to column 'D', but this column does not exist to remove"
18683+
# try to add a new empty list by list(NULL) with := in standard form, warns and does not change
18684+
test(2264.21, copy(DT)[, D := list(NULL)], DT, warning=warn)
18685+
test(2264.22, set(copy(DT), j = "D", value = list(NULL)), DT, warning=warn)
18686+
# add a new column by wrapping list(NULL), consistent with old behavior
18687+
DT$D = list(list(NULL))
18688+
test(2264.23, DT, ans)
1862818689
DT = data.table(L = list("A"), i = 1L)
18629-
DT1 = data.table(L = list("A"), i = 1L)
18630-
# Test that functional operator and let have the same result
18631-
test(2264.04, DT[, `:=`(L = list(NULL))], DT1[, let(L = list(NULL))])
18632-
# Similar tests below but DT list column has more than one entry
18633-
DT = data.table(L = list("B", "C"), i = 1L)
18634-
DT$L = list(NULL)
18635-
test(2264.05, DT, data.table(L = list(NULL, NULL), i = 1L))
18636-
DT = data.table(L = list("B", "C"), i = 1L)
18637-
test(2264.06, DT[, L := list(NULL)], data.table(L = list(NULL, NULL), i = 1L))
18638-
DT = data.table(L = list("B", "C"), i = 1L)
18639-
DT1 = data.table(L = list("B", "C"), i = 1L)
18640-
test(2264.07, DT[, L := list(NULL)], DT1[, `:=`(L = list(NULL))])
18641-
DT = data.table(L = list("B", "C"), i = 1L)
18642-
DT1 = data.table(L = list("B", "C"), i = 1L)
18643-
test(2264.08, DT[, `:=`(L = list(NULL))], DT1[, let(L = list(NULL))])
18644-
# Tests for add/remove columns
18645-
DT = data.table(L = list("B"), i = 1L)
18646-
# warns, does not add
18647-
DT$new = list(NULL)
18648-
test(2264.09, DT, data.table(L = list("B"), i = 1L))
18649-
test(2264.10, copy(DT)[, D := list(NULL)], data.table(L = list("B"), i = 1L), warning="Tried to assign NULL to column 'D', but this column does not exist to remove")
18650-
# works, adds in functional form
18651-
test(2264.11, copy(DT)[, `:=`(D = list(NULL))], data.table(L = list("B"), i = 1L, D = list(NULL)))
18652-
# remove list column, must use NULL instead of list(NULL)
18653-
test(2264.12, copy(DT)[, L := NULL], data.table(i = 1L))
18654-
# remove non-list column, same as old
18655-
test(2264.13, copy(DT)[, i := NULL], data.table(L = list("B")))
18656-
# Same tests, with multiple rows
18657-
DT = data.table(L = list("B", "C"), i = 1L)
18658-
test(2264.14, copy(DT)[, D := list(NULL)], data.table(L = list("B", "C"), i = 1L), warning="Tried to assign NULL to column 'D', but this column does not exist to remove")
18659-
test(2264.15, copy(DT)[, `:=`(D = list(NULL))], data.table(L = list("B", "C"), i = 1L, D = list(NULL)))
18660-
test(2264.16, copy(DT)[, L := NULL], data.table(i = c(1L, 1L)))
18661-
test(2264.17, copy(DT)[, i := NULL], data.table(L = list("B", "C")))
18662-
# test for deleting a list column and adding a new empty list column in the same query, same as before
18663-
test(2264.18, copy(DT)[, c("L", "D") := list(NULL, list(NULL))], data.table(i = c(1L, 1L), D = list(NULL)))
18664-
# test for deleting everything
18665-
test(2264.19, copy(DT)[, c("L", "i") := NULL], data.table())
18666-
# test for replacement of multiple empty list columns
18667-
DT = data.table(L1 = list("B", "C"), L2 = list("C", "D"), i = 1L)
18668-
test(2264.20, copy(DT)[, c("L1", "L2") := list(list(NULL), list(NULL))], data.table(L1 = list(NULL), L2 = list(NULL), i = c(1L, 1L)))
18669-
# test for removal of multiple empty list columns
18670-
test(2264.21, copy(DT)[, c("L1", "L2") := NULL], data.table(i = c(1L, 1L)))
18690+
# test adding empty list column in standard form with := operator
18691+
test(2264.24, copy(DT)[, D := .(list(NULL))], ans)
18692+
# functional form with := operator
18693+
test(2264.25, copy(DT)[, `:=`(D = list(NULL))], ans)
18694+
# functional form with 'let' alias
18695+
test(2264.26, copy(DT)[, let(D = list(NULL))], ans)
18696+
# using set()
18697+
test(2264.27, set(copy(DT), j = "D", value = list(list(NULL))), ans)
18698+
# tests doing the same as above, but with multiple columns
18699+
DT = data.table(L = list("A"), i = 1L)
18700+
ans = data.table(L = list("A"), i = 1L, D = list(NULL), R = list(NULL))
18701+
DT$D = list(list(NULL))
18702+
DT$R = list(list(NULL))
18703+
test(2264.28, DT, ans)
18704+
DT = data.table(L = list("A"), i = 1L)
18705+
# standard form with := operator
18706+
test(2264.29, copy(DT)[, c("D", "R"):= .(list(NULL))], ans)
18707+
# functional form with := operator
18708+
test(2264.30, copy(DT)[, `:=`(D = list(NULL), R = list(NULL))], ans)
18709+
# functional form with 'let' alias
18710+
test(2264.31, copy(DT)[, let(D = list(NULL), R = list(NULL))], ans)
18711+
# using set()
18712+
test(2264.32, set(copy(DT), j = c("D", "R"), value = list(list(NULL))), ans)
18713+
18714+
# Adding an empty list column to a multi-row data.table, using different assignment methods
18715+
DT = data.table(L = list("A", "B"), i = 1L)
18716+
ans = data.table(L = list("A", "B"), i = 1L, D = list(NULL, NULL))
18717+
warn = "Tried to assign NULL to column 'D', but this column does not exist to remove"
18718+
# try to add a new empty list by list(NULL) with := in standard form, warns and does not change
18719+
test(2264.33, copy(DT)[, D := list(NULL)], DT, warning=warn)
18720+
test(2264.34, set(copy(DT), j = "D", value = list(NULL)), DT, warning=warn)
18721+
# add a new column by wrapping list(NULL), consistent with old behavior
18722+
DT$D = list(list(NULL))
18723+
test(2264.35, DT, ans)
18724+
DT = data.table(L = list("A", "B"), i = 1L)
18725+
# test adding empty list column in standard form with := operator
18726+
test(2264.36, copy(DT)[, D := .(list(NULL))], ans)
18727+
# functional form with := operator
18728+
test(2264.37, copy(DT)[, `:=`(D = list(NULL))], ans)
18729+
# functional form with 'let' alias
18730+
test(2264.38, copy(DT)[, let(D = list(NULL))], ans)
18731+
# using set()
18732+
test(2264.39, set(copy(DT), j = "D", value = list(list(NULL))), ans)
18733+
# tests doing the same as above, but with multiple columns
18734+
DT = data.table(L = list("A", "B"), i = 1L)
18735+
ans = data.table(L = list("A", "B"), i = 1L, D = list(NULL, NULL), R = list(NULL, NULL))
18736+
DT$D = list(list(NULL))
18737+
DT$R = list(list(NULL))
18738+
test(2264.40, DT, ans)
18739+
DT = data.table(L = list("A", "B"), i = 1L)
18740+
# standard form with := operator
18741+
test(2264.41, copy(DT)[, c("D", "R"):= .(list(NULL))], ans)
18742+
# functional form with := operator
18743+
test(2264.42, copy(DT)[, `:=`(D = list(NULL), R = list(NULL))], ans)
18744+
# functional form with 'let' alias
18745+
test(2264.43, copy(DT)[, let(D = list(NULL), R = list(NULL))], ans)
18746+
# using set()
18747+
test(2264.44, set(copy(DT), j = c("D", "R"), value = list(list(NULL))), ans)
18748+
18749+
# Removal of a list column in a single-row data.table, using different assignment methods
18750+
# NOTE: There is only one way to remove columns now, by assigning to NULL
18751+
DT = data.table(L = list("A"), i = 1L)
18752+
ans = data.table(i = 1L)
18753+
# test removing a list column by assigning to NULL
18754+
DT$L = NULL
18755+
test(2264.45, DT, ans)
18756+
DT = data.table(L = list("A"), i = 1L)
18757+
# standard form with := operator
18758+
test(2264.46, copy(DT)[, L := NULL], ans)
18759+
# functional form with := operator
18760+
test(2264.47, copy(DT)[, `:=`(L = NULL)], ans)
18761+
# functional form with 'let' alias
18762+
test(2264.48, copy(DT)[, let(L = NULL)], ans)
18763+
# using set()
18764+
test(2264.49, set(copy(DT), j = "L", value = NULL), ans)
18765+
# test doing the same as above, but with multiple columns
18766+
DT = data.table(L1 = list("A"), L2 = list("B"), i = 1L)
18767+
# test removing two list columns by assigning to NULL
18768+
DT$L1 = NULL
18769+
DT$L2 = NULL
18770+
test(2264.50, DT, ans)
18771+
DT = data.table(L1 = list("A"), L2 = list("B"), i = 1L)
18772+
# standard form with := operator
18773+
test(2264.51, copy(DT)[, c("L1", "L2") := NULL], ans)
18774+
# functional form with := operator
18775+
test(2264.52, copy(DT)[, `:=`(L1 = NULL, L2 = NULL)], ans)
18776+
# functional form with 'let' alias
18777+
test(2264.53, copy(DT)[, let(L1 = NULL, L2 = NULL)], ans)
18778+
# using set()
18779+
test(2264.54, set(copy(DT), j = c("L1", "L2"), value = NULL), ans)
18780+
18781+
# Removal of a list column in a multi-row data.table, using different assignment methods
18782+
DT = data.table(L = list("A", "B"), i = 1L)
18783+
ans = data.table(i = c(1L, 1L))
18784+
# test removing a list column by assigning to NULL
18785+
DT$L = NULL
18786+
test(2264.55, DT, ans)
18787+
DT = data.table(L = list("A", "B"), i = 1L)
18788+
# standard form with := operator
18789+
test(2264.56, copy(DT)[, L := NULL], ans)
18790+
# functional form with := operator
18791+
test(2264.57, copy(DT)[, `:=`(L = NULL)], ans)
18792+
# functional form with 'let' alias
18793+
test(2264.58, copy(DT)[, let(L = NULL)], ans)
18794+
# using set()
18795+
test(2264.59, set(copy(DT), j = "L", value = NULL), ans)
18796+
# test doing the same as above, but with multiple columns
18797+
DT = data.table(L1 = list("A", "B"), L2 = list("B", "C"), i = 1L)
18798+
# test removing two list columns by assigning to NULL
18799+
DT$L1 = NULL
18800+
DT$L2 = NULL
18801+
test(2264.60, DT, ans)
18802+
DT = data.table(L1 = list("A", "B"), L2 = list("B", "C"), i = 1L)
18803+
# standard form with := operator
18804+
test(2264.61, copy(DT)[, c("L1", "L2") := NULL], ans)
18805+
# functional form with := operator
18806+
test(2264.62, copy(DT)[, `:=`(L1 = NULL, L2 = NULL)], ans)
18807+
# functional form with 'let' alias
18808+
test(2264.63, copy(DT)[, let(L1 = NULL, L2 = NULL)], ans)
18809+
# using set()
18810+
test(2264.64, set(copy(DT), j = c("L1", "L2"), value = NULL), ans)
18811+
18812+
# Combining queries (add/remove/replace columns in the same query) for a single-row data.table
18813+
DT = data.table(L = list("A"), i = 1L)
18814+
# test for adding a new empty list column D and removing column L in the same query
18815+
ans = data.table(i = 1L, D = list(NULL))
18816+
test(2264.65, copy(DT)[, c("L", "D") := list(NULL, list(NULL))], ans)
18817+
test(2264.66, copy(DT)[, `:=`(L = NULL, D = list(NULL))], ans)
18818+
test(2264.67, copy(DT)[, let(L = NULL, D = list(NULL))], ans)
18819+
test(2264.68, set(copy(DT), j = c("L", "D"), value = list(NULL, list(NULL))), ans)
18820+
DT = data.table(L = list("A"), i = 1L)
18821+
# test for adding a new empty list column D and replacing column L with empty list in the same query
18822+
ans = data.table(L = list(NULL), i = 1L, D = list(NULL))
18823+
test(2264.69, copy(DT)[, c("L", "D") := list(list(NULL), list(NULL))], ans)
18824+
test(2264.70, copy(DT)[, `:=`(L = list(NULL), D = list(NULL))], ans)
18825+
test(2264.71, copy(DT)[, let(L = list(NULL), D = list(NULL))], ans)
18826+
test(2264.72, set(copy(DT), j = c("L", "D"), value = list(list(NULL), list(NULL))), ans)
18827+
DT = data.table(L = list("A"), D = list("B"), i = 1L)
18828+
# test for replacing column L with an empty list and removing list column D in the same query
18829+
ans = data.table(L = list(NULL), i = 1L)
18830+
test(2264.73, copy(DT)[, c("L", "D") := list(list(NULL), NULL)], ans)
18831+
test(2264.74, copy(DT)[, `:=`(L = list(NULL), D = NULL)], ans)
18832+
test(2264.75, copy(DT)[, let(L = list(NULL), D = NULL)], ans)
18833+
test(2264.76, set(copy(DT), j = c("L", "D"), value = list(list(NULL), NULL)), ans)
18834+
DT = data.table(L = list("A"), D = list("B"), i = 1L)
18835+
# test for combining add, replace, remove in the same query
18836+
ans = data.table(L = list(NULL), i = 1L, E = list(NULL))
18837+
test(2264.77, copy(DT)[, c("L", "D", "E") := list(list(NULL), NULL, list(NULL))], ans)
18838+
test(2264.78, copy(DT)[, `:=`(L = list(NULL), D = NULL, E = list(NULL))], ans)
18839+
test(2264.79, copy(DT)[, let(L = list(NULL), D = NULL, E = list(NULL))], ans)
18840+
test(2264.80, set(copy(DT), j = c("L", "D", "E"), value = list(list(NULL), NULL, list(NULL))), ans)

0 commit comments

Comments
 (0)