Skip to content

Layout Rules

This section contains 15 rule(s) related to layout.

Rule Summary

CodeNameAliasesFix Compatible
LT01layout.spacingL001, L005, L006, L008, L023, L024, L039, L048, L071
LT02layout.indentL002, L003, L004
LT03layout.operatorsL007
LT04layout.commasL019
LT05layout.long_linesL016
LT06layout.functionsL017
LT07layout.cte_bracketL018
LT08layout.cte_newlineL022
LT09layout.select_targetsL036
LT10layout.select_modifiersL041
LT11layout.set_operatorsL065
LT12layout.end_of_fileL009, layout.end-of-file
LT13layout.start_of_fileL050
LT14layout.keyword_newline-
LT15layout.newlines-

LT01: layout.spacing

Inappropriate Spacing.

PropertyValue
Namelayout.spacing
AliasesL001, L005, L006, L008, L023, L024, L039, L048, L071
Groupsall, core, layout
Auto-fixableYes ✅

This rule checks for an enforces the spacing as configured in layoutconfig. This includes excessive whitespace, trailing whitespace at the end of a line and also the wrong spacing between elements on the line. Because of this wide reach you may find that you wish to add specific configuration in your project to tweak how specific elements are treated. Rather than configuration on this specific rule, use the sqlfluff.layout section of your configuration file to customise how this rule operates.

The character represents a space in the examples below.

Anti-pattern

sql
SELECT
    a,        b(c) as d◦◦
FROM foo◦◦◦◦
JOIN bar USING(a)

Best practice

  • Unless an indent or preceding a comment, whitespace should be a single space.

  • There should also be no trailing whitespace at the ends of lines.

  • There should be a space after USING so that it's not confused for a function.

sql
SELECT
    a, b(c) as d
FROM foo
JOIN bar USING (a)

LT02: layout.indent

Incorrect Indentation.

PropertyValue
Namelayout.indent
AliasesL002, L003, L004
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

The character represents a space and the character represents a tab. In this example, the third line contains five spaces instead of four and the second line contains two spaces and one tab.

sql
SELECT
◦◦→a,
◦◦◦◦◦b
FROM foo

Best practice

Change the indentation to use a multiple of four spaces. This example also assumes that the indent_unit config value is set to space. If it had instead been set to tab, then the indents would be tabs instead.

sql
SELECT
◦◦◦◦a,
◦◦◦◦b
FROM foo

LT03: layout.operators

Operators should follow a standard for being before/after newlines.

PropertyValue
Namelayout.operators
AliasesL007
Groupsall, layout
Auto-fixableYes ✅

The configuration for whether operators should be trailing or leading is part of layoutconfig. The default configuration is:

ini
[sqlfluff:layout:type:binary_operator]
line_position = leading

[sqlfluff:layout:type:comparison_operator]
line_position = leading

Anti-pattern

In this example, if line_position = leading (or unspecified, as is the default), then the operator + should not be at the end of the second line.

sql
SELECT
    a +
    b
FROM foo

Best practice

If line_position = leading (or unspecified, as this is the default), place the operator after the newline.

sql
SELECT
    a
    + b
FROM foo

If line_position = trailing, place the operator before the newline.

sql
SELECT
    a +
    b
FROM foo

LT04: layout.commas

Leading/Trailing comma enforcement.

PropertyValue
Namelayout.commas
AliasesL019
Groupsall, layout
Auto-fixableYes ✅

The configuration for whether operators should be trailing or leading is part of layoutconfig. The default configuration is:

ini
[sqlfluff:layout:type:comma]
line_position = trailing

Anti-pattern

There is a mixture of leading and trailing commas.

sql
SELECT
    a
    , b,
    c
FROM foo

Best practice

By default, SQLFluff prefers trailing commas. However it is configurable for leading commas. The chosen style must be used consistently throughout your SQL.

sql
SELECT
    a,
    b,
    c
FROM foo

-- Alternatively, set the configuration file to 'leading'
-- and then the following would be acceptable:

SELECT
    a
    , b
    , c
FROM foo

LT05: layout.long_lines

Line is too long.

PropertyValue
Namelayout.long_lines
AliasesL016
Groupsall, core, layout
Auto-fixableYes ✅

Configuration

OptionDescription
ignore_comment_clausesShould comment clauses (e.g. column comments) be ignored when linting line lengths? Must be one of [True, False].
ignore_comment_linesShould lines that contain only whitespace and comments be ignored when linting line lengths? Must be one of [True, False].

LT06: layout.functions

Function name not immediately followed by parenthesis.

PropertyValue
Namelayout.functions
AliasesL017
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

In this example, there is a space between the function and the parenthesis.

sql
SELECT
    sum (a)
FROM foo

Best practice

Remove the space between the function and the parenthesis.

sql
SELECT
    sum(a)
FROM foo

LT07: layout.cte_bracket

WITH clause closing bracket should be on a new line.

PropertyValue
Namelayout.cte_bracket
AliasesL018
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

In this example, the closing bracket is on the same line as CTE.

sql
WITH zoo AS (
    SELECT a FROM foo)

SELECT * FROM zoo

Best practice

Move the closing bracket on a new line.

sql
WITH zoo AS (
    SELECT a FROM foo
)

SELECT * FROM zoo

LT08: layout.cte_newline

Blank line expected but not found after CTE closing bracket.

PropertyValue
Namelayout.cte_newline
AliasesL022
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

There is no blank line after the CTE closing bracket. In queries with many CTEs, this hinders readability.

sql
WITH plop AS (
    SELECT * FROM foo
)
SELECT a FROM plop

Best practice

Add a blank line.

sql
WITH plop AS (
    SELECT * FROM foo
)

SELECT a FROM plop

LT09: layout.select_targets

Select targets should be on a new line unless there is only one select target.

PropertyValue
Namelayout.select_targets
AliasesL036
Groupsall, layout
Auto-fixableYes ✅

NOTE

By default, a wildcard (e.g. SELECT *) is considered a single select target. If you want it to be treated as multiple select targets, configure wildcard_policy = multiple.

NOTE

By default (single_target_policy = same_line), a single select target is allowed to remain on the same line as the SELECT keyword (e.g. SELECT a FROM foo). If you want all select targets, including single ones, to be placed on a new line set single_target_policy = new_line. This gives consistent formatting regardless of the number of select targets.

Anti-pattern

Multiple select targets on the same line.

sql
select a, b
from foo;

-- Single select target on its own line
-- (with default `single_target_policy = same_line`).

SELECT
    a
FROM foo;

Best practice

Multiple select targets each on their own line.

sql
select
    a,
    b
from foo;

-- Single select target on the same line as the `SELECT`
-- keyword (with default `single_target_policy = same_line`).

SELECT a
FROM foo;

-- With `single_target_policy = new_line`, single select
-- targets must also be on a new line for consistency.

SELECT
    a
FROM foo;

-- When select targets span multiple lines, however they
-- can still be on a new line.

SELECT
    SUM(
        1 + SUM(
            2 + 3
        )
    ) AS col
FROM test_table;

Configuration

OptionDescription
single_target_policyTreatment of single select targets. same_line (default) allows a single select target on the same line as SELECT. new_line requires even single targets to be on a new line, giving consistent formatting regardless of the number of select targets. Must be one of ['same_line', 'new_line'].
wildcard_policyTreatment of wildcards. Defaults to single. Must be one of ['single', 'multiple'].

LT10: layout.select_modifiers

SELECT modifiers (e.g. DISTINCT) must be on the same line as SELECT.

PropertyValue
Namelayout.select_modifiers
AliasesL041
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

sql
select
    distinct a,
    b
from x

Best practice

sql
select distinct
    a,
    b
from x

LT11: layout.set_operators

Set operators should be surrounded by newlines.

PropertyValue
Namelayout.set_operators
AliasesL065
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

In this example, UNION ALL is not on a line itself.

sql
SELECT 'a' AS col UNION ALL
SELECT 'b' AS col

Best practice

sql
SELECT 'a' AS col
UNION ALL
SELECT 'b' AS col

LT12: layout.end_of_file

Files must end with a single trailing newline.

PropertyValue
Namelayout.end_of_file
AliasesL009, layout.end-of-file
Groupsall, core, layout
Auto-fixableYes ✅

Anti-pattern

The content in file does not end with a single trailing newline. The $ represents end of file.

sql
SELECT
    a
FROM foo$

-- Ending on an indented line means there is no newline
-- at the end of the file, the ◦ represents space.

SELECT
◦◦◦◦a
FROM
◦◦◦◦foo
◦◦◦◦$

-- Ending on a semi-colon means the last line is not a
-- newline.

SELECT
    a
FROM foo
;$

-- Ending with multiple newlines.

SELECT
    a
FROM foo

$

Best practice

Add trailing newline to the end. The $ character represents end of file.

sql
SELECT
    a
FROM foo
$

-- Ensuring the last line is not indented so is just a
-- newline.

SELECT
◦◦◦◦a
FROM
◦◦◦◦foo
$

-- Even when ending on a semi-colon, ensure there is a
-- newline after.

SELECT
    a
FROM foo
;
$

LT13: layout.start_of_file

Files must not begin with newlines or whitespace.

PropertyValue
Namelayout.start_of_file
AliasesL050
Groupsall, layout
Auto-fixableYes ✅

Anti-pattern

The file begins with newlines or whitespace. The ^ represents the beginning of the file.

sql
^

SELECT
    a
FROM foo

-- Beginning on an indented line is also forbidden,
-- (the ◦ represents space).

◦◦◦◦SELECT
◦◦◦◦a
FROM
◦◦◦◦foo

Best practice

Start file on either code or comment. (The ^ represents the beginning of the file.)

sql
^SELECT
    a
FROM foo

-- Including an initial block comment.

^/*
This is a description of my SQL code.
*/
SELECT
    a
FROM
    foo

-- Including an initial inline comment.

^--This is a description of my SQL code.
SELECT
    a
FROM
    foo

LT14: layout.keyword_newline

Keyword clauses should follow a standard for being before/after newlines.

PropertyValue
Namelayout.keyword_newline
Groupsall, layout
Auto-fixableYes ✅

Anti-pattern

In this example, the keyword are not at the beginning of or alone on the line.

sql
SELECT 'a' AS col FROM tab WHERE x = 4 ORDER BY y LIMIT 5

Best practice

sql
SELECT 'a' AS col
FROM tab
WHERE x = 4
ORDER BY y
LIMIT 5
sql
SELECT 'a' AS col
FROM
    tab
WHERE
    x = 4
ORDER BY
    y
LIMIT
    5

LT15: layout.newlines

Too many consecutive blank lines.

PropertyValue
Namelayout.newlines
Groupsall, layout
Auto-fixableYes ✅

Anti-pattern

In this example, the maximum number of empty lines inside a statement is set to 0.

sql
SELECT 'a' AS col
FROM tab

WHERE x = 4
ORDER BY y

LIMIT 5
;

Best practice

sql
SELECT 'a' AS col
FROM tab
WHERE x = 4
ORDER BY y
LIMIT 5
;

Configuration

OptionDescription
maximum_empty_lines_between_batchesThe maximum number of empty lines allowed between batches in T-SQL. Batches are separated by GO statements. Must be one of range(0, 1000).
maximum_empty_lines_between_statementsThe maximum number of empty lines allowed between statements. Note that currently, the gap before and after the semicolon is considered 'between' statements. Must be one of range(0, 1000).
maximum_empty_lines_inside_statementsThe maximum number of empty lines allowed inside statements. Must be one of range(0, 1000).

Released under the MIT License.