Layout Rules
This section contains 15 rule(s) related to layout.
Rule Summary
| Code | Name | Aliases | Fix Compatible |
|---|---|---|---|
| LT01 | layout.spacing | L001, L005, L006, L008, L023, L024, L039, L048, L071 | ✅ |
| LT02 | layout.indent | L002, L003, L004 | ✅ |
| LT03 | layout.operators | L007 | ✅ |
| LT04 | layout.commas | L019 | ✅ |
| LT05 | layout.long_lines | L016 | ✅ |
| LT06 | layout.functions | L017 | ✅ |
| LT07 | layout.cte_bracket | L018 | ✅ |
| LT08 | layout.cte_newline | L022 | ✅ |
| LT09 | layout.select_targets | L036 | ✅ |
| LT10 | layout.select_modifiers | L041 | ✅ |
| LT11 | layout.set_operators | L065 | ✅ |
| LT12 | layout.end_of_file | L009, layout.end-of-file | ✅ |
| LT13 | layout.start_of_file | L050 | ✅ |
| LT14 | layout.keyword_newline | - | ✅ |
| LT15 | layout.newlines | - | ✅ |
LT01: layout.spacing
Inappropriate Spacing.
| Property | Value |
|---|---|
| Name | layout.spacing |
| Aliases | L001, L005, L006, L008, L023, L024, L039, L048, L071 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
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
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
USINGso that it's not confused for a function.
SELECT
a, b(c) as d
FROM foo
JOIN bar USING (a)LT02: layout.indent
Incorrect Indentation.
| Property | Value |
|---|---|
| Name | layout.indent |
| Aliases | L002, L003, L004 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
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.
SELECT
◦◦→a,
◦◦◦◦◦b
FROM fooBest 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.
SELECT
◦◦◦◦a,
◦◦◦◦b
FROM fooLT03: layout.operators
Operators should follow a standard for being before/after newlines.
| Property | Value |
|---|---|
| Name | layout.operators |
| Aliases | L007 |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
The configuration for whether operators should be trailing or leading is part of layoutconfig. The default configuration is:
[sqlfluff:layout:type:binary_operator]
line_position = leading
[sqlfluff:layout:type:comparison_operator]
line_position = leadingAnti-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.
SELECT
a +
b
FROM fooBest practice
If line_position = leading (or unspecified, as this is the default), place the operator after the newline.
SELECT
a
+ b
FROM fooIf line_position = trailing, place the operator before the newline.
SELECT
a +
b
FROM fooLT04: layout.commas
Leading/Trailing comma enforcement.
| Property | Value |
|---|---|
| Name | layout.commas |
| Aliases | L019 |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
The configuration for whether operators should be trailing or leading is part of layoutconfig. The default configuration is:
[sqlfluff:layout:type:comma]
line_position = trailingAnti-pattern
There is a mixture of leading and trailing commas.
SELECT
a
, b,
c
FROM fooBest practice
By default, SQLFluff prefers trailing commas. However it is configurable for leading commas. The chosen style must be used consistently throughout your 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 fooLT05: layout.long_lines
Line is too long.
| Property | Value |
|---|---|
| Name | layout.long_lines |
| Aliases | L016 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Configuration
| Option | Description |
|---|---|
ignore_comment_clauses | Should comment clauses (e.g. column comments) be ignored when linting line lengths? Must be one of [True, False]. |
ignore_comment_lines | Should 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.
| Property | Value |
|---|---|
| Name | layout.functions |
| Aliases | L017 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
In this example, there is a space between the function and the parenthesis.
SELECT
sum (a)
FROM fooBest practice
Remove the space between the function and the parenthesis.
SELECT
sum(a)
FROM fooLT07: layout.cte_bracket
WITH clause closing bracket should be on a new line.
| Property | Value |
|---|---|
| Name | layout.cte_bracket |
| Aliases | L018 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
In this example, the closing bracket is on the same line as CTE.
WITH zoo AS (
SELECT a FROM foo)
SELECT * FROM zooBest practice
Move the closing bracket on a new line.
WITH zoo AS (
SELECT a FROM foo
)
SELECT * FROM zooLT08: layout.cte_newline
Blank line expected but not found after CTE closing bracket.
| Property | Value |
|---|---|
| Name | layout.cte_newline |
| Aliases | L022 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
There is no blank line after the CTE closing bracket. In queries with many CTEs, this hinders readability.
WITH plop AS (
SELECT * FROM foo
)
SELECT a FROM plopBest practice
Add a blank line.
WITH plop AS (
SELECT * FROM foo
)
SELECT a FROM plopLT09: layout.select_targets
Select targets should be on a new line unless there is only one select target.
| Property | Value |
|---|---|
| Name | layout.select_targets |
| Aliases | L036 |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
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.
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.
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
| Option | Description |
|---|---|
single_target_policy | Treatment 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_policy | Treatment 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.
| Property | Value |
|---|---|
| Name | layout.select_modifiers |
| Aliases | L041 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
select
distinct a,
b
from xBest practice
select distinct
a,
b
from xLT11: layout.set_operators
Set operators should be surrounded by newlines.
| Property | Value |
|---|---|
| Name | layout.set_operators |
| Aliases | L065 |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
In this example, UNION ALL is not on a line itself.
SELECT 'a' AS col UNION ALL
SELECT 'b' AS colBest practice
SELECT 'a' AS col
UNION ALL
SELECT 'b' AS colLT12: layout.end_of_file
Files must end with a single trailing newline.
| Property | Value |
|---|---|
| Name | layout.end_of_file |
| Aliases | L009, layout.end-of-file |
| Groups | all, core, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
The content in file does not end with a single trailing newline. The $ represents end of file.
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.
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.
| Property | Value |
|---|---|
| Name | layout.start_of_file |
| Aliases | L050 |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
The file begins with newlines or whitespace. The ^ represents the beginning of the file.
^
SELECT
a
FROM foo
-- Beginning on an indented line is also forbidden,
-- (the ◦ represents space).
◦◦◦◦SELECT
◦◦◦◦a
FROM
◦◦◦◦fooBest practice
Start file on either code or comment. (The ^ represents the beginning of the file.)
^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
fooLT14: layout.keyword_newline
Keyword clauses should follow a standard for being before/after newlines.
| Property | Value |
|---|---|
| Name | layout.keyword_newline |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
In this example, the keyword are not at the beginning of or alone on the line.
SELECT 'a' AS col FROM tab WHERE x = 4 ORDER BY y LIMIT 5Best practice
SELECT 'a' AS col
FROM tab
WHERE x = 4
ORDER BY y
LIMIT 5SELECT 'a' AS col
FROM
tab
WHERE
x = 4
ORDER BY
y
LIMIT
5LT15: layout.newlines
Too many consecutive blank lines.
| Property | Value |
|---|---|
| Name | layout.newlines |
| Groups | all, layout |
| Auto-fixable | Yes ✅ |
Anti-pattern
In this example, the maximum number of empty lines inside a statement is set to 0.
SELECT 'a' AS col
FROM tab
WHERE x = 4
ORDER BY y
LIMIT 5
;Best practice
SELECT 'a' AS col
FROM tab
WHERE x = 4
ORDER BY y
LIMIT 5
;Configuration
| Option | Description |
|---|---|
maximum_empty_lines_between_batches | The 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_statements | The 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_statements | The maximum number of empty lines allowed inside statements. Must be one of range(0, 1000). |