Rust-powered · Free and open source · Apache-2.0
Catch SQL that looks right but isn't.
A SQL linter and formatter, 50× faster than sqruff.
sqlclean checkerror[SQLCLEAN001]: Comparison with NULL is never true --> models/open_orders.sql:2:20 | 2 | where cancelled_at = null | ^ = help: Use IS NULL or IS NOT NULL when testing for NULL. = fix: available with `sqlclean fix --unsafe-fixes` error[SQLCLEAN019]: NOT IN list contains NULL and can reject every row --> models/returns.sql:2:14 | 2 | where reason not in ('fraud', null) | ^^^ = help: Remove NULL from the list or express the anti-join with a NULL-safe NOT EXISTS. Found 2 problems (1 fixable with `sqlclean fix --unsafe-fixes`).
sqlclean fix --unsafe-fixes --diff models/open_orders.sql--- a/models/open_orders.sql+++ b/models/open_orders.sql@@ -1,2 +1,5 @@-select order_id, customer_id from orders-where cancelled_at = null+SELECT+ order_id,+ customer_id+FROM orders+WHERE cancelled_at IS NULLWould fix 1 problem(s) and change 1 file(s). sqlclean fix --unsafe-fixes models/open_orders.sqlFixed 1 problem(s) and changed 1 file(s).All checks passed.
# sql_rules/team.py@rule( code="XSQLCLEANTEAM001", message="Use COALESCE instead of IFNULL", …)def prefer_coalesce(…): … sqlclean checkerror[XSQLCLEANTEAM001]: Use COALESCE instead of IFNULL --> models/discounts.sql:1:8 | 1 | SELECT IFNULL(discount, 0) FROM orders | ^^^^^^ = help: Write COALESCE(value, fallback). Found 1 problem.
Under a second, in a fifth of the memory
How we measured →10,282 SQL files · lint
- sqlclean
- 0.81 s
- sqruff
- 41.0 s
10,282 SQL files · peak memory
- sqlclean
- 48 MiB
- sqruff
- 235 MiB
- SQLFluff
- 992 MiB
10,282 SQL files · lint again, nothing changed
- sqlclean
- 0.23 s
- sqruff
- no result cache41.0 s
From a messy query to a clean one
Check, fix and format, then keep it that way with your own Rules.
Find the problems
Flag the SQL traps reviewers miss
Default Rules point at the exact characters and say what to write instead.
sqlclean check models/open_orders.sqlerror[SQLCLEAN018]: ROW_NUMBER has no deterministic ordering --> models/open_orders.sql:1:18 | 1 | SELECT order_id, ROW_NUMBER() OVER (PARTITION BY customer_id) AS order_rank | ^^^^^^^^^^ = help: Add ORDER BY with a stable tie-breaker inside the window specification. error[SQLCLEAN001]: Comparison with NULL is never true --> models/open_orders.sql:3:20 | 3 | WHERE cancelled_at = NULL | ^ = help: Use IS NULL or IS NOT NULL when testing for NULL. = fix: available with `sqlclean fix --unsafe-fixes` Found 2 problems (1 fixable with `sqlclean fix --unsafe-fixes`).
Fix them
Apply safe fixes, hold back the rest
A fix that could change which rows match, like = NULL to IS NULL, waits for --unsafe-fixes.
sqlclean fix --unsafe-fixes --diff models/open_orders.sql--- a/models/open_orders.sql+++ b/models/open_orders.sql@@ -1,3 +1,5 @@-SELECT order_id, ROW_NUMBER() OVER (PARTITION BY customer_id) AS order_rank+SELECT+ order_id,+ ROW_NUMBER() OVER (PARTITION BY customer_id) AS order_rank FROM orders-WHERE cancelled_at = NULL+WHERE cancelled_at IS NULLWould fix 1 problem(s) and change 1 file(s).
One layout for every file
Upper-case keywords, one column per line and every clause on its own line.
sqlclean format --diff models/revenue.sql--- a/models/revenue.sql+++ b/models/revenue.sql@@ -1,3 +1,6 @@-select o.order_id, coalesce(sum(p.amount_cents), 0) as paid_cents-from orders o left join payments p on p.order_id = o.order_id-group by o.order_id+SELECT+ o.order_id,+ COALESCE(SUM(p.amount_cents), 0) AS paid_cents+FROM orders o+LEFT JOIN payments p ON p.order_id = o.order_id+GROUP BY o.order_id1 file(s) would be reformatted, 0 file(s) already formatted.
Keep them fixed
Write your conventions as Rules
A custom Rule is a plain Python function over SQL that is already parsed.
# sql_rules/team.pyfrom sqlclean import Finding, RuleContext, Statement, rule @rule( code="XSQLCLEANTEAM001", message="Use COALESCE instead of IFNULL", remediation="Write COALESCE(value, fallback).", enabled_by_default=True,)def prefer_coalesce(*, statement: Statement, ctx: RuleContext) -> list[Finding]: return [ ctx.finding(subject=statement, node=function) for function in statement.functions if function.name.upper() == "IFNULL" ] sqlclean check models/discounts.sqlerror[XSQLCLEANTEAM001]: Use COALESCE instead of IFNULL --> models/discounts.sql:1:8 | 1 | SELECT IFNULL(discount, 0) FROM orders | ^^^^^^ = help: Write COALESCE(value, fallback).
Test Rules like any other code
sqlclean.testing checks each case with the same engine as sqlclean check.
# tests/test_team.pyfrom sql_rules.team import prefer_coalescefrom sqlclean.testing import RuleCase, evaluate_rule def test_ifnull_is_reported() -> None: evaluate_rule( rule=prefer_coalesce, test_case=RuleCase( description="prefer portable null handling", source="SELECT IFNULL(discount, 0) FROM orders;", expected_finding_count=1, ), ) pytest -q tests. [100%]1 passed in 0.02s
Allow a finding once, with a reason
An exception that stops matching fails the check, so it can't outlive the SQL it covered.
# sqlclean.toml[[rules.rule_exceptions]]rule = "SQLCLEAN038"path = "models/calendar.sql"relation = "regions"reason = "Every day is reported for every region." # the CROSS JOIN is now a keyed joinsqlclean check --select SQLCLEAN038 models/calendar.sqlerror: stale rule exception: SQLCLEAN038 in models/calendar.sql for relation regions matches no finding; remove it from sqlclean.toml Found 1 problem.
Before every commit and in CI
Pre-commit hooks fix and format staged SQL. In CI, check fails on findings and writes SARIF or JSON.
# .pre-commit-config.yamlrepos: - repo: https://github.com/sqlstack-io/sqlclean rev: v0.12.5 hooks: - id: sqlclean-fix - id: sqlclean-format sqlclean check --output-format sarif > sqlclean.sarifhead -3 sqlclean.sarif{ "$schema": "https://json.schemastore.org/sarif-2.1.0.json", "version": "2.1.0",
Each panel shows real sqlclean output on a small example project.
Start in a minute
Install it and point it at your SQL. Add a sqlclean.toml to pick a dialect or Rules.
- No account, server or plugins to set up.
- Free and open source, with no paid tier.
pip install sqlclean sqlclean check … Found 1 problem (1 fixable with `sqlclean fix --unsafe-fixes`). sqlclean fix --unsafe-fixes Fixed 1 problem(s) and changed 1 file(s). All checks passed.
Make clean SQL the default
Browse every Rule with examples, then set it up in CI.