collate-sqlfluff is a fork of SQLFluff (the SQL Linter for Humans), maintained by the OpenMetadata community. It provides a modular, dialect-flexible, and configurable SQL linter and auto-formatter, designed particularly for ELT applications. It supports multiple SQL dialects (e.g., BigQuery, Snowflake, PostgreSQL) and templating languages like Jinja and dbt. The library frequently syncs with upstream SQLFluff, incorporating its features and adhering to its semantic versioning, while adding specific enhancements relevant to OpenMetadata's ecosystem.
pip install collate-sqlfluffVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the programmatic API to lint and fix a SQL string using the `sqlfluff` module. The `dialect` parameter is often crucial for accurate linting and fixing.
Review the upstream `sqlfluff` release notes and migration guides (e.g., for 3.x and 4.x) to adapt code and configurations.
Always use `import sqlfluff` when importing the library in Python code.
Ensure that `templater` configuration is defined in a higher-level configuration file (e.g., in the project root or user home directory) and not in subdirectories where it will be ignored.
Exercise caution when granting edit access to SQL or configuration files containing templated code. Review templated SQL for malicious constructs, especially when sourcing from untrusted users or environments.
Always use `import sqlfluff` (or `from sqlfluff import ...`) in your Python code, even if the installed package is `collate-sqlfluff`.
Specify the dialect using a `.sqlfluff` configuration file (e.g., `[sqlfluff] dialect = snowflake`) in your project root, or via the command line (e.g., `sqlfluff lint --dialect snowflake my_file.sql`).
For dbt projects, configure SQLFluff to use the `dbt` templater (e.g., in `.sqlfluff` add `templater = dbt` under `[sqlfluff]`) and ensure your dbt project is compilable. For generic Jinja, provide dummy variables in the `[sqlfluff:templater:jinja:context]` section of your `.sqlfluff` file or specify `load_macros_from_path` if using external macro files. Alternatively, you can use `--ignore=templating` to skip templating errors, though this may lead to less accurate linting.
Move the `templater` setting to a `.sqlfluff` file in a higher-level directory (e.g., the root of your project or your user's home directory) to ensure it is correctly picked up by SQLFluff.