rsqlparse 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 (47) hide show
  1. rsqlparse-0.1.0/.github/workflows/ci.yml +49 -0
  2. rsqlparse-0.1.0/.github/workflows/wheels.yml +161 -0
  3. rsqlparse-0.1.0/.gitignore +14 -0
  4. rsqlparse-0.1.0/AGENTS.md +23 -0
  5. rsqlparse-0.1.0/CLAUDE.md +1 -0
  6. rsqlparse-0.1.0/Cargo.lock +132 -0
  7. rsqlparse-0.1.0/Cargo.toml +29 -0
  8. rsqlparse-0.1.0/LICENSE +29 -0
  9. rsqlparse-0.1.0/PKG-INFO +183 -0
  10. rsqlparse-0.1.0/README.md +155 -0
  11. rsqlparse-0.1.0/benchmarks/bench.py +306 -0
  12. rsqlparse-0.1.0/mise.toml +39 -0
  13. rsqlparse-0.1.0/pyproject.toml +47 -0
  14. rsqlparse-0.1.0/python/rsqlparse/__init__.py +145 -0
  15. rsqlparse-0.1.0/python/rsqlparse/__main__.py +8 -0
  16. rsqlparse-0.1.0/python/rsqlparse/_core.pyi +35 -0
  17. rsqlparse-0.1.0/python/rsqlparse/cli.py +245 -0
  18. rsqlparse-0.1.0/python/rsqlparse/engine/__init__.py +16 -0
  19. rsqlparse-0.1.0/python/rsqlparse/engine/filter_stack.py +51 -0
  20. rsqlparse-0.1.0/python/rsqlparse/engine/grouping.py +544 -0
  21. rsqlparse-0.1.0/python/rsqlparse/engine/statement_splitter.py +196 -0
  22. rsqlparse-0.1.0/python/rsqlparse/exceptions.py +5 -0
  23. rsqlparse-0.1.0/python/rsqlparse/filters/__init__.py +39 -0
  24. rsqlparse-0.1.0/python/rsqlparse/filters/aligned_indent.py +139 -0
  25. rsqlparse-0.1.0/python/rsqlparse/filters/others.py +180 -0
  26. rsqlparse-0.1.0/python/rsqlparse/filters/output.py +131 -0
  27. rsqlparse-0.1.0/python/rsqlparse/filters/reindent.py +288 -0
  28. rsqlparse-0.1.0/python/rsqlparse/filters/right_margin.py +49 -0
  29. rsqlparse-0.1.0/python/rsqlparse/filters/tokens.py +59 -0
  30. rsqlparse-0.1.0/python/rsqlparse/formatter.py +193 -0
  31. rsqlparse-0.1.0/python/rsqlparse/keywords.py +1111 -0
  32. rsqlparse-0.1.0/python/rsqlparse/lexer.py +124 -0
  33. rsqlparse-0.1.0/python/rsqlparse/py.typed +0 -0
  34. rsqlparse-0.1.0/python/rsqlparse/sql.py +593 -0
  35. rsqlparse-0.1.0/python/rsqlparse/tokens.py +97 -0
  36. rsqlparse-0.1.0/python/rsqlparse/utils.py +77 -0
  37. rsqlparse-0.1.0/scripts/attribution-guard.sh +45 -0
  38. rsqlparse-0.1.0/src/ast.rs +342 -0
  39. rsqlparse-0.1.0/src/formatter.rs +1825 -0
  40. rsqlparse-0.1.0/src/grouping.rs +922 -0
  41. rsqlparse-0.1.0/src/keywords.rs +841 -0
  42. rsqlparse-0.1.0/src/lexer.rs +1397 -0
  43. rsqlparse-0.1.0/src/lib.rs +348 -0
  44. rsqlparse-0.1.0/src/splitter.rs +299 -0
  45. rsqlparse-0.1.0/src/tokens.rs +131 -0
  46. rsqlparse-0.1.0/tests/test_core_compat.py +248 -0
  47. rsqlparse-0.1.0/tests/test_redos_security.py +159 -0
@@ -0,0 +1,49 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ rust:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: dtolnay/rust-toolchain@stable
17
+ with:
18
+ components: rustfmt, clippy
19
+ - uses: Swatinem/rust-cache@v2
20
+ - run: cargo fmt --check
21
+ - run: cargo clippy --all-targets -- -D warnings
22
+ - run: cargo test
23
+
24
+ python:
25
+ strategy:
26
+ fail-fast: false
27
+ matrix:
28
+ os: [ubuntu-24.04, macos-latest, windows-latest]
29
+ python-version: ["3.9", "3.11", "3.13"]
30
+ runs-on: ${{ matrix.os }}
31
+ steps:
32
+ - uses: actions/checkout@v4
33
+ - uses: actions/setup-python@v5
34
+ with:
35
+ python-version: ${{ matrix.python-version }}
36
+ - uses: dtolnay/rust-toolchain@stable
37
+ - uses: Swatinem/rust-cache@v2
38
+ - run: python -m pip install --upgrade pip maturin pytest
39
+ - run: maturin build --release --out dist
40
+ - run: python -m pip install --no-index --find-links dist rsqlparse
41
+ - run: python -m pytest
42
+
43
+ guard:
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: actions/checkout@v4
47
+ with:
48
+ fetch-depth: 0
49
+ - run: bash scripts/attribution-guard.sh files
@@ -0,0 +1,161 @@
1
+ name: Wheels & Release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+ workflow_dispatch:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ linux:
14
+ runs-on: ${{ matrix.runner }}
15
+ strategy:
16
+ fail-fast: false
17
+ matrix:
18
+ include:
19
+ - target: x86_64
20
+ runner: ubuntu-24.04
21
+ manylinux: auto
22
+ - target: aarch64
23
+ runner: ubuntu-24.04-arm
24
+ manylinux: auto
25
+ - target: x86_64
26
+ runner: ubuntu-24.04
27
+ manylinux: musllinux_1_2
28
+ - target: aarch64
29
+ runner: ubuntu-24.04-arm
30
+ manylinux: musllinux_1_2
31
+ steps:
32
+ - uses: actions/checkout@v4
33
+ - uses: actions/setup-python@v5
34
+ with:
35
+ python-version: "3.12"
36
+ - uses: PyO3/maturin-action@v1
37
+ with:
38
+ target: ${{ matrix.target }}
39
+ args: --release --out dist --find-interpreter
40
+ sccache: "true"
41
+ manylinux: ${{ matrix.manylinux }}
42
+ - name: Test manylinux wheel
43
+ if: matrix.manylinux == 'auto'
44
+ run: |
45
+ python -m pip install --upgrade pip pytest
46
+ python -m pip install --no-index --find-links dist rsqlparse
47
+ python -m pytest
48
+ python -c "import rsqlparse as sp; assert sp.split('SELECT 1; SELECT 2;') == ['SELECT 1;', 'SELECT 2;']"
49
+ - name: Test musllinux wheel
50
+ if: matrix.manylinux == 'musllinux_1_2'
51
+ run: |
52
+ docker run --rm -v "${{ github.workspace }}:/work" -w /work python:3.12-alpine sh -c '
53
+ python -m pip install --no-cache-dir pytest &&
54
+ python -m pip install --no-index --find-links dist rsqlparse &&
55
+ python -m pytest &&
56
+ python -c "import rsqlparse as sp; assert sp.split(\"SELECT 1; SELECT 2;\") == [\"SELECT 1;\", \"SELECT 2;\"]"
57
+ '
58
+ - uses: actions/upload-artifact@v4
59
+ with:
60
+ name: wheels-linux-${{ matrix.manylinux }}-${{ matrix.target }}
61
+ path: dist
62
+
63
+ macos:
64
+ runs-on: ${{ matrix.runner }}
65
+ strategy:
66
+ fail-fast: false
67
+ matrix:
68
+ include:
69
+ - runner: macos-15
70
+ target: aarch64-apple-darwin
71
+ - runner: macos-15-intel
72
+ target: x86_64-apple-darwin
73
+ steps:
74
+ - uses: actions/checkout@v4
75
+ - uses: actions/setup-python@v5
76
+ with:
77
+ python-version: "3.12"
78
+ - uses: PyO3/maturin-action@v1
79
+ with:
80
+ target: ${{ matrix.target }}
81
+ args: --release --out dist --find-interpreter
82
+ sccache: "true"
83
+ - name: Test macOS wheel
84
+ run: |
85
+ python -m pip install --upgrade pip pytest
86
+ python -m pip install --no-index --find-links dist rsqlparse
87
+ python -m pytest
88
+ python -c "import rsqlparse as sp; assert sp.split('SELECT 1; SELECT 2;') == ['SELECT 1;', 'SELECT 2;']"
89
+ - uses: actions/upload-artifact@v4
90
+ with:
91
+ name: wheels-macos-${{ matrix.target }}
92
+ path: dist
93
+
94
+ windows:
95
+ runs-on: windows-latest
96
+ strategy:
97
+ fail-fast: false
98
+ matrix:
99
+ target: [x64]
100
+ steps:
101
+ - uses: actions/checkout@v4
102
+ - uses: actions/setup-python@v5
103
+ with:
104
+ python-version: "3.12"
105
+ - uses: PyO3/maturin-action@v1
106
+ with:
107
+ target: ${{ matrix.target }}
108
+ args: --release --out dist --find-interpreter
109
+ sccache: "true"
110
+ - name: Test Windows wheel
111
+ shell: bash
112
+ run: |
113
+ python -m pip install --upgrade pip pytest
114
+ python -m pip install --no-index --find-links dist rsqlparse
115
+ python -m pytest
116
+ python -c "import rsqlparse as sp; assert sp.split('SELECT 1; SELECT 2;') == ['SELECT 1;', 'SELECT 2;']"
117
+ - uses: actions/upload-artifact@v4
118
+ with:
119
+ name: wheels-windows-${{ matrix.target }}
120
+ path: dist
121
+
122
+ sdist:
123
+ runs-on: ubuntu-24.04
124
+ steps:
125
+ - uses: actions/checkout@v4
126
+ - uses: actions/setup-python@v5
127
+ with:
128
+ python-version: "3.12"
129
+ - uses: dtolnay/rust-toolchain@stable
130
+ - uses: PyO3/maturin-action@v1
131
+ with:
132
+ command: sdist
133
+ args: --out dist
134
+ - name: Verify sdist metadata and source install
135
+ run: |
136
+ python -m pip install --upgrade pip twine pytest maturin
137
+ twine check dist/*
138
+ python -m pip install --no-build-isolation dist/*.tar.gz
139
+ python -m pytest
140
+ - uses: actions/upload-artifact@v4
141
+ with:
142
+ name: wheels-sdist
143
+ path: dist
144
+
145
+ release:
146
+ name: Publish to PyPI
147
+ runs-on: ubuntu-24.04
148
+ if: startsWith(github.ref, 'refs/tags/v')
149
+ needs: [linux, macos, windows, sdist]
150
+ environment: pypi
151
+ permissions:
152
+ id-token: write
153
+ steps:
154
+ - uses: actions/download-artifact@v4
155
+ with:
156
+ pattern: wheels-*
157
+ merge-multiple: true
158
+ path: dist
159
+ - uses: pypa/gh-action-pypi-publish@release/v1
160
+ with:
161
+ packages-dir: dist/
@@ -0,0 +1,14 @@
1
+ /target/
2
+ /dist/
3
+ /.venv/
4
+ /.pytest_cache/
5
+ /.benchmarks/
6
+ /__pycache__/
7
+ **/__pycache__/
8
+ *.pyc
9
+ *.pyo
10
+ *.so
11
+ *.pyd
12
+ *.dylib
13
+ *.egg-info/
14
+ .DS_Store
@@ -0,0 +1,23 @@
1
+ # rsqlparse
2
+
3
+ Zero-dependency, ReDoS-immune O(n) Rust drop-in replacement for Python `sqlparse` via PyO3 + maturin.
4
+
5
+ ## Architecture
6
+
7
+ - `src/tokens.rs` — compact `TokenKind` (`#[repr(u8)]`) hierarchy matching `sqlparse.tokens`.
8
+ - `src/keywords.rs` — sorted static lookup table of all 803 SQL keywords across 9 SQL dialects (`COMMON`, `ORACLE`, `MYSQL`, `PLPGSQL`, `HQL`, `MSACCESS`, `SNOWFLAKE`, `BIGQUERY`, `KEYWORDS`).
9
+ - `src/lexer.rs` — hand-written single-pass O(n) deterministic state-machine SQL lexer (no regex engine, no backtracking, O(n) delimited-span pre-pass for dollar quotes and multiline comments).
10
+ - `src/splitter.rs` — stack-based O(n) `StatementSplitter` handling `BEGIN`/`DECLARE`/`IF`/`CASE`/`FOR`/`WHILE`/`LOOP`/`END`, `GO`, and parenthesized blocks.
11
+ - `src/ast.rs` & `src/grouping.rs` — pure-Rust 25-pass SQL AST grouper with configurable depth (`MAX_GROUPING_DEPTH`) and token count (`MAX_GROUPING_TOKENS`) limits.
12
+ - `src/formatter.rs` — pure-Rust SQL formatting pipeline (case folding, string truncation, operator spacing, comment stripping, whitespace normalization, reindent, aligned indent, Python/PHP output escaping, O(n) unquoted newline splitting).
13
+ - `src/lib.rs` — `#![forbid(unsafe_code)]`, public Rust library API and `pyo3` (`abi3-py39`) `_core` module.
14
+ - `python/rsqlparse/` — drop-in Python package mirroring `sqlparse` (`parse`, `parsestream`, `format`, `split`, `tokens`, `sql`, `lexer`, `keywords`, `formatter`, `filters`, `engine`, `exceptions`, `utils`, `cli`).
15
+
16
+ ## Conventions
17
+
18
+ - `#![forbid(unsafe_code)]` across the crate.
19
+ - Zero non-PyO3 Rust dependencies.
20
+ - Keep Python typing 3.9-compatible (`typing.Union`, `typing.Optional`, etc. — no PEP 604 `X | Y` at runtime).
21
+ - Keep pytest `ids=` short so Windows path/name limits never break.
22
+ - Run `mise run check` before committing: `cargo fmt --check`, `cargo clippy --all-targets -- -D warnings`, `cargo test`, `maturin develop --release`, `pytest`, and `scripts/attribution-guard.sh files`.
23
+ - Git identity: `Brixton Pham <117471232+brixtonpham@users.noreply.github.com>`. Never add AI attribution anywhere.
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,132 @@
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 = "pyo3"
40
+ version = "0.30.1"
41
+ source = "registry+https://github.com/rust-lang/crates.io-index"
42
+ checksum = "229ca13b8a15bb0d1487b81ab975bfcd97513416499a8312011b6036e42e0de4"
43
+ dependencies = [
44
+ "libc",
45
+ "once_cell",
46
+ "portable-atomic",
47
+ "pyo3-build-config",
48
+ "pyo3-ffi",
49
+ "pyo3-macros",
50
+ ]
51
+
52
+ [[package]]
53
+ name = "pyo3-build-config"
54
+ version = "0.30.1"
55
+ source = "registry+https://github.com/rust-lang/crates.io-index"
56
+ checksum = "70130e2b6dd1ee589a8788026a429561ca5c40fa7a2fa7914c2282eddae24700"
57
+ dependencies = [
58
+ "target-lexicon",
59
+ ]
60
+
61
+ [[package]]
62
+ name = "pyo3-ffi"
63
+ version = "0.30.1"
64
+ source = "registry+https://github.com/rust-lang/crates.io-index"
65
+ checksum = "5e7ff73c555c286d212d4ce7e5fbda845a973c4f61bd5f160f5f2c3c2a04d1c4"
66
+ dependencies = [
67
+ "libc",
68
+ "pyo3-build-config",
69
+ ]
70
+
71
+ [[package]]
72
+ name = "pyo3-macros"
73
+ version = "0.30.1"
74
+ source = "registry+https://github.com/rust-lang/crates.io-index"
75
+ checksum = "c46d2e5862c7bb8207e3b639f713dda3c499933444784154c11d013c5a691b60"
76
+ dependencies = [
77
+ "proc-macro2",
78
+ "pyo3-macros-backend",
79
+ "quote",
80
+ "syn",
81
+ ]
82
+
83
+ [[package]]
84
+ name = "pyo3-macros-backend"
85
+ version = "0.30.1"
86
+ source = "registry+https://github.com/rust-lang/crates.io-index"
87
+ checksum = "38b89f066f2ae72a2aef52d1537ca2fcd104440ebfadee06e97aa90738a25b9b"
88
+ dependencies = [
89
+ "heck",
90
+ "proc-macro2",
91
+ "quote",
92
+ "syn",
93
+ ]
94
+
95
+ [[package]]
96
+ name = "quote"
97
+ version = "1.0.47"
98
+ source = "registry+https://github.com/rust-lang/crates.io-index"
99
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
100
+ dependencies = [
101
+ "proc-macro2",
102
+ ]
103
+
104
+ [[package]]
105
+ name = "rsqlparse"
106
+ version = "0.1.0"
107
+ dependencies = [
108
+ "pyo3",
109
+ ]
110
+
111
+ [[package]]
112
+ name = "syn"
113
+ version = "3.0.10"
114
+ source = "registry+https://github.com/rust-lang/crates.io-index"
115
+ checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
116
+ dependencies = [
117
+ "proc-macro2",
118
+ "quote",
119
+ "unicode-ident",
120
+ ]
121
+
122
+ [[package]]
123
+ name = "target-lexicon"
124
+ version = "0.13.6"
125
+ source = "registry+https://github.com/rust-lang/crates.io-index"
126
+ checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da"
127
+
128
+ [[package]]
129
+ name = "unicode-ident"
130
+ version = "1.0.27"
131
+ source = "registry+https://github.com/rust-lang/crates.io-index"
132
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
@@ -0,0 +1,29 @@
1
+ [package]
2
+ name = "rsqlparse"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ rust-version = "1.85"
6
+ description = "Blazing-fast, zero-dependency, ReDoS-immune O(n) Rust drop-in replacement for sqlparse"
7
+ license = "BSD-3-Clause"
8
+ readme = "README.md"
9
+ repository = "https://github.com/brixtonpham/rsqlparse"
10
+ keywords = ["sql", "parser", "formatter", "lexer", "pyo3"]
11
+ categories = ["parsing", "database"]
12
+
13
+ [lib]
14
+ name = "rsqlparse"
15
+ crate-type = ["cdylib", "rlib"]
16
+
17
+ [dependencies]
18
+ pyo3 = { version = "0.30", optional = true, features = ["abi3-py39"] }
19
+
20
+ [features]
21
+ default = []
22
+ python = ["dep:pyo3"]
23
+ extension-module = ["python", "pyo3/extension-module"]
24
+
25
+ [profile.release]
26
+ lto = "fat"
27
+ codegen-units = 1
28
+ opt-level = 3
29
+ strip = "symbols"
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Brixton Pham
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,183 @@
1
+ Metadata-Version: 2.4
2
+ Name: rsqlparse
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: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.9
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Rust
15
+ Classifier: Topic :: Database
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ License-File: LICENSE
18
+ Summary: Zero-dependency, ReDoS-immune O(n) Rust drop-in replacement for sqlparse
19
+ Keywords: sql,parser,formatter,sqlparse,rust,pyo3
20
+ Author-email: Brixton Pham <117471232+brixtonpham@users.noreply.github.com>
21
+ License: BSD-3-Clause
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
24
+ Project-URL: Homepage, https://github.com/brixtonpham/rsqlparse
25
+ Project-URL: Issues, https://github.com/brixtonpham/rsqlparse/issues
26
+ Project-URL: Repository, https://github.com/brixtonpham/rsqlparse
27
+
28
+ # rsqlparse
29
+
30
+ [![License: BSD-3-Clause](https://img.shields.io/badge/license-BSD--3--Clause-blue.svg)](https://github.com/brixtonpham/rsqlparse/blob/main/LICENSE)
31
+ [![Python 3.9–3.14 (abi3)](https://img.shields.io/badge/python-3.9%E2%80%933.14%20(abi3)-blue.svg)](https://github.com/brixtonpham/rsqlparse/blob/main/pyproject.toml)
32
+ [![Rust: zero unsafe](https://img.shields.io/badge/rust-forbid(unsafe__code)-orange.svg)](https://github.com/brixtonpham/rsqlparse/blob/main/src/lib.rs)
33
+ [![Upstream Compatibility](https://img.shields.io/badge/upstream%20tests-537%2F537%20(100%25)-brightgreen.svg)](https://github.com/brixtonpham/rsqlparse#upstream-compatibility)
34
+
35
+ **A zero-dependency, ReDoS-immune O(n) Rust drop-in replacement for [`sqlparse`](https://github.com/andialbrecht/sqlparse).**
36
+
37
+ Upstream `sqlparse` (129.6M PyPI downloads/month) entered **end-of-maintenance** with v0.6.1 after a wave of 2026 DoS, ReDoS, and O(n²) security advisories. `rsqlparse` replaces the regex-driven lexer, statement splitter, 25-pass AST grouper, and SQL formatter with a hand-written linear-time Rust state machine (`#![forbid(unsafe_code)]`, zero non-PyO3 Cargo dependencies, `abi3-py39` wheels supporting Python 3.9–3.14):
38
+
39
+ - **Up to 78x faster** `split()`, **62–71x faster** `format()`, and **17x faster** `parse()` on real-world SQL workloads
40
+ - **Up to 75x smaller peak Python heap** on `format()` (zero intermediate Python `Token` or `TokenList` allocations)
41
+ - **Sub-millisecond O(n) immunity** against all 6 published 2026 `sqlparse` DoS/ReDoS/injection CVEs (up to **208x faster**)
42
+ - **537 / 537 (100%) pass rate** on upstream `andialbrecht/sqlparse`'s full test suite (all 10 test modules)
43
+
44
+ ---
45
+
46
+ ## Install & 1-Line Drop-In Swap
47
+
48
+ ```bash
49
+ pip install rsqlparse
50
+ # or
51
+ uv add rsqlparse
52
+ ```
53
+
54
+ Swap one import line — zero other code changes:
55
+
56
+ ```python
57
+ import rsqlparse as sqlparse
58
+
59
+ # Split multi-statement SQL migrations (pure Rust, zero Python Token allocation)
60
+ stmts = sqlparse.split("SELECT 1; CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;")
61
+
62
+ # Format SQL end-to-end in Rust (returns a single str)
63
+ pretty = sqlparse.format(
64
+ "select u.id, u.name from users u where u.active = 1 and u.role = 'admin'",
65
+ reindent=True,
66
+ keyword_case="upper",
67
+ strip_comments=True,
68
+ )
69
+
70
+ # Full sqlparse.sql AST compatibility (Statement, Identifier, Where, Comparison, Function, ...)
71
+ stmt = sqlparse.parse("SELECT a.col AS alias1 FROM tbl a WHERE a.id = 42")[0]
72
+ assert stmt.get_type() == "SELECT"
73
+ ```
74
+
75
+ ---
76
+
77
+ ## Benchmarks
78
+
79
+ Measured against upstream `sqlparse` v0.6.1 on Apple Silicon (`Darwin arm64`, Python 3.14.10, `--release` LTO build). Every workload asserts **exact output equality** (including full recursive AST equivalence for `parse()`) before timing. Reproduce locally with `python benchmarks/bench.py`.
80
+
81
+ ### Real-World SQL Speed (median latency, lower is better)
82
+
83
+ | Workload | Input Size | `sqlparse` (Python) | `rsqlparse` (Rust) | Speedup |
84
+ |---|---:|---:|---:|---:|
85
+ | `split()` — 270-statement migration script | 50.2 KB | 34.23 ms | **0.44 ms** | **78.2x** |
86
+ | `format(keyword_case='upper', strip_comments=True)` | 13.4 KB | 47.46 ms | **0.67 ms** | **71.2x** |
87
+ | `format(reindent=True, keyword_case='upper', strip_comments=True)` | 13.4 KB | 57.12 ms | **0.92 ms** | **62.3x** |
88
+ | `parse()` — complex multi-CTE / window / JOIN analytics | 13.4 KB | 41.71 ms | **2.44 ms** | **17.1x** |
89
+
90
+ ```text
91
+ split() — 270-statement migration script (50.2 KB)
92
+ sqlparse ████████████████████████████████████████████ 34.23 ms
93
+ rsqlparse █ 0.44 ms (78.2x faster)
94
+
95
+ format(reindent=True, keyword_case='upper', strip_comments=True) (13.4 KB)
96
+ sqlparse ████████████████████████████████████████████ 57.12 ms
97
+ rsqlparse █ 0.92 ms (62.3x faster)
98
+
99
+ parse() — complex multi-CTE analytics batch (13.4 KB)
100
+ sqlparse ████████████████████████████████████████████ 41.71 ms
101
+ rsqlparse ███ 2.44 ms (17.1x faster)
102
+ ```
103
+
104
+ ### Peak Python Heap Memory (`tracemalloc`, lower is better)
105
+
106
+ | Workload | `sqlparse` Peak Py Heap | `rsqlparse` Peak Py Heap | Heap Reduction |
107
+ |---|---:|---:|---:|
108
+ | `format(reindent=True, keyword_case='upper', strip_comments=True)` | 1,128.4 KiB | **15.0 KiB** | **75.3x smaller** |
109
+ | `format(keyword_case='upper', strip_comments=True)` | 642.6 KiB | **14.4 KiB** | **44.6x smaller** |
110
+ | `split()` — 270-statement migration script | 394.5 KiB | **61.6 KiB** | **6.4x smaller** |
111
+ | `parse()` — full Python `sql.Statement` AST | 1,042.6 KiB | **1,016.7 KiB** | **1.0x** |
112
+
113
+ Because `split()` and `format()` execute lexing, statement splitting, grouping, and formatting **100% inside Rust**, Python allocates only the final `list[str]` or `str` — zero intermediate `Token` or `TokenList` objects.
114
+
115
+ ---
116
+
117
+ ## ReDoS & 2026 CVE Security Matrix
118
+
119
+ `rsqlparse` has **zero regular-expression engine dependencies** in its hot path. All lexing, delimited-span resolution, unquoted newline splitting, comment stripping, and grouping run in deterministic linear time with configurable depth (`MAX_GROUPING_DEPTH = 100`) and token (`MAX_GROUPING_TOKENS = 10000`) guards.
120
+
121
+ | Advisory / CVE | Attack Vector | `sqlparse 0.6.1` | `rsqlparse 0.1.0` | Speedup | Status |
122
+ |---|---|---:|---:|---:|---|
123
+ | **CVE-2026-59893** (`GHSA-prg7-hcfm-mfcr`) | 10,000 unclosed `$tag$` dollar quotes (`split`) | 71.51 ms | **0.62 ms** | **114.6x** | O(n) pre-indexed spans |
124
+ | **CVE-2026-59893** (`GHSA-prg7-hcfm-mfcr`) | 10,000 unclosed `/*` multiline comments (`split`) | 77.62 ms | **1.05 ms** | **74.0x** | O(n) pre-indexed spans |
125
+ | **CVE-2026-54284** | 1,000 deeply nested parentheses (`parse`) | 11.70 ms | **0.06 ms** | **208.2x** | Bounded depth -> `SQLParseError` |
126
+ | **CVE-2026-71491** (`GHSA-f2ff-p2ww-7p4p`) | 5,000 comment-only lines (`format(strip_comments=True)`) | 35.44 ms | **0.38 ms** | **92.7x** | Single-pass O(n) comment filter |
127
+ | **GHSA-3r5h-5jcg-gmgg** | 20,000 unclosed `'\` quotes (`format`) | 4.13 ms | **0.09 ms** | **43.5x** | O(n) unquoted newline scanner |
128
+ | **GHSA-cfqr-cjx5-5jcm** | 5,000-tuple `IN (...)` list (`format(reindent=True)`) | Bounded | **Bounded** | — | O(line) offset walk + token cap |
129
+ | **CVE-2026-59894** (`GHSA-3496-9g83-7v6x`) | Backslash breakout in `python`/`php` output | Escaped | **Escaped** | — | Backslash escaped before quotes |
130
+ | **CVE-2026-112307** (`GHSA-xmww-55xg-h3vc`) | `$var` / `${expr}` injection in `php` output | Escaped | **Escaped** | — | `$` escaped in PHP literals |
131
+
132
+ ---
133
+
134
+ ## Upstream Compatibility
135
+
136
+ Verified directly against [`andialbrecht/sqlparse`](https://github.com/andialbrecht/sqlparse) v0.6.1's complete pytest suite by aliasing `sqlparse` -> `rsqlparse` across all submodules:
137
+
138
+ | Upstream Test Suite | Result |
139
+ |---|---:|
140
+ | Full upstream suite (all 10 files: `test_cli`, `test_dos_prevention`, `test_format`, `test_grouping`, `test_keywords`, `test_parse`, `test_regressions`, `test_split`, `test_tokenize`, `test_utils`) | **537 / 537 passed (100%)** (+ 2 upstream `xfail`, 1 upstream `xpass` — identical to `sqlparse 0.6.1`, 4.1x faster wall time) |
141
+
142
+ All public modules and entry points are mirrored:
143
+ - `rsqlparse.split(sql, encoding=None, strip_semicolon=False)`
144
+ - `rsqlparse.format(sql, encoding=None, **options)`
145
+ - `rsqlparse.parse(sql, encoding=None)` & `rsqlparse.parsestream(stream, encoding=None)`
146
+ - `rsqlparse.tokens`, `rsqlparse.sql`, `rsqlparse.lexer`, `rsqlparse.keywords`, `rsqlparse.formatter`, `rsqlparse.filters`, `rsqlparse.engine`, `rsqlparse.exceptions`, `rsqlparse.utils`, `rsqlparse.cli`
147
+ - CLI entry point: `rsqlformat` (and `python -m rsqlparse`)
148
+ - Dynamic `Lexer` customization (`Lexer.get_default_instance().add_keywords(...)` / `set_SQL_REGEX(...)`) automatically falls back to custom rules and restores the Rust fast path on `default_initialization()`.
149
+
150
+ ---
151
+
152
+ ## Architecture
153
+
154
+ - [`src/tokens.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/tokens.rs) — compact 1-byte `TokenKind` hierarchy mirroring `sqlparse.tokens`.
155
+ - [`src/keywords.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/keywords.rs) — sorted static table of all 803 keywords across 9 SQL dialects (`COMMON`, `ORACLE`, `MYSQL`, `PLPGSQL`, `HQL`, `MSACCESS`, `SNOWFLAKE`, `BIGQUERY`, `KEYWORDS`) with zero heap allocation.
156
+ - [`src/lexer.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/lexer.rs) — hand-written O(n) deterministic state-machine SQL lexer (no backtracking regexes, O(n) pre-indexed delimited spans).
157
+ - [`src/splitter.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/splitter.rs) — stack-based O(n) `StatementSplitter` (`BEGIN`/`DECLARE`/`IF`/`CASE`/`FOR`/`WHILE`/`LOOP`/`END`, `GO`, parentheses).
158
+ - [`src/ast.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/ast.rs) & [`src/grouping.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/grouping.rs) — 25-pass SQL AST grouper with bounded depth and token caps.
159
+ - [`src/formatter.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/formatter.rs) — full pure-Rust formatting pipeline (case folding, comment stripping, whitespace normalization, reindent, aligned indent, Python/PHP output escaping, O(n) unquoted newline splitting).
160
+ - [`src/lib.rs`](https://github.com/brixtonpham/rsqlparse/blob/main/src/lib.rs) — `#![forbid(unsafe_code)]`, Rust library API, and PyO3 (`abi3-py39`) `_core` extension.
161
+
162
+ ---
163
+
164
+ ## Development
165
+
166
+ ```bash
167
+ # Run Rust fmt, clippy, unit tests, release maturin build, pytest, and attribution guard
168
+ cargo fmt --check
169
+ cargo clippy --all-targets -- -D warnings
170
+ cargo clippy --lib --features extension-module -- -D warnings
171
+ cargo test
172
+ maturin develop --release
173
+ pytest
174
+ python benchmarks/bench.py
175
+ bash scripts/attribution-guard.sh files
176
+ ```
177
+
178
+ ---
179
+
180
+ ## License
181
+
182
+ [BSD-3-Clause](https://github.com/brixtonpham/rsqlparse/blob/main/LICENSE)
183
+