Skip to main content

A fast SQL parser with Python wrapper and C++ core

Project description

fast-pysqlparse: High-Performance SQL Parsing Library

Build Status Language License

README.md (Chinese)

A high-performance, cross-platform SQL parsing library, designed to handle the most complex SQL queries with ease.

Overview

This library provides a robust set of tools for parsing and analyzing SQL statements. Built with a core engine in C++17 for maximum performance, it offers native Python bindings, making it the ideal choice for data-intensive applications where speed and accuracy are critical.

It excels at parsing extremely long SQL statements and queries with deeply nested subqueries, delivering performance far superior to pure-Python alternatives.

The parser is primarily tested against MySQL-style SQL. Current supported dialect names are exposed in fastsqlparse.conf.SUPPORT_DIALECTS.

Features

  • Fast SQL Parsing: Leverages a high-performance C++17 core to parse SQL statements rapidly
  • 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: 600+ characters
  • Total Time: 0.60s
  • PPS (Parses Per Second): 8000+
  • Average per parse: 0.1ms~0.15ms

Test 2: 10 Million Character SQL

  • SQL Length: 10,000,000 characters
  • Total Time: 1.40s
  • CPS (Characters Per Second): ~7,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 1.3054s 7,660,928.40
pglast 4.3322s 2,308,429.85
sqlglot (postgres) 22.9163s 436,392.53
sqlparse 87.2098s 114,671.60

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, and ParsedView.

  • 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

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) statement 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.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fast_pysqlparse-0.7.1-cp314-cp314-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.14Windows x86-64

fast_pysqlparse-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

fast_pysqlparse-0.7.1-cp313-cp313-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.13Windows x86-64

fast_pysqlparse-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

fast_pysqlparse-0.7.1-cp312-cp312-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.12Windows x86-64

fast_pysqlparse-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

fast_pysqlparse-0.7.1-cp311-cp311-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.11Windows x86-64

fast_pysqlparse-0.7.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

fast_pysqlparse-0.7.1-cp310-cp310-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.10Windows x86-64

fast_pysqlparse-0.7.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

File details

Details for the file fast_pysqlparse-0.7.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b7004c4df4a3d0969a67af4ce2f27b6c6b43fb673378a157ecb4fa5882f84d9d
MD5 2ae6a20ee399875b7199ca25d8b5ff64
BLAKE2b-256 0114b390ac4756834692f15620cf5758e1d3b18a41bfc825391558a44cffda81

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp314-cp314-win_amd64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e3605221919ab7aca4041e1fd01affeee3e1698989418e963de058220eec3325
MD5 82b33eb882ef269240c222f99bc524c5
BLAKE2b-256 eca45d744d45f1cbd0f640ebcec9b4b16cbd5539e0354243ca82ad23ee677bea

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6d50adf6f10df38331c8382db4ea6cc8d35e5f1d523198900c0373fa53e50d1e
MD5 a5611e6016bf4a4c92c06c17b78de3e8
BLAKE2b-256 5845643b05f42036892ab35ee9f669a6e2aef22f4b59fd3b5e7270004e090227

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp313-cp313-win_amd64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6c676a867dcd2a1d7daf609ba7f74674a4b0ec1219df7ca55b63bbef49c42bf4
MD5 52263b2e8b2a5abc6b1b7edb2f3a11e2
BLAKE2b-256 bf0eaf899e0fd760e7f58f2116277611ff1bac676e7a4f42fdd3f088f98a6b93

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6f6587a623af09796309517c8a449f8e99ff34e13ef0df7b2b5fb2ea4df6ae16
MD5 103d8c2cf01a6b9842b914dbaa099fe5
BLAKE2b-256 cdf28c375d018515863ec34d0679a0c81902971dda8449ccaf8ad5f13a690758

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp312-cp312-win_amd64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 792bbab1b7f0836c2fa3fc1759a9d3645c6690412c7db6aa3cbfedeab94dd74f
MD5 20f1f115c751c044e9ebce78e002b576
BLAKE2b-256 450636d2492b73d5d19cdd9b3c829e3a8e4640ccc0b98dc891e2499df8005688

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6f3db306f8b0b3258410d514153dd6e56485cf6018d29d3d6f1382a223b804b8
MD5 bc75d6ece6d550cb53af4e1bfbde3558
BLAKE2b-256 b23d33da50318665559949d37925199ff13208fc25f1e16feefa2f377e627398

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp311-cp311-win_amd64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 2ce1add36b9d9786b6e291f0ffcb9f0954bf81601ac35cbac0d1611a7b17a3ec
MD5 05f2d0d3367fc9b04fd6dcc3f17216d0
BLAKE2b-256 d01d64c4907f5268fefae18656236d0a360d2d16f8ec5a88dab3b7d8cb8eb585

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2d079837e2b5acb0b2d5dcc966e6d80e8c4c1d9b9debcb4c0f0f5f37ab4a2ea7
MD5 99a84b2554151e1e2045d50e14a959c2
BLAKE2b-256 a0049d1e7092ae1ef9285e7a1ccbd60df2004fafb0f759066fd39a3370920162

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp310-cp310-win_amd64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_pysqlparse-0.7.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_pysqlparse-0.7.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 58e948ab891a98925ebaf10b933f7aa9585eee52788aed28c8bb9ce6e30b0718
MD5 93398e4b9733943a0b717722f942ccf4
BLAKE2b-256 4c66ba96125f8235d650ff47f5feefdae1dac5af433c483032d88a42b0a542ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_pysqlparse-0.7.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: python-publish.yml on Nohaltsail/fast-pysqlparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page