Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(WIP) Adds deparse support (AST -> SQL).
Background
pg-parser uses libpg_query for core parsing logic (Postgres C code compiled to WASM). libpg_query supports either JSON or protobuf as the output format for SQL -> AST parsing, but only protobuf input for AST -> SQL deparsing (no JSON input). Since our library intends to only work with JSON, this makes deparse challenging.
To make deparse work, we need to convert to/from protobufs. Our options are:
Use a JS protobuf library like protobuf.js to convert the input JSON to protobuf. Note protobuf.js does not support the
json_name
proto field option, but this PR does. So we'd have to use a forked version of protobuf.js to make this work.Pros
Cons
Convert to/from protobuf in C.
Pros
Cons
json_name
field, so need to add thisThis PR implement approach 2.
Implementation
Since protobuf2json-c doesn't work with libpg_query's proto3 file, we copy an embedded version of this library directly into this codebase and modify it to work with proto3. This gives us more control over the conversion logic and allows us to strip out functions we don't need (like
json2protobuf-file()
). After battle testing we can upstream these changes back if the original maintainer is open to it.Like the protobuf.js library, protobuf-c also doesn't support the
json_name
field option yet, which libpg_query heavily uses. We need this to correctly convert to/from JSON. I've created a PR on protobuf-c that adds support forjson_name
. Assuming this gets merged, I will create a PR on libpg_query that uses this new option when generating protobuf-c code. In the mean time, libpg_query already re-generates protobuf-c files during its build pipeline, so we can use the above forked version of protobuf-c in our own Docker container to produce the required protobuf C code that includes thejson_name
field.Current status
The embedded protobuf2json lib has been largely modified to support proto3, but there are still some bugs I'm working through.