From 8e1b3f8b7bd6e38970371eabd35781c437a36c8e Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 17 Jul 2024 04:45:10 +0000 Subject: [PATCH] R's startsWith() doesn't accept non-character input -> regression --- R/data.table.R | 8 ++++---- inst/tests/tests.Rraw | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/R/data.table.R b/R/data.table.R index 133c987fe..7f6c7f984 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -1987,11 +1987,11 @@ DT = function(x, ...) { #4872 .optmean = function(expr) { # called by optimization of j inside [.data.table only. Outside for a small speed advantage. if (length(expr)==2L) # no parameters passed to mean, so defaults of trim=0 and na.rm=FALSE - return(call(".External",quote(Cfastmean),expr[[2L]], FALSE)) + return(call(".External", quote(Cfastmean), expr[[2L]], FALSE)) # return(call(".Internal",expr)) # slightly faster than .External, but R now blocks .Internal in coerce.c from apx Sep 2012 - if (length(expr)==3L && startsWith(names(expr)[3L], "na")) # one parameter passed to mean() - return(call(".External",quote(Cfastmean),expr[[2L]], expr[[3L]])) # faster than .Call - assign("nomeanopt",TRUE,parent.frame()) + if (length(expr)==3L && isTRUE(startsWith(as.character(names(expr)[3L]), "na"))) # one named parameter passed to mean(); as.character() for NULL names, isTRUE() for NA/0-length + return(call(".External", quote(Cfastmean), expr[[2L]], expr[[3L]])) # faster than .Call + assign("nomeanopt", TRUE, parent.frame()) expr # e.g. trim is not optimized, just na.rm } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 4f388bf78..64a012d6b 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18739,3 +18739,7 @@ test(2268, rbindlist(y, fill=TRUE), rbindlist(x, fill=TRUE)[rep(1:5, N)]) dt = data.table(x=as.POSIXct(c(NA, NA))) test(2269.1, fread("x\n \n \n", colClasses="POSIXct"), dt) test(2269.2, fread("x\n?\n \n", colClasses="POSIXct", na.strings="?"), dt) + +# Error found by revdep in #6284: mean(a,b) is valid, expr names() can be NULL +DT = data.table(a = 1, b = 2) +test(2270, options=c(datatable.optimize=1L), DT[, mean(b, 1), by=a], data.table(a=1, V1=2), warning="Unable to optimize call to mean()")