Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[env]
# Increase default stack size for tests due to deep recursion in parse tree conversion
RUST_MIN_STACK = "16777216"
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ regex = "1.6.0"
[[bench]]
name = "parse_vs_summary"
harness = false

# Optimize build scripts even in debug mode to reduce stack usage.
# This is needed because bindgen uses deep recursion when processing
# PostgreSQL header files, which can overflow the stack on Windows.
[profile.dev.build-override]
opt-level = 2
376 changes: 375 additions & 1 deletion build.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libpg_query
Submodule libpg_query updated 65 files
+26 −0 CHANGELOG.md
+11 −7 Makefile
+5 −1 Makefile.msvc
+2 −2 README.md
+47 −2 pg_query.h
+17 −0 pg_query_raw.h
+182 −0 src/pg_query_deparse.c
+70 −0 src/pg_query_is_utility_stmt.c
+44 −0 src/pg_query_parse.c
+11 −10 src/pg_query_summary.c
+10 −7 src/pg_query_summary_truncate.c
+1 −1 src/postgres/COPYRIGHT
+1 −1 src/postgres/include/access/amapi.h
+1 −1 src/postgres/include/access/slru.h
+11 −4 src/postgres/include/access/tableam.h
+1 −0 src/postgres/include/access/xlog.h
+2 −1 src/postgres/include/access/xlogdefs.h
+1 −1 src/postgres/include/commands/defrem.h
+18 −0 src/postgres/include/commands/trigger.h
+4 −0 src/postgres/include/executor/executor.h
+2 −1 src/postgres/include/miscadmin.h
+6 −8 src/postgres/include/nodes/execnodes.h
+10 −9 src/postgres/include/pg_config.h
+10 −2 src/postgres/include/port/atomics/generic-gcc.h
+9 −3 src/postgres/include/port/pg_iovec.h
+25 −9 src/postgres/include/replication/reorderbuffer.h
+2 −0 src/postgres/include/replication/slot.h
+1 −0 src/postgres/include/utils/elog.h
+1 −1 src/postgres/include/utils/guc.h
+0 −2 src/postgres/include/utils/guc_hooks.h
+5 −0 src/postgres/include/utils/pg_locale.h
+739 −732 src/postgres/src_backend_parser_gram.c
+11 −0 src/postgres/src_backend_utils_error_elog.c
+22 −7 src/postgres/src_backend_utils_mmgr_alignedalloc.c
+3 −3 src/postgres/src_backend_utils_mmgr_aset.c
+1 −1 src/postgres/src_backend_utils_mmgr_bump.c
+33 −8 test/framework/main.c
+6 −3 test/framework/main.h
+125 −0 test/is_utility_stmt.c
+12 −12 test/parse_opts_tests.c
+36 −36 test/parse_tests.c
+30 −2 test/sql/postgres_regress/alter_table.sql
+14 −1 test/sql/postgres_regress/collate.icu.utf8.sql
+11 −0 test/sql/postgres_regress/constraints.sql
+1 −1 test/sql/postgres_regress/create_index.sql
+8 −0 test/sql/postgres_regress/create_table.sql
+62 −9 test/sql/postgres_regress/event_trigger.sql
+48 −0 test/sql/postgres_regress/foreign_key.sql
+32 −0 test/sql/postgres_regress/generated.sql
+4 −1 test/sql/postgres_regress/limit.sql
+49 −0 test/sql/postgres_regress/merge.sql
+24 −3 test/sql/postgres_regress/privileges.sql
+2 −0 test/sql/postgres_regress/psql.sql
+79 −0 test/sql/postgres_regress/publication.sql
+50 −1 test/sql/postgres_regress/rowsecurity.sql
+93 −0 test/sql/postgres_regress/stats_ext.sql
+23 −0 test/sql/postgres_regress/strings.sql
+26 −0 test/sql/postgres_regress/subselect.sql
+10 −0 test/sql/postgres_regress/triggers.sql
+14 −0 test/sql/postgres_regress/with.sql
+2 −0 test/sql/postgres_regress/xml.sql
+1 −1 test/summary.c
+11 −0 test/summary_tests.c
+2 −0 test/summary_tests_list.c
+1 −1 test/summary_truncate.c
12 changes: 12 additions & 0 deletions src/bindings_raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Raw FFI bindings for PostgreSQL parse tree types.
//!
//! These bindings provide direct access to PostgreSQL's internal node structures,
//! allowing us to convert them to Rust types without going through protobuf serialization.

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]
#![allow(clippy::all)]
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/bindings_raw.rs"));
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
//!

mod bindings;
mod bindings_raw;
mod error;
mod node_enum;
mod node_mut;
Expand All @@ -48,6 +49,8 @@ mod parse_result;
#[rustfmt::skip]
pub mod protobuf;
mod query;
mod raw_deparse;
mod raw_parse;
mod summary;
mod summary_result;
mod truncate;
Expand All @@ -58,12 +61,17 @@ pub use node_mut::*;
pub use node_ref::*;
pub use parse_result::*;
pub use query::*;
pub use raw_deparse::deparse_raw;
pub use raw_parse::parse_raw;
pub use summary::*;
pub use summary_result::*;
pub use truncate::*;

pub use protobuf::Node;

/// PostgreSQL version number (e.g., 170007 for PostgreSQL 17.0.7)
pub use bindings::PG_VERSION_NUM;

// From Postgres source: src/include/storage/lockdefs.h
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
Expand Down
7 changes: 7 additions & 0 deletions src/node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ impl<'a> NodeRef<'a> {
})
}

pub fn deparse_raw(&self) -> Result<String> {
crate::deparse_raw(&protobuf::ParseResult {
version: crate::bindings::PG_VERSION_NUM as i32,
stmts: vec![protobuf::RawStmt { stmt: Some(Box::new(Node { node: Some(self.to_enum()) })), stmt_location: 0, stmt_len: 0 }],
})
}

pub fn to_enum(&self) -> NodeEnum {
match self {
NodeRef::Alias(n) => NodeEnum::Alias((*n).clone()),
Expand Down
9 changes: 9 additions & 0 deletions src/parse_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl protobuf::ParseResult {
crate::deparse(self)
}

pub fn deparse_raw(&self) -> Result<String> {
crate::deparse_raw(self)
}

// Note: this doesn't iterate over every possible node type, since we only care about a subset of nodes.
pub fn nodes(&self) -> Vec<(NodeRef<'_>, i32, Context, bool)> {
self.stmts
Expand Down Expand Up @@ -253,6 +257,11 @@ impl ParseResult {
crate::deparse(&self.protobuf)
}

/// Converts the parsed query back into a SQL string (bypasses protobuf serialization)
pub fn deparse_raw(&self) -> Result<String> {
crate::deparse_raw(&self.protobuf)
}

/// Intelligently truncates queries to a max length.
///
/// # Example
Expand Down
2,953 changes: 2,953 additions & 0 deletions src/raw_deparse.rs

Large diffs are not rendered by default.

Loading