-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
104 lines (76 loc) · 2.61 KB
/
README.Rmd
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
---
output:
github_document:
html_preview: false
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# polarssql
<!-- badges: start -->
[![polarssql status badge](https://rpolars.r-universe.dev/badges/polarssql)](https://rpolars.r-universe.dev)
[![CRAN status](https://www.r-pkg.org/badges/version/polarssql)](https://CRAN.R-project.org/package=polarssql)
<!-- badges: end -->
`{polarssql}` is an experimental DBI-compliant interface to [Polars](https://www.pola.rs/).
Polars is not an actual database, so does not support full `{DBI}` functionality.
Please check [the Polars User Guide](https://pola-rs.github.io/polars/user-guide/sql/intro/)
for supported SQL features.
## Installation
The [`polars`](https://rpolars.github.io/) R package and `{polarssql}`
can be installed from [R-universe](https://rpolars.r-universe.dev/):
```r
Sys.setenv(NOT_CRAN = "true") # for installing the polars package with pre-built binary
install.packages("polarssql", repos = c("https://rpolars.r-universe.dev", getOption("repos")))
```
## Example
```{r}
library(DBI)
con <- dbConnect(polarssql::polarssql())
dbWriteTable(con, "mtcars", mtcars)
# We can fetch all results:
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(res)
# Clear the result
dbClearResult(res)
# Or a chunk at a time
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
while (!dbHasCompleted(res)) {
chunk <- dbFetch(res, n = 5)
print(nrow(chunk))
}
# Clear the result
dbClearResult(res)
# We can use table functions to read files directly:
tf <- tempfile(fileext = ".parquet")
on.exit(unlink(tf))
polars::as_polars_lf(mtcars)$sink_parquet(tf)
dbGetQuery(con, paste0("SELECT * FROM read_parquet('", tf, "') ORDER BY mpg DESC LIMIT 3"))
```
`{polarssql}` also provides functions that are simpler to use, inspired by the `{duckdb}` package,
```{r}
library(polarssql)
# These functions use the built-in connection by default, so we don't need to specify connection
# Resgister a data.frame to the built-in connection
polarssql_register(df = mtcars)
# Get the query result as a polars LazyFrame
polarssql_query("SELECT * FROM df WHERE cyl = 4")
# Unregister the table
polarssql_unregister("df")
```
And, basic supports for `{dbplyr}` is also implemented.
```{r}
library(dplyr, warn.conflicts = FALSE)
# Resgister a data.frame to the built-in connection, and query it via dbplyr
tbl_polarssql(mtcars) |>
filter(cyl == 4) |>
arrange(desc(mpg)) |>
head(3) |>
compute()
```