Does sql_variant in dbplyr work as it should?
Asked Answered
E

1

9

Let's take a look at the example in ?sql_variant:

We define a new translator function for aggregated functions, expanded from the default one:

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")
)

We then define a new variant, which is made from translation functions of the 3 different types (here 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")

These don't look translated to me, shouldn't they be "CORR" and "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")

This one behaves as expected, which is just like the other 2.

On the other hand default translated functions work, see:

translate_sql(mean(x), variant = postgres_var)
#<SQL> avg("x") OVER ()

It's a bug right ? or am I missing something ?

My goal is to create some variants for Oracle and use it in the following fashion,then for more complicated functions (example with SQLite to be reproducible):

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 ?

EDIT:

One bounty later still no solution, I've cross posted the issue in the dplyr/dbplyr github page directly where I'm not sure if it has or will get attention, but in case I (or someone else) don't update this in time, check this url : https://github.com/tidyverse/dplyr/issues/3117

Elementary answered 16/9, 2017 at 10:52 Comment(0)
E
2

This is what Hadley Wickham answered on provided github link:

translate_sql() doesn't have a variant argument any more

Indeed the variant argument is not documented, though the examples use it, I suppose it will be corrected for next version.

Asked how to define custom SQL translations he had this to offer:

Have a look at http://dbplyr.tidyverse.org/articles/new-backend.html and http://dbplyr.tidyverse.org/articles/sql-translation.html

I guess another option is to get the older version of dbplyr::sql_variant.

Elementary answered 16/11, 2017 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.