-
Notifications
You must be signed in to change notification settings - Fork 185
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
Add support for reading in JSON-based nodes format (to run pg_query_deparse) #178
Comments
@jgoux Can you share more about the use case? Generally the idea is that you'd parse, modify the parse tree, and then deparse - and protobuf is the way the parsetree gets passed back and forth. |
@lfittl libpg-query-node seems to rely on pg_query_parse. Reading the tests of libpg_query it doesn't seem possible to pass the result of pg_query_parse to So I'm wondering why is there a Does it mean that libpg-query-node should rely on My use case is really just to be able to pass what I obtain from |
I guess what I want is the equivalent of libpg_query/src/pg_query_parse.c Line 101 in 1ada550
|
Ah, thanks for clarifying! There is currently no support for reading in the JSON format (to turn it into C structs again, which are used by the deparser) - that was part of the motivation for the Protobuf work, since its a bit more straightforward to read than JSON. Its not impossible to add this, but its a lot more work than just exposing a function, as what would be needed is essentially Note that matters are complicated a bit by the fact that we currently don't have a JSON parser bundled in the C library. This may be something we can add in a future version, as Postgres 16 had major rework of how the out/read/copy funcs code works, and it may get easier to add extra formats with that work in place. But for now this is not a priority on our end, though a PR would of course be welcome. |
Just so I'm sure I'm understanding how the protobuf work impacts the consumption of libpg_query. Is it an implementation detail? For example, if I would like to expose import { parse, deparse } from "libpg-query";
const tree = parse("SELECT 1;");
// tree here would be a normal JS object
console.log(tree.stmts[0].stmt.SelectStmt);
// we pass the normal JS object
const query = deparse(tree);
// output "select 1"
console.log(query) Or is the JSON-based nodes format necessary to have such an API from the JS/TS world? |
You would have to have a step that turns the Protobuf into Javascript objects (e.g. using a library like protobufjs), and then does the same in reverse before passing things back to libpg_query. For reference, the protobuf definition can be found here: https://github.com/pganalyze/libpg_query/blob/15-latest/protobuf/pg_query.proto#L17 And you can e.g. look at the Ruby library for how it does that: parse, deparse (note how the Ruby object "tree" gets passed to the auto-generated Protobuf message |
Excellent, exactly what I was looking for, thanks for the pointer @lfittl! 😄 |
Hello 👋,
Would it be possible to expose a
pg_query_deparse
which would directly work on the result ofpg_query_parse
(so skipping the protobuf layer)?My goal would be to have the deparsing capability of libpg_query exposed to the JavaScript world through libpg-query-node.
I'm not a C programmer, so I'm not sure about the possibility/difficulty of the task. 😅
The text was updated successfully, but these errors were encountered: