Skip to content

Commit

Permalink
R's startsWith() doesn't accept non-character input -> regression
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Jul 17, 2024
1 parent 1fa9439 commit 8e1b3f8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -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()")

0 comments on commit 8e1b3f8

Please sign in to comment.