Skip to content

About

sqlparse's format(), split() and parse() in Rust: same output byte for byte, 30-150x faster. Drop-in for Django and django-debug-toolbar.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

sqlparse-turbo

PyPI version Python versions CI Wheels for Linux, macOS and Windows BSD-3-Clause license

sqlparse's format(), split() and parse(), rewritten in Rust:
same output, byte for byte, 30-150 times faster.

sqlparse.format(reindent=True, keyword_case='upper') over 400 SQL queries, each file at its measured time: sqlparse takes 3.61 s, sqlparse-turbo 0.053 s (69x), and both give the same output (same sha256).

pip install sqlparse-turbo "sqlparse==0.6.1"

Important

The speed-up needs sqlparse 0.6.1. With any other sqlparse every call still works, at sqlparse's own speed: sqlparse_turbo.ACTIVE is False and the import warns. Why

Use

import sqlparse_turbo

sqlparse_turbo.format(sql, reindent=True, keyword_case='upper')   # == sqlparse.format(sql, reindent=True, ...)
sqlparse_turbo.split(sql)                                          # == sqlparse.split(sql)
sqlparse_turbo.split_many(sqls)                                    # split() of each, on all CPU threads
sqlparse_turbo.parse(sql)                                          # == sqlparse.parse(sql): sqlparse's own objects

sqlparse_turbo.install()    # sqlparse.format / split / parse and sqlparse's lexer become the Rust ones,
                            # for every caller
sqlformat-turbo -r -k upper query.sql      # sqlparse's own `sqlformat` command, formatting in Rust

Django: add "sqlparse_turbo.django" to INSTALLED_APPS. Migrations, sqlmigrate, the test runner and django-debug-toolbar then run in Rust; the toolbar's SQL panel is 62x faster. More

Everything else, install(), FilterStack.run, bytes and streams: Usage.

Speed

Seconds for 400 SQL queries, one call per file, Python 3.13: split 0.97 s against 0.0065 s (150x), format with keyword_case 1.00 s against 0.0092 s (109x), format with reindent 3.61 s against 0.053 s (69x), parse 2.92 s against 0.097 s (30x).

Speed-ups with install(): format of bytes 87x, format of one short ORM query 71x, split of one short query 67x, FilterStack.run with an AlignedIndentFilter 66x, django-debug-toolbar's SQL panel 62x, parsestream with str() of each statement 23x.

Measure it on your machine in a few seconds with bench/quick_speed.py:

$ python bench/quick_speed.py
                              sqlparse       turbo    faster
format(reindent, upper)         145 ms      1.5 ms      100x
split                            38 ms      0.2 ms      213x
parse                           115 ms      3.9 ms       29x

The whole corpus, memory, against markocerkez/sqlparse-rs, how each number was made: Benchmarks.

How it works

Every call is checked first: when every check passes it runs in the Rust port, when any fails it goes to sqlparse 0.6.1 itself, unchanged; either way the output and errors are sqlparse's.

sqlparse is the pure-Python SQL splitter and formatter Django depends on, and it is slow: two megabytes of SQL take over ten seconds to format. Rust SQL libraries are fast but format differently. This is sqlparse 0.6.1 itself ported to Rust, quirks included, so the output does not change. When the Rust runs · Internals

Proven the same

Checked against sqlparse itself: 7,842 of 7,842 recorded calls give sqlparse's answer, sqlparse's own test suite passes (537), 260,837 trees compared node by node, 26.7 million texts over every Unicode code point, 25 of 25 platform and Python jobs in CI, and the recursion error at sqlparse's nesting depth on 3.10-3.14.

Wheels and CI: Linux x86_64, Linux aarch64, Windows x64, macOS arm64 and macOS Intel, each with CPython 3.10 to 3.14, all 25 jobs pass.

Every check and how it runs: Verification. What is known to differ: Limits.

Documentation

Page What is in it
Install why sqlparse 0.6.1, the warning on other versions, wheels, building from source
Usage every function, install(), FilterStack.run, bytes and streams, parsestream, the CLI
Django the app, check W001, django-debug-toolbar, migrations, tests
When the Rust runs which calls go to sqlparse instead, patched sqlparse, Unicode versions
Benchmarks every measurement, memory, against markocerkez/sqlparse-rs
Verification how the port is proven to give sqlparse's answers
Internals the lexer, the tree's quirks, what made it fast
Limits known differences and what is not tested
Development layout, building, the checks, releases
Changelog what each version changed

License

BSD-3-Clause, like sqlparse (LICENSE). The port follows sqlparse's code closely, so sqlparse's copyright notice is kept next to ours.

About

sqlparse's format(), split() and parse() in Rust: same output byte for byte, 30-150x faster. Drop-in for Django and django-debug-toolbar.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages