How quarylabs/sqruff works
Project type and purpose
• The project is a command‑line tool (with extensions for code editors, a playground website, and integration tests) whose main job is to lint and format SQL code.
• The README and file tree specify that its primary use is to enforce SQL style and quality across various dialects. Its core functionality is split into linting, formatting, and multiple dialect support.
Key components (modules)
• CLI Application: Found in the “crates/cli” directory. This component deals with reading input (file paths, options) and exposing commands such as “lint” and “fix”. It serves as the entry point for both local terminal usage and integration in CI/CD pipelines.
• Core Linter / Formatter Engine: This is the heart of the tool, including modules for parsing, linting, and fixing SQL code. Look particularly at “crates/lib-core” and “crates/lib” which include parser, rule engine (with rules split in subfoldes like ambiguous, capitalisation, convention, layout, etc.) and API logic.
• Dialects Library: “crates/lib-dialects” encapsulates support for various SQL dialects (ANSI, BigQuery, Athena, Clickhouse, etc.). This layer defines dialect-specific grammar, keywords, and parsing rules.
• Templaters Component: Within “crates/lib/src/templaters” and especially the sqruff_templaters subdirectory, these modules are responsible for detecting and processing dynamic SQL embedded in templating languages (dbt, Jinja, Python, etc.).
• Additional Language and Integration Layers:
– LSP: “crates/lsp” indicates that the project offers language server features for improved integration with editors.
– WASM: “crates/lib-wasm” is likely used to compile parts of the core functionality to WebAssembly for use in browser environments.
– SQL Inference: “crates/sqlinference” manages type or schema inference over SQL (or helps in testing rules).
– Lineage: “crates/lineage” possibly maps out SQL dependency or query lineage analysis.
• External Integrations:
– Editor Extensions: The “editors/code” directory packages a Visual Studio Code extension.
– Playground: A complete web-based interface in the “playground” folder (using React/Vite) allows users to try out the linter/formatter online.
Relationships and flow of control
• User Interaction: Begin with a user invoking the sqruff CLI or using the editor/online playground.
– Input (SQL files, configuration, ignore patterns) is passed to the CLI.
• Core Engine Flow: The CLI hands off processing to the core engine:
– The Parser and Lexer (found in lib-core and lib) break down SQL.
– The Dialects module adapts rules and grammar for the appropriate SQL dialect.
– The Rule Engine (with rules organized by category such as aliasing, ambiguous, convention, layout, etc.) is run against the parsed AST.
– If “fix” is selected, the Formatter submodule produces corrected SQL output.
• Configuration and “NoQA” features are loaded (via configuration files in project root such as .sqruff and .sqruffignore).
• Integration Layers like the LSP or WASM modules expose the same core functions to other consumers such as code editors or browser components.
Architectural patterns and design principles
• Modularity and Separation of Concerns: The multi‑crate workspace cleanly separates the concerns – with independent modules for command interpretation (CLI), core processing (parser, rule engine, formatter), dialect handling, and templating.
• Extendibility: Using libraries for dialects and templaters makes it straightforward to add new SQL dialects or templating systems.
• Reusability: Building the core logic as a library makes it easy to integrate with external systems like VSCode or a web playground.
• Testability: The extensive tests (found in “crates/cli/tests”, “crates/lib/test”, etc.) ensure that each component (e.g., individual lint rules) works independently.
Additional details to capture
• That the project is a full-stack linter – not a web app per se, but one that includes both CLI and external integration (visual studio code extension, playground web UI).
• How the “sqruff” engine configures itself via .sqruff configuration files and supports directives like “noqa” to customize behavior.
• Development pipelines (CI/CD in .github/workflows and integration tests) as part of the overall ecosystem.