Developing Rules¶
Rules in SQLFluff are implemented as classes inheriting from BaseRule
.
SQLFluff crawls through the parse tree of a SQL file, calling the rule’s
_eval()
function for each segment in the tree. For many rules, this allows
the rule code to be really streamlined and only contain the logic for the rule
itself, with all the other mechanics abstracted away.
Traversal Options¶
recurse_into
¶
Some rules are a poor fit for the simple traversal pattern described above. Typical reasons include:
The rule only looks at a small portion of the file (e.g. the beginning or end).
The rule needs to traverse the parse tree in a non-standard way.
These rules can override BaseRule
’s recurse_into
field, setting it to
False
. For these rules False
, _eval()
is only called once, with
the root segment of the tree. This can be much more efficient, especially on
large files. For example, see rules LT13
and LT12
, which only look at
the beginning or end of the file, respectively.
_works_on_unparsable
¶
By default, SQLFluff calls _eval()
for all segments, even “unparsable”
segments, i.e. segments that didn’t match the parsing rules in the dialect.
This causes issues for some rules. If so, setting _works_on_unparsable
to False
tells SQLFluff not to call _eval()
for unparsable segments and
their descendants.
Base Rules¶
base_rules Module¶
Functional API¶
These newer modules provide a higher-level API for rules working with segments and slices. Rules that need to navigate or search the parse tree may benefit from using these. Eventually, the plan is for all rules to use these modules. As of December 30, 2021, 17+ rules use these modules.
The modules listed below are submodules of sqlfluff.utils.functional.