Skip to content

Commit 794437f

Browse files
Initial commit: use warning helper
1 parent a599557 commit 794437f

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

R/data.table.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,7 +2900,7 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
29002900
home = function(x, env) {
29012901
if (identical(env, emptyenv()))
29022902
stopf("Cannot find symbol %s", cname)
2903-
else if (exists(x, env, inherits=FALSE)) env
2903+
else if (exists(x, env, inherits=FALSE)) env # NB: _not_ get0(), since returning 'env' not 'get(x)'
29042904
else home(x, parent.env(env))
29052905
}
29062906
cname = as.character(name)
@@ -2918,10 +2918,15 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
29182918
} else if (is.data.frame(x)) {
29192919
# check no matrix-like columns, #3760. Allow a single list(matrix) is unambiguous and depended on by some revdeps, #3581
29202920
# for performance, only warn on the first such column, #5426
2921+
test_matrix_column = test_posixl_column = TRUE
29212922
for (jj in seq_along(x)) {
2922-
if (length(dim(x[[jj]])) > 1L) {
2923+
if (test_posixl_column && inherits(x[[jj]], "POSIXlt")) {
2924+
.Call(Cwarn_posixl_column_r, jj)
2925+
test_posixl_column = FALSE
2926+
}
2927+
if (test_matrix_column && length(dim(x[[jj]])) > 1L) {
29232928
.Call(Cwarn_matrix_column_r, jj)
2924-
break
2929+
test_matrix_column = FALSE
29252930
}
29262931
}
29272932

inst/tests/tests.Rraw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8655,7 +8655,7 @@ dt = data.table(d1="1984-03-17")
86558655
ans = data.table(d1="1984-03-17", d2=as.POSIXct("1984-03-17", tz='UTC'))
86568656
test(1612.2, dt[, d2 := strptime(d1, "%Y-%m-%d", tz='UTC')], ans, warning="POSIXlt detected and converted to POSIXct")
86578657
ll = list(a=as.POSIXlt("2015-01-01"), b=2L)
8658-
test(1612.3, setDT(ll), error="Column 1 has class 'POSIXlt'")
8658+
test(1612.3, setDT(ll), ll, warning="Column 1 has class 'POSIXlt'")
86598659

86608660
# tests for all.equal.data.table #1106
86618661
# diff nrow

src/assign.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,16 @@ void warn_matrix_column(/* 1-indexed */ int i) {
203203
warning(_("Some columns are a multi-column type (such as a matrix column), for example column %d. setDT will retain these columns as-is but subsequent operations like grouping and joining may fail. Please consider as.data.table() instead which will create a new column for each embedded column."), i);
204204
}
205205

206+
void warn_posixl_column(/* 1-indexed */ int i) {
207+
warning(_("Column %d has class 'POSIXlt'. setDT will retain these columns as-is but subsequent operations may fail. We do not recommend the use of POSIXlt at all because it uses 40 bytes to store one date. Please consider as.data.table() instead which will convert to POSIXct."), i+1);
208+
}
209+
206210
// input validation for setDT() list input; assume is.list(x) was tested in R
207211
SEXP setdt_nrows(SEXP x)
208212
{
209213
int base_length = 0;
210214
bool test_matrix_cols = true;
215+
bool test_posixl_cols = true;
211216

212217
for (R_len_t i = 0; i < LENGTH(x); ++i) {
213218
SEXP xi = VECTOR_ELT(x, i);
@@ -216,8 +221,9 @@ SEXP setdt_nrows(SEXP x)
216221
* e.g. in package eplusr which calls setDT on a list when parsing JSON. Operations which
217222
* fail for NULL columns will give helpful error at that point, #3480 and #3471 */
218223
if (Rf_isNull(xi)) continue;
219-
if (Rf_inherits(xi, "POSIXlt")) {
220-
error(_("Column %d has class 'POSIXlt'. Please convert it to POSIXct (using as.POSIXct) and run setDT() again. We do not recommend the use of POSIXlt at all because it uses 40 bytes to store one date."), i+1);
224+
if (test_posixl_cols && Rf_inherits(xi, "POSIXlt")) {
225+
warn_posixl_column(i+1);
226+
test_posixl_cols = false;
221227
}
222228
SEXP dim_xi = getAttrib(xi, R_DimSymbol);
223229
R_len_t len_xi;

src/data.table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ SEXP alloccol(SEXP dt, R_len_t n, Rboolean verbose);
182182
const char *memrecycle(const SEXP target, const SEXP where, const int start, const int len, SEXP source, const int sourceStart, const int sourceLen, const int colnum, const char *colname);
183183
SEXP shallowwrapper(SEXP dt, SEXP cols);
184184
void warn_matrix_column(int i);
185+
void warn_posixl_column(int i);
185186

186187
SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols,
187188
SEXP xjiscols, SEXP grporder, SEXP order, SEXP starts,
@@ -328,6 +329,7 @@ SEXP gfirst(SEXP);
328329
SEXP gnthvalue(SEXP, SEXP);
329330
SEXP dim(SEXP);
330331
SEXP warn_matrix_column_r(SEXP);
332+
SEXP warn_posixl_column_r(SEXP);
331333
SEXP gvar(SEXP, SEXP);
332334
SEXP gsd(SEXP, SEXP);
333335
SEXP gprod(SEXP, SEXP);

src/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ R_CallMethodDef callMethods[] = {
150150
{"CconvertDate", (DL_FUNC)&convertDate, -1},
151151
{"Cnotchin", (DL_FUNC)&notchin, -1},
152152
{"Cwarn_matrix_column_r", (DL_FUNC)&warn_matrix_column_r, -1},
153+
{"Cwarn_posixl_column_r", (DL_FUNC)&warn_posixl_column_r, -1},
153154
{NULL, NULL, 0}
154155
};
155156

src/wrappers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ SEXP warn_matrix_column_r(SEXP i) {
124124
warn_matrix_column(INTEGER(i)[0]);
125125
return R_NilValue;
126126
}
127+
128+
SEXP warn_posixl_column_r(SEXP i) {
129+
warn_posixl_column(INTEGER(i)[0]);
130+
return R_NilValue;
131+
}

0 commit comments

Comments
 (0)