qraft-utils
Standard macro library for Qraft SQL projects.
Provides reusable SQL macros that work across DuckDB, PostgreSQL, MySQL, and Trino. Macros are plain Python functions that generate SQL strings — they are expanded at compile time by Qraft's macro system.
Installation
pip install qraft-utils
# or with uv
uv add qraft-utils
For local development from the Qraft repository:
uv pip install ./python/qraft-utils
Usage
- Add
qraft-utilsto your environment (pip install or local install). - Reference it in your model's front-matter:
---
materialization: table
macros: [qraft_utils]
---
SELECT
surrogate_key(customer_id, order_date) AS sk_order,
safe_divide(revenue, order_count) AS avg_order_value,
cents_to_dollars(amount_cents) AS amount_usd
FROM ref('stg_orders')
Qraft automatically injects vars["engine"] at compile time, so macros adapt their SQL output to the target database.
Available Macros
Scalar Transforms (scalar.py)
| Function | Description |
|---|---|
surrogate_key(*cols) |
MD5-based surrogate key from concatenated columns |
generate_surrogate_key(*cols) |
Alias for surrogate_key |
safe_divide(num, den) |
Division with zero protection (returns NULL on zero) |
cents_to_dollars(col) |
Divide by 100.0 |
coalesce_zero(col) |
COALESCE(col, 0) |
bool_or(col) |
Boolean OR aggregation |
Conditions (conditions.py)
| Function | Description |
|---|---|
is_valid_email(col) |
Email format validation WHERE clause |
recency(date_column, days) |
Date-based recency filter (e.g., last 30 days) |
not_deleted(col) |
Soft-delete filter (col IS NULL) |
accepted_values(col, *values) |
col IN (...) filter |
Structural (structural.py)
| Function | Description |
|---|---|
pivot(col, values, agg, val_col) |
CASE-WHEN pivot columns |
pivot_agg(col, values, agg, val_col) |
Pivot with custom aggregation |
union_relations(*relations) |
UNION ALL with source tracking column |
star_except(table, *exclude) |
SELECT all columns except named ones (requires sqlalchemy and conn_str in vars) |
Date Utilities (date.py)
| Function | Description |
|---|---|
date_spine(start, end, granularity) |
Generate a date series |
fiscal_year_filter(col, start_month) |
Filter for current fiscal year |
date_trunc_to(col, granularity) |
Truncate date to given granularity |
Engine Adaptation
Many macros produce different SQL depending on the target database. The engine is detected from vars["engine"] (injected automatically by Qraft at compile time). Supported engines:
duckdb— DuckDBpostgres— PostgreSQLmysql— MySQL / MariaDBtrino— Trino
If the engine is not recognized, macros fall back to PostgreSQL syntax.
Local Development Without PyPI
If you are developing locally and qraft-utils is not yet on PyPI, you have two options:
Option 1: Install from the local path
uv pip install ./python/qraft-utils
Option 2: Copy the qraft_utils/ directory into your project's macros/
my_project/
macros/
qraft_utils/ # Copy from python/qraft-utils/qraft_utils/
__init__.py
scalar.py
conditions.py
structural.py
date.py
engine.py
models/
...
Then reference it in your model: macros: [qraft_utils].
License
MIT
Metadata
Release files for qraft-utils 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| qraft_utils-0.1.0.tar.gz | 25.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| qraft_utils-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.2 kB
Release files / qraft_utils-0.1.0.tar.gz
| Download URL | qraft_utils-0.1.0.tar.gz |
|---|---|
| Size | 25.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7eeb8173b5cf56a5c6d9844a32e9d5881d434f1b3a799332de567d3d500c11c8
|
|
BLAKE2b-256 checksum How to use checksums |
fdbef4d49367f392008f28d1a141a48e3aca3e2cf43b151d6e1d9114bc678e85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|
Release files / qraft_utils-0.1.0-py3-none-any.whl
| Download URL | qraft_utils-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f2aa3f6ac17958bcfabd4a9b0e458ffadcf44bdf636dedbaff99514367fff6c3
|
|
BLAKE2b-256 checksum How to use checksums |
d17a3789c5374bd4a61a05d8bffcfc18735c489d5d3724fb75372e9762a8ed08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.9
|
Security for qraft-utils 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.