Skip to content
Open
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
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(())
}
2 changes: 1 addition & 1 deletion libpg_query
Submodule libpg_query updated 84 files
+8 −1 .github/workflows/ci.yml
+9 −0 CHANGELOG.md
+16 −8 Makefile
+3 −3 README.md
+2 −2 pg_query.h
+2 −2 scripts/extract_source.rb
+17 −4 scripts/pg_config_overrides.h
+23 −0 src/postgres/COPYRIGHT
+4 −0 src/postgres/include/access/amapi.h
+9 −0 src/postgres/include/access/genam.h
+3 −6 src/postgres/include/access/slru.h
+2 −1 src/postgres/include/access/tableam.h
+43 −0 src/postgres/include/access/transam.h
+1 −1 src/postgres/include/c.h
+4 −0 src/postgres/include/catalog/objectaddress.h
+6 −0 src/postgres/include/commands/event_trigger.h
+6 −52 src/postgres/include/common/hashfn_unstable.h
+1 −1 src/postgres/include/datatype/timestamp.h
+1 −1 src/postgres/include/executor/execdesc.h
+6 −3 src/postgres/include/libpq/libpq-be.h
+1 −0 src/postgres/include/mb/pg_wchar.h
+10 −2 src/postgres/include/miscadmin.h
+3 −0 src/postgres/include/nodes/execnodes.h
+4 −1 src/postgres/include/nodes/pathnodes.h
+1 −1 src/postgres/include/nodes/pg_list.h
+9 −5 src/postgres/include/nodes/primnodes.h
+3 −0 src/postgres/include/parser/parse_coerce.h
+28 −20 src/postgres/include/pg_config.h
+32 −1 src/postgres/include/port.h
+2 −2 src/postgres/include/port/pg_iovec.h
+0 −2 src/postgres/include/port/win32_port.h
+9 −18 src/postgres/include/replication/reorderbuffer.h
+5 −1 src/postgres/include/replication/slot.h
+2 −0 src/postgres/include/storage/lockdefs.h
+13 −16 src/postgres/include/storage/proc.h
+5 −2 src/postgres/include/storage/smgr.h
+1 −0 src/postgres/include/utils/catcache.h
+19 −0 src/postgres/include/utils/pgstat_internal.h
+1 −1 src/postgres/include/utils/portal.h
+5 −0 src/postgres/include/utils/syscache.h
+1 −1 src/postgres/src_backend_parser_gram.c
+47 −1 src/postgres/src_common_wchar.c
+14 −17 src/postgres/src_port_snprintf.c
+199 −156 src/postgres_deparse.c
+6 −1 test/deparse_tests.c
+12 −12 test/parse_opts_tests.c
+36 −36 test/parse_tests.c
+17 −0 test/sql/plpgsql_regress/plpgsql_call.sql
+12 −0 test/sql/plpgsql_regress/plpgsql_simple.sql
+7 −2 test/sql/postgres_regress/aggregates.sql
+0 −2 test/sql/postgres_regress/alter_table.sql
+22 −0 test/sql/postgres_regress/arrays.sql
+5 −0 test/sql/postgres_regress/case.sql
+59 −0 test/sql/postgres_regress/collate.icu.utf8.sql
+5 −0 test/sql/postgres_regress/conversion.sql
+15 −9 test/sql/postgres_regress/copy2.sql
+4 −0 test/sql/postgres_regress/copydml.sql
+10 −0 test/sql/postgres_regress/create_am.sql
+7 −0 test/sql/postgres_regress/database.sql
+11 −0 test/sql/postgres_regress/domain.sql
+44 −0 test/sql/postgres_regress/fast_default.sql
+72 −1 test/sql/postgres_regress/foreign_key.sql
+1 −0 test/sql/postgres_regress/horology.sql
+8 −0 test/sql/postgres_regress/inherit.sql
+13 −0 test/sql/postgres_regress/join.sql
+4 −0 test/sql/postgres_regress/jsonb_jsonpath.sql
+52 −0 test/sql/postgres_regress/merge.sql
+4 −0 test/sql/postgres_regress/partition_join.sql
+15 −0 test/sql/postgres_regress/partition_prune.sql
+46 −10 test/sql/postgres_regress/predicate.sql
+36 −0 test/sql/postgres_regress/privileges.sql
+24 −0 test/sql/postgres_regress/returning.sql
+58 −0 test/sql/postgres_regress/rowsecurity.sql
+25 −0 test/sql/postgres_regress/sqljson.sql
+4 −1 test/sql/postgres_regress/sqljson_queryfuncs.sql
+16 −0 test/sql/postgres_regress/stats.sql
+31 −0 test/sql/postgres_regress/subselect.sql
+6 −0 test/sql/postgres_regress/tablespace.sql
+2 −1 test/sql/postgres_regress/timestamptz.sql
+46 −0 test/sql/postgres_regress/triggers.sql
+13 −0 test/sql/postgres_regress/vacuum.sql
+7 −0 test/sql/postgres_regress/window.sql
+12 −0 test/sql/postgres_regress/with.sql
+7 −3 test/sql/postgres_regress/xml.sql
2 changes: 2 additions & 0 deletions src/node_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ impl NodeEnum {
}
s.index_params.iter().for_each(|n| {
if let Some(NodeEnum::IndexElem(n)) = n.node.as_ref() {
iter.push((n.to_ref(), depth, Context::DDL, false));

if let Some(n) = n.expr.as_ref().and_then(|n| n.node.as_ref()) {
iter.push((n.to_ref(), depth, Context::DDL, false));
}
Expand Down
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