Skip to content

Commit

Permalink
feat!: rewrite the prql_compile() function
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Sep 20, 2024
1 parent dde9f1b commit cefc8e8
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 84 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Config/Needs/dev:
rhub
Config/Needs/website:
pkgdown
Config/prqlr/LibVersion: 0.13.0
Config/prqlr/LibVersion: 0.13.1
4 changes: 2 additions & 2 deletions R/000-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ NULL
#' @param signature_comment a logical flag. Whether to add a signature comment to the output SQL query.
#' @return a list contains a SQL string or an error message.
#' @noRd
`compile` <- function(`prql_query`, `target`, `format`, `signature_comment`) {
.Call(savvy_compile__impl, `prql_query`, `target`, `format`, `signature_comment`)
`compile` <- function(`prql_query`, `target`, `format`, `signature_comment`, `display`) {
.Call(savvy_compile__impl, `prql_query`, `target`, `format`, `signature_comment`, `display`)
}

#' @noRd
Expand Down
28 changes: 19 additions & 9 deletions R/compile.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#' @title Compile a PRQL query into a SQL query
#' @param prql_query a PRQL query string.
#' @param target a compile target name to use. If not specified (`NULL`),
#' the target contained in the query will be used.
#' All available target names can be listed with the [prql_get_targets] function.
#' @param format a logical flag (default: `TRUE`). Whether to format the SQL query.
#' @param prql_query A character of PRQL query.
#' @param target A character of the target name to use or `NULL`.
#' If `NULL`, the target contained in the query will be used.
#' All available target names can be listed with the [prql_get_targets()] function.
#' @param ... Ignored.
#' @param format A logical flag (default: `TRUE`). Whether to format the SQL query.
#' @param signature_comment a logical flag. (default: `TRUE`).
#' Whether to add a signature comment to the output SQL query.
#' @return a SQL query string
#' @seealso [prql_get_targets]
#' @param display A character, one of `"plain"` (default) or `"ansi_color"`.
#' If `"ansi_color"`, error will be displayed with ANSI color.
#' @return A character of the compiled SQL query.
#' @examples
#' "from mtcars | filter cyl > 6 | select {cyl, mpg}" |>
#' prql_compile()
Expand Down Expand Up @@ -37,10 +39,18 @@
#' @export
prql_compile <- function(
prql_query,
...,
target = getOption("prqlr.target", default = NULL),
format = getOption("prqlr.format", default = TRUE),
signature_comment = getOption("prqlr.signature_comment", default = TRUE)) {
compile(prql_query, target %||% "sql.any", format, signature_comment)
signature_comment = getOption("prqlr.signature_comment", default = TRUE),
display = getOption("prqlr.display", default = "plain")) {
compile(
prql_query,
target = target %||% "sql.any",
format = format,
signature_comment = signature_comment,
display = display
)
}

#' @title prqlc's version
Expand Down
24 changes: 14 additions & 10 deletions man/prql_compile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ SEXP handle_result(SEXP res_) {
return (SEXP)res;
}

SEXP savvy_compile__impl(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment) {
SEXP res = savvy_compile__ffi(c_arg__prql_query, c_arg__target, c_arg__format, c_arg__signature_comment);
SEXP savvy_compile__impl(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment, SEXP c_arg__display) {
SEXP res = savvy_compile__ffi(c_arg__prql_query, c_arg__target, c_arg__format, c_arg__signature_comment, c_arg__display);
return handle_result(res);
}

Expand Down Expand Up @@ -66,7 +66,7 @@ SEXP savvy_prql_get_targets__impl(void) {


static const R_CallMethodDef CallEntries[] = {
{"savvy_compile__impl", (DL_FUNC) &savvy_compile__impl, 4},
{"savvy_compile__impl", (DL_FUNC) &savvy_compile__impl, 5},
{"savvy_prql_to_pl__impl", (DL_FUNC) &savvy_prql_to_pl__impl, 1},
{"savvy_pl_to_rq__impl", (DL_FUNC) &savvy_pl_to_rq__impl, 1},
{"savvy_rq_to_sql__impl", (DL_FUNC) &savvy_rq_to_sql__impl, 1},
Expand Down
3 changes: 1 addition & 2 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prqlr"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
rust-version = "1.69"
publish = false
Expand All @@ -15,4 +15,3 @@ savvy = "0.6.8"
# prqlc 0.13.0 is not compatible with Rust 1.69.0, so a slightly modified version is installed
# See https://github.com/PRQL/prql/pull/4916
prqlc = { git = "https://github.com/PRQL/prql", rev = "c7bd7a6fc73040394ffbbd85cea2ed6f986fd9dd", default-features = false }
anstream = { version = "0", features = ["auto"] }
2 changes: 1 addition & 1 deletion src/rust/api.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SEXP savvy_compile__ffi(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment);
SEXP savvy_compile__ffi(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment, SEXP c_arg__display);
SEXP savvy_prql_to_pl__ffi(SEXP c_arg__prql_query);
SEXP savvy_pl_to_rq__ffi(SEXP c_arg__pl_json);
SEXP savvy_rq_to_sql__ffi(SEXP c_arg__rq_json);
Expand Down
38 changes: 15 additions & 23 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anstream::ColorChoice;
use prqlc::ErrorMessages;
use savvy::{savvy, Sexp};
use std::str::FromStr;

Expand All @@ -15,48 +15,40 @@ pub fn compile(
target: &str,
format: bool,
signature_comment: bool,
display: &str,
) -> savvy::Result<Sexp> {
let options = convert_options(CompileOptions {
format,
target: target.to_string(),
signature_comment,
});

let result = options
.and_then(|opts| {
Ok(prql_query)
.and_then(prqlc::prql_to_pl)
.and_then(prqlc::pl_to_rq)
.and_then(|rq| prqlc::rq_to_sql(rq, &opts))
})
.map_err(|e| e.composed(&prql_query.into()));

ColorChoice::write_global(self::ColorChoice::Never);
display: display.to_string(),
})
.map_err(|e| e.to_string())?;

match result {
Ok(msg) => msg.try_into(),
Err(e) => Err(e.to_string().into()),
}
prqlc::compile(prql_query, &options)
.map_err(|e| savvy::Error::from(e.to_string()))
.and_then(|x| x.try_into())
}

struct CompileOptions {
format: bool,
target: String,
signature_comment: bool,
display: String,
}

fn convert_options(
o: CompileOptions,
) -> core::result::Result<prqlc::Options, prqlc::ErrorMessages> {
let target = prqlc::Target::from_str(&o.target).map_err(prqlc::ErrorMessages::from)?;
fn convert_options(o: CompileOptions) -> core::result::Result<prqlc::Options, ErrorMessages> {
let target = prqlc::Target::from_str(&o.target).map_err(ErrorMessages::from)?;
let display = prqlc::DisplayOptions::from_str(&o.display).map_err(|e| ErrorMessages {
inner: vec![prqlc::Error::new_simple(format!("Invalid display option: {}", e)).into()],
})?;

// TODO: support `display` option
Ok(prqlc::Options {
format: o.format,
target,
signature_comment: o.signature_comment,
color: false,
..std::default::Default::default()
display,
})
}

Expand Down
37 changes: 19 additions & 18 deletions tests/testthat/_snaps/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
---

Code
cat(prql_compile("from a | select {b}", NULL, FALSE, FALSE))
cat(prql_compile("from a | select {b}", target = NULL, format = FALSE,
signature_comment = FALSE))
Output
SELECT b FROM a

---

Code
cat(compile(
cat(prql_compile(
"from star_wars\n select {star_wars.*}\n select !{jar_jar_binks, midichlorians}",
"sql.duckdb", TRUE, TRUE))
target = "sql.duckdb", format = TRUE, signature_comment = TRUE))
Output
SELECT
* EXCLUDE (jar_jar_binks, midichlorians)
Expand All @@ -34,7 +35,7 @@
# Syntax error query=Mississippi has four S’s and four I’s.

Code
cat(prql_compile(query, "sql.any", TRUE, FALSE))
cat(prql_compile(query, target = "sql.any", format = TRUE, signature_comment = FALSE))
Condition
Error:
! Error:
Expand Down Expand Up @@ -62,7 +63,7 @@
# Syntax error query=from a | select [b]

Code
cat(prql_compile(query, "sql.any", TRUE, FALSE))
cat(prql_compile(query, target = "sql.any", format = TRUE, signature_comment = FALSE))
Condition
Error:
! Error:
Expand All @@ -76,7 +77,7 @@
# Syntax error query=from a | select {{{b

Code
cat(prql_compile(query, "sql.any", TRUE, FALSE))
cat(prql_compile(query, target = "sql.any", format = TRUE, signature_comment = FALSE))
Condition
Error:
! Error:
Expand All @@ -90,7 +91,7 @@
# Targets target=sql.any

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -114,7 +115,7 @@
# Targets target=sql.ansi

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -138,7 +139,7 @@
# Targets target=sql.bigquery

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -162,7 +163,7 @@
# Targets target=sql.clickhouse

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -186,7 +187,7 @@
# Targets target=sql.duckdb

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -210,7 +211,7 @@
# Targets target=sql.generic

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -234,7 +235,7 @@
# Targets target=sql.glaredb

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -258,7 +259,7 @@
# Targets target=sql.mssql

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -282,7 +283,7 @@
# Targets target=sql.mysql

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -306,7 +307,7 @@
# Targets target=sql.postgres

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -330,7 +331,7 @@
# Targets target=sql.sqlite

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand All @@ -354,7 +355,7 @@
# Targets target=sql.snowflake

Code
cat(prql_compile(query, target, TRUE, FALSE))
cat(prql_compile(query, target = target, format = TRUE, signature_comment = FALSE))
Output
SELECT
origin,
Expand Down
Loading

0 comments on commit cefc8e8

Please sign in to comment.