1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
test_that("window functions without group have empty over", {
expect_equal(translate_sql(n()), sql("COUNT(*) OVER ()"))
expect_equal(translate_sql(sum(x, na.rm = TRUE)), sql("SUM(`x`) OVER ()"))
})
test_that("aggregating window functions ignore order_by", {
expect_equal(
translate_sql(n(), vars_order = "x"),
sql("COUNT(*) OVER ()")
)
expect_equal(
translate_sql(sum(x, na.rm = TRUE), vars_order = "x"),
sql("SUM(`x`) OVER ()")
)
})
test_that("count uses order_by if frame is used", {
expect_equal(
translate_sql(n(), vars_order = "x", vars_frame = c(-2, 1)),
sql("COUNT(*) OVER (ORDER BY `x` ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING)")
)
})
test_that("order_by overrides default ordering", {
expect_equal(
translate_sql(order_by(y, cumsum(x)), vars_order = "x"),
sql("SUM(`x`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING)")
)
expect_equal(
translate_sql(order_by(y, cummean(x)), vars_order = "x"),
sql("AVG(`x`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING)")
)
expect_equal(
translate_sql(order_by(y, cummin(x)), vars_order = "x"),
sql("MIN(`x`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING)")
)
expect_equal(
translate_sql(order_by(y, cummax(x)), vars_order = "x"),
sql("MAX(`x`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING)")
)
})
test_that("cumulative windows warn if no order", {
expect_warning(translate_sql(cumsum(x)), "does not have explicit order")
expect_warning(translate_sql(cumsum(x), vars_order = "x"), NA)
})
test_that("ntile always casts to integer", {
expect_equal(
translate_sql(ntile(x, 10.5)),
sql("NTILE(10) OVER (ORDER BY `x`)")
)
})
test_that("first, last, and nth translated to _value", {
expect_equal(
translate_sql(first(x)),
sql("FIRST_VALUE(`x`) OVER ()")
)
# `last()` must default to unbounded preceding and following
expect_equal(
translate_sql(last(x), vars_order = "a"),
sql("LAST_VALUE(`x`) OVER (ORDER BY `a` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)")
)
expect_equal(
translate_sql(last(x), vars_order = "a", vars_frame = c(0, Inf)),
sql("LAST_VALUE(`x`) OVER (ORDER BY `a` ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)")
)
expect_equal(
translate_sql(nth(x, 3), vars_order = "a", vars_frame = c(-Inf, 0)),
sql("NTH_VALUE(`x`, 3) OVER (ORDER BY `a` ROWS UNBOUNDED PRECEDING)")
)
})
test_that("can override frame of recycled functions", {
expect_equal(
translate_sql(sum(x, na.rm = TRUE), vars_frame = c(-1, 0), vars_order = "y"),
sql("SUM(`x`) OVER (ORDER BY `y` ROWS 1 PRECEDING)")
)
})
test_that("frame is checked", {
expect_snapshot(
error = TRUE,
translate_sql(sum(x, na.rm = TRUE), vars_frame = c(1, 0))
)
})
test_that("win_rank works", {
local_con(simulate_dbi())
sql_row_number <- win_rank("ROW_NUMBER")
expect_equal(
sql_row_number(ident("x")),
sql("CASE
WHEN (NOT((`x` IS NULL))) THEN ROW_NUMBER() OVER (PARTITION BY (CASE WHEN ((`x` IS NULL)) THEN 1 ELSE 0 END) ORDER BY `x`)
END")
)
})
test_that("win_cumulative works", {
local_con(simulate_dbi())
sql_cumsum <- win_cumulative("SUM")
expect_equal(
sql_cumsum(ident("x"), "y"),
sql("SUM(`x`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING)")
)
# NA values results in NA rank
db <- memdb_frame(x = c(1, 2, NA, 3))
expect_equal(
db %>% mutate(rank = dense_rank(x)) %>% collect() %>% arrange(x),
tibble(x = c(1:3, NA), rank = c(1:3, NA))
)
})
# win_over ----------------------------------------------------------------
test_that("over() only requires first argument", {
local_con(simulate_dbi())
expect_equal(win_over("X"), sql("'X' OVER ()"))
})
test_that("multiple group by or order values don't have parens", {
local_con(simulate_dbi())
expect_equal(
win_over(ident("x"), order = c("x", "y")),
sql("`x` OVER (ORDER BY `x`, `y`)")
)
expect_equal(
win_over(ident("x"), partition = c("x", "y")),
sql("`x` OVER (PARTITION BY `x`, `y`)")
)
})
# window_frame ------------------------------------------------------------
test_that("window_frame()", {
lf <- lazy_frame(x = runif(10), y = 1:10)
expect_snapshot(
lf %>%
window_frame(-3, 0) %>%
window_order(x) %>%
mutate(z = sum(y, na.rm = TRUE)) %>%
show_query()
)
expect_snapshot(
lf %>%
window_frame(-3) %>%
window_order(x) %>%
mutate(z = sum(y, na.rm = TRUE)) %>%
show_query()
)
})
test_that("window_frame() checks arguments", {
skip_if(getRversion() <= '3.5.0', "R too old")
lf <- lazy_frame(x = runif(10), y = 1:10)
expect_snapshot(error = TRUE, window_frame(lf, "a"))
expect_snapshot(error = TRUE, window_frame(lf, 1:2))
expect_snapshot(error = TRUE, window_frame(lf, 1, "a"))
expect_snapshot(error = TRUE, window_frame(lf, 1, 1:2))
})
# named windows -----------------------------------------------------------
test_that("names windows automatically", {
lf <- lazy_frame(
col1 = runif(3),
col2 = runif(3),
col3 = runif(3),
col4 = runif(3),
part = c("a", "a", "b"),
ord = 3:1,
con = simulate_sqlite()
) %>%
group_by(part) %>%
window_order(ord)
lf1 <- lf %>%
transmute(
across(c(col1, col2), ~ sum(.x, na.rm = TRUE)),
across(c(col3, col4), ~ order_by(desc(ord), cumsum(.x)))
)
sql_list <- get_select_sql(lf1$lazy_query$select, "mutate", op_vars(lf), simulate_sqlite())
expect_equal(
sql_list$window_sql,
sql(
"`win1` AS (PARTITION BY `part`)",
"`win2` AS (PARTITION BY `part` ORDER BY `ord` DESC ROWS UNBOUNDED PRECEDING)"
)
)
expect_equal(
sql_list$select_sql,
sql(
part = ident("part"),
col1 = sql("SUM(`col1`) OVER `win1`"),
col2 = sql("SUM(`col2`) OVER `win1`"),
col3 = sql("SUM(`col3`) OVER `win2`"),
col4 = sql("SUM(`col4`) OVER `win2`")
)
)
# Different order does not confuse naming of windows
lf2 <- lf %>%
transmute(
col1 = sum(col1, na.rm = TRUE),
col3 = order_by(desc(ord), cumsum(col3)),
col2 = sum(col2, na.rm = TRUE),
col4 = order_by(desc(ord), cumsum(col4))
)
sql_list <- get_select_sql(lf2$lazy_query$select, "mutate", op_vars(lf), simulate_sqlite())
expect_equal(
sql_list$window_sql,
sql(
"`win1` AS (PARTITION BY `part`)",
"`win2` AS (PARTITION BY `part` ORDER BY `ord` DESC ROWS UNBOUNDED PRECEDING)"
)
)
expect_equal(
sql_list$select_sql,
sql(
part = ident("part"),
col1 = sql("SUM(`col1`) OVER `win1`"),
col3 = sql("SUM(`col3`) OVER `win2`"),
col2 = sql("SUM(`col2`) OVER `win1`"),
col4 = sql("SUM(`col4`) OVER `win2`")
)
)
})
test_that("only name windows if they appear multiple times", {
lf <- lazy_frame(
col1 = runif(3),
col2 = runif(3),
col3 = runif(3),
part = c("a", "a", "b"),
ord = 3:1,
con = simulate_sqlite()
) %>%
group_by(part) %>%
window_order(ord) %>%
transmute(
across(c(col1, col2), ~ sum(.x, na.rm = TRUE)),
across(c(col3), ~ order_by(desc(ord), cumsum(.x)))
)
sql_list <- get_select_sql(lf$lazy_query$select, "mutate", op_vars(lf), simulate_sqlite())
expect_equal(sql_list$window_sql, sql("`win1` AS (PARTITION BY `part`)"))
expect_equal(
sql_list$select_sql,
sql(
part = ident("part"),
col1 = sql("SUM(`col1`) OVER `win1`"),
col2 = sql("SUM(`col2`) OVER `win1`"),
col3 = sql("SUM(`col3`) OVER (PARTITION BY `part` ORDER BY `ord` DESC ROWS UNBOUNDED PRECEDING)")
)
)
})
test_that("name windows only if supported", {
lf <- lazy_frame(
col1 = runif(3),
col2 = runif(3),
part = c("a", "a", "b"),
con = simulate_hana()
) %>%
group_by(part) %>%
transmute(
across(c(col1, col2), ~ sum(.x, na.rm = TRUE))
)
sql_list <- get_select_sql(lf$lazy_query$select, "mutate", op_vars(lf), simulate_hana())
expect_equal(sql_list$window_sql, character())
expect_equal(
sql_list$select_sql,
sql(
part = ident("part"),
col1 = sql("SUM(`col1`) OVER (PARTITION BY `part`)"),
col2 = sql("SUM(`col2`) OVER (PARTITION BY `part`)")
)
)
})
|