Linting with SQLFluff

Linting with SQLFluff

SQLFluff for linting and auto-fixing of your code.

SQLFluff for linting and auto-fixing of your code.

Overview

SQLFluff (opens in a new tab) is a SQL linter that improves your SQL code quality and development workflow. The key benefits of SQLFluff are:

  • SQL Linting: SQLFluff helps find issues in your SQL code to enforce a consistent code style and early detection of errors. This ensures better alignment within your team, as everyone follows the same code style.

  • Auto-Fixing: You can activate an automatic fix for most issues detected in the linting. This feature helps you save time and stay focused on coding.

SQL Linting

SQLFluff analyzes the code for a set of predefined rules such as syntax errors, undefined variables, unused variables, type inconsistencies, unreachable code, unused imports, code formatting, or configurable custom rules.

SQL Auto-Fix

SQLFluff can automatically fix your SQL code by handling indentation, line breaks, capitalization, and other formatting rules. To activate the formatting of your SQL file, you have two options:

  • Use the keyboard shortcut SHIFT + CMD / CTRL + F in the Asset List mode.
  • Use the keyboard shortcut SHIFT + OPT / ALT + F or right-click on the canvas in the Code Editor.

SQLFLuff configurations

You can change SQLFluff rules by adding a .sqlfluff file at the root level of your project. Here's an example:

    • .gitignore
    • .sqlfluff
    • dbt_project.yml
  • .sqlfluff

    _10
    [sqlfluff:rules]
    _10
    allow_scalar= True
    _10
    single_table_references = consistent
    _10
    unquoted_identifiers_policy = all
    _10
    _10
    [sqlfluff:rules:capitalisation.keywords]
    _10
    capitalisation_policy = upper

    Refer to the complete list of SQLFluff rules (opens in a new tab) for more details.

    Customizing your SQLFluff settings

    You can adjust your SQLFluff settings directly in Code mode. To access the configuration menu, you can:

    • Go to File > Preferences > Settings.
    • Select the Manage menu at the bottom left (wheel icon) > Settings.
    • Use the shortcut Ctrl+,.

    Navigating to settings menu.

    Navigating to settings menu.

    Settings are categorized into User and Workspace. User settings have priority over Workspace settings.

    To find SQLFluff settings, either use the top search bar by typing "SQLFluff" or go to Extensions > sqlfluff.

    Locating SQLFluff settings.

    Locating SQLFluff settings.

    User settings modify the User > settings.json file, while the Workspace settings modify the .vscode > settings.json file.

    SQLFluff is active by default, offering real-time linting. You can also set it to lint on save, or completely disable it. To disable SQLFluff, toggle off the Linter: Run setting in the UI, which updates the User > settings.json. Alternatively, you can adjust settings manually in the User > settings.json file.

    Turning off SQLFluff linter in Settings also modifies the settings.json file.

    Turning off SQLFluff linter in Settings also modifies the settings.json file.

    To edit the User > settings.json file, select Open settings (JSON). For side-by-side configuration, click the Split Editor Right icon.

    Opening setting as JSON and split screen options.

    Opening setting as JSON and split screen options.

    FAQ

    Why are certain dbt macros not recognized properly?

    If you're encountering issues with dbt macros not being recognized, it is due to SQLFluff's current rule set. This issue will be resolved once custom SQLFluff rules are implemented.