Rules Reference¶
Rules in sqlfluff are implemented as crawlers. These are entities which work their way through the parsed structure of a query to evaluate a particular rule or set of rules. The intent is that the definition of each specific rule should be really streamlined and only contain the logic for the rule itself, with all the other mechanics abstracted away.
Specific Rules¶
Standard SQL Linting Rules.
-
class
sqlfluff.rules.std.
Rule_L001
(code, description, **kwargs)¶ Unneccessary trailing whitespace.
-
class
sqlfluff.rules.std.
Rule_L002
(tab_space_size=4, **kwargs)¶ Mixed Tabs and Spaces in single whitespace.
This rule will fail if a single section of whitespace contains both tabs and spaces.
- Parameters
tab_space_size (
int
) – The number of spaces to consider equal to one tab. Used in the fixing step of this rule. Defaults to 4.
-
class
sqlfluff.rules.std.
Rule_L003
(tab_space_size=4, indent_unit='space', **kwargs)¶ Indentation not consistent with previous lines.
- Parameters
tab_space_size (
int
) – The number of spaces to consider equal to one tab. Used in the fixing step of this rule. Defaults to 4.indent_unit (
str
) – Whether to use tabs or spaces to add new indents. Defaults to space.
Note
This rule used to be _”Indentation length is not a multiple of tab_space_size”_, but was changed to be much smarter.
-
class
sqlfluff.rules.std.
Rule_L004
(code, description, **kwargs)¶ Mixed Tab and Space indentation found in file.
-
class
sqlfluff.rules.std.
Rule_L005
(code, description, **kwargs)¶ Commas should not have whitespace directly before them.
Unless it’s an indent.We deal with trailing/leading commas in a different rule.
-
class
sqlfluff.rules.std.
Rule_L006
(code, description, **kwargs)¶ Operators should be surrounded by a single whitespace.
-
class
sqlfluff.rules.std.
Rule_L007
(code, description, **kwargs)¶ Operators near newlines should be after, not before the newline.
-
class
sqlfluff.rules.std.
Rule_L008
(code, description, **kwargs)¶ Commas should be followed by a single whitespace unless followed by a comment.
-
class
sqlfluff.rules.std.
Rule_L009
(code, description, **kwargs)¶ Files must end with a trailing newline.
-
class
sqlfluff.rules.std.
Rule_L010
(capitalisation_policy='consistent', **kwargs)¶ Inconsistent capitalisation of keywords.
- Parameters
capitalisation_policy (
str
) – The capitalisation policy to enforce. One of consistent, upper, lower, capitalise.
-
class
sqlfluff.rules.std.
Rule_L011
(code, description, **kwargs)¶ Implicit aliasing of table not allowed. Use explicit AS clause.
-
class
sqlfluff.rules.std.
Rule_L012
(code, description, **kwargs)¶ Implicit aliasing of column not allowed. Use explicit AS clause.
NB: This rule inherits it’s functionality from obj:Rule_L011 but is seperate so that they can be enabled and disabled seperately.
-
class
sqlfluff.rules.std.
Rule_L013
(allow_scalar=True, **kwargs)¶ Column expression without alias. Use explicit AS clause.
- Parameters
allow_scalar (
bool
) – If True then this rule will not fail if there is only one element in the select clause e.g. SELECT 1 + 2 FROM blah. It will still fail if there are multiple columns. (Default True)
-
class
sqlfluff.rules.std.
Rule_L014
(capitalisation_policy='consistent', **kwargs)¶ Inconsistent capitalisation of unquoted identifiers.
The functionality for this rule is inherited from
Rule_L010
.- Parameters
capitalisation_policy (
str
) – The capitalisation policy to enforce. One of ‘consistent’, ‘upper’, ‘lower’, ‘capitalise’.
-
class
sqlfluff.rules.std.
Rule_L015
(code, description, **kwargs)¶ DISTINCT used with parentheses.
-
class
sqlfluff.rules.std.
Rule_L016
(max_line_length=80, tab_space_size=4, indent_unit='space', **kwargs)¶ Line is too long.
- Parameters
max_line_length (
int
) – The maximum length of a line to allow without raising a violation.tab_space_size (
int
) – The number of spaces to consider equal to one tab. Used in the fixing step of this rule. Defaults to 4.indent_unit (
str
) – Whether to use tabs or spaces to add new indents. Defaults to space.
-
class
sqlfluff.rules.std.
Rule_L017
(code, description, **kwargs)¶ Function name not immediately followed by bracket.
-
class
sqlfluff.rules.std.
Rule_L018
(tab_space_size=4, **kwargs)¶ WITH clause closing bracket should be aligned with WITH keyword.
-
class
sqlfluff.rules.std.
Rule_L019
(comma_style='trailing', **kwargs)¶ Leading/Trailing comma enforcement.
- Parameters
comma_style (
str
) – The comma style to to enforce. One of trailing, leading (default is trailing).
-
class
sqlfluff.rules.std.
Rule_L020
(code, description, **kwargs)¶ Table aliases should be unique within each clause.
-
class
sqlfluff.rules.std.
Rule_L021
(code, description, **kwargs)¶ Ambiguous use of DISTINCT in select statement with GROUP BY.
-
class
sqlfluff.rules.std.
Rule_L022
(code, description, **kwargs)¶ Blank line expected but not found after CTE definition.
-
class
sqlfluff.rules.std.
Rule_L023
(code, description, **kwargs)¶ Single whitespace expected after AS in WITH clause.
-
class
sqlfluff.rules.std.
Rule_L024
(code, description, **kwargs)¶ Single whitespace expected after USING in JOIN clause.
-
class
sqlfluff.rules.std.
Rule_L025
(code, description, **kwargs)¶ Tables should not be aliased if that alias is not used.
-
class
sqlfluff.rules.std.
Rule_L026
(code, description, **kwargs)¶ References cannot reference objects not present in FROM clause.
-
class
sqlfluff.rules.std.
Rule_L027
(code, description, **kwargs)¶ References should be qualified if select has more than one referenced table/view.
NB: Except if they’re present in a USING clause.
-
class
sqlfluff.rules.std.
Rule_L028
(single_table_references='consistent', **kwargs)¶ References should be consistent in statements with a single table.
- Parameters
single_table_references (
str
) – The expectation for references in single-table select. One of qualified, unqualified, consistent (default is consistent).
-
class
sqlfluff.rules.std.
Rule_L029
(only_aliases=True, **kwargs)¶ Keywords should not be used as identifiers.
- Parameters
only_aliases (
bool
) – Should this rule only raise issues for aiases. By default this is true, and therefore only flags violations for alias expressions (which are directly in control of the sql writer). When set to false this rule flags issues for all unquoted identifiers.
-
class
sqlfluff.rules.std.
Rule_L030
(capitalisation_policy='consistent', **kwargs)¶ Inconsistent capitalisation of function names.
The functionality for this rule is inherited from
Rule_L010
.- Parameters
capitalisation_policy (
str
) – The capitalisation policy to enforce. One of ‘consistent’, ‘upper’, ‘lower’, ‘capitalise’.
Implementation¶
-
class
sqlfluff.rules.base.
RuleSet
(name)¶ Class to define a ruleset.
A rule set is instantiated on module load, but the references to each of it’s classes are instantiated at runtime. This means that configuration values can be passed to those rules live and be responsive to any changes in configuration from the path that the file is in.
Rules should be fetched using the
get_rulelist()
command which also handles any filtering (i.e. whitelisting and blacklisting).New rules should be added to the instance of this class using the
register()
decorator. That decorator registers the class, but also performs basic type and name-convention checks.The code for the rule will be parsed from the name, the description from the docstring. The eval function is assumed that it will be overriden by the subclass, and the parent class raises an error on this function if not overriden.
-
get_rulelist
(config)¶ Use the config to return the appropriate rules.
We use the config both for whitelisting and blacklisting, but also for configuring the rules given the given config.
- Returns
list
of instantiatedBaseCrawler
.
-
register
(cls)¶ Decorate a class with this to add it to the ruleset.
@myruleset.register class Rule_L001(BaseCrawler): "Description of rule." def eval(self, **kwargs): return LintResult()
We expect that rules are defined as classes with the name Rule_XXXX where XXXX is of the form LNNN, where L is a letter (literally L for linting by default) and N is a three digit number.
If this receives classes by any other name, then it will raise a
ValueError
.
-
-
class
sqlfluff.rules.base.
BaseCrawler
(code, description, **kwargs)¶ The base class for a crawler, of which all rules are derived from.
- Parameters
code (
str
) – The identifier for this rule, used in inclusion or exclusion.description (
str
) – A human readable description of what this rule does. It will be displayed when any violations are found.
-
_eval
(**kwargs)¶ Evaluate this rule against the current context.
This should indicate whether a linting violation has occured and/or whether there is something to remember from this evaluation.
Note that an evaluate function shoul always accept **kwargs, but if it relies on any available kwargs, it should explicitly call them out at definition.
- Returns
LintResult
orNone
.
The reason that this method is called
_eval()
and not eval is a bit of a hack with sphinx autodoc, to make it so that the rule documentation auto-generates nicely.
-
crawl
(segment, dialect, parent_stack=None, siblings_pre=None, siblings_post=None, raw_stack=None, fix=False, memory=None)¶ Recursively perform the crawl operation on a given segment.
- Returns
A tuple of (vs, raw_stack, fixes, memory)
-
static
filter_meta
(segments, keep_meta=False)¶ Filter the segments to non-meta.
Or optionally the opposite if keep_meta is True.
-
classmethod
get_parent_of
(segment, root_segment)¶ Return the segment immediately containing segment.
NB: This is recursive.
- Parameters
segment – The segment to look for.
root_segment – Some known parent of the segment we’re looking for (although likely not the direct parent in question).
-
classmethod
make_keyword
(raw, pos_marker)¶ Make a keyword segment.
-
classmethod
make_newline
(pos_marker, raw=None)¶ Make a newline segment.
-
classmethod
make_whitespace
(raw, pos_marker)¶ Make a whitespace segment.
-
class
sqlfluff.rules.base.
LintResult
(anchor=None, fixes=None, memory=None, description=None)¶ A class to hold the results of a crawl operation.
- Parameters
anchor (
BaseSegment
, optional) – A segment which represents the position of the a problem. NB: Each fix will also hold it’s own reference to position, so this position is mostly for alterting the user to where the problem is.fixes (
list
ofLintFix
, optional) – An array of any fixes which would correct this issue. If not present then it’s assumed that this issue will have to manually fixed.memory (
dict
, optional) – An object which stores any working memory for the crawler. The memory returned in any LintResult will be passed as an input to the next segment to be crawled.description (
str
, optional) – A description of the problem identified as part of this result. This will override the description of the rule as what gets reported to the user with the problem if provided.
-
to_linting_error
(rule)¶ Convert a linting result to a
SQLLintError
if appropriate.
-
class
sqlfluff.rules.base.
LintFix
(edit_type, anchor, edit=None)¶ A class to hold a potential fix to a linting violation.
- Parameters
edit_type (
str
) – One of create, edit, delete to indicate the kind of fix this represents.anchor (
BaseSegment
) – A segment which represents the position that this fix should be applied at. For deletions it represents the segment to delete, for creations it implies the position to create at (with the existing element at this position to be moved after the edit), for an edit it implies the segment to be replaced.edit (
BaseSegment
, optional) – For edit and create fixes, this hold the segment, or iterable of segments to create to replace at the given anchor point.
The _eval function of each rule should take enough arguments that it can evaluate the position of the given segment in relation to it’s neighbors, and that the segment which finally “triggers” the error, should be the one that would be corrected OR if the rule relates to something that is missing, then it should flag on the segment FOLLOWING, the place that the desired element is missing.