|
11 | 11 |
|
12 | 12 | ## Macros |
13 | 13 |
|
14 | | -Macros are plain Python functions that return final SQL strings. Every public function in a macro |
15 | | -file is callable from SQL as `@name(...)`; prefix helpers, constants and classes with `_`. |
| 14 | +Every macro call names its kind: `@sql.name(...)` for a SQL macro, `@py.name(...)` for a Python |
| 15 | +macro. A bare `@name(...)` fails with `P014`, and a macro file directly under a `macros/` folder |
| 16 | +fails with `D013`. |
| 17 | + |
| 18 | +**SQL macros** live in `macros/sql/`, one per file named after the macro. A `MACRO (...)` header |
| 19 | +declares typed arguments; the body is an expression, a `WITH` list of CTEs or a `SELECT` list of |
| 20 | +columns. Prefer them whenever no logic is needed: the compiler checks every call's arguments |
| 21 | +against the caller's column types. |
| 22 | + |
| 23 | +```sql |
| 24 | +-- macros/sql/cents_to_dollars.sql |
| 25 | +MACRO ( |
| 26 | + description "Cents to dollars", |
| 27 | + arguments (amount_cents BIGINT, places INT CONST DEFAULT 2), |
| 28 | + returns DECIMAL(18, 2), |
| 29 | +); |
| 30 | + |
| 31 | +ROUND(CAST(@amount_cents AS DECIMAL(18, 4)) / 100, @places) |
| 32 | +``` |
| 33 | + |
| 34 | +```sql |
| 35 | +SELECT order_id, @sql.cents_to_dollars(amount_cents) AS amount_dollars |
| 36 | +FROM __ref("stg_orders") |
| 37 | +``` |
| 38 | + |
| 39 | +Full reference: [docs/concepts/macros/sql-macros.md](docs/concepts/macros/sql-macros.md). |
| 40 | + |
| 41 | +**Python macros** live in `macros/python/` and are plain Python functions that return final SQL |
| 42 | +strings. Every public function in a macro file is callable from SQL as `@py.name(...)`; prefix |
| 43 | +helpers, constants and classes with `_`. |
16 | 44 |
|
17 | 45 | ```python |
18 | | -# macros/currency.py |
| 46 | +# macros/python/currency.py |
19 | 47 | def cents_to_dollars(expression: str) -> str: |
20 | 48 | return f"ROUND(CAST({expression} AS DOUBLE) / 100, 2)" |
21 | 49 | ``` |
22 | 50 |
|
23 | 51 | ```sql |
24 | | -SELECT order_id, @cents_to_dollars("amount_cents") AS amount_dollars |
| 52 | +SELECT order_id, @py.cents_to_dollars("amount_cents") AS amount_dollars |
25 | 53 | FROM __ref("stg_orders") |
26 | 54 | ``` |
27 | 55 |
|
28 | 56 | - Arguments are Python literals: strings, numbers, `True`/`False`, lists, dicts, `None`, or another |
29 | 57 | macro call. Pass SQL expressions and column names as quoted strings. Keyword arguments work: |
30 | | - `@mock_orders(count=5, status="completed")`. |
| 58 | + `@py.mock_orders(count=5, status="completed")`. |
31 | 59 | - Pass the models, sources and seeds a macro reads as typed arguments: |
32 | | - `@union_all([__ref("orders_eu"), __ref("orders_us")])`. The macro receives `SqlResourceRef` |
| 60 | + `@py.union_all([__ref("orders_eu"), __ref("orders_us")])`. The macro receives `SqlResourceRef` |
33 | 61 | values (`sqlbuild.refs`); formatting one into SQL renders the relation, and the dependency |
34 | 62 | belongs to the calling model. |
35 | 63 | - A macro used in SQL must return a string, and its output is final SQL. Never return text |
36 | | - containing another `@macro()` call; compose by calling the Python functions directly, including |
37 | | - functions imported from other visible macro files (`from macros.currency import add_tax`). |
| 64 | + containing another `@py.macro()` call; compose by calling the Python functions directly, including |
| 65 | + functions imported from other visible macro files (`from macros.python.currency import add_tax`). |
38 | 66 | - Never return `__ref()`, `__source()` or `__seed()` from a macro, including through a quoted |
39 | 67 | string argument: it fails compile with `P006`. Only `[references] enforce_explicit = false` in |
40 | 68 | `sqlbuild_project.toml` allows it, as a project-wide migration switch. |
@@ -102,7 +130,7 @@ other models or named hooks. |
102 | 130 |
|
103 | 131 | | Location | Visible to | |
104 | 132 | |---|---| |
105 | | -| Top-level `macros/`, `enums/`, `constants/` | The whole project | |
| 133 | +| Top-level `macros/python/`, `macros/sql/`, `enums/`, `constants/` | The whole project | |
106 | 134 | | `<folder>/_sqlbuild/macros/` (also `enums/`, `constants/`) | That folder and all folders below | |
107 | 135 | | `<folder>/_sqlbuild/_macros/` (also `_enums/`, `_constants/`) | Files directly in that folder | |
108 | 136 | | Underscored value in `MODEL()` | That model only | |
|
0 commit comments