Skip to content

sql

SQL files: layout, capitalization, and references in the dialect of your database. Level all adds the join, cast, subquery, and LIMIT rules. Guide: SQL.

A scope has file extensions .sql, .pgsql, or .psql. Also when postgres requires it.

Lints every SQL file for layout, capitalization, aliasing, and ambiguous references, in the dialect that sql.dialect names.

Runs at the commit stage. gspot check --fix fixes what it can.

When every SQL file follows one style, a reviewer can focus on what each statement does.

Run the fix: command that the report prints for the rules that fix themselves, then fix each line that SQLFluff still names.

The query select id from user_accounts; reports CP01 for lowercase keywords. Write it as SELECT id FROM user_accounts;.

gspot ignore sql/sqlfluff --paths "<path>" --reason "<why>"
gspot ignore sql/sqlfluff --rule <rule> --reason "<why>"

To silence one place, write a comment such as:

SELECT * FROM orders -- noqa: AM04 -- reason: the view mirrors every column

Checks, in the Postgres dialects, the keywords that SQLFluff does not read: the keywords and built-in types of PL/pgSQL, ANY, ALL, SOME, EXISTS, NOT, DISTINCT, and ARRAY before a bracket with their space, and the case of NEW, OLD, and the other PL/pgSQL names.

Runs at the commit stage.

SQLFluff reads a function body as a string and some keywords as function calls, so its capitalization rules pass them in any case.

Run gspot check --fix --only sql/keywords, which writes the keywords and types in upper case, the space before each bracket, and each PL/pgSQL name in one case.

if v_id is null then in a function body reports keyword-case on if and then. Run gspot check --fix to write them in upper case.

Rule What it checks What to do
keyword-case A keyword that SQLFluff does not read as one stands in upper case: ANY, ALL, SOME, EXISTS, NOT, DISTINCT, and ARRAY before a bracket, and the keywords of PL/pgSQL in a function body, such as BEGIN, IF, INTO, PERFORM, and RETURN. Run gspot check --fix, which writes the keyword in upper case.
keyword-bracket One space stands between ANY, ALL, SOME, EXISTS, NOT, or DISTINCT and the bracket after it, and none between ARRAY and its bracket. Run gspot check --fix, which writes the space.
special-name A PL/pgSQL name, NEW, OLD, FOUND, SQLERRM, SQLSTATE, or a TG_ name, stands in upper case or in lower case. Run gspot check --fix, which writes the name in upper case.
type-case A built-in type in the declarations of a PL/pgSQL body stands in upper case. Run gspot check --fix, which writes the type in upper case.
gspot ignore sql/keywords --paths "<path>" --reason "<why>"
gspot ignore sql/keywords --rule keyword-case --reason "<why>"
Setting What it sets
limits.max_file_lines The most lines one file may have, not counting blank lines and comment-only lines.
sql.dialect The SQL dialect that SQLFluff parses, such as postgres. The postgres configuration sets it. A [[scope]] sets another dialect for its folder.
Rule file Installed when
.gspot/rules/SQL.md Always
Tool Version
sqlfluff 4.4.0

.gspot/configurations/sql/sqlfluff.cfg