Skip to content

Commit

Permalink
Add median() translation to SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 22, 2020
1 parent 56d08a3 commit a79c441
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# dbplyr (development version)

* SQLite gets correct translation for `median()` (#357).

* `copy_lahman()` and `copy_nycflights13()` (and hence `nycflights13_sqlite()`)
and friends now return DBI connections rather than the now deprecated
`src_dbi()` (#440).
Expand Down
9 changes: 5 additions & 4 deletions R/backend-sqlite.R
Expand Up @@ -35,15 +35,16 @@ sql_translate_env.SQLiteConnection <- function(con) {
paste0 = sql_paste_infix("", "||", function(x) sql_expr(cast(!!x %as% text))),
# https://www.sqlite.org/lang_corefunc.html#maxoreunc
pmin = sql_prefix("MIN"),
pmax = sql_prefix("MAX"),

pmax = sql_prefix("MAX")
),
sql_translator(.parent = base_agg,
sd = sql_aggregate("STDEV", "sd")
sd = sql_aggregate("STDEV", "sd"),
median = sql_prefix("MEDIAN"),
),
if (sqlite_version() >= "3.25") {
sql_translator(.parent = base_win,
sd = win_aggregate("STDEV")
sd = win_aggregate("STDEV"),
median = win_absent("median")
)
} else {
base_no_win
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-backend-sqlite.R
Expand Up @@ -25,6 +25,12 @@ test_that("sqlite mimics two argument log", {
expect_equal(translate_sql(log(x, 10)), sql('LOG(`x`) / LOG(10.0)'))
})

test_that("custom aggregates translated", {
local_con(simulate_sqlite())

expect_equal(translate_sql(median(x), window = FALSE), sql('MEDIAN(`x`)'))
expect_equal(translate_sql(sd(x, na.rm = TRUE), window = FALSE), sql('STDEV(`x`)'))
})
# live database -----------------------------------------------------------

test_that("as.numeric()/as.double() get custom translation", {
Expand Down

0 comments on commit a79c441

Please sign in to comment.