psqlparse-rs 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. psqlparse_rs-0.1.0/.github/workflows/ci.yml +136 -0
  2. psqlparse_rs-0.1.0/.gitignore +12 -0
  3. psqlparse_rs-0.1.0/Cargo.lock +133 -0
  4. psqlparse_rs-0.1.0/Cargo.toml +19 -0
  5. psqlparse_rs-0.1.0/LICENSE +25 -0
  6. psqlparse_rs-0.1.0/PKG-INFO +83 -0
  7. psqlparse_rs-0.1.0/README.md +55 -0
  8. psqlparse_rs-0.1.0/SECURITY.md +11 -0
  9. psqlparse_rs-0.1.0/benchmarks/bench.py +75 -0
  10. psqlparse_rs-0.1.0/pyproject.toml +61 -0
  11. psqlparse_rs-0.1.0/python/psqlparse/__init__.py +69 -0
  12. psqlparse_rs-0.1.0/python/psqlparse/__init__.pyi +41 -0
  13. psqlparse_rs-0.1.0/python/psqlparse/__main__.py +23 -0
  14. psqlparse_rs-0.1.0/python/psqlparse/_psqlparse.pyi +8 -0
  15. psqlparse_rs-0.1.0/python/psqlparse/cli.py +227 -0
  16. psqlparse_rs-0.1.0/python/psqlparse/engine/__init__.py +17 -0
  17. psqlparse_rs-0.1.0/python/psqlparse/engine/filter_stack.py +52 -0
  18. psqlparse_rs-0.1.0/python/psqlparse/engine/grouping.py +612 -0
  19. psqlparse_rs-0.1.0/python/psqlparse/engine/statement_splitter.py +196 -0
  20. psqlparse_rs-0.1.0/python/psqlparse/exceptions.py +5 -0
  21. psqlparse_rs-0.1.0/python/psqlparse/filters/__init__.py +40 -0
  22. psqlparse_rs-0.1.0/python/psqlparse/filters/aligned_indent.py +143 -0
  23. psqlparse_rs-0.1.0/python/psqlparse/filters/others.py +183 -0
  24. psqlparse_rs-0.1.0/python/psqlparse/filters/output.py +127 -0
  25. psqlparse_rs-0.1.0/python/psqlparse/filters/reindent.py +297 -0
  26. psqlparse_rs-0.1.0/python/psqlparse/filters/right_margin.py +47 -0
  27. psqlparse_rs-0.1.0/python/psqlparse/filters/tokens.py +60 -0
  28. psqlparse_rs-0.1.0/python/psqlparse/formatter.py +191 -0
  29. psqlparse_rs-0.1.0/python/psqlparse/keywords.py +1050 -0
  30. psqlparse_rs-0.1.0/python/psqlparse/lexer.py +169 -0
  31. psqlparse_rs-0.1.0/python/psqlparse/py.typed +0 -0
  32. psqlparse_rs-0.1.0/python/psqlparse/sql.py +684 -0
  33. psqlparse_rs-0.1.0/python/psqlparse/tokens.py +70 -0
  34. psqlparse_rs-0.1.0/python/psqlparse/utils.py +129 -0
  35. psqlparse_rs-0.1.0/src/keywords.rs +865 -0
  36. psqlparse_rs-0.1.0/src/lexer.rs +1267 -0
  37. psqlparse_rs-0.1.0/src/lib.rs +103 -0
  38. psqlparse_rs-0.1.0/src/simd.rs +388 -0
  39. psqlparse_rs-0.1.0/src/splitter.rs +340 -0
  40. psqlparse_rs-0.1.0/tests/test_adversarial.py +128 -0
  41. psqlparse_rs-0.1.0/tests/test_concurrency_and_leaks.py +53 -0
  42. psqlparse_rs-0.1.0/tests/test_differential.py +165 -0
  43. psqlparse_rs-0.1.0/tests/test_ecosystem.py +67 -0
@@ -0,0 +1,136 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ tags: ["v*"]
7
+ pull_request:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ lint-and-test-rust:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: dtolnay/rust-toolchain@stable
18
+ with:
19
+ components: rustfmt, clippy
20
+ - uses: Swatinem/rust-cache@v2
21
+ - run: cargo fmt --check
22
+ - run: cargo clippy --all-targets -- -D warnings
23
+ - run: cargo test --lib
24
+
25
+ build-wheels:
26
+ strategy:
27
+ fail-fast: false
28
+ matrix:
29
+ include:
30
+ - os: ubuntu-latest
31
+ target: x86_64
32
+ args: --release --compatibility manylinux2014
33
+ run_tests: true
34
+ - os: ubuntu-latest
35
+ target: musl-x86_64
36
+ args: --release --compatibility musllinux_1_2
37
+ run_tests: false
38
+ - os: ubuntu-24.04-arm
39
+ target: aarch64
40
+ args: --release --compatibility manylinux2014
41
+ run_tests: true
42
+ - os: ubuntu-24.04-arm
43
+ target: musl-aarch64
44
+ args: --release --compatibility musllinux_1_2
45
+ run_tests: false
46
+ - os: macos-15
47
+ target: aarch64
48
+ args: --release
49
+ run_tests: true
50
+ - os: macos-15
51
+ target: x86_64
52
+ args: --release --target x86_64-apple-darwin
53
+ run_tests: false
54
+ - os: windows-latest
55
+ target: x64
56
+ args: --release
57
+ run_tests: true
58
+ runs-on: ${{ matrix.os }}
59
+ steps:
60
+ - uses: actions/checkout@v4
61
+ - uses: actions/setup-python@v5
62
+ with:
63
+ python-version: "3.12"
64
+ - uses: PyO3/maturin-action@v1
65
+ with:
66
+ command: build
67
+ args: ${{ matrix.args }} -o dist
68
+ manylinux: auto
69
+ - name: Check wheel metadata
70
+ shell: bash
71
+ run: |
72
+ python -m pip install --upgrade pip twine
73
+ twine check dist/*.whl
74
+ - name: Install and test abi3 wheel
75
+ if: matrix.run_tests
76
+ shell: bash
77
+ run: |
78
+ python -m pip install pytest hypothesis sqlparse mypy ruff abi3audit
79
+ python -m pip install dist/*.whl
80
+ pytest -v
81
+ - name: Static typing, stubtest, and abi3audit (Linux)
82
+ if: runner.os == 'Linux' && matrix.run_tests
83
+ run: |
84
+ ruff format --check python tests
85
+ ruff check python tests
86
+ mypy --strict python/psqlparse
87
+ stubtest --ignore-disjoint-base psqlparse
88
+ abi3audit --strict dist/*.whl
89
+ - uses: actions/upload-artifact@v4
90
+ with:
91
+ name: wheels-${{ matrix.os }}-${{ matrix.target }}
92
+ path: dist/*.whl
93
+
94
+ build-sdist:
95
+ runs-on: ubuntu-latest
96
+ steps:
97
+ - uses: actions/checkout@v4
98
+ - uses: PyO3/maturin-action@v1
99
+ with:
100
+ command: sdist
101
+ args: -o dist
102
+ - uses: actions/upload-artifact@v4
103
+ with:
104
+ name: sdist
105
+ path: dist/*.tar.gz
106
+
107
+ publish:
108
+ if: startsWith(github.ref, 'refs/tags/v')
109
+ needs: [lint-and-test-rust, build-wheels, build-sdist]
110
+ runs-on: ubuntu-latest
111
+ environment: pypi
112
+ permissions:
113
+ id-token: write
114
+ attestations: write
115
+ contents: write
116
+ steps:
117
+ - uses: actions/download-artifact@v4
118
+ with:
119
+ path: dist
120
+ merge-multiple: true
121
+ - name: Attest build provenance (SLSA)
122
+ uses: actions/attest-build-provenance@v2
123
+ with:
124
+ subject-path: "dist/*"
125
+ - uses: pypa/gh-action-pypi-publish@release/v1
126
+ with:
127
+ packages-dir: dist/
128
+ - name: Generate SHA-256 checksums
129
+ run: (cd dist && sha256sum *.whl *.tar.gz) > SHA256SUMS.txt
130
+ - name: Create GitHub Release
131
+ uses: softprops/action-gh-release@v2
132
+ with:
133
+ generate_release_notes: true
134
+ files: |
135
+ dist/*
136
+ SHA256SUMS.txt
@@ -0,0 +1,12 @@
1
+ AGENTS.md
2
+ CLAUDE.md
3
+ .venv/
4
+ target/
5
+ dist/
6
+ *.so
7
+ *.pyd
8
+ __pycache__/
9
+ .mypy_cache/
10
+ .pytest_cache/
11
+ .ruff_cache/
12
+ .hypothesis/
@@ -0,0 +1,133 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "heck"
7
+ version = "0.5.0"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
10
+
11
+ [[package]]
12
+ name = "libc"
13
+ version = "0.2.191"
14
+ source = "registry+https://github.com/rust-lang/crates.io-index"
15
+ checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
16
+
17
+ [[package]]
18
+ name = "once_cell"
19
+ version = "1.21.4"
20
+ source = "registry+https://github.com/rust-lang/crates.io-index"
21
+ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
22
+
23
+ [[package]]
24
+ name = "portable-atomic"
25
+ version = "1.15.0"
26
+ source = "registry+https://github.com/rust-lang/crates.io-index"
27
+ checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
28
+
29
+ [[package]]
30
+ name = "proc-macro2"
31
+ version = "1.0.107"
32
+ source = "registry+https://github.com/rust-lang/crates.io-index"
33
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
34
+ dependencies = [
35
+ "unicode-ident",
36
+ ]
37
+
38
+ [[package]]
39
+ name = "psqlparse"
40
+ version = "0.1.0"
41
+ dependencies = [
42
+ "pyo3",
43
+ ]
44
+
45
+ [[package]]
46
+ name = "pyo3"
47
+ version = "0.28.3"
48
+ source = "registry+https://github.com/rust-lang/crates.io-index"
49
+ checksum = "91fd8e38a3b50ed1167fb981cd6fd60147e091784c427b8f7183a7ee32c31c12"
50
+ dependencies = [
51
+ "libc",
52
+ "once_cell",
53
+ "portable-atomic",
54
+ "pyo3-build-config",
55
+ "pyo3-ffi",
56
+ "pyo3-macros",
57
+ ]
58
+
59
+ [[package]]
60
+ name = "pyo3-build-config"
61
+ version = "0.28.3"
62
+ source = "registry+https://github.com/rust-lang/crates.io-index"
63
+ checksum = "e368e7ddfdeb98c9bca7f8383be1648fd84ab466bf2bc015e94008db6d35611e"
64
+ dependencies = [
65
+ "target-lexicon",
66
+ ]
67
+
68
+ [[package]]
69
+ name = "pyo3-ffi"
70
+ version = "0.28.3"
71
+ source = "registry+https://github.com/rust-lang/crates.io-index"
72
+ checksum = "7f29e10af80b1f7ccaf7f69eace800a03ecd13e883acfacc1e5d0988605f651e"
73
+ dependencies = [
74
+ "libc",
75
+ "pyo3-build-config",
76
+ ]
77
+
78
+ [[package]]
79
+ name = "pyo3-macros"
80
+ version = "0.28.3"
81
+ source = "registry+https://github.com/rust-lang/crates.io-index"
82
+ checksum = "df6e520eff47c45997d2fc7dd8214b25dd1310918bbb2642156ef66a67f29813"
83
+ dependencies = [
84
+ "proc-macro2",
85
+ "pyo3-macros-backend",
86
+ "quote",
87
+ "syn",
88
+ ]
89
+
90
+ [[package]]
91
+ name = "pyo3-macros-backend"
92
+ version = "0.28.3"
93
+ source = "registry+https://github.com/rust-lang/crates.io-index"
94
+ checksum = "c4cdc218d835738f81c2338f822078af45b4afdf8b2e33cbb5916f108b813acb"
95
+ dependencies = [
96
+ "heck",
97
+ "proc-macro2",
98
+ "pyo3-build-config",
99
+ "quote",
100
+ "syn",
101
+ ]
102
+
103
+ [[package]]
104
+ name = "quote"
105
+ version = "1.0.47"
106
+ source = "registry+https://github.com/rust-lang/crates.io-index"
107
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
108
+ dependencies = [
109
+ "proc-macro2",
110
+ ]
111
+
112
+ [[package]]
113
+ name = "syn"
114
+ version = "2.0.119"
115
+ source = "registry+https://github.com/rust-lang/crates.io-index"
116
+ checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
117
+ dependencies = [
118
+ "proc-macro2",
119
+ "quote",
120
+ "unicode-ident",
121
+ ]
122
+
123
+ [[package]]
124
+ name = "target-lexicon"
125
+ version = "0.13.6"
126
+ source = "registry+https://github.com/rust-lang/crates.io-index"
127
+ checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da"
128
+
129
+ [[package]]
130
+ name = "unicode-ident"
131
+ version = "1.0.27"
132
+ source = "registry+https://github.com/rust-lang/crates.io-index"
133
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
@@ -0,0 +1,19 @@
1
+ [package]
2
+ name = "psqlparse"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ license = "BSD-3-Clause"
6
+ description = "Fast non-validating SQL parser and formatter for Python"
7
+ readme = "README.md"
8
+
9
+ [lib]
10
+ name = "_psqlparse"
11
+ crate-type = ["cdylib", "rlib"]
12
+
13
+ [dependencies]
14
+ pyo3 = { version = "0.28", features = ["abi3-py39"] }
15
+
16
+ [profile.release]
17
+ lto = "thin"
18
+ codegen-units = 1
19
+ opt-level = 3
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2016, Andi Albrecht <albrecht.andi@gmail.com>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name of the authors nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.4
2
+ Name: psqlparse-rs
3
+ Version: 0.1.0
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: License :: OSI Approved :: BSD License
7
+ Classifier: Operating System :: OS Independent
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.9
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Classifier: Programming Language :: Rust
16
+ Classifier: Topic :: Database
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: Typing :: Typed
19
+ License-File: LICENSE
20
+ Summary: Fast non-validating SQL parser and formatter for Python
21
+ Keywords: sql,sqlparse,parser,lexer,formatter,tokenizer,rust
22
+ License-Expression: BSD-3-Clause
23
+ Requires-Python: >=3.9
24
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
25
+ Project-URL: Homepage, https://github.com/gatewaytofredom/psqlparse-rs
26
+ Project-URL: Repository, https://github.com/gatewaytofredom/psqlparse-rs
27
+
28
+ # psqlparse-rs
29
+
30
+ [![CI](https://github.com/gatewaytofredom/psqlparse-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/gatewaytofredom/psqlparse-rs/actions/workflows/ci.yml)
31
+ [![PyPI](https://img.shields.io/pypi/v/psqlparse-rs.svg)](https://pypi.org/project/psqlparse-rs/)
32
+ [![Python Versions](https://img.shields.io/pypi/pyversions/psqlparse-rs.svg)](https://pypi.org/project/psqlparse-rs/)
33
+ [![License: BSD-3-Clause](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](https://github.com/gatewaytofredom/psqlparse-rs/blob/main/LICENSE)
34
+
35
+ A fast, drop-in replacement for [`sqlparse`](https://pypi.org/project/sqlparse/) to tokenize, split, parse, and format SQL in Python — up to **97x faster** on statement splitting and **25x faster** on lexing.
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ pip install psqlparse-rs
41
+ ```
42
+
43
+ Prebuilt wheels are included for Linux, macOS, and Windows (Python 3.9+, no Rust toolchain needed).
44
+
45
+ ## Usage
46
+
47
+ ```python
48
+ import psqlparse
49
+
50
+ # Split a multi-statement SQL script
51
+ statements = psqlparse.split("SELECT 1; SELECT 2;")
52
+
53
+ # Format SQL with familiar sqlparse options
54
+ pretty = psqlparse.format("select a, b from users where id = 1", keyword_case="upper", reindent=True)
55
+
56
+ # Parse into a statement tree
57
+ parsed = psqlparse.parse("SELECT id, name FROM users WHERE active = true")
58
+ ```
59
+
60
+ `psqlparse` mirrors the `sqlparse` API (`parse`, `parsestream`, `split`, `format`, `tokens`, `sql`, `engine`, `filters`, `formatter`, `exceptions`) and accepts `str`, `bytes`, `bytearray`, or file-like streams.
61
+
62
+ ## Performance
63
+
64
+ Measured against `sqlparse` 0.6.1 (`benchmarks/bench.py`):
65
+
66
+ | Workload | `sqlparse` (x86-64) | `psqlparse` (x86-64) | `sqlparse` (Apple M4) | `psqlparse` (Apple M4) |
67
+ |---|---:|---:|---:|---:|
68
+ | `split` (simple) | 266.21 µs | **2.74 µs** (97.1x) | 189.65 µs | **2.33 µs** (81.5x) |
69
+ | `split` (200 statements) | 24,106.30 µs | **314.64 µs** (76.6x) | 17,675.32 µs | **212.17 µs** (83.3x) |
70
+ | `tokenize` (simple) | 152.38 µs | **6.16 µs** (24.8x) | 133.18 µs | **5.21 µs** (25.6x) |
71
+ | `tokenize` (200 statements) | 16,515.92 µs | **878.75 µs** (18.8x) | 13,127.40 µs | **761.33 µs** (17.2x) |
72
+ | `parse` (simple) | 864.84 µs | **361.59 µs** (2.4x) | 562.30 µs | **226.41 µs** (2.5x) |
73
+ | `format` (simple) | 1,042.42 µs | **540.91 µs** (1.9x) | 689.53 µs | **338.25 µs** (2.0x) |
74
+
75
+ ## Under the hood
76
+
77
+ - Written in Rust (PyO3) with zero external runtime dependencies and runtime SIMD dispatch (AVX2 / SSE2 / ARM64 NEON)
78
+ - Compact `cp39-abi3` wheel (`gil_used = false`, free-threaded Python ready) with PEP 561 type stubs (`py.typed` + `.pyi` stubs)
79
+
80
+ ## License
81
+
82
+ BSD-3-Clause
83
+
@@ -0,0 +1,55 @@
1
+ # psqlparse-rs
2
+
3
+ [![CI](https://github.com/gatewaytofredom/psqlparse-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/gatewaytofredom/psqlparse-rs/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/psqlparse-rs.svg)](https://pypi.org/project/psqlparse-rs/)
5
+ [![Python Versions](https://img.shields.io/pypi/pyversions/psqlparse-rs.svg)](https://pypi.org/project/psqlparse-rs/)
6
+ [![License: BSD-3-Clause](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](https://github.com/gatewaytofredom/psqlparse-rs/blob/main/LICENSE)
7
+
8
+ A fast, drop-in replacement for [`sqlparse`](https://pypi.org/project/sqlparse/) to tokenize, split, parse, and format SQL in Python — up to **97x faster** on statement splitting and **25x faster** on lexing.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ pip install psqlparse-rs
14
+ ```
15
+
16
+ Prebuilt wheels are included for Linux, macOS, and Windows (Python 3.9+, no Rust toolchain needed).
17
+
18
+ ## Usage
19
+
20
+ ```python
21
+ import psqlparse
22
+
23
+ # Split a multi-statement SQL script
24
+ statements = psqlparse.split("SELECT 1; SELECT 2;")
25
+
26
+ # Format SQL with familiar sqlparse options
27
+ pretty = psqlparse.format("select a, b from users where id = 1", keyword_case="upper", reindent=True)
28
+
29
+ # Parse into a statement tree
30
+ parsed = psqlparse.parse("SELECT id, name FROM users WHERE active = true")
31
+ ```
32
+
33
+ `psqlparse` mirrors the `sqlparse` API (`parse`, `parsestream`, `split`, `format`, `tokens`, `sql`, `engine`, `filters`, `formatter`, `exceptions`) and accepts `str`, `bytes`, `bytearray`, or file-like streams.
34
+
35
+ ## Performance
36
+
37
+ Measured against `sqlparse` 0.6.1 (`benchmarks/bench.py`):
38
+
39
+ | Workload | `sqlparse` (x86-64) | `psqlparse` (x86-64) | `sqlparse` (Apple M4) | `psqlparse` (Apple M4) |
40
+ |---|---:|---:|---:|---:|
41
+ | `split` (simple) | 266.21 µs | **2.74 µs** (97.1x) | 189.65 µs | **2.33 µs** (81.5x) |
42
+ | `split` (200 statements) | 24,106.30 µs | **314.64 µs** (76.6x) | 17,675.32 µs | **212.17 µs** (83.3x) |
43
+ | `tokenize` (simple) | 152.38 µs | **6.16 µs** (24.8x) | 133.18 µs | **5.21 µs** (25.6x) |
44
+ | `tokenize` (200 statements) | 16,515.92 µs | **878.75 µs** (18.8x) | 13,127.40 µs | **761.33 µs** (17.2x) |
45
+ | `parse` (simple) | 864.84 µs | **361.59 µs** (2.4x) | 562.30 µs | **226.41 µs** (2.5x) |
46
+ | `format` (simple) | 1,042.42 µs | **540.91 µs** (1.9x) | 689.53 µs | **338.25 µs** (2.0x) |
47
+
48
+ ## Under the hood
49
+
50
+ - Written in Rust (PyO3) with zero external runtime dependencies and runtime SIMD dispatch (AVX2 / SSE2 / ARM64 NEON)
51
+ - Compact `cp39-abi3` wheel (`gil_used = false`, free-threaded Python ready) with PEP 561 type stubs (`py.typed` + `.pyi` stubs)
52
+
53
+ ## License
54
+
55
+ BSD-3-Clause
@@ -0,0 +1,11 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+
5
+ Please report security vulnerabilities privately using GitHub's **Private Vulnerability Reporting** feature on the **Security** tab of this repository (or email `gateway@laindex.net`).
6
+
7
+ Please include:
8
+ - A minimal reproducer (input bytes/string and affected version)
9
+ - Expected vs. observed behavior (e.g., crash, quadratic CPU / ReDoS, memory issue)
10
+
11
+ We aim to acknowledge reports within 48 hours and ship a verified fix promptly.
@@ -0,0 +1,75 @@
1
+ """Benchmark psqlparse vs sqlparse on realistic SQL workloads."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import statistics
6
+ import time
7
+
8
+ import psqlparse
9
+
10
+ try:
11
+ import sqlparse
12
+ except ImportError:
13
+ sqlparse = None # type: ignore[assignment]
14
+
15
+ SIMPLE_SQL = (
16
+ "SELECT u.id, u.email, o.total FROM users u "
17
+ "INNER JOIN orders o ON u.id = o.user_id "
18
+ "WHERE o.status = 'paid' AND o.total >= 100.50 "
19
+ "ORDER BY o.created_at DESC LIMIT 50;"
20
+ )
21
+
22
+ BATCH_SQL = "\n".join(
23
+ f"SELECT id, name, 'val_{i}' FROM tbl_{i % 10} WHERE active = 1 AND score > {i * 1.5};" for i in range(200)
24
+ )
25
+
26
+ ADVERSARIAL_DOLLAR = "$tag$ " * 2000 + "SELECT 1;"
27
+
28
+
29
+ def bench(fn, iters: int) -> float: # type: ignore[no-untyped-def]
30
+ for _ in range(5):
31
+ fn()
32
+ times = []
33
+ for _ in range(5):
34
+ t0 = time.perf_counter()
35
+ for _ in range(iters):
36
+ fn()
37
+ times.append((time.perf_counter() - t0) / iters * 1e6)
38
+ return statistics.median(times)
39
+
40
+
41
+ def main() -> None:
42
+ workloads = [
43
+ (
44
+ "tokenize(simple)",
45
+ lambda: list(psqlparse.lexer.tokenize(SIMPLE_SQL)),
46
+ lambda: list(sqlparse.lexer.tokenize(SIMPLE_SQL)),
47
+ 2000,
48
+ ),
49
+ ("split(simple)", lambda: psqlparse.split(SIMPLE_SQL), lambda: sqlparse.split(SIMPLE_SQL), 2000),
50
+ ("split(200_stmts)", lambda: psqlparse.split(BATCH_SQL), lambda: sqlparse.split(BATCH_SQL), 50),
51
+ ("parse(simple)", lambda: psqlparse.parse(SIMPLE_SQL), lambda: sqlparse.parse(SIMPLE_SQL), 500),
52
+ (
53
+ "format(simple)",
54
+ lambda: psqlparse.format(SIMPLE_SQL, keyword_case="upper", reindent=True),
55
+ lambda: sqlparse.format(SIMPLE_SQL, keyword_case="upper", reindent=True),
56
+ 500,
57
+ ),
58
+ (
59
+ "tokenize(200_stmts)",
60
+ lambda: list(psqlparse.lexer.tokenize(BATCH_SQL)),
61
+ lambda: list(sqlparse.lexer.tokenize(BATCH_SQL)),
62
+ 50,
63
+ ),
64
+ ]
65
+ for name, fn_new, fn_old, iters in workloads:
66
+ us_new = bench(fn_new, iters)
67
+ if sqlparse is not None:
68
+ us_old = bench(fn_old, iters)
69
+ print(f"{name:22s}: psqlparse={us_new:8.2f} us sqlparse={us_old:9.2f} us speedup={us_old / us_new:6.2f}x")
70
+ else:
71
+ print(f"{name:22s}: psqlparse={us_new:8.2f} us")
72
+
73
+
74
+ if __name__ == "__main__":
75
+ main()
@@ -0,0 +1,61 @@
1
+ [build-system]
2
+ requires = ["maturin>=1.7,<2.0"]
3
+ build-backend = "maturin"
4
+
5
+ [project]
6
+ name = "psqlparse-rs"
7
+ version = "0.1.0"
8
+ description = "Fast non-validating SQL parser and formatter for Python"
9
+ readme = "README.md"
10
+ license = "BSD-3-Clause"
11
+ requires-python = ">=3.9"
12
+ keywords = [
13
+ "sql",
14
+ "sqlparse",
15
+ "parser",
16
+ "lexer",
17
+ "formatter",
18
+ "tokenizer",
19
+ "rust",
20
+ ]
21
+ classifiers = [
22
+ "Development Status :: 4 - Beta",
23
+ "Intended Audience :: Developers",
24
+ "License :: OSI Approved :: BSD License",
25
+ "Operating System :: OS Independent",
26
+ "Programming Language :: Python :: 3",
27
+ "Programming Language :: Python :: 3.9",
28
+ "Programming Language :: Python :: 3.10",
29
+ "Programming Language :: Python :: 3.11",
30
+ "Programming Language :: Python :: 3.12",
31
+ "Programming Language :: Python :: 3.13",
32
+ "Programming Language :: Python :: 3.14",
33
+ "Programming Language :: Rust",
34
+ "Topic :: Database",
35
+ "Topic :: Software Development :: Libraries :: Python Modules",
36
+ "Typing :: Typed",
37
+ ]
38
+
39
+ [project.urls]
40
+ Homepage = "https://github.com/gatewaytofredom/psqlparse-rs"
41
+ Repository = "https://github.com/gatewaytofredom/psqlparse-rs"
42
+
43
+ [tool.maturin]
44
+ python-source = "python"
45
+ module-name = "psqlparse._psqlparse"
46
+ features = ["pyo3/extension-module"]
47
+
48
+ [tool.ruff]
49
+ line-length = 120
50
+ target-version = "py39"
51
+
52
+ [tool.ruff.lint]
53
+ select = ["E", "F", "I", "UP", "B", "SIM"]
54
+
55
+ [tool.mypy]
56
+ python_version = "3.10"
57
+ strict = true
58
+ warn_unused_ignores = true
59
+
60
+ [tool.pytest.ini_options]
61
+ testpaths = ["tests"]
@@ -0,0 +1,69 @@
1
+ """Parse SQL statements."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Generator
6
+ from typing import IO, Any
7
+
8
+ from psqlparse import _psqlparse, cli, engine, exceptions, filters, formatter, keywords, lexer, sql, tokens, utils
9
+
10
+ __version__ = "0.1.0"
11
+ __all__ = [
12
+ "cli",
13
+ "engine",
14
+ "exceptions",
15
+ "filters",
16
+ "format",
17
+ "formatter",
18
+ "keywords",
19
+ "lexer",
20
+ "parse",
21
+ "parsestream",
22
+ "split",
23
+ "sql",
24
+ "tokens",
25
+ "utils",
26
+ ]
27
+
28
+
29
+ def parse(
30
+ sql: str | bytes | bytearray | IO[str],
31
+ encoding: str | None = None,
32
+ ) -> tuple[sql.Statement, ...]:
33
+ """Parse sql and return a tuple of statements."""
34
+ return tuple(parsestream(sql, encoding))
35
+
36
+
37
+ def parsestream(
38
+ stream: str | bytes | bytearray | IO[str],
39
+ encoding: str | None = None,
40
+ ) -> Generator[sql.Statement, None, None]:
41
+ """Parses sql statements from a string, bytes, bytearray, or file-like object."""
42
+ stack = engine.FilterStack()
43
+ stack.enable_grouping()
44
+ return stack.run(stream, encoding)
45
+
46
+
47
+ def format(
48
+ sql: str | bytes | bytearray | IO[str],
49
+ encoding: str | None = None,
50
+ **options: Any,
51
+ ) -> str:
52
+ """Format *sql* according to *options*."""
53
+ stack = engine.FilterStack()
54
+ options = formatter.validate_options(options)
55
+ stack = formatter.build_filter_stack(stack, options)
56
+ stack.postprocess.append(filters.SerializerUnicode())
57
+ return "".join(stack.run(sql, encoding))
58
+
59
+
60
+ def split(
61
+ sql: str | bytes | bytearray | IO[str],
62
+ encoding: str | None = None,
63
+ strip_semicolon: bool = False,
64
+ ) -> list[str]:
65
+ """Split *sql* into single statements."""
66
+ if isinstance(sql, (str, bytes, bytearray)):
67
+ return _psqlparse.split_fast(sql, encoding, strip_semicolon)
68
+ stack = engine.FilterStack(strip_semicolon=strip_semicolon)
69
+ return [str(stmt).strip() for stmt in stack.run(sql, encoding)]