Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-Forge #2298] If the RHS of := is recycled plonk, there's no column copy, tripping up setkey() and more later. #185

Closed
arunsrinivasan opened this issue Jun 8, 2014 · 2 comments
Labels
Milestone

Comments

@arunsrinivasan
Copy link
Member

@arunsrinivasan arunsrinivasan commented Jun 8, 2014

Submitted by: Timothee Carayol; Assigned to: Nobody; R-Forge link

I say it's scary because it returns no error and is potentially quite difficult to detect in existing code..

FYI I tried this on data.table 1.8.2 and R 2.14.1.

A <- data.table(
keyvar=rev(letters[1:10]),
count=1:10
)
A[, c(
'count1',
'count2'
) := count, with=F
]

A

keyvar count count1 count2

1: j 1 1 1
2: i 2 2 2
3: h 3 3 3
4: g 4 4 4
5: f 5 5 5
6: e 6 6 6
7: d 7 7 7
8: c 8 8 8
9: b 9 9 9
10: a 10 10 10

All looks good.
Let's make a copy of A; call it B. We'll use it later on.

B <- copy(A)

Now let's set a key on "keyvar" in A:

setkeyv(A, 'keyvar')
A

keyvar count count1 count2

1: a 10 1 1
2: b 9 2 2
3: c 8 3 3
4: d 7 4 4
5: e 6 5 5
6: f 5 6 6
7: g 4 7 7
8: h 3 8 8
9: i 2 9 9
10: j 1 10 10

That's the scary bit. "keyvar" and "count" got sorted nicely, but "count1" and "count2" are now matched to wrong rows.

Let's try the same thing on the copy of A that we made earlier..

setkeyv(B, 'keyvar')
B

keyvar count count1 count2

1: j 1 1 1
2: i 2 2 2
3: h 3 3 3
4: g 4 4 4
5: f 5 5 5
6: e 6 6 6
7: d 7 7 7
8: c 8 8 8
9: b 9 9 9
10: a 10 10 10

That's fine!

I.e. whatever was wrong with A, is fixed in its copy B.

@mattdowle
Copy link
Member

@mattdowle mattdowle commented Oct 18, 2016

@MarkusBonsch
Copy link
Contributor

@MarkusBonsch MarkusBonsch commented Jan 6, 2018

Somehow, the issue has reappeared (#2540). The old bugfix by Matt (a0b27e8) must have fallen victim to code refactoring.
Existing tests do not spot the issue reliably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants