# Regular maths is translated in a very straightforward way
translate_sql(x + 1)
translate_sql(sin(x) + tan(y))
# Logical operators are converted to their sql equivalents
translate_sql(x < 5 & !(y >= 5))
# If is translated into select case
translate_sql(if (x > 5) "big" else "small")
# Infix functions are passed onto SQL with % removed
translate_sql(first %like% "Had*")
translate_sql(first %is% NULL)
translate_sql(first %in% c("John", "Roger", "Robert"))
# Note that variable names will be escaped if needed
translate_sql(like == 7)
# And be careful if you really want integers
translate_sql(x == 1)
translate_sql(x == 1L)
# If you have an already quoted object, use translate_sql_q:
x <- quote(y + 1 / sin(t))
translate_sql(x)
translate_sql_q(list(x))
# Translation with data source --------------------------------------------
hflights <- tbl(hflights_postgres(), "hflights")
# Note distinction between integers and reals
translate_sql(Month == 1, tbl = hflights)
translate_sql(Month == 1L, tbl = hflights)
# Know how to translate most simple mathematical expressions
translate_sql(Month %in% 1:3, tbl = hflights)
translate_sql(Month >= 1L & Month <= 3L, tbl = hflights)
translate_sql((Month >= 1L & Month <= 3L) | Carrier == "AA", tbl = hflights)
# Some R functions don't have equivalents in SQL: where possible they
# will be translated to the equivalent
translate_sql(xor(Month <= 3L, Carrier == "AA"), tbl = hflights)
# Local variables will be automatically inserted into the SQL
x <- 5L
translate_sql(Month == x, tbl = hflights)
# By default all computation will happen in sql
translate_sql(Month < 1 + 1, source = hflights)
# Use local to force local evaluation
translate_sql(Month < local(1 + 1), source = hflights)
# This is also needed if you call a local function:
inc <- function(x) x + 1
translate_sql(Month == inc(x), source = hflights)
translate_sql(Month == local(inc(x)), source = hflights)
# Windowed translation --------------------------------------------
planes <- arrange(group_by(hflights, TailNum), desc(DepTime))
translate_sql(DepTime > mean(DepTime), tbl = planes, window = TRUE)
translate_sql(DepTime == min(DepTime), tbl = planes, window = TRUE)
translate_sql(rank(), tbl = planes, window = TRUE)
translate_sql(rank(DepTime), tbl = planes, window = TRUE)
translate_sql(ntile(DepTime, 2L), tbl = planes, window = TRUE)
translate_sql(lead(DepTime, 2L), tbl = planes, window = TRUE)
translate_sql(cumsum(DepDelay), tbl = planes, window = TRUE)
translate_sql(order_by(DepDelay, cumsum(DepDelay)), tbl = planes, window = TRUE)
Run the code above in your browser using DataLab