A Rust-based transpiler that converts R dplyr syntax to SQL queries.
libdplyr enables R users to write database queries using familiar dplyr syntax and converts them to efficient SQL for execution. It supports multiple SQL dialects (PostgreSQL, MySQL, SQLite, DuckDB) for use across various database environments.
- dplyr Syntax Support: Core support for
select(),distinct(),filter(),mutate(),arrange(),group_by(),summarise(), and limitedcount()/tally() - Pipeline Operations: Chain operations using the
%>%pipe operator - Multiple Dialects: PostgreSQL, MySQL, SQLite, DuckDB
- Performance: High-performance Rust implementation
- Dual Mode: Use as a Rust library or standalone CLI tool
Linux/macOS:
curl -sSL https://raw.githubusercontent.com/mrchypark/libdplyr/main/install.sh | bashWindows (PowerShell):
Irm https://raw.githubusercontent.com/mrchypark/libdplyr/main/install.ps1 | iexTry it out:
echo "select(name, age) %>% filter(age > 18)" | libdplyr --prettyNote: For detailed installation options, troubleshooting, and platform support, see the Installation Guide.
The most efficient way to use libdplyr is through stdin/stdout pipelines:
# Basic usage
echo "select(name, age) %>% filter(age > 18)" | libdplyr
# Specify dialect (postgres, mysql, sqlite, duckdb)
echo "select(name)" | libdplyr --dialect mysql
# Output formatting
echo "select(name)" | libdplyr --pretty
echo "select(name)" | libdplyr --json
echo "select(name)" | libdplyr --compactuse libdplyr::{Transpiler, PostgreSqlDialect};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let transpiler = Transpiler::new(Box::new(PostgreSqlDialect::new()));
let dplyr_code = "select(name, age) %>% filter(age > 18)";
let sql = transpiler.transpile(dplyr_code)?;
println!("{}", sql);
Ok(())
}DuckDB 1.5.x์์๋ SET allow_parser_override_extension = 'fallback';๋ก dplyr pipeline์ ๋ค์ดํฐ๋ธ AST๋ก ๋ณํํ ์ ์์ต๋๋ค. parser override๋ ํธ์ถ ์ธ์
์ ์์ ํ
์ด๋ธ๊ณผ ํธ๋์ญ์
์ ๊ทธ๋๋ก ์ฌ์ฉํฉ๋๋ค. ์์์ pipeline์ dplyr_pipe_syntax๋ parser override API์ ClientContext๊ฐ ์๋ ์ ์ฝ ๋๋ฌธ์ DB-global ๊ฐ๋ง ์ฝ์ผ๋ฉฐ, SET GLOBAL dplyr_pipe_syntax = 'native';์ฒ๋ผ ์ง์ ํฉ๋๋ค. session ๊ฐ์ ์์์ pipeline์ ์ํฅ์ ์ฃผ์ง ์์ผ๋ฉฐ, ์ฐ๊ฒฐ๋ณ ๋ฌธ๋ฒ์ด ํ์ํ๋ฉด dplyr(query, mode)๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ค์ ์ด ์์ผ๋ฉด DPLYR_PIPE_SYNTAX ํ๊ฒฝ ๋ณ์(๊ธฐ๋ณธ๊ฐ magrittr)๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์์ธํ ํธํ์ฑ ๋ฐ ์ค์ ๊ท์น์ submodule compatibility ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
libdplyr supports a wide range of dplyr verbs and R functions. Exact partial-support and rejection boundaries are documented in the dplyr syntax support matrix.
| Function | Description | Example |
|---|---|---|
select() |
Select/rename columns | select(id, name) |
distinct() |
Keep distinct rows or identifier columns | distinct(dept, role) |
filter() |
Filter rows | filter(age > 18) |
mutate() |
Create/modify columns | mutate(total = price * qty) |
rename() |
Rename columns | rename(new = old) |
arrange() |
Sort rows | arrange(desc(date)) |
group_by() |
Group rows | group_by(dept) |
summarise() |
Aggregate data | summarise(avg = mean(val)) |
count() / tally() |
Count rows by identifier-only keys or current groups | count(dept) |
*_join() |
Equality joins (inner, left, etc.) | left_join(other, by=c("id", "left"="right")) |
| Set Ops | union, intersect, setdiff | union(other) |
- Aggregation:
mean,sum,min,max,n,n_distinct,count,median,mode - Window:
row_number,rank,lead,lag,ntile - Math:
abs,sqrt,round,floor,log,exp - String:
tolower,toupper,substr,trimws - Logic:
ifelse,case_when,between,is.na,coalesce
let transpiler = Transpiler::new(Box::new(PostgreSqlDialect::new()));
let sql = transpiler.transpile("select(name) %>% filter(age > 18)")?;
// SELECT "name" FROM "data" WHERE "age" > 18let code = r#"
select(dept, salary) %>%
filter(salary > 50000) %>%
group_by(dept) %>%
summarise(avg_sal = mean(salary)) %>%
arrange(desc(avg_sal))
"#;libdplyr provides detailed error codes:
Exit 0: SuccessExit 4: Validation Error (syntax issues)Exit 5: Transpilation Error (generation failed)
Debug Mode:
libdplyr --verbose --debugFor more troubleshooting details, see INSTALL.md.
libdplyr is optimized for speed using Rust's zero-cost abstractions. Run benchmarks:
cargo bench# Setup
git clone https://github.com/mrchypark/libdplyr.git
cd libdplyr
cargo build
# Test
cargo test- Documentation: docs.rs/libdplyr
- Installation: INSTALL.md
- Issues: GitHub Issues
MIT License. See LICENSE.