fast-pysqlparse: High-Performance SQL Parsing Library
A high-performance, cross-platform, lightweight SQL parsing library whose core trait is speed — built on a C++17 core with native Python bindings, it rapidly performs structured parsing of SQL, especially statements with highly complex structure and deep nesting.
Overview
fast-pysqlparse aims to overcome the performance and capability limits of traditional Python SQL parsers. By moving compute-intensive parsing into a native C++ layer, it maintains fast parsing even on large, deeply nested, structurally complex SQL.
The parser primarily parses ANSI-style SQL. For statements with a specific dialect, specifying the dialect is recommended; supported dialects: MySQL, PostgreSQL, SQLite, Doris.
Features
- Fast SQL Parsing: Leverages a high-performance C++17 core to parse SQL statements rapidly
- Structured Parsing: Oriented toward complex structure and deep nesting, especially statements mixing CTEs with SELECT/INSERT/VIEW
- Cross-Platform: Compiled into native extensions (.pyd for Windows, .so for Linux)
- Comprehensive SQL Support: Supports a wide range of SQL statements, including:
- SELECT (with complex JOIN, WHERE, GROUP BY, subqueries, etc.)
- INSERT
- Data Definition Language (CREATE)
- VIEW
- DELETE
- UPDATE
- Common Table Expressions (CTEs), including nested CTEs
- Abstract Syntax Tree (AST): Generates a detailed JSON representation of the parsed SQL AST for easy traversal and analysis
- SQL Formatting: Automatically reformats messy SQL into a clean, readable structure
- Table Lineage Parsing: Automatically traces and reveals the source-to-target relationships between tables (data lineage)
- Tokenization: Breaks down SQL statements into their fundamental tokens for lexical analysis
- Python API: A clean and intuitive Python library built around the high-speed native extension
Performance
This library is engineered for speed. By moving the computationally intensive parsing work to a native C++ layer, it significantly outperforms pure-Python parsing libraries, especially when dealing with large, complex SQL scripts.
Benchmark Results
Test 1: 5000 Iterations
- SQL Length: 639 characters
- Total Time: 0.48s
- PPS (Parses Per Second): ~10300
- Average per parse: ~0.097ms
Test 2: 10 Million Character SQL
- SQL Length: 10,500,998 characters
- Total Time: 0.54s
- CPS (Characters Per Second): ~19,500,000
- Parse successful!
Test 3: Python-Only Comparison on ~10M PostgreSQL SQL (No C Benchmark)
Benchmark script: test/python_parsers_10m.py
| Parser | Avg Time | CPS |
|---|---|---|
| fastsqlparse | 0.2683s | 37,275,695.97 |
| pglast | 4.8541s | 2,060,217.57 |
| sqlglot (postgres) | 20.5364s | 486,963.12 |
| sqlparse | 84.9591s | 117,709.35 |
Notes:
- SQL size: 10,000,484 chars
- Runs per parser: 1
- Results source: results of
test/python_parsers_10m.py
Installation
pip install fast-pysqlparse
From Source:
git clone https://github.com/Nohaltsail/fast-pysqlparse.git
cd fast-pysqlparse
pip install build
python -m build
cd dist
pip install fast_pysqlparse-*.whl
Quick Start
from fastsqlparse import Parsed, ParsedQuery
if __name__ == '__main__':
sql = """
-- main query
SELECT
'Monthly Sales Report' AS report_type,
ms.year,
ms.month,
ms.region,
ms.customer_segment,
ms.unique_customers,
ms.total_orders,
ms.gross_sales,
ms.avg_order_value,
ms.cancelled_orders,
(SELECT SUM(gross_sales) FROM sub_monthly_sales WHERE year = ms.year AND month = ms.month) AS total_monthly_sales,
ms.gross_sales / NULLIF((SELECT SUM(gross_sales) FROM monthly_sales WHERE year = ms.year AND month = ms.month), 0) * 100 AS sales_percentage,
(SELECT AVG(avg_order_value) FROM monthly_sales WHERE year = ms.year AND month = ms.month) AS overall_avg_order_value
FROM monthly_sales ms
UNION ALL
SELECT
'Category Performance' AS report_type,
cs.year,
cs.month,
NULL AS region,
cs.category_name AS customer_segment,
cs.unique_buyers AS unique_customers,
cs.order_count AS total_orders,
cs.total_sales AS gross_sales,
cs.total_sales / NULLIF(cs.order_count, 0) AS avg_order_value,
NULL AS cancelled_orders,
(SELECT SUM(total_sales) FROM sub_category_sales WHERE year = cs.year AND month = cs.month) AS total_monthly_sales,
cs.total_sales / NULLIF((SELECT SUM(total_sales) FROM category_sales WHERE year = cs.year AND month = cs.month), 0) * 100 AS sales_percentage,
NULL AS overall_avg_order_value
FROM category_sales cs
LIMIT 50, 100"""
sql_len = len(sql)
print("sql length: ", sql_len)
# parse sql statements to SQL object
sql_stmt = Parsed(sql)
# Format and print the SQL statement with proper indentation
print(sql_stmt.format()) # Output formatted SQL statement
# Tokenization - returns list of tuples containing (token_value, token_type, position)
tokens = ParsedQuery.tokenize(sql) # Get tuple list of token information (token_value, token_type, position)
# Alternative tokenization - returns list of token objects with attributes
token_obj_list = sql_stmt.tokens() # Get object list of token information
# Generate and print Abstract Syntax Tree (AST) in JSON format
print(sql_stmt.AST()) # Get JSON structure of the SQL statement
# Extract table lineage/dependencies from the query
src_tables = ParsedQuery.parse_dependence(sql) # Get source tables (dependencies) of the query
Comment Handling (pure)
pure controls SQL comment handling in parser constructors such as Parsed,
ParsedQuery, ParsedInsert, ParsedCTE, ParsedUpdate, ParsedDelete,
ParsedView, and ParsedCreate.
pure=False(default): keep comments in parsing/formatting output.pure=True: strip--and/* ... */comments before parsing; formatted output and token results exclude comments, and parsing may be faster.
from fastsqlparse import Parsed
parsed_keep = Parsed(sql, pure=False) # preserve comments
parsed_clean = Parsed(sql, pure=True) # strip comments before parse
Dialects (dialect)
dialect selects the SQL dialect for parsing and lexical analysis (default
"ansi"). Every parser constructor accepts dialect as a string; tokenize
classmethods and ParsedQuery.parse_dependence accept a DialectType
(default DialectType.ANSI).
Supported dialects: ansi, mysql, postgresql, sqlite, doris
(see the Dialects enum and the DIALECT_* constants in fastsqlparse.conf).
Dialect handling is lenient by design: an unknown or empty dialect string falls
back to ansi in the native parser. The Python boundary emits a UserWarning
when this happens, so typos such as "postgre" or "MySQL" are visible instead
of fully silent (use fastsqlparse.check_dialect /
fastsqlparse.SUPPORTED_DIALECTS to check programmatically); non-string values
raise TypeError.
from fastsqlparse import Parsed, ParsedQuery, Dialects, DialectType
parsed = Parsed(sql, dialect=Dialects.MYSQL.value) # "mysql"
query = ParsedQuery(sql, "q", dialect="postgresql")
ParsedQuery.tokenize(sql, dialect=DialectType.MYSQL) # typed DialectType
ParsedQuery.parse_dependence(sql, dialect="mysql")
ParsedQuery(sql, "q", dialect="postgre") # UserWarning: falling back to 'ansi'
When to Use Which Parser
| Scenario | Parser to Use |
|---|---|
| SQL statement type is unknown or you don't want to specify the type | Parsed/ParsedOne |
Multiple SQL statements separated by ; (script execution) |
Parsed |
| SELECT / query statement | ParsedQuery |
| INSERT statement | ParsedInsert |
| DELETE statement | ParsedDelete |
| UPDATE statement | ParsedUpdate |
| CREATE TABLE statement | ParsedCreate |
| CREATE VIEW statement | ParsedView |
| CTE (WITH clause) fragment only | ParsedCTE |
Note: If your SQL contains multiple statements separated by semicolons (e.g., a script with CREATE, INSERT, SELECT), you must use
Parsed. The type-specific parsers are designed for single, known-type statements only.
ParsedCTEaccepts the WITH-clause fragment (WITH a AS (...) , b AS (...)) — not a full statement. A completeWITH ... SELECT ...statement must be parsed withParsedQuery(use its.cte/.available_cte()to inspect the CTEs).
Behavior Notes
ParsedQuery(...).parsed.sourceslists top-level FROM tables only. Tables that only appear inside scalar subqueries of the SELECT list are found in the ASTsubqueryfield and inparse_dependence()output.ParsedQuery.parse_dependence()returns CTE names unresolved: forWITH a AS (SELECT ...) SELECT * FROM a JOIN b, it returns{b, a}. Distinguish CTE names from physical tables yourself if needed.- The top-level
format(sql, s_type, ...)function requires a statement type:"query","insert","update","delete","create","cte","view". - Column objects expose the column name as
.field(DqlColumnExpr.field, not.name).
Documentation
For complete API documentation, see: API_DOC.md
Contributing
Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest new features.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Note on Dynamic Libraries
This project currently distributes precompiled dynamic libraries (.pyd and .so).
The corresponding C++ source code for these dynamic libraries is temporarily not public and is planned to be opened in a future release.
For the full supplementary notice, see LICENSE.
You can also use the dynamic libraries from the source code directly to develop your own SQL parsing library.
Metadata
Release files for fast-pysqlparse 0.8.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 14.9 MB
Release files / fast_pysqlparse-0.8.6-cp314-cp314-win_amd64.whl
| Download URL | fast_pysqlparse-0.8.6-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
07e63f905572c319d4b74373606fc1760d2fbc2fe9ce26c4bf2d2d03260ff60e
|
|
BLAKE2b-256 checksum How to use checksums |
adcdf0f92215e9103549127312314744de657527bf22d888e0b48b341297c704
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | fast_pysqlparse-0.8.6-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
26f9176d2695032c436934fcee6bf94f57eed088ed62f5b1a91363d6dde7830a
|
|
BLAKE2b-256 checksum How to use checksums |
47d55bbb0274d2d94b08bd1c8c8eef6c7eef48002b4d794103acc54d51484fd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp313-cp313-win_amd64.whl
| Download URL | fast_pysqlparse-0.8.6-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
3e537375b9e8501f2f4234c7c1b93226ad9da7c98bda85b9b997e77ac555eff9
|
|
BLAKE2b-256 checksum How to use checksums |
36645f7037836721f2a4e253a9f0a8c3ef86b5822536438d6de17d2f229fa75a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | fast_pysqlparse-0.8.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
1e9f2eec721e4ffcd06a436b7d8b6a3f15867dd90f9628f54479d252851afc9b
|
|
BLAKE2b-256 checksum How to use checksums |
00e7f3960f0f64422a224de91c4eb553f1399f7a04c7719b3dd3a2f5d0bf6272
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp312-cp312-win_amd64.whl
| Download URL | fast_pysqlparse-0.8.6-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c0185198eec859fcc7fceb813ceda3d05e8e7ca42355b711bc6eff3a40b14089
|
|
BLAKE2b-256 checksum How to use checksums |
42ab18a6870e82e7063c5a993a022206b256b178f8db28c33cff0d919320dc1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | fast_pysqlparse-0.8.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
9af99347ae2e91023c80f26d70e58f2e44f2c31690da74b603523417bad6ae94
|
|
BLAKE2b-256 checksum How to use checksums |
70c6fb0f3cd6b9c872cbcd69cebe00f14a56c6bcabe19bca07b0f5de504a6198
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp311-cp311-win_amd64.whl
| Download URL | fast_pysqlparse-0.8.6-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
6bee276d0effda315d24b80ea52e454ea93dcd08e9a4d5202407f9a00b83843b
|
|
BLAKE2b-256 checksum How to use checksums |
aa175f16c6b6c53d8d42d1aecf463b46bbff20a7c7285b43ebddadf95f8e138d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | fast_pysqlparse-0.8.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
c22d6b779bbb1560da8e11714f7e10a9317f126b1c17804c3c153acd5e1629ea
|
|
BLAKE2b-256 checksum How to use checksums |
d119656002a2e7f7cf6b33464baa5a8d56e381c93bdcbe947bfb71f982382a82
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp310-cp310-win_amd64.whl
| Download URL | fast_pysqlparse-0.8.6-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c60fdf0eb2150b65db18710f995fbbf780c260bf96088f733ac162e1c7b2872d
|
|
BLAKE2b-256 checksum How to use checksums |
6ccbe3796914de3dcc9c7b8c57d236e9e65b4074f7199b03ae1b87d1baf36d6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency logRelease files / fast_pysqlparse-0.8.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | fast_pysqlparse-0.8.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f938734f59c9eeda9c4b05ea97268aa8aed05aa80300d3e40f77c8b08b758454
|
|
BLAKE2b-256 checksum How to use checksums |
1536c2880269e551bcda910d09954421cddf16d5b088af7a1d713bac837aa578
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.
Transparency log