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

Support parsing complex queries with deeply nested ASTs #17

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- name: Install protobuf-compiler
run: sudo apt-get install -y protobuf-compiler

- uses: actions/checkout@v4
with:
submodules: recursive
Expand Down Expand Up @@ -68,6 +71,9 @@ jobs:
name: Check file formatting and style
runs-on: ubuntu-latest
steps:
- name: Install protobuf-compiler
run: sudo apt-get install -y protobuf-compiler

- uses: actions/checkout@v4
with:
submodules: recursive
Expand Down
45 changes: 21 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ repository = "https://github.com/pganalyze/pg_query.rs"

[dependencies]
itertools = "0.10.3"
prost = "0.10.4"
prost = { git = "https://github.com/pganalyze/prost", branch = "recursion-limit-macro" }
serde = { version = "1.0.139", features = ["derive"] }
serde_json = "1.0.82"
thiserror = "1.0.31"

[build-dependencies]
bindgen = "0.66.1"
clippy = { version = "0.0.302", optional = true }
prost-build = "0.10.4"
prost-build = { git = "https://github.com/pganalyze/prost", branch = "recursion-limit-macro" }
fs_extra = "1.2.0"
cc = "1.0.83"
glob = "0.3.1"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ let result = pg_query::parse(query).unwrap();
assert_eq!(result.truncate(32).unwrap(), "INSERT INTO x (...) VALUES (...)");
```

## Caveats

When parsing very complex queries you may run into a stack overflow. This can be worked around by using a thread with a custom stack size ([stdlib](https://doc.rust-lang.org/std/thread/index.html#stack-size), [tokio](https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.thread_stack_size)), or using the stacker crate to resize the main thread's stack:

```rust
stacker::grow(20 * 1024 * 1024, || pg_query::parse(query))
```

However, a sufficiently complex query could still run into a stack overflow after you increase the stack size. With some work it may be possible to add an adapter API to the prost crate in order to dynamically increase the stack size as needed like [serde_stacker](https://crates.io/crates/serde_stacker) does (if anyone wants to take that on).

## Credits

Thanks to [Paul Mason](https://github.com/paupino) for his work on [pg_parse](https://github.com/paupino/pg_parse) that this crate is based on.
Expand Down
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.write_to_file(out_dir.join("bindings.rs"))?;

// Generate the protobuf definition
prost_build::compile_protos(&[&out_protobuf_path.join(LIBRARY_NAME).with_extension("proto")], &[&out_protobuf_path])?;
let mut config = prost_build::Config::new();
config.recursion_limit("ParseResult", 1000);
config.compile_protos(&[&out_protobuf_path.join(LIBRARY_NAME).with_extension("proto")], &[&out_protobuf_path])?;

Ok(())
}
5 changes: 4 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub fn parse(statement: &str) -> Result<ParseResult> {
} else {
let data = unsafe { std::slice::from_raw_parts(result.parse_tree.data as *const u8, result.parse_tree.len as usize) };
let stderr = unsafe { CStr::from_ptr(result.stderr_buffer) }.to_string_lossy().to_string();
protobuf::ParseResult::decode(data).map_err(Error::Decode).map(|result| ParseResult::new(result, stderr))
match protobuf::ParseResult::decode(data) {
Ok(result) => Ok(ParseResult::new(result, stderr)),
Err(error) => Err(Error::Decode(error)),
}
};
unsafe { pg_query_free_protobuf_parse_result(result) };
parse_result
Expand Down
13 changes: 8 additions & 5 deletions tests/parse_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#[cfg(test)]
use itertools::sorted;

#[cfg(test)]
use std::thread::Builder;

use pg_query::{
parse,
protobuf::{self, a_const::Val},
Expand Down Expand Up @@ -31,15 +34,15 @@ fn it_handles_errors() {
}

#[test]
fn it_handles_recursion_error() {
fn it_handles_recursion_without_error_1() {
let query = "SELECT a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(b))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))";
parse(query).err().unwrap();
// TODO: unsure how to unwrap the private fields on a protobuf decode error
// assert_eq!(error, Error::Decode("recursion limit reached".into()));
let result = Builder::new().stack_size(20 * 1024 * 1024).spawn(move || parse(query)).unwrap().join().unwrap().unwrap();
assert_eq!(result.tables().len(), 0);
assert_eq!(result.statement_types(), ["SelectStmt"]);
}

#[test]
fn it_handles_recursion_without_error() {
fn it_handles_recursion_without_error_2() {
// The Ruby version of pg_query fails here because of Ruby protobuf limitations
let query = r#"SELECT * FROM "t0"
JOIN "t1" ON (1) JOIN "t2" ON (1) JOIN "t3" ON (1) JOIN "t4" ON (1) JOIN "t5" ON (1)
Expand Down
Loading