-
Notifications
You must be signed in to change notification settings - Fork 9
/
db_22.R
51 lines (38 loc) · 1.3 KB
/
db_22.R
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
library(DBI)
library(dplyr)
### DuckDB + SQL showcase #######################################################
# Create data -------------------------------------------------------------------
arrow::write_parquet(nycflights13::flights, "flights.parquet")
con_memory <- dbConnect(duckdb::duckdb(), dbdir = ":memory:")
tbl <- duckdb::tbl_file(con_memory, "flights.parquet")
# Application: DBI <=> dbplyr and pivoting -------------------------------------------------
daily_flights_by_dest <-
tbl |>
count(year, month, day, dest)
daily_flights_by_dest
daily_flights_by_dest_sql <-
daily_flights_by_dest |>
dbplyr::sql_render()
daily_flights_by_dest_sql
pivot_sql <- paste0(
"PIVOT (", daily_flights_by_dest_sql, ") ON dest USING SUM(n)"
)
as_tibble(dbGetQuery(con_memory, pivot_sql))
system.time(
as_tibble(dbGetQuery(con_memory, pivot_sql))
)
system.time(
nycflights13::flights |>
count(year, month, day, dest) |>
tidyr::pivot_wider(names_from = dest, values_from = n, values_fill = 0)
)
write_pivot_sql <- paste0(
"COPY (", pivot_sql, ") TO 'pivot.parquet' (FORMAT PARQUET)"
)
dbExecute(con_memory, write_pivot_sql)
q_unpivot_dyn <-
"(SELECT * FROM (
UNPIVOT 'pivot.parquet'
ON COLUMNS(* EXCLUDE (year, month, day))
INTO NAME dest VALUE n))"
tbl(con_memory, from = q_unpivot_dyn)