SuperAPI AI Directory

anfra-aml

Data & analytics

View repository

Updated 11-18 16:17

About this skill

AML modeling

AML is a declarative language for defining semantic models: the models, fields, relationships, and datasets that AQL queries run against.

Files use type-suffixed names: *.model.aml (a model), *.dataset.aml (a dataset), and plain *.aml for shared definitions. Edit these files directly in the repo.

References (progressive disclosure)

Load only what the task needs. Never assume AML syntax or properties — check the type definition.

  1. references/type_definitions/*.aml — the schema for each AML block (its properties, types, defaults). Open the one you're working with:
    • data_model.aml — models (TableModel, QueryModel) and their fields (dimension, measure, param), field types, and how to reference fields with r(...) / ref(...).
    • dataset.aml — datasets: which models + relationships they bundle, plus dataset-level dimension and metric fields.
    • relationship.aml — relationships between models and rel_config: equality (many_to_one, one_to_one, with extra on keys for a composite key), range (a fact row matches the row whose window contains it, such as price bands or SCD Type 2), many_to_many, and where filters on either side. The relationship(a.x > b.id, true) shorthand makes a single-key equality one. Write the others in full, as RelationshipConfig { rel: RangeRelationship { ... } active: true }.
    • persistence.aml — materializing a query model as a table (full / incremental / external).
    • pre_aggregate.aml — pre-aggregated tables for performance.
    • format.aml — the Format type used by a field's format property.
  2. references/examples/*.aml — complete worked files to pattern-match against: ecommerce_orders.model.aml (a model) and demo_ecommerce.dataset.aml (a dataset).
  3. The AML docs, for what these references don't cover, such as modeling patterns and how joins are generated: look it up with .

Core concepts

  • Models define data. A TableModel (type: 'table') maps to a table; a QueryModel (type: 'query') maps to a SQL query. Each has dimensions (attributes), measures (aggregations), and optional params.
  • Reference fields with r(...) — e.g. r(users.country) (model field), r(ecommerce.revenue) (dataset metric). r(...) is compile-time typechecked; prefer it over the legacy ref('users','country').
  • Relationships connect models (from a many-side field to a one-side field). AQL uses them to join automatically — you never write joins in queries.
  • A relationship's second argument is whether it is active: relationship(a.x > b.id, true). An inactive one (false) is not optional, it is off: no query joins through it unless an expression enables it with with_relationships. Mark one inactive only when it is a second route between models that already have one (a role-playing date). A metric that can only reach a dimension through an inactive relationship is not split by it: it repeats one value on every row, with no error. See and .
  • Datasets bundle models + relationships and expose dataset-level metrics and dimensions. A dataset is what AQL queries target.
  • data_source_name on a model/dataset names a source configured in the repo's .anfra/data_sources.yml — data sources aren't defined in AML here.

Studying the data source

anfra query --lang sql runs raw SQL against a data source:

anfra query --lang sql --data-source <name> '<sql>'   # -s <name> for short; SQL read from stdin when omitted

<name> is a source in .anfra/data_sources.yml. Use it only to study the source schema (tables, columns, types) and a high-level data profile (row counts, cardinality, null rates, value ranges, key uniqueness) so you can model the data in AML more accurately and effectively — e.g. pick the right primary key, field types, and relationship direction. Never use it to answer data questions; those go through AQL against a dataset ().

Best practices

  • Always define a model's primary key — mark the identifying dimension with primary_key: true. Relationships and correct aggregation rely on it.
  • Prefer AQL definitions; only fall back to raw SQL when referencing #SOURCE columns.
  • Define relationships between the models you query so AQL can join them automatically — never hand-write joins.

Workflow

  1. Model first. Create/extend the *.model.aml: set type, data_source_name, table_name (or query), and define its dimensions and measures. Check data_model.aml and the model example for exact syntax.
  2. Relationships. Define relationships between the models you'll join.
  3. Dataset. Create/extend the *.dataset.aml: list the models and relationships, and add any cross-model dataset-level metrics.
  4. Validate with — run anfra validate (whole repo, or specific files) and fix any diagnostics.
  5. Query the dataset with .

Related skills

  • — AQL knowledge (field/metric definitions are AQL).
  • — write and run queries against a dataset.
  • — validate AML/AQL before relying on it.

anfra skills 0.21.0