Skip to content

DT[TRUE] can lead to invalid key #3215

@mattdowle

Description

@mattdowle

I didn't realize DT[TRUE] was a way to achieve a shallow copy. shallow copy was only intended for internal use. Thanks to @renkun-ken for highlighting this in #3214, and related #2254.

In v1.11.8 we see this :

DT = data.table(id = 1:5, key="id")
DT1 = DT[TRUE]
key(DT1)
[1] "id"
DT1[3, id:=6L]
key(DT1)
# NULL              # correct
DT$id
# [1] 1 2 6 4 5     # should be 1:5
key(DT)
# [1] "id"          # invalid key

It only occurs after DT[TRUE], iiuc, which hopefully folk have not discovered or relied on too much?! I hope the usage out there is like @renkun-ken described to add new columns to the shallow copy, not to change existing columns!

New test 1542.08 was added in PR #2313 ready for when this is fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions