Macros are blocks which content will be inlined (substituted) into the caller block. This mimic the current behavior of our catalog assets.
Macros are defined using an #+macro
directive. Optionally, it could accept arguments.
Macros are called from interpolation expressions. Visually, this gives a clear separation between syntax for calling a block ${block()}
and syntax for calling a macros {{macros()}}
. User should get an error if he tries to call a block from interpolation expression.
In the following example we define countryGroup
macro, which return a column expression that we are using inside a SELECT statement to get a country group.
#+macro countryGroup(country) CASE WHEN {{ country }} IN ('US', 'CA') THEN 'NA' WHEN {{ country }} IN ('GB', 'FR', 'DE', 'IT', 'PL', 'SE') THEN 'EU' WHEN {{ country }} IN ('AU') THEN {{ country }} ELSE 'Other' END #+end SELECT country AS country, {{ countryGroup(country="country") }} AS country_group, device_type AS device_type, sum(revenue) AS revenue, sum(pageviews) AS pageviews, sum(sessions) AS sessions FROM ${fact.sales()} GROUP BY country, country_group, device_type ;