Macros are Python functions that generate SQL fragments at compile time. Instead of Jinja templates, you write real Python - testable, debuggable, and composable with standard tooling.Documentation Index
Fetch the complete documentation index at: https://docs.sqlbuild.com/llms.txt
Use this file to discover all available pages before exploring further.
Defining macros
Create Python files undermacros/ in your project. Every public function in a macro file becomes a callable macro:
Using macros in models
Call macros in model SQL using the@macro_name(args) syntax:
@cents_to_dollars('SUM(p.amount_cents)') expands to ROUND(CAST(SUM(p.amount_cents) AS DOUBLE) / 100, 2).
Using macros in tests
Because unit tests are written in SQL, they support macro calls. This is useful for reusable mock data generators:Using macros in hooks
Macros are expanded insidepre_hook and post_hook strings:
Macro arguments
Macro arguments use Python literal syntax. Supported types:- Strings:
'value'or"value" - Numbers:
42,3.14,-1 - Booleans:
True,False - Lists:
[1, 2, 3] - Dicts:
{'key': 'value'} - None:
None - Nested macro calls:
@other_macro('arg')
Nested macro calls in arguments
Macros can be passed as arguments to other macros. The inner macro evaluates first and its result becomes an argument to the outer macro:Composing macros
Macro output cannot contain macro calls. Expansion is single-pass: if a macro returns SQL containing@another_macro(), SQLBuild raises an error. If you need composition, compose in Python:
Compilation order
SQLBuild processes SQL in this order:- Project variables (
@namewithout parentheses) are substituted first - Macro calls (
@name(args)) are expanded second - SQLGlot validation runs against the fully expanded SQL
- Context templates (
${CTX:...}) in hooks and config are resolved during config compilation
- Macros see already-substituted variable values in the SQL
- Macros can emit
${CTX:...}references that will be resolved later - SQLGlot validates the final expanded SQL, catching syntax errors from both vars and macros
Where macros are allowed
- Model query SQL - the SELECT statement after the MODEL() header
- Hook strings -
pre_hookandpost_hookvalues in MODEL() config - Test SQL - unit test CTE bodies
- Audit SQL - singular audit queries
@macro(), SQLBuild raises a compile error.
Project variables
Project variables are simple string substitutions defined insqlbuild_project.yml or per-environment. They use the @name syntax (without parentheses) and are distinct from macros:
@ namespace. If a variable name collides with a macro name, SQLBuild raises an error at compile time.
Variables can also be set in sqlbuild_local.yml for developer-specific overrides.
Context templates
Hooks and config can reference context variables using${CTX:...} templates:
| Template | Value |
|---|---|
${CTX:target.qualified} | Fully qualified target relation name |
${CTX:target.schema} | Target schema |
${CTX:target.name} | Target relation name |
${CTX:model_name} | Model name |
${CTX:environment} | Current environment name |
${CTX:run_id} | Current run ID |
${CTX:...} references in hook strings that will be resolved at that stage.
Discovery rules
- SQLBuild discovers all
.pyfiles undermacros/recursively - Every public function (not starting with
_) becomes a macro - Macro names must be unique across all macro files - duplicates raise a compile error
- Macros are loaded once at compile time, not per-model

