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
-
Install the plugin:
pip install sqlfluff-templater-dataform-full
-
Configure SQLFluff: Add the following to your
.sqlfluffconfiguration file:[sqlfluff] dialect = bigquery templater = dataform-full sql_file_exts = .sql,.sqlx
-
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:
- Identify Blocks: The plugin first identifies different types of blocks in your
.sqlxfile: Dataform templated SQL (${...}), JavaScript blocks (js {...}), and configuration blocks (config {...}). - 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. - Compile with Dataform CLI: A temporary Dataform project is created, relevant files are copied, and the
dataform compilecommand is executed on the transformed.sqlxfile. The Dataform CLI must be installed and accessible in your system's PATH for this plugin to function. - 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
.sqlxsource 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'sPATH. This setting takes precedence over theDATAFORM_EXECUTABLEenvironment variable. -
parsing_method: This templater offers two different methods for parsing.sqlxfiles before compilation. "Parsing" here means identifying the different blocks likeconfig {...},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 identifyingjs {}andconfig {}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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlfluff_templater_dataform_full-0.1.0.tar.gz.
File metadata
- Download URL: sqlfluff_templater_dataform_full-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
485654e6c5e2e6eb13b3d8a2f39678c790e364ea9b9f4dce89df59f20b2b15e2
|
|
| MD5 |
14ea6deb5fa5fa6ce7676db6f2db3db3
|
|
| BLAKE2b-256 |
b5f7f98c0f0752404e0994abee90c36a1784e857f3c0aa85a7f016332ff00cda
|
File details
Details for the file sqlfluff_templater_dataform_full-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlfluff_templater_dataform_full-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96cfe441315c8db4520ba4a6a12d01bec17aaf98403d1ec4753cf51e6d2a837b
|
|
| MD5 |
44dc33445a20b377ab65f823bc39d208
|
|
| BLAKE2b-256 |
789744ea4ac7c1694ad06950bef993bc5c60ccb54acf5eb6c18659c0992e0251
|