Skip to main content

Define & reuse global SQL definitions

Let's say that you define a dimension called price_category using SQL in one data model. As you work on other data models, you notice that the same SQL definition is also needed.

With AML Constant, you can declare the SQL definition as a const within a separate AML file, and then reuse it in any relevant data models in your AML project. This will help reduce code duplication, and eliminate the need to update multiple piece of code.


// Define a SQL definition as const in a separate AML file
// In reused_sql_defs.aml
const price_category_sql = @sql select
case when price >= 3000 then 'Expensive'
else 'Cheap' end as price_category;;

// In order.model.aml
DataModel order {
// ...
table_name: 'ecommerce_orders'
data_source_name: 'ecommerce'
dimension price_category {
label: "Price Category"
type: "text"
hidden: false
definition: price_category_sql
}
}

// In order_item.model.aml
DataModel order_item {
// ...
table_name: 'ecommerce_order_items'
data_source_name: 'ecommerce'
dimension price_category {
label: "Price Category"
type: "text"
hidden: false
definition: price_category_sql
}
}

Let us know what you think about this document :)