Skip to content

Commit 0f2105f

Browse files
authored
feat!: rewrite the prql_compile() function (#317)
1 parent dde9f1b commit 0f2105f

File tree

14 files changed

+152
-113
lines changed

14 files changed

+152
-113
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Suggests:
2525
dplyr,
2626
testthat (>= 3.2.0),
2727
patrick,
28-
withr
28+
withr,
29+
cli
2930
License: MIT + file LICENSE
3031
Language: en-US
3132
Encoding: UTF-8
@@ -47,4 +48,4 @@ Config/Needs/dev:
4748
rhub
4849
Config/Needs/website:
4950
pkgdown
50-
Config/prqlr/LibVersion: 0.13.0
51+
Config/prqlr/LibVersion: 0.13.1

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# prqlr (development version)
22

3+
## Breaking changes
4+
5+
- For the `prql_compile()` funtion, arguments `format` and `singnature_comment` should be named arguments. (#317)
6+
7+
## New features
8+
9+
- New experimental argument `display` of `prql_compile()` function to colorize the error message from prqlc. (#317)
10+
311
# prqlr 0.9.0
412

513
## Breaking changes

R/000-wrappers.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ NULL
3030
#' @param signature_comment a logical flag. Whether to add a signature comment to the output SQL query.
3131
#' @return a list contains a SQL string or an error message.
3232
#' @noRd
33-
`compile` <- function(`prql_query`, `target`, `format`, `signature_comment`) {
34-
.Call(savvy_compile__impl, `prql_query`, `target`, `format`, `signature_comment`)
33+
`compile` <- function(`prql_query`, `target`, `format`, `signature_comment`, `display`) {
34+
.Call(savvy_compile__impl, `prql_query`, `target`, `format`, `signature_comment`, `display`)
3535
}
3636

3737
#' @noRd

R/compile.R

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#' @title Compile a PRQL query into a SQL query
2-
#' @param prql_query a PRQL query string.
3-
#' @param target a compile target name to use. If not specified (`NULL`),
4-
#' the target contained in the query will be used.
5-
#' All available target names can be listed with the [prql_get_targets] function.
6-
#' @param format a logical flag (default: `TRUE`). Whether to format the SQL query.
2+
#' @param prql_query A character of PRQL query.
3+
#' @param target A character of the target name to use or `NULL`.
4+
#' If `NULL`, the target contained in the query will be used.
5+
#' All available target names can be listed with the [prql_get_targets()] function.
6+
#' @param ... Ignored.
7+
#' @param format A logical flag (default: `TRUE`). Whether to format the SQL query.
78
#' @param signature_comment a logical flag. (default: `TRUE`).
89
#' Whether to add a signature comment to the output SQL query.
9-
#' @return a SQL query string
10-
#' @seealso [prql_get_targets]
10+
#' @param display A character, one of `"plain"` (default) or `"ansi_color"`.
11+
#' If `"ansi_color"`, error will be displayed with ANSI color.
12+
#' @return A character of the compiled SQL query.
1113
#' @examples
1214
#' "from mtcars | filter cyl > 6 | select {cyl, mpg}" |>
1315
#' prql_compile()
@@ -38,9 +40,17 @@
3840
prql_compile <- function(
3941
prql_query,
4042
target = getOption("prqlr.target", default = NULL),
43+
...,
4144
format = getOption("prqlr.format", default = TRUE),
42-
signature_comment = getOption("prqlr.signature_comment", default = TRUE)) {
43-
compile(prql_query, target %||% "sql.any", format, signature_comment)
45+
signature_comment = getOption("prqlr.signature_comment", default = TRUE),
46+
display = getOption("prqlr.display", default = "plain")) {
47+
compile(
48+
prql_query,
49+
target = target %||% "sql.any",
50+
format = format,
51+
signature_comment = signature_comment,
52+
display = display
53+
)
4454
}
4555

4656
#' @title prqlc's version

R/knitr-engine.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# TODO: support the `error=TRUE` option and support `ansi_color`ed error message with the `cli` package.
12
#' @title PRQL knitr engine
23
#' @description
34
#' If options$connection is NULL, the output is SQL query.
@@ -27,7 +28,8 @@ eng_prql <- function(options) {
2728
prql_compile(
2829
target = options$engine.opts[["target"]] %||% getOption("prqlr.target"),
2930
format = TRUE,
30-
signature_comment = options$engine.opts[["signature_comment"]] %||% getOption("prqlr.signature_comment", TRUE)
31+
signature_comment = options$engine.opts[["signature_comment"]] %||% getOption("prqlr.signature_comment", TRUE),
32+
display = "plain"
3133
)
3234

3335
# Prints a SQL code block if there is no connection

man/prql_compile.Rd

Lines changed: 14 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ SEXP handle_result(SEXP res_) {
3434
return (SEXP)res;
3535
}
3636

37-
SEXP savvy_compile__impl(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment) {
38-
SEXP res = savvy_compile__ffi(c_arg__prql_query, c_arg__target, c_arg__format, c_arg__signature_comment);
37+
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) {
38+
SEXP res = savvy_compile__ffi(c_arg__prql_query, c_arg__target, c_arg__format, c_arg__signature_comment, c_arg__display);
3939
return handle_result(res);
4040
}
4141

@@ -66,7 +66,7 @@ SEXP savvy_prql_get_targets__impl(void) {
6666

6767

6868
static const R_CallMethodDef CallEntries[] = {
69-
{"savvy_compile__impl", (DL_FUNC) &savvy_compile__impl, 4},
69+
{"savvy_compile__impl", (DL_FUNC) &savvy_compile__impl, 5},
7070
{"savvy_prql_to_pl__impl", (DL_FUNC) &savvy_prql_to_pl__impl, 1},
7171
{"savvy_pl_to_rq__impl", (DL_FUNC) &savvy_pl_to_rq__impl, 1},
7272
{"savvy_rq_to_sql__impl", (DL_FUNC) &savvy_rq_to_sql__impl, 1},

src/rust/Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prqlr"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
edition = "2021"
55
rust-version = "1.69"
66
publish = false
@@ -15,4 +15,3 @@ savvy = "0.6.8"
1515
# prqlc 0.13.0 is not compatible with Rust 1.69.0, so a slightly modified version is installed
1616
# See https://github.com/PRQL/prql/pull/4916
1717
prqlc = { git = "https://github.com/PRQL/prql", rev = "c7bd7a6fc73040394ffbbd85cea2ed6f986fd9dd", default-features = false }
18-
anstream = { version = "0", features = ["auto"] }

src/rust/api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SEXP savvy_compile__ffi(SEXP c_arg__prql_query, SEXP c_arg__target, SEXP c_arg__format, SEXP c_arg__signature_comment);
1+
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);
22
SEXP savvy_prql_to_pl__ffi(SEXP c_arg__prql_query);
33
SEXP savvy_pl_to_rq__ffi(SEXP c_arg__pl_json);
44
SEXP savvy_rq_to_sql__ffi(SEXP c_arg__rq_json);

0 commit comments

Comments
 (0)