AMQL Docs
AML ReferenceProgrammable Constructs

AML Constant

This document describes how to use AML Constant to declare and reuse values across an AML project

Introduction

An AML constant allows you to specify a value that can be reused throughout a project.

  • Once a constant is declared, user can not change its value through reassignment (it is immutable)
  • Cannot declare same name constant in the same scope (i.e. AML modules and functions)

Syntax

There are two equivalent ways to declare a new constant. You can declare the constant using the const keyword without explicit type:

const <constant_name> = <expression>
const a = 'hello'

or you can explicitly declare the constant’s type

<constant_type> <constant_name> = <expression>
Int e = 3

In the first case, the type of const variable is automatically inferred from its value.

Example usages

Basic types

const a = 'hello' // a's type is String
const b = 2 // b's type is Number

// Explicit type declaration
String c = 'world'
Number d = 2.5 + b // 4.5
Int e = 3

// Use the constant with string interpolation
const signup_threshold = 0.6  // signup_threshold's type is Number

Dataset my_dataset {
  metric count {
    definition: @aql count(*)
		// highlight-next-line
      | where users.signup_threshold < ${signup_threshold}
    ;;
  }
}

String interpolation is a common way to reuse basic constant types.

Lists and dictionaries

  • Declare with const keyword
// AML list
const models = ['users', 'countries', 'orders']
// access to list's elements
models(0) // 'users'
models(1) // 'countries'
models(2) // 'orders'

// AML dictionary
const modelDict = {
	users: 'users'
	countries: 'countries'
	orders: 'orders'
}
// access to dictionary's elements
modelDict('users') // 'users'
modelDict('countries') // 'countries'
modelDict('orders') // 'countries'
  • Explicit type
// AML list
Type ListString = List[String]
ListString models = ['users', 'countries', 'orders']
// access to list's elements
models(0) // 'users'
models(1) // 'countries'
models(2) // 'orders'

// AML dictionary
Type StringDict = Dict[String, String]
StringDict modelDict = {
	users: 'users'
	countries: 'countries'
	orders: 'orders'
}

Object types

const my_filter = FilterBlock { // my_filter's type is FilterBlock
  type: 'field'
  label: 'User role'
  source: FieldFilterSource {
    dataset: 'tenant_user'
    field: r(public_users.role)
  }
}

const my_dashboard_1 = Dashboard {
	...
	block f1: my_filter // reuse my_filter in multiple dashboards
	...
}

Dashboard my_dashboard_2 {
	...
	block f2: my_filter // reuse my_filter in multiple dashboards
	...
}

Model extended_users = users.extend({
	label: 'Extended users'
	measure count {
    label: 'count'
    type: 'number'
    definition: @aql sum(extended_users.id) ;;
  }
}

// equivalent to
const extended_users2 = users.extend({
	label: 'Extended users'
	measure count {
    label: 'count'
    type: 'number'
    definition: @aql sum(extended_users.id) ;;
  }
}

See also

On this page