當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。