From 1d17aaa92deb26a79d9ce2fe6fb08848ac374416 Mon Sep 17 00:00:00 2001 From: Benjamin Schwendinger Date: Fri, 29 May 2026 21:59:50 +0200 Subject: [PATCH] add shared guard for setkey --- NEWS.md | 2 ++ src/reorder.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d87656c9f7..49fd456cc3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -54,6 +54,8 @@ 10. `setkey()`, `setindex()`, `CJ()`, and `setnames()` now prevent creating ambiguous keys from duplicated column names, and keyed joins now error on existing duplicated key columns rather than silently giving incorrect results, [#4888](https://github.com/Rdatatable/data.table/issues/4888) and [#4891](https://github.com/Rdatatable/data.table/issues/4891). Thanks @magerton and @MichaelChirico for the reports, and @ben-schwen for the fix. +11. `setkey()` no longer modifies the original data.table when called on a shallow copy (e.g. `DT1 = DT[TRUE]`; `setkey(DT1, col)` was silently reordering `DT` in-place as well, while retaining the key), [#5230](https://github.com/Rdatatable/data.table/issues/5230). Thanks @daynefiler for the report and @ben-schwen the fix. + ### Notes 1. {data.table} now depends on R 3.5.0 (2018). diff --git a/src/reorder.c b/src/reorder.c index 61a65b20dc..920b46043b 100644 --- a/src/reorder.c +++ b/src/reorder.c @@ -66,7 +66,14 @@ SEXP reorder(SEXP x, SEXP order) void *TMP = R_alloc(nmid, maxSize); for (int i=0; i