Skip to main content

Standard macro library for Qraft SQL projects — cross-database SQL helpers for DuckDB, PostgreSQL, MySQL, and Trino

Project description

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

  1. Add qraft-utils to your environment (pip install or local install).
  2. 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 — DuckDB
  • postgres — PostgreSQL
  • mysql — MySQL / MariaDB
  • trino — 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qraft_utils-0.1.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qraft_utils-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file qraft_utils-0.1.0.tar.gz.

File metadata

  • Download URL: qraft_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for qraft_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7eeb8173b5cf56a5c6d9844a32e9d5881d434f1b3a799332de567d3d500c11c8
MD5 6f96c8cb24557007418f394f86414eac
BLAKE2b-256 fdbef4d49367f392008f28d1a141a48e3aca3e2cf43b151d6e1d9114bc678e85

See more details on using hashes here.

File details

Details for the file qraft_utils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: qraft_utils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for qraft_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2aa3f6ac17958bcfabd4a9b0e458ffadcf44bdf636dedbaff99514367fff6c3
MD5 77a21d3504fc3b9c5f7979b62eb82faa
BLAKE2b-256 d17a3789c5374bd4a61a05d8bffcfc18735c489d5d3724fb75372e9762a8ed08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page