{"title":"Macros","description":"Define reusable scalar and table macros in Altertable catalog metadata with standard CREATE MACRO syntax.","order":650,"askSuggestions":["Can I use a macro in a sort order or a view?","Do macros change when I query an older snapshot?"],"navigation":true,"body":{"code":"var Component=(()=>{var m=Object.create;var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var _=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),R=(n,e)=>{for(var s in e)o(n,s,{get:e[s],enumerable:!0})},t=(n,e,s,a)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let c of g(e))!E.call(n,c)&&c!==s&&o(n,c,{get:()=>e[c],enumerable:!(a=u(e,c))||a.enumerable});return n};var A=(n,e,s)=>(s=n!=null?m(f(n)):{},t(e||!n||!n.__esModule?o(s,\"default\",{value:n,enumerable:!0}):s,n)),b=n=>t(o({},\"__esModule\",{value:!0}),n);var d=_((y,i)=>{i.exports=_jsx_runtime});var C={};R(C,{default:()=>p,frontmatter:()=>x});var r=A(d()),x={title:\"Macros\",description:\"Define reusable scalar and table macros in Altertable catalog metadata with standard CREATE MACRO syntax.\",order:650,askSuggestions:[\"Can I use a macro in a sort order or a view?\",\"Do macros change when I query an older snapshot?\"]};function l(n){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...n.components},{DocsPreviewDocument:s,DocsPreviewSection:a}=e;return s||h(\"DocsPreviewDocument\",!0),a||h(\"DocsPreviewSection\",!0),(0,r.jsxs)(s,{anchors:\"macros scalar-macros table-macros default-parameters typed-parameters drop-macros when-to-use-macros learn-more\",children:[(0,r.jsxs)(a,{sectionId:\"macros\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,r.jsx)(e.h1,{id:\"macros\",children:\"Macros\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Altertable catalogs support scalar and table macros created with standard \",(0,r.jsx)(e.code,{children:\"CREATE MACRO\"}),\" syntax. Macros are stored in catalog metadata and can be referenced from queries, view definitions, and sort-order expressions.\"]}),`\n`,(0,r.jsxs)(e.p,{children:[\"Macros participate in \",(0,r.jsx)(e.a,{href:\"/docs/lakehouse/features/time-travel\",children:\"time travel\"}),\" \\u2014 querying a catalog at an earlier snapshot reflects the macros that existed at that point.\"]}),`\n`,(0,r.jsxs)(e.p,{children:[\"Run the statements below through Altertable's \",(0,r.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"}),\" against an \",(0,r.jsx)(e.a,{href:\"/docs/lakehouse/catalogs\",children:\"Altertable catalog\"}),\" you own.\"]}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"scalar-macros\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,r.jsx)(e.h2,{id:\"scalar-macros\",children:\"Scalar macros\"}),`\n`,(0,r.jsx)(e.p,{children:\"Scalar macros return a single value:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`CREATE MACRO margin_pct(price, cost) AS (price - cost) / price * 100;\n\nSELECT margin_pct(49.99, 31.50);\n`})}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"table-macros\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,r.jsx)(e.h2,{id:\"table-macros\",children:\"Table macros\"}),`\n`,(0,r.jsx)(e.p,{children:\"Table macros return a table:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`CREATE MACRO high_stock_skus(min_qty) AS TABLE\n  SELECT sku, warehouse_id, on_hand\n  FROM my_catalog.main.inventory_levels\n  WHERE on_hand >= min_qty;\n\nSELECT * FROM high_stock_skus(500);\n`})}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"default-parameters\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,r.jsx)(e.h2,{id:\"default-parameters\",children:\"Default parameters\"}),`\n`,(0,r.jsx)(e.p,{children:\"Macros can define default values for parameters:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`CREATE MACRO apply_discount(price, pct := 10) AS price * (1 - pct / 100.0);\n\nSELECT apply_discount(120.00);\n`})}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"typed-parameters\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,r.jsx)(e.h2,{id:\"typed-parameters\",children:\"Typed parameters\"}),`\n`,(0,r.jsx)(e.p,{children:\"Macros can specify types for their parameters:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`CREATE MACRO pallet_count(units INTEGER, units_per_pallet INTEGER) AS ceil(units / units_per_pallet);\n`})}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"drop-macros\",anchors:\"\",ancestors:\"\",index:\"5\",children:[(0,r.jsx)(e.h2,{id:\"drop-macros\",children:\"Drop macros\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Drop scalar macros with \",(0,r.jsx)(e.code,{children:\"DROP MACRO\"}),\" and table macros with \",(0,r.jsx)(e.code,{children:\"DROP MACRO TABLE\"}),\":\"]}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`DROP MACRO margin_pct;\nDROP MACRO TABLE high_stock_skus;\n`})}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"when-to-use-macros\",anchors:\"\",ancestors:\"\",index:\"6\",children:[(0,r.jsx)(e.h2,{id:\"when-to-use-macros\",children:\"When to use macros\"}),`\n`,(0,r.jsx)(e.p,{children:\"Use macros to centralize repeated expressions \\u2014 date bucketing, unit conversions, or shared filter logic \\u2014 so queries stay readable and changes propagate from one definition.\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"For sort orders and views that depend on a macro, define the macro before referencing it in \",(0,r.jsx)(e.code,{children:\"SET SORTED BY\"}),\" or \",(0,r.jsx)(e.code,{children:\"CREATE VIEW\"}),\".\"]}),`\n`]}),(0,r.jsxs)(a,{sectionId:\"learn-more\",anchors:\"\",ancestors:\"\",index:\"7\",children:[(0,r.jsx)(e.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,r.jsxs)(e.ul,{children:[`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse/features/sorted-tables\",children:\"Sorted Tables\"})}),\": Use macros in expression-based sort keys\"]}),`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse/features/views\",children:\"Views\"})}),\": Encapsulate queries behind stable names\"]}),`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL Engine\"})}),\": DuckDB dialect and federated queries\"]}),`\n`]})]})]})}function p(n={}){let{wrapper:e}=n.components||{};return e?(0,r.jsx)(e,{...n,children:(0,r.jsx)(l,{...n})}):l(n)}function h(n,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+n+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return b(C);})();\n;return Component;"},"_id":"docs/lakehouse/features/macros.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/macros.mdx","sourceFileName":"macros.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/macros"},"type":"Docs","path":"/lakehouse/features/macros","level":3,"parent":"lakehouse/features"}