Skip to main content

Macros

A macro is a SQL function you write once and call from any step. Keep macros in .sql files and list the files under macros: in a source's source.yaml, a DuckDB job or a Spark job. Paths are relative to the manifest's folder, so one file can serve many sources and jobs.

shared/ad_codes.sql
CREATE MACRO from_micros(v) AS round(v::BIGINT * 0.000001, 2);

-- our status code; a status without a code fails the step
CREATE MACRO status_code(network, status) AS coalesce(
MAP {
'google_ads': MAP {'ENABLED': 'live', 'PAUSED': 'paused', 'REMOVED': 'ended'},
'meta_ads': MAP {'ACTIVE': 'live', 'PAUSED': 'paused', 'DELETED': 'ended'}
}[network][status],
CASE WHEN status IS NOT NULL THEN error(format('status_code: no code for {} ({})', status, network)) END);
jobs/campaigns.yaml
macros: [../shared/ad_codes.sql]
steps:
- name: campaigns
sql: SELECT campaign_id, status_code(network, status) AS status, from_micros(cost_micros) AS cost FROM raw

Rules​

  • Each statement is CREATE MACRO name(parameters) AS expression. Table macros and other statements are refused.
  • A macro uses its parameters, built-in functions and the macros made before it. It cannot read tables.
  • A parameter's default (b := 5) is a literal: a number, text, true, false or null.
  • A macro file is at most 1 MB.
  • Each name is used once, and never a DuckDB function's name.
  • streamwright validate checks the files and compiles the steps with them. Errors point to the file and line.
  • The run log names each file with its checksum.

Your own codes​

To turn each network's values into your own codes, such as status, channel or bid strategy, write one MAP per network as above. Then choose what a value without a code becomes:

  • an error that fails the step: coalesce(<lookup>, CASE WHEN x IS NOT NULL THEN error(...) END);
  • a fallback code: coalesce(<lookup>, 'other');
  • the value itself: coalesce(<lookup>, x).

A key written twice in a MAP fails the step, so a typo can't slip through. Keep each network's own values in the source and map them in the job that reads it: one source then serves teams with different codes. examples/files/jobs/campaigns/standardize.yaml maps the status, channel and bid strategy of three networks.

Spark jobs​

A Spark job lists its macros the same way, but writes them in Spark SQL. Spark has no SQL macros, so StreamWright replaces each call with the macro's body before Spark runs the query.

shared/spark_codes.sql
CREATE MACRO from_micros(v) AS round(CAST(v AS BIGINT) * 0.000001, 2);

CREATE MACRO status_code(network, status) AS coalesce(
try_element_at(try_element_at(map('google_ads', map('ENABLED', 'live', 'PAUSED', 'paused')), network), status),
CASE WHEN status IS NOT NULL THEN raise_error(concat('status_code: no code for ', status)) END);
  • Use map(...) and try_element_at instead of DuckDB's MAP {...}, and raise_error instead of error.
  • No overloads. A parameter or macro can't be named like a SQL keyword or unit, such as date, map, day or order.
  • A body uses only its parameters (and a lambda's own variables): another column, t.col or a :name is refused.
  • Alias each call in a select list (from_micros(cost) AS cost): Spark names an unaliased column after the expanded text. Expanding one call may add at most 1 MiB of SQL.
  • A cluster run ships the macro files with the job, so their file names must differ (a local run doesn't need that).