Skip to main content

Lint your Dataform project SQL

Project description

Dataform Templater Plugin for SQLFluff

This plugin integrates SQLFluff with Dataform projects, allowing SQLFluff to lint and format your Dataform SQLX files after templating.

Getting Started

  1. Install the plugin:

    pip install sqlfluff-templater-dataform-full
    
  2. Configure SQLFluff: Add the following to your .sqlfluff configuration file:

    [sqlfluff]
    dialect = bigquery
    templater = dataform-full
    sql_file_exts = .sql,.sqlx
    
  3. Usage: Run SQLFluff as usual:

    sqlfluff lint your_dataform_project/
    

How it works

This templater operates by using the Dataform CLI to compile .sqlx files. It performs the following steps:

  1. Identify Blocks: The plugin first identifies different types of blocks in your .sqlx file: Dataform templated SQL (${...}), JavaScript blocks (js {...}), and configuration blocks (config {...}).
  2. Insert Markers: For Dataform templated SQL blocks (${...}), the content is temporarily wrapped with unique, invisible markers within an Immediately Invoked Function Expression (IIFE). JavaScript and config blocks are passed through largely unchanged.
  3. Compile with Dataform CLI: A temporary Dataform project is created, relevant files are copied, and the dataform compile command is executed on the transformed .sqlx file. The Dataform CLI must be installed and accessible in your system's PATH for this plugin to function.
  4. Map Slices: After compilation, the plugin parses the compiled output. It uses the inserted markers to accurately map the compiled SQL back to its original positions in the .sqlx source file. This allows SQLFluff to report linting and formatting issues at the correct locations.

Configuration

You can configure the templater by adding the following options to your .sqlfluff file under the [sqlfluff:templater:dataform-full] section.

  • project_dir: Specifies the path to your Dataform project root. If not provided, the templater will search for a Dataform project in the current working directory.

  • dataform_executable: Sets a custom path to the Dataform executable. This is useful if the executable is not in your system's PATH. This setting takes precedence over the DATAFORM_EXECUTABLE environment variable.

  • parsing_method: This templater offers two different methods for parsing .sqlx files before compilation. "Parsing" here means identifying the different blocks like config {...}, js {...}, and ${...}.

    • regex (Default): A fast parser that uses regular expressions. Recommended for most use cases.
    • char: A character-by-character parser that is more resilient to complex or unusual nesting of blocks.

Example Configuration

[sqlfluff:templater:dataform-full]
# Path to your Dataform project
project_dir = path/to/your/dataform/project

# Custom path to the Dataform executable
dataform_executable = /path/to/your/dataform_cli

# To use the character-based parser instead of the default regex parser
# parsing_method = char

Development

This plugin follows the standard SQLFluff plugin development guide. The core logic resides in src/sqlfluff_templater_dataform/templater.py, specifically the process method.

Known Issues

  • The templater relies on balanced curly braces ({}) for identifying js {} and config {} blocks. Unbalanced braces within these blocks (e.g., unmatched { or }) will cause parsing errors during templating and lead to incorrect linting. Ensure all such blocks have a matching number of opening and closing braces.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlfluff_templater_dataform_full-0.1.2.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file sqlfluff_templater_dataform_full-0.1.2.tar.gz.

File metadata

File hashes

Hashes for sqlfluff_templater_dataform_full-0.1.2.tar.gz
Algorithm Hash digest
SHA256 862f74a125dfbb2f9051c49a42f75933f86f6f744883af3abbcf933df755d825
MD5 bc532dcea7b55a9758f2d056a30ebc7e
BLAKE2b-256 24f7c8d9653c5212ab00ff3a9757209e76c12687a3f801adc9fea3a72f9c6069

See more details on using hashes here.

File details

Details for the file sqlfluff_templater_dataform_full-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlfluff_templater_dataform_full-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e61bfaa9cf3ac8e748ebd0d2a7d5dfd149e82568cae572bebc46099b2de54ff4
MD5 f104f8e4eabc42dcf3c10f2117016edc
BLAKE2b-256 09816fd7749aa7c8ac4290daa43d54f8d1dce15fc4ca3742c8ac161104c6e88c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page