API Reference

SQLFluff exposes a public api for other python applications to use. A basic example of this usage is given here, with the documentation for each of the methods below.

"""This is an example of how to use the simple sqlfluff api."""

from typing import Any, Dict, Iterator, List, Union
import sqlfluff

#  -------- LINTING ----------

my_bad_query = "SeLEct  *, 1, blah as  fOO  from mySchema.myTable"

# Lint the given string and return an array of violations in JSON representation.
lint_result = sqlfluff.lint(my_bad_query, dialect="bigquery")
# lint_result =
# [
#     {"code": "L010", "line_no": 1, "line_pos": 1, "description": "Keywords must be consistently upper case."}
#     ...
# ]

#  -------- FIXING ----------

# Fix the given string and get a string back which has been fixed.
fix_result_1 = sqlfluff.fix(my_bad_query, dialect="bigquery")
# fix_result_1 = 'SELECT  *, 1, blah AS  foo  FROM myschema.mytable\n'

# We can also fix just specific rules.
fix_result_2 = sqlfluff.fix(my_bad_query, rules=["L010"])
# fix_result_2 = 'SELECT  *, 1, blah AS  fOO  FROM mySchema.myTable'

# Or a subset of rules...
fix_result_3 = sqlfluff.fix(my_bad_query, rules=["L010", "L014"])
# fix_result_3 = 'SELECT  *, 1, blah AS  fOO  FROM myschema.mytable'

#  -------- PARSING ----------

# Parse the given string and return a JSON representation of the parsed tree.
parse_result = sqlfluff.parse(my_bad_query)
# parse_result = {'file': {'statement': {...}, 'newline': '\n'}}

# This JSON structure can then be parsed as required.
# An example usage is shown below:


def get_json_segment(
    parse_result: Dict[str, Any], segment_type: str
) -> Iterator[Union[str, Dict[str, Any], List[Dict[str, Any]]]]:
    """Recursively search JSON parse result for specified segment type.

    Args:
        parse_result (Dict[str, Any]): JSON parse result from `sqlfluff.fix`.
        segment_type (str): The segment type to search for.

    Yields:
        Iterator[Union[str, Dict[str, Any], List[Dict[str, Any]]]]: Retrieve children of specified segment type
                                                                    as either a string for a raw segment or as
                                                                    JSON or an array of JSON for non-raw segments.
    """
    for k, v in parse_result.items():
        if k == segment_type:
            yield v
        elif isinstance(v, dict):
            yield from get_json_segment(v, segment_type)
        elif isinstance(v, list):
            for s in v:
                yield from get_json_segment(s, segment_type)


# e.g. Retrieve array of JSON for table references.
table_references = list(get_json_segment(parse_result, "table_reference"))
print(table_references)
# [[{'identifier': 'mySchema'}, {'dot': '.'}, {'identifier': 'myTable'}]]

# Retrieve raw table name from last identifier in the table reference.
for table_reference in table_references:
    table_name = list(get_json_segment(parse_result, "identifier"))[-1]
    print(f"table_name: {table_name}")
# table_name: myTable

Simple API commands

Sqlfluff is a SQL linter for humans.

fix(sql: str, dialect: str = 'ansi', rules: Optional[List[str]] = None, exclude_rules: Optional[List[str]] = None, config_path: Optional[str] = None) str

Fix a SQL string.

Parameters
  • sql (str) – The SQL to be fixed.

  • dialect (str, optional) – A reference to the dialect of the SQL to be fixed. Defaults to ansi.

  • rules (Optional[List[str], optional) – A subset of rule references to fix for. Defaults to None.

  • exclude_rules (Optional[List[str], optional) – A subset of rule references to avoid fixing for. Defaults to None.

  • config_path (Optional[str], optional) – A path to a .sqlfluff config. Defaults to None.

Returns

str for the fixed SQL if possible.

lint(sql: str, dialect: str = 'ansi', rules: Optional[List[str]] = None, exclude_rules: Optional[List[str]] = None, config_path: Optional[str] = None) List[Dict[str, Any]]

Lint a SQL string.

Parameters
  • sql (str) – The SQL to be linted.

  • dialect (str, optional) – A reference to the dialect of the SQL to be linted. Defaults to ansi.

  • rules (Optional[List[str], optional) – A list of rule references to lint for. Defaults to None.

  • exclude_rules (Optional[List[str], optional) – A list of rule references to avoid linting for. Defaults to None.

  • config_path (Optional[str], optional) – A path to a .sqlfluff config. Defaults to None.

Returns

List[Dict[str, Any]] for each violation found.

parse(sql: str, dialect: str = 'ansi', config_path: Optional[str] = None) Dict[str, Any]

Parse a SQL string.

Parameters
  • sql (str) – The SQL to be parsed.

  • dialect (str, optional) – A reference to the dialect of the SQL to be parsed. Defaults to ansi.

  • config_path (Optional[str], optional) – A path to a .sqlfluff config. Defaults to None.

Returns

Dict[str, Any] JSON containing the parsed structure.

Advanced API usage

The simple API presents only a fraction of the functionality present within the core SQLFluff library. For more advanced use cases, users can import the Linter() and FluffConfig() classes from sqlfluff.core. As of version 0.4.0 this is considered as experimental only as the internals may change without warning in any future release. If you come to rely on the internals of SQLFluff, please post an issue on GitHub to share what you’re up to. This will help shape a more reliable, tidy and well documented public API for use.

The core elements of sqlfluff.

class FluffConfig(configs: Optional[dict] = None, extra_config_path: Optional[str] = None, ignore_local_config: bool = False, overrides: Optional[dict] = None, plugin_manager: Optional[pluggy._manager.PluginManager] = None)

The class that actually gets passed around as a config object.

diff_to(other: sqlfluff.core.config.FluffConfig) dict

Compare this config to another.

Parameters

other (FluffConfig) – Another config object to compare against. We will return keys from this object that are not in other or are different to those in other.

Returns

A filtered dict of items in this config that are not in the other or are different to the other.

classmethod from_kwargs(config: Optional[sqlfluff.core.config.FluffConfig] = None, dialect: Optional[str] = None, rules: Optional[List[str]] = None, exclude_rules: Optional[List[str]] = None) sqlfluff.core.config.FluffConfig

Instantiate a config from either an existing config or kwargs.

This is a convenience method for the ways that the public classes like Linter(), Parser() and Lexer() can be instantiated with a FluffConfig or with the convenience kwargs: dialect & rules.

classmethod from_path(path: str, extra_config_path: Optional[str] = None, ignore_local_config: bool = False, overrides: Optional[dict] = None, plugin_manager: Optional[pluggy._manager.PluginManager] = None) sqlfluff.core.config.FluffConfig

Loads a config object given a particular path.

classmethod from_root(extra_config_path: Optional[str] = None, ignore_local_config: bool = False, overrides: Optional[dict] = None) sqlfluff.core.config.FluffConfig

Loads a config object just based on the root directory.

get(val: str, section: Union[str, Iterable[str]] = 'core', default: Optional[Any] = None)

Get a particular value from the config.

get_section(section: Union[str, Iterable[str]]) Optional[dict]

Return a whole section of config as a dict.

If the element found at the address is a value and not a section, it is still returned and so this can be used as a more advanced from of the basic get method.

Parameters

section – An iterable or string. If it’s a string we load that root section. If it’s an iterable of strings, then we treat it as a path within the dictionary structure.

get_templater(templater_name='jinja', **kwargs)

Fetch a templater by name.

iter_vals(cfg: Optional[dict] = None) Iterable[tuple]

Return an iterable of tuples representing keys.

We show values before dicts, the tuple contains an indent value to know what level of the dict we’re in. Dict labels will be returned as a blank value before their content.

make_child_from_path(path: str) sqlfluff.core.config.FluffConfig

Make a new child config at a path but pass on overrides and extra_config_path.

process_inline_config(config_line: str)

Process an inline config command and update self.

process_raw_file_for_config(raw_str: str)

Process a full raw file for inline config and update self.

set_value(config_path: Iterable[str], val: Any)

Set a value at a given path.

class Lexer(config: Optional[sqlfluff.core.config.FluffConfig] = None, last_resort_lexer: Optional[sqlfluff.core.parser.lexer.StringLexer] = None, dialect: Optional[str] = None)

The Lexer class actually does the lexing step.

elements_to_segments(elements: List[sqlfluff.core.parser.lexer.TemplateElement], templated_file: sqlfluff.core.templaters.base.TemplatedFile) Tuple[sqlfluff.core.parser.segments.raw.RawSegment, ...]

Convert a tuple of lexed elements into a tuple of segments.

lex(raw: Union[str, sqlfluff.core.templaters.base.TemplatedFile]) Tuple[Tuple[sqlfluff.core.parser.segments.base.BaseSegment, ...], List[sqlfluff.core.errors.SQLLexError]]

Take a string or TemplatedFile and return segments.

If we fail to match the whole string, then we must have found something that we cannot lex. If that happens we should package it up as unlexable and keep track of the exceptions.

static lex_match(forward_string: str, lexer_matchers: List[sqlfluff.core.parser.lexer.StringLexer]) sqlfluff.core.parser.lexer.LexMatch

Iteratively match strings using the selection of submatchers.

static map_template_slices(elements: List[sqlfluff.core.parser.lexer.LexedElement], template: sqlfluff.core.templaters.base.TemplatedFile) List[sqlfluff.core.parser.lexer.TemplateElement]

Create a tuple of TemplateElement from a tuple of LexedElement.

This adds slices in the templated file to the original lexed elements. We’ll need this to work out the position in the source file.

static violations_from_segments(segments: Tuple[sqlfluff.core.parser.segments.raw.RawSegment, ...]) List[sqlfluff.core.errors.SQLLexError]

Generate any lexing errors for any unlexables.

class Linter(config: Optional[sqlfluff.core.config.FluffConfig] = None, formatter: Optional[Any] = None, dialect: Optional[str] = None, rules: Optional[List[str]] = None, user_rules: Optional[List[sqlfluff.core.rules.base.BaseRule]] = None, exclude_rules: Optional[List[str]] = None)

The interface class to interact with the linter.

classmethod extract_ignore_from_comment(comment: sqlfluff.core.parser.segments.raw.RawSegment, rule_codes: List[str])

Extract ignore mask entries from a comment segment.

classmethod extract_ignore_mask(tree: sqlfluff.core.parser.segments.base.BaseSegment, rule_codes: List[str]) Tuple[List[sqlfluff.core.linter.common.NoQaDirective], List[sqlfluff.core.errors.SQLBaseError]]

Look for inline ignore comments and return NoQaDirectives.

fix(tree: sqlfluff.core.parser.segments.base.BaseSegment, config: Optional[sqlfluff.core.config.FluffConfig] = None, fname: Optional[str] = None, templated_file: Optional[sqlfluff.core.templaters.base.TemplatedFile] = None) Tuple[sqlfluff.core.parser.segments.base.BaseSegment, List[sqlfluff.core.errors.SQLBaseError]]

Return the fixed tree and violations from lintfix when we’re fixing.

get_ruleset(config: Optional[sqlfluff.core.config.FluffConfig] = None) List[sqlfluff.core.rules.base.BaseRule]

Get hold of a set of rules.

lint(tree: sqlfluff.core.parser.segments.base.BaseSegment, config: Optional[sqlfluff.core.config.FluffConfig] = None, fname: Optional[str] = None, templated_file: Optional[sqlfluff.core.templaters.base.TemplatedFile] = None) List[sqlfluff.core.errors.SQLBaseError]

Return just the violations from lintfix when we’re only linting.

classmethod lint_fix_parsed(tree: sqlfluff.core.parser.segments.base.BaseSegment, config: sqlfluff.core.config.FluffConfig, rule_set: List[sqlfluff.core.rules.base.BaseRule], fix: bool = False, fname: Optional[str] = None, templated_file: Optional[sqlfluff.core.templaters.base.TemplatedFile] = None, formatter: Optional[Any] = None) Tuple[sqlfluff.core.parser.segments.base.BaseSegment, List[sqlfluff.core.errors.SQLBaseError], List[sqlfluff.core.linter.common.NoQaDirective]]

Lint and optionally fix a tree object.

classmethod lint_parsed(parsed: sqlfluff.core.linter.common.ParsedString, rule_set: List[sqlfluff.core.rules.base.BaseRule], fix: bool = False, formatter: Optional[Any] = None, encoding: str = 'utf8')

Lint a ParsedString and return a LintedFile.

lint_path(path: str, fix: bool = False, ignore_non_existent_files: bool = False, ignore_files: bool = True, processes: int = 1) sqlfluff.core.linter.linted_dir.LintedDir

Lint a path.

lint_paths(paths: Tuple[str, ...], fix: bool = False, ignore_non_existent_files: bool = False, ignore_files: bool = True, processes: int = 1) sqlfluff.core.linter.linting_result.LintingResult

Lint an iterable of paths.

classmethod lint_rendered(rendered: sqlfluff.core.linter.common.RenderedFile, rule_set: List[sqlfluff.core.rules.base.BaseRule], fix: bool = False, formatter: Optional[Any] = None) sqlfluff.core.linter.linted_file.LintedFile

Take a RenderedFile and return a LintedFile.

lint_string(in_str: str = '', fname: str = '<string input>', fix: bool = False, config: Optional[sqlfluff.core.config.FluffConfig] = None, encoding: str = 'utf8') sqlfluff.core.linter.linted_file.LintedFile

Lint a string.

Returns

an object representing that linted file.

Return type

LintedFile

lint_string_wrapped(string: str, fname: str = '<string input>', fix: bool = False) sqlfluff.core.linter.linting_result.LintingResult

Lint strings directly.

static parse_noqa(comment: str, line_no: int, rule_codes: List[str])

Extract ignore mask entries from a comment string.

parse_path(path: str, recurse: bool = True) Iterator[sqlfluff.core.linter.common.ParsedString]

Parse a path of sql files.

NB: This a generator which will yield the result of each file within the path iteratively.

classmethod parse_rendered(rendered: sqlfluff.core.linter.common.RenderedFile, recurse: bool = True)

Parse a rendered file.

parse_string(in_str: str, fname: str = '<string>', recurse: bool = True, config: Optional[sqlfluff.core.config.FluffConfig] = None, encoding: str = 'utf-8') sqlfluff.core.linter.common.ParsedString

Parse a string.

paths_from_path(path: str, ignore_file_name: str = '.sqlfluffignore', ignore_non_existent_files: bool = False, ignore_files: bool = True, working_path: str = '/home/docs/checkouts/readthedocs.org/user_builds/sqlfluff/checkouts/0.9.1/docs/source') List[str]

Return a set of sql file paths from a potentially more ambiguous path string.

Here we also deal with the .sqlfluffignore file if present.

When a path to a file to be linted is explicitly passed we look for ignore files in all directories that are parents of the file, up to the current directory.

If the current directory is not a parent of the file we only look for an ignore file in the direct parent of the file.

static remove_templated_errors(linting_errors: List[sqlfluff.core.errors.SQLBaseError]) List[sqlfluff.core.errors.SQLBaseError]

Filter a list of lint errors, removing those which only occur in templated slices.

render_file(fname: str, root_config: sqlfluff.core.config.FluffConfig) sqlfluff.core.linter.common.RenderedFile

Load and render a file with relevant config.

render_string(in_str: str, fname: str, config: sqlfluff.core.config.FluffConfig, encoding: str) sqlfluff.core.linter.common.RenderedFile

Template the file.

rule_tuples() List[sqlfluff.core.linter.common.RuleTuple]

A simple pass through to access the rule tuples of the rule set.

class Parser(config: Optional[sqlfluff.core.config.FluffConfig] = None, dialect: Optional[str] = None)

Instantiates parsed queries from a sequence of lexed raw segments.

parse(segments: Sequence[BaseSegment], recurse=True, fname: str = None) Optional[BaseSegment]

Parse a series of lexed tokens using the current dialect.