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 dbplyr tbl_lazy 創建本地惰性 tibble
- R dbplyr tbl.src_dbi 對遠程數據庫表使用 dplyr 動詞
- R dbplyr backend-teradata 後端:Teradata
- R dbplyr escape 轉義/引用字符串。
- R dbplyr expand.tbl_lazy 擴展 SQL 表以包含所有可能的值組合
- R dbplyr distinct.tbl_lazy 子集不同/唯一行
- R dbplyr backend-sqlite 後端:SQLite
- R dbplyr pivot_wider.tbl_lazy 將數據從長軸轉向寬軸
- R dbplyr build_sql 構建 SQL 字符串。
- R dbplyr mutate.tbl_lazy 創建、修改和刪除列
- R dbplyr collapse.tbl_sql 計算查詢的結果
- R dbplyr sql_expr 從 R 表達式生成 SQL
- R dbplyr get_returned_rows 提取並檢查返回的行
- R dbplyr dbplyr_uncount “計數”數據庫表
- R dbplyr count.tbl_lazy 按組計數觀察值
- R dbplyr backend-odbc 後端:ODBC
- R dbplyr head.tbl_lazy 對第一行進行子集化
- R dbplyr db-quote SQL 轉義/引用泛型
- R dbplyr copy_inline 在 dbplyr 查詢中使用本地 DataFrame
- R dbplyr backend-oracle 後端:甲骨文
- R dbplyr backend-snowflake 後端:雪花
- R dbplyr lahman 緩存並檢索 Lahman 棒球數據庫的 src_sqlite。
- R dbplyr backend-redshift 後端:紅移
- R dbplyr partial_eval 部分評估表達式。
- R dbplyr group_by.tbl_lazy 按一個或多個變量分組
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Translate an expression to SQL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。