Skip to content

Add optional Python bindings via PyO3 + maturin#48

Open
muk2 wants to merge 3 commits intomainfrom
feature/issue-45-python-pyo3
Open

Add optional Python bindings via PyO3 + maturin#48
muk2 wants to merge 3 commits intomainfrom
feature/issue-45-python-pyo3

Conversation

@muk2
Copy link
Owner

@muk2 muk2 commented Feb 20, 2026

Summary

Implements an optional Python interoperability layer for pgrsql using PyO3 and maturin, enabling SQL parsing, optimization, and analysis from Python.

  • Feature-gated: Python bindings compile only with --features python, keeping the core Rust crate unaffected
  • PyO3 bindings (src/python.rs): 5 Python-callable functions exposing the Rust SQL engine
  • maturin packaging (pyproject.toml): Build wheels for macOS, Linux, and Windows with maturin build --release
  • Python package (python/pgrsql/__init__.py): Clean import pgrsql interface with re-exports
  • Library crate (src/lib.rs): Exposes all modules publicly for external consumption

Python API

import pgrsql

# Parse SQL statements to normalized form
statements = pgrsql.parse_sql("SELECT * FROM users WHERE age > 18")

# Format and optimize a single SQL statement
formatted = pgrsql.format_sql("select id,name from users where age>18")

# Analyze query structure (returns dict with feature flags)
analysis = pgrsql.analyze_query("SELECT * FROM a JOIN b ON a.id = b.id")
# {'has_select': True, 'has_joins': True, 'join_count': 1, ...}

# Parse PostgreSQL EXPLAIN output
plan = pgrsql.parse_explain("Seq Scan on users (cost=0.00..35.50 rows=100)")

# Check if a query is an EXPLAIN query
pgrsql.is_explain_query("EXPLAIN SELECT 1")  # True

Building

# Local development
maturin develop

# Release wheel
maturin build --release

# Standard Rust build (no Python)
cargo build

# Build with Python feature
cargo build --features python

Files Changed

File Description
src/python.rs PyO3 module with 5 functions + 6 unit tests
src/lib.rs New library crate entry point
src/main.rs Updated imports to use pgrsql:: paths
Cargo.toml Added pyo3 optional dep, [features], [lib] section
pyproject.toml maturin build configuration
python/pgrsql/__init__.py Python package with re-exports

Test plan

  • cargo build passes
  • cargo test — all 225 tests pass
  • Python module compiles with feature flag
  • maturin develop builds wheel and import pgrsql works
  • Verify on Python 3.9+

Closes #45

🤖 Generated with Claude Code

muk2 and others added 3 commits February 20, 2026 07:54
Implements feature-gated Python interop layer that exposes pgrsql's
SQL parsing, optimization, analysis, and EXPLAIN plan parsing to Python.
The bindings are compiled only when the `python` feature flag is enabled,
keeping the core Rust crate unaffected.

Key additions:
- src/python.rs: PyO3 module with parse_sql, format_sql, analyze_query,
  parse_explain, and is_explain_query functions
- pyproject.toml: maturin build configuration for wheel distribution
- python/pgrsql/__init__.py: Python package wrapper for clean imports
- src/lib.rs: Library crate exposing all modules publicly
- Feature-gated with `python = ["pyo3"]` in Cargo.toml

Build: `maturin develop` for local dev, `maturin build --release` for wheels
Install: `pip install pgrsql` (once published)

Closes #45

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add #[allow(ambiguous_glob_reexports)] for conflicting SQL_FUNCTIONS re-exports
- Add Default impl for ConnectionManager and App (new_without_default)
- Add #[allow(clippy::should_implement_trait)] for QueryHistory::next()
- Run cargo fmt for consistent formatting

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Run cargo fmt for consistent code formatting in python.rs and main.rs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: Optional Python Interoperability Layer + PyPI Distribution (PyO3 + maturin + uv publish)

1 participant