Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rpolars/r-polarssql
A Polars backend for R DBI and dbplyr
https://github.com/rpolars/r-polarssql
dplyr dplyr-sql-backends polars r
Last synced: 3 months ago
JSON representation
A Polars backend for R DBI and dbplyr
- Host: GitHub
- URL: https://github.com/rpolars/r-polarssql
- Owner: rpolars
- License: other
- Created: 2023-11-09T15:50:07.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-05-25T15:39:45.000Z (6 months ago)
- Last Synced: 2024-06-30T09:16:10.100Z (4 months ago)
- Topics: dplyr, dplyr-sql-backends, polars, r
- Language: R
- Homepage: https://rpolars.github.io/r-polarssql/
- Size: 3.92 MB
- Stars: 21
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- awesome-polars - polarssql - `polarssql` experimental package which is a DBI-compliant interface to Polars. (Libraries/Packages/Scripts / R)
README
---
output:
github_document:
html_preview: false
---```{r}
#| include: falseknitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# polarssql
[![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)`{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()
```