我在定义聚合函数时遇到了一些麻烦。这是一个可重复的例子:
library(MonetDBLite)
library(dplyr)
library(DBI)
dbdir <- tempdir()
con <- dbConnect(MonetDBLite())
dbWriteTable(con, "mtcars", mtcars, overwrite = TRUE)
ms <- src_monetdblite(dbdir)
mt <- tbl(ms, "mtcars")
dbSendUpdate(con, "CREATE AGGREGATE rapi04(val DOUBLE) RETURNS DOUBLE LANGUAGE R {
aggregate(val, by=list(aggr_group), FUN=median)$x
};")
mt %>% group_by(carb) %>% summarise(rapi04(wt))
执行此代码会产生以下错误:
Error in .local(conn, statement, ...) :
Unable to execute statement 'SELECT *
FROM (SELECT "carb", RAPI04("wt")
FROM (SELECT * FROM "mtcars") AS "_W3"
GROUP BY "carb") A...'.
Server says 'SQLException:SQLparser:Errors encountered in query'.
如果我在摘要语句中使用MonetDB的中值函数,它可以很好地工作,但如果我定义函数则不然。在MonetDBLite中定义聚合函数的正确方法是什么?