SQLFluff
SQLFluff is an SQL linter and auto-formatter designed for humans, with a focus on configurability and extensibility. It helps enforce coding styles and catch common errors in SQL queries across various dialects. The current version, 4.1.0, continues to expand its Rust-backed execution, improve performance, and enhance dialect support. It maintains an active release cadence with minor versions often introducing performance gains and bug fixes, and major versions bringing significant architectural shifts.
Warnings
- breaking SQLFluff version 4.0.0 dropped support for Python 3.8. Users on Python 3.8 or older must upgrade to Python 3.9+ or remain on a SQLFluff 3.x release.
- breaking Version 4.1.0 introduced recursion protection via the `max_parse_depth` configuration setting. Extremely complex or deeply nested queries that previously parsed successfully might now fail with a recursion limit error if they exceed the default depth. This is a security feature to prevent resource exhaustion.
- gotcha The `sqlfluff[rs]` extra, introduced in 4.0.0, enables a Rust-backed parser for significant performance improvements on large SQL files. However, due to the overhead of marshalling data between Python and Rust, it can be slightly slower than the pure Python parser for very small, simple SQL files.
- gotcha SQLFluff configuration is hierarchical, loading from default values, `.sqlfluff` files, `pyproject.toml`, environment variables, and CLI arguments, with later sources overriding earlier ones. Misunderstanding this precedence can lead to rules not being applied as expected or unexpected dialect choices.
Install
-
pip install sqlfluff -
pip install sqlfluff[rs]
Imports
- lint
from sqlfluff import lint
- fix
from sqlfluff import fix
- parse
from sqlfluff import parse
Quickstart
from sqlfluff import lint, fix
sql_string = "SELECT a , b FROM my_table"
# Lint the SQL string
linter_results = lint(sql_string, dialect='ansi')
print("Linting Results:")
for result in linter_results:
print(result.as_dict()) # or result.to_string()
# Fix the SQL string
fixed_sql = fix(sql_string, dialect='ansi')
print("\nFixed SQL:")
print(fixed_sql)