Skip to content

prql #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

prql #1078

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/prql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
index: true
sql:
gaia: ./lib/gaia-sample.parquet
---

# PRQL <a href="https://github.com/observablehq/framework/pull/1078" target="_blank" class="observablehq-version-badge" data-version="prerelease" title="Added in #1078"></a>

[PRQL](https://prql-lang.org/) (pronounced “prequel”) is a “modern language for transforming data — a simple, powerful, pipelined SQL replacement.” To use PRQL instead of SQL, create a PRQL fenced code block (<code>```prql</code>). For example:

````md
```prql
from gaia
sort {phot_g_mean_mag}
take 10
```
````

This produces:

```prql
from gaia
sort {phot_g_mean_mag}
take 10
```

Because PRQL is compiled to SQL during build, Framework does not support a `prql` tagged template literal; PRQL can only be used in PRQL code blocks.

## prql-js

You can load `prql-js` as:

````md run=false
<script src="npm:prql-js/dist/web/prql_js.js"></script>
<script>self.wasm_bindgen = wasm_bindgen;</script>
````

```js echo
const {compile} = wasm_bindgen;
await wasm_bindgen(import.meta.resolve("npm:prql-js/dist/web/prql_js_bg.wasm"));
```

Then to use it:

```js echo
compile("from employees\nfilter salary > $1\nselect first_name")
```

<script src="npm:prql-js/dist/web/prql_js.js"></script>
<script>self.wasm_bindgen = wasm_bindgen;</script>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"mime": "^4.0.0",
"minisearch": "^6.3.0",
"open": "^10.1.0",
"prql-js": "^0.11.3",
"rollup": "^4.6.0",
"rollup-plugin-esbuild": "^6.1.0",
"semver": "^7.5.4",
Expand Down
3 changes: 3 additions & 0 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {parseInfo} from "./info.js";
import type {JavaScriptNode} from "./javascript/parse.js";
import {parseJavaScript} from "./javascript/parse.js";
import {relativePath} from "./path.js";
import {transpilePrql} from "./prql.js";
import {transpileSql} from "./sql.js";
import {transpileTag} from "./tag.js";
import {InvalidThemeError} from "./theme.js";
Expand Down Expand Up @@ -60,6 +61,8 @@ function getLiveSource(content: string, tag: string, attributes: Record<string,
? transpileTag(content, "html.fragment", true)
: tag === "sql"
? transpileSql(content, attributes)
: tag === "prql"
? transpilePrql(content, attributes)
: tag === "svg"
? transpileTag(content, "svg.fragment", true)
: tag === "dot"
Expand Down
18 changes: 18 additions & 0 deletions src/prql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {CompileOptions, compile as prql2sql} from "prql-js";
import {transpileSql} from "./sql.js";

const prqlOptions = Object.assign(new CompileOptions(), {
target: "sql.duckdb",
format: false,
signature_comment: false
});

export function transpilePrql(content: string, attributes?: Record<string, string>): string {
try {
content = prql2sql(content, prqlOptions) ?? "";
} catch (error: any) {
const [message] = JSON.parse(error.message).inner;
throw new SyntaxError(message.display);
}
return transpileSql(content, attributes);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2965,6 +2965,11 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

prql-js@^0.11.3:
version "0.11.3"
resolved "https://registry.yarnpkg.com/prql-js/-/prql-js-0.11.3.tgz#17e6799144874003135a2d207d61368f3c07b719"
integrity sha512-Od8Glg/m2KaaQyeu9/xpZvHi/tUW5S6sp3pozDzhht+R6+JqAfLW0VUDT30nfNBQ9guuEABs1t5w+0CemUJC6A==

psl@^1.1.33:
version "1.9.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"
Expand Down