sqlparse's format(),
split() and parse(), rewritten in Rust:
same output, byte for byte, 30-150 times
faster.
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
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 callersqlformat-turbo -r -k upper query.sql # sqlparse's own `sqlformat` command, formatting in RustDjango: 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.
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.
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
Every check and how it runs: Verification. What is known to differ: Limits.
| 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 |
BSD-3-Clause, like sqlparse (LICENSE). The port follows sqlparse's code closely, so sqlparse's
copyright notice is kept next to ours.
