Skip to content

Commit

Permalink
Merge branch 'main' into parse-recursion-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlinsley committed Dec 2, 2024
2 parents c9334d5 + 23ab4ae commit 09db96a
Show file tree
Hide file tree
Showing 20 changed files with 459 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/data/* binary
73 changes: 44 additions & 29 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ name: Continuous integration
jobs:
ci:
env:
RUSTFLAGS: ${{ matrix.rust == 'nightly' && '-Z sanitizer=leak' || '' }}
runs-on: ubuntu-latest
RUSTFLAGS: ${{ matrix.rust == 'nightly' && matrix.os == 'ubuntu-latest' && '-Z sanitizer=leak' || '' }}
strategy:
fail-fast: false
matrix:
rust:
- stable
- nightly
os:
- ubuntu-latest
- windows-latest
include:
- rust: stable-x86_64-pc-windows-gnu
os: windows-latest
- rust: stable-i686-pc-windows-msvc
os: windows-latest
runs-on: ${{ matrix.os }}

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

- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive

Expand All @@ -34,24 +42,30 @@ jobs:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-${{ matrix.backend }}-cargo-${{ hashFiles('**/Cargo.toml') }}
key: ${{ matrix.os }}-${{ matrix.rust }}-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Install rustup if needed
run: |
if ! command -v rustup &>/dev/null; then
curl --proto '=https' --tlsv1.2 --retry 10 --retry-connrefused --location --silent --show-error --fail "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y
echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH
fi
if: runner.os != 'Windows'
shell: bash

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
run: rustup toolchain install ${{ matrix.rust }} --profile minimal --no-self-update
shell: bash

- name: Default to nightly if requested
run: rustup default nightly
if: matrix.rust == 'nightly'

- name: Build pg_query
uses: actions-rs/cargo@v1
with:
command: build
run: cargo build

- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
run: cargo test

check_style:
name: Check file formatting and style
Expand All @@ -60,16 +74,22 @@ jobs:
- name: Install protobuf-compiler
run: sudo apt-get install -y protobuf-compiler

- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive

- uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
components: clippy, rustfmt
override: true
- name: Install rustup if needed
run: |
if ! command -v rustup &>/dev/null; then
curl --proto '=https' --tlsv1.2 --retry 10 --retry-connrefused --location --silent --show-error --fail "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y
echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH
fi
if: runner.os != 'Windows'
shell: bash

- name: Install toolchain
run: rustup toolchain install stable --component clippy --component rustfmt --profile minimal --no-self-update
shell: bash

- name: Cache cargo registry
uses: actions/cache@v2
Expand All @@ -80,12 +100,7 @@ jobs:
key: clippy-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Check file formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
run: cargo fmt --all -- --check

- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy
run: cargo clippy
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.idea/
.DS_Store
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## 6.0.0 2024-11-26

* Upgrade to libpg_query 17-6.0.0
- Updates to the Postgres 17 parser
- Deparser improvements:
- Add support for deparsing `JSON_TABLE`, `JSON_QUERY`, `JSON_EXISTS`, `JSON_VALUE`
- Add support for deparsing `JSON`, `JSON_SCALAR`, `JSON_SERIALIZE`
- Add support for deparsing `COPY ... FORCE_NULL(*)`
- Add support for deparsing `ALTER COLUMN ... SET EXPRESSION AS`
- Add support for deparsing `SET STATISTICS DEFAULT`
- Add support for deparsing `SET ACCESS METHOD DEFAULT`
- Add support for deparsing `... AT LOCAL`
- Add support for deparsing `merge_action()`
- Add support for deparsing `MERGE ... RETURNING`
- Add support for deparsing `NOT MATCHED [ BY TARGET ]`

## 5.1.1 2024-10-30

* Make `ParseResult` struct public and implement `Debug`

## 5.1.0 2024-01-09

* Update to libpg_query 16-5.1.0
- Add support for running on Windows
- Add support for compiling on 32-bit systems
* Always build C library using "cc" crate
* Add `filter_columns` for getting columns that a query filters by
- This returns the table name (if present) and column name for every
column that's referenced in a JOIN or WHERE clause.


## 5.0.0 2023-12-22

* Align versioning scheme with that of other pg_query libraries
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pg_query"
description = "PostgreSQL parser that uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree."
version = "5.0.0"
version = "6.0.0"
edition = "2021"
documentation = "https://docs.rs/pg_query/"
build = "build.rs"
Expand All @@ -23,6 +23,8 @@ bindgen = "0.66.1"
clippy = { version = "0.0.302", optional = true }
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"

[dev-dependencies]
easy-parallel = "3.2.0"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pg_query.rs   [![Build Status]][actions] [![Latest Version]][crates.io] [![Docs Badge]][docs]
===========

[Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpganalyze%2Fpg_query%2Fbadge&label=build&logo=none
[actions]: https://actions-badge.atrox.dev/pganalyze/pg_query/goto
[Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpganalyze%2Fpg_query.rs%2Fbadge%3Fref%3Dmain&style=flat&label=build&logo=none
[actions]: https://actions-badge.atrox.dev/pganalyze/pg_query.rs/goto?ref=main
[Latest Version]: https://img.shields.io/crates/v/pg_query.svg
[crates.io]: https://crates.io/crates/pg_query
[Docs Badge]: https://docs.rs/pg_query/badge.svg
Expand All @@ -22,7 +22,7 @@ Add the following to your `Cargo.toml`

```toml
[dependencies]
pg_query = "5.0"
pg_query = "5.1"
```

## Examples
Expand Down
36 changes: 22 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
#![cfg_attr(feature = "clippy", plugin(clippy))]

use fs_extra::dir::CopyOptions;
use glob::glob;
use std::env;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

static SOURCE_DIRECTORY: &str = "libpg_query";
static LIBRARY_NAME: &str = "pg_query";

fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let build_path = Path::new(".").join(SOURCE_DIRECTORY);
let makefile_path = build_path.join("Makefile");
let out_header_path = out_dir.join(LIBRARY_NAME).with_extension("h");
let out_protobuf_path = out_dir.join("protobuf");
let target = env::var("TARGET").unwrap();

// Configure cargo through stdout
println!("cargo:rerun-if-changed={}", makefile_path.display()); // Includes version number
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={LIBRARY_NAME}");

Expand All @@ -35,19 +34,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fs_extra::copy_items(&source_paths, &out_dir, &copy_options)?;

// Compile the C library.
let mut make = Command::new("make");

make.env_remove("PROFILE").arg("-C").arg(&out_dir).arg("build");

if env::var("PROFILE").unwrap() == "debug" {
make.arg("DEBUG=1");
let mut build = cc::Build::new();
build
.files(glob(out_dir.join("src/*.c").to_str().unwrap()).unwrap().map(|p| p.unwrap()))
.files(glob(out_dir.join("src/postgres/*.c").to_str().unwrap()).unwrap().map(|p| p.unwrap()))
.file(out_dir.join("vendor/protobuf-c/protobuf-c.c"))
.file(out_dir.join("vendor/xxhash/xxhash.c"))
.file(out_dir.join("protobuf/pg_query.pb-c.c"))
.include(out_dir.join("."))
.include(out_dir.join("./vendor"))
.include(out_dir.join("./src/postgres/include"))
.include(out_dir.join("./src/include"))
.warnings(false); // Avoid unnecessary warnings, as they are already considered as part of libpg_query development
if env::var("PROFILE").unwrap() == "debug" || env::var("DEBUG").unwrap() == "1" {
build.define("USE_ASSERT_CHECKING", None);
}

let status = make.stdin(Stdio::null()).stdout(Stdio::inherit()).stderr(Stdio::inherit()).status()?;

if !status.success() {
return Err("libpg_query compilation failed".into());
if target.contains("windows") {
build.include(out_dir.join("./src/postgres/include/port/win32"));
if target.contains("msvc") {
build.include(out_dir.join("./src/postgres/include/port/win32_msvc"));
}
}
build.compile(LIBRARY_NAME);

// Generate bindings for Rust
bindgen::Builder::default()
Expand Down
2 changes: 1 addition & 1 deletion libpg_query
Submodule libpg_query updated 673 files
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
//! Rust pg_query &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![Docs Badge]][docs]
//! ===========
//!
//! [Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpaupino%2Fpg_query%2Fbadge&label=build&logo=none
//! [actions]: https://actions-badge.atrox.dev/paupino/pg_query/goto
//! [Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpganalyze%2Fpg_query.rs%2Fbadge%3Fref%3Dmain&style=flat&label=build&logo=none
//! [actions]: https://actions-badge.atrox.dev/pganalyze/pg_query.rs/goto?ref=main
//! [Latest Version]: https://img.shields.io/crates/v/pg_query.svg
//! [crates.io]: https://crates.io/crates/pg_query
//! [Docs Badge]: https://docs.rs/pg_query/badge.svg
//! [docs]: https://docs.rs/pg_query
//!
//! PostgreSQL parser that uses the [actual PostgreSQL server source]((https://github.com/pganalyze/libpg_query)) to parse
//! SQL queries and return the internal PostgreSQL parse tree.
//! This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.
//!
//! Warning! This library is in early stages of development so any APIs exposed are subject to change.
//! It also allows you to normalize queries (replacing constant values with $1, etc.) and parse these normalized queries into a parse tree again.
//!
//! When you build this library, it builds parts of the PostgreSQL server source (see [libpg_query](https://github.com/pganalyze/libpg_query)), and then statically links it into this library.
//!
//! You can find further examples and a longer rationale for the original Ruby implementation [here](https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html). The Rust version tries to have a very similar API.
//!
//! ## Getting started
//!
//! Add the following to your `Cargo.toml`
//!
//! ```toml
//! [dependencies]
//! pg_query = "0.7"
//! pg_query = "5.1"
//! ```
//!
//! # Example: Parsing a query
Expand Down Expand Up @@ -49,7 +52,7 @@ pub use error::*;
pub use node_enum::*;
pub use node_mut::*;
pub use node_ref::*;
pub use node_structs::*;
pub use parse_result::*;
pub use query::*;
pub use truncate::*;

Expand Down
Loading

0 comments on commit 09db96a

Please sign in to comment.