Skip to content
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

PRQL #1075

Open
mbostock opened this issue Mar 15, 2024 · 6 comments · May be fixed by #1078
Open

PRQL #1075

mbostock opened this issue Mar 15, 2024 · 6 comments · May be fixed by #1078
Labels
enhancement New feature or request

Comments

@mbostock
Copy link
Member

It compiles to SQL, so it should be a pretty easy lift, maybe?

https://prql-lang.org/

@mbostock mbostock added the enhancement New feature or request label Mar 15, 2024
@mbostock mbostock linked a pull request Mar 16, 2024 that will close this issue
@mbostock
Copy link
Member Author

To load it client-side:

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

```js
const {compile} = wasm_bindgen;
await wasm_bindgen(import.meta.resolve("npm:prql-js/dist/web/prql_js_bg.wasm"));
const sql = compile("from employees | select first_name");
display(sql);
```

@max-sixty
Copy link

Hi from the PRQL team — let us know if we can be helpful here! I saw on a linked issue that a release would be helpful, so we can do one of those shortly...

@mbostock
Copy link
Member Author

Thanks @max-sixty! An ESM release is very welcome. As far as the integration we’re prototyping in #1078, the main outstanding task for us is transpiling PRQL to SQL while preserving placeholders. We have as input PRQL with interpolated expressions such as this:

from gaia
filter phot_g_mean_mag < ${mag}
sort {phot_g_mean_mag}
take 10

Our goal is to rewrite this as SQL with interpolated expressions in the correct place, like this:

SELECT *
FROM gaia
WHERE phot_g_mean_mag < ${mag}
ORDER BY phot_g_mean_mag
LIMIT 10

My current plan is to pass the following to compile:

from gaia
filter phot_g_mean_mag < $1
sort {phot_g_mean_mag}
take 10

Then take the output SQL:

SELECT *
FROM gaia
WHERE phot_g_mean_mag < $1
ORDER BY phot_g_mean_mag
LIMIT 10

And re-insert the original placeholders (${mag}) in the appropriate places ($1), producing the desired templated SQL:

SELECT *
FROM gaia
WHERE phot_g_mean_mag < ${mag}
ORDER BY phot_g_mean_mag
LIMIT 10

The approach assumes that PRQL’s compile maintains the cardinality of each placeholder, so that if the input PRQL references $1 once, it output SQL references $1 exactly once too (though I don’t assume that $1, $2, etc. are necessarily in the same order in the output SQL)… but I can workaround that if this assumption is false. If you have any thoughts on this approach, advice is welcome. 🙏

@max-sixty
Copy link

Great @mbostock !

While PRQL doesn't support ${mag}, it does support $mag, so that might be a bit easier than translating to $1 and then back again. Let us know if you hit any issues with that!

@mbostock
Copy link
Member Author

Nice to know, @max-sixty. I was just using mag as a placeholder for an expression. It can be an arbitrary JavaScript expression such as ${await fetch("https://example.com").then((response) => response.json())} so using the numeric placeholders is perfect for tracking the identity of placeholders through transpilation.

@max-sixty
Copy link

Ah great @mbostock , thanks for explaining. That works well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants