SQLFluff SQL linter and formatter

Version 4.4.0 · source

Start here

sqlfluff lint <path>
Report violations
sqlfluff fix <path>
Auto-fix them
sqlfluff format <path>
Reformat only
sqlfluff lint --dialect <name> <path>
Pick the SQL flavour
sqlfluff parse <file>
Show the parse tree
sqlfluff rules
List all rules
sqlfluff dialects
List dialects
cat <file> | sqlfluff lint -
Lint stdin

Concepts

  1. Template jinja / dbt
  2. Lex + parse per dialect
  3. Lint violations
  4. Fixed SQL written back

Lint

sqlfluff lint <path>
Lint a file or directory
sqlfluff lint .
Lint the current directory
sqlfluff lint -d <dialect> <path>
Set the dialect
sqlfluff lint -r <rules> <path>
Check only these rules
Comma-separated codes or names, e.g. LT01,CP01
sqlfluff lint -e <rules> <path>
Skip these rules
sqlfluff lint -f json <path>
Machine-readable output
sqlfluff lint --nofail <path>
Always exit 0
Useful while rolling out to a legacy codebase
sqlfluff lint -p <n> <path>
Parallel processes
0 = all CPUs, -1 = all but one
sqlfluff lint --stdin-filename <path> -
Pick config for stdin
sqlfluff lint --warn-unused-ignores <path>
Flag useless noqa comments

Fix and format

sqlfluff fix <path>
Fix violations in place
Rewrites files; commit or stash first
sqlfluff format <path>
Autoformat in place
sqlfluff fix -r <rules> <path>
Fix only these rules
sqlfluff fix --check <path>
Preview, then confirm
sqlfluff fix -x _fixed <path>
Write to a suffixed copy
sqlfluff fix --FIX-EVEN-UNPARSABLE <path>
Fix files with template errors
Risky: can produce broken SQL
cat <file> | sqlfluff fix -
Fix stdin to stdout

Inspect

sqlfluff parse <file>
Print the parse tree
sqlfluff parse -c <file>
Code elements only
sqlfluff render <file>
Show templated SQL
sqlfluff rules
List all rules
sqlfluff dialects
List all dialects

Options

--templater / -t <name>
raw, jinja, python, placeholder
--config <file>
Add an extra config file
--ignore-local-config
Skip discovered config files
--disable-noqa
Ignore all noqa comments

Config

[sqlfluff]
Core settings section
dialect = <name>
Default dialect
templater = jinja
Choose the templater
exclude_rules = <list>
Rules to skip
rules = <list>
Rules to run only
max_line_length = <n>
Line length limit (LT05)
[sqlfluff:rules:<rule>]
Per-rule settings
[sqlfluff:indentation]
Indent settings
[tool.sqlfluff]
Section in pyproject.toml
capitalisation_policy = <p>
consistent, upper or lower
.sqlfluffignore
Paths to skip

Ignoring code

-- noqa
Ignore all rules on this line
-- noqa: <rules>
Ignore listed rules on this line
-- noqa: disable=<rules>
Ignore from here on
-- noqa: enable=<rules>
Resume checking
-- noqa: disable=all
Ignore everything from here

Common rules

LT01
Spacing
LT02
Indentation
LT05
Line too long
LT12
Single trailing newline
LT09
Select targets on own lines
CP01
Keyword capitalisation
CP02
Identifier capitalisation
AL01
Table alias with AS
AL05
Unused table alias
RF01
Unresolvable references
RF02
Qualify multi-table columns
ST03
Unused CTE
ST05
Subquery instead of CTE

Integration

id: sqlfluff-lint
pre-commit hook: lint
Hook repo: https://github.com/sqlfluff/sqlfluff
id: sqlfluff-fix
pre-commit hook: fix
pip install sqlfluff
Install
pip install sqlfluff-templater-dbt
dbt templater plugin

Recipes

Minimal .sqlfluff
[sqlfluff]dialect = postgresexclude_rules = LT05, ST07max_line_length = 100[sqlfluff:indentation]tab_space_size = 4
CI lint of changed files
git diff --name-only origin/main -- '*.sql' \  | xargs sqlfluff lint -f github-annotation \  --annotation-level warning
pre-commit config
repos:  - repo: https://github.com/sqlfluff/sqlfluff    rev: <version>    hooks:      - id: sqlfluff-lint      - id: sqlfluff-fix
Adopt gradually
sqlfluff lint models/ --nofail -f json > baseline.jsonsqlfluff fix models/ -r LT01,LT02,CP01sqlfluff lint models/