我们来看看以下示例?sql_variant:
我们为聚合函数定义了一个新的转换函数,从默认函数扩展:
postgres_agg <- sql_translator(.parent = base_agg,
cor = sql_prefix("corr"),
cov = sql_prefix("covar_samp"),
sd = sql_prefix("stddev_samp"),
var = sql_prefix("var_samp")
)
Run Code Online (Sandbox Code Playgroud)
然后我们定义一个新的变体,它由3种不同类型的翻译函数组成(这里是2):
postgres_var <- sql_variant(
base_scalar,
postgres_agg
)
translate_sql(cor(x, y), variant = postgres_var)
# <SQL> COR("x", "y")
translate_sql(sd(income / years), variant = postgres_var)
# <SQL> SD("income" / "years")
Run Code Online (Sandbox Code Playgroud)
这些看起来不会翻译给我,它们不应该"CORR"和"STDDEV_SAMP"吗?
# Original comment:
# Any functions not explicitly listed in the converter will be translated
# to sql as is, so you don't need to convert all functions.
translate_sql(regr_intercept(y, x), variant = postgres_var)
# <SQL> REGR_INTERCEPT("y", "x")
Run Code Online (Sandbox Code Playgroud)
这个行为符合预期,就像其他2一样.
另一方面,默认翻译函数有效,请参阅:
translate_sql(mean(x), variant = postgres_var)
#<SQL> avg("x") OVER ()
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?还是我错过了什么?
我的目标是为Oracle以下方式创建一些变体并使用它,然后用于更复杂的功能(例如,SQLite可重现):
con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
copy_to(con, cars, "cars")
con %>% tbl("cars") %>% summarize(dist = group_concat(dist)) # works as expected, as we're stealing the keyword from sqlite directly
sqlite_variant <- sql_variant(aggregate=sql_translator(.parent = base_agg,gpc = sql_prefix("group_concat")))
con %>% tbl("cars") %>% summarize(dist = gpc(dist)) # how do I make this work ?
Run Code Online (Sandbox Code Playgroud)
编辑:
一个赏金后来仍然没有解决方案,我已经在dplyr/ dbplyr github页面直接发布了问题,我不确定它是否已经或将得到关注,但是如果我(或其他人)没有及时更新这个,检查此网址:https://github.com/tidyverse/dplyr/issues/3117
这是 Hadley Wickham 在提供的 github 链接上的回答:
translate_sql() 不再有变体参数
事实上,变体参数没有记录,尽管示例使用它,我想它会在下一个版本中得到纠正。
当被问及如何定义自定义 SQL 翻译时,他提供了以下信息:
看一下http://dbplyr.tidyverse.org/articles/new-backend.html 和http://dbplyr.tidyverse.org/articles/sql-translation.html
我想另一个选择是获取旧版本的dbplyr::sql_variant.