当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R dbplyr translate_sql 将表达式转换为 SQL


dbplyr 翻译常用的基本函数,包括逻辑运算符 ( !&| )、算术运算符 ( ^ ) 和比较运算符 ( != ),以及常见的摘要运算符 ( mean()var() ) 和转换 ( log() ) 函数。所有其他函数将按原样保留。 R 的中缀函数(例如 %like% )将转换为其 SQL 等效项(例如 LIKE )。

vignette("translation-function") 中了解更多信息。

用法

translate_sql(
  ...,
  con = NULL,
  vars = character(),
  vars_group = NULL,
  vars_order = NULL,
  vars_frame = NULL,
  window = TRUE
)

translate_sql_(
  dots,
  con = NULL,
  vars_group = NULL,
  vars_order = NULL,
  vars_frame = NULL,
  window = TRUE,
  context = list()
)

参数

..., dots

要翻译的表达式。 translate_sql() 自动为您引用它们。 translate_sql_() 需要一个已引用对象的列表。

con

用于控制翻译细节的可选数据库连接。默认值 NULL 生成 ANSI SQL。

vars

已弃用。现在直接调用partial_eval()

vars_group, vars_order, vars_frame

窗口函数的 OVER 表达式中使用的参数。

window

使用FALSE 禁止生成用于窗口函数的OVER 语句。当为分组摘要生成 SQL 时,这是必需的。

context

用于携带特殊翻译案例的信息。例如,MS SQL 需要对 WHERE 与 SELECT 子句中的 is.na() 进行不同的转换。期待一份清单。

例子

# Regular maths is translated in a very straightforward way
translate_sql(x + 1)
#> <SQL> `x` + 1.0
translate_sql(sin(x) + tan(y))
#> <SQL> SIN(`x`) + TAN(`y`)

# Note that all variable names are escaped
translate_sql(like == "x")
#> <SQL> `like` = 'x'
# In ANSI SQL: "" quotes variable _names_, '' quotes strings

# Logical operators are converted to their sql equivalents
translate_sql(x < 5 & !(y >= 5))
#> <SQL> `x` < 5.0 AND NOT((`y` >= 5.0))
# xor() doesn't have a direct SQL equivalent
translate_sql(xor(x, y))
#> <SQL> `x` OR `y` AND NOT (`x` AND `y`)

# If is translated into case when
translate_sql(if (x > 5) "big" else "small")
#> <SQL> CASE WHEN (`x` > 5.0) THEN 'big' WHEN NOT (`x` > 5.0) THEN 'small' END

# Infix functions are passed onto SQL with % removed
translate_sql(first %like% "Had%")
#> <SQL> `first` like 'Had%'
translate_sql(first %is% NA)
#> <SQL> `first` is NULL
translate_sql(first %in% c("John", "Roger", "Robert"))
#> <SQL> `first` IN ('John', 'Roger', 'Robert')

# And be careful if you really want integers
translate_sql(x == 1)
#> <SQL> `x` = 1.0
translate_sql(x == 1L)
#> <SQL> `x` = 1

# If you have an already quoted object, use translate_sql_:
x <- quote(y + 1 / sin(t))
translate_sql_(list(x), con = simulate_dbi())
#> <SQL> `y` + 1.0 / SIN(`t`)

# Windowed translation --------------------------------------------
# Known window functions automatically get OVER()
translate_sql(mpg > mean(mpg))
#> Warning: Missing values are always removed in SQL aggregation functions.
#> Use `na.rm = TRUE` to silence this warning
#> This warning is displayed once every 8 hours.
#> <SQL> `mpg` > AVG(`mpg`) OVER ()

# Suppress this with window = FALSE
translate_sql(mpg > mean(mpg), window = FALSE)
#> <SQL> `mpg` > AVG(`mpg`)

# vars_group controls partition:
translate_sql(mpg > mean(mpg), vars_group = "cyl")
#> <SQL> `mpg` > AVG(`mpg`) OVER (PARTITION BY `cyl`)

# and vars_order controls ordering for those functions that need it
translate_sql(cumsum(mpg))
#> Warning: Windowed expression `SUM(`mpg`)` does not have explicit order.
#> ℹ Please use `arrange()` or `window_order()` to make deterministic.
#> <SQL> SUM(`mpg`) OVER (ROWS UNBOUNDED PRECEDING)
translate_sql(cumsum(mpg), vars_order = "mpg")
#> <SQL> SUM(`mpg`) OVER (ORDER BY `mpg` ROWS UNBOUNDED PRECEDING)
源代码:R/translate-sql.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Translate an expression to SQL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。