YAML Definition Format¶
Specification of the YAML schema accepted by CREATE SEMANTIC VIEW ... FROM YAML. Every semantic view definition — whether created from inline YAML, a YAML file, or exported via READ_YAML_FROM_SEMANTIC_VIEW() — follows this format.
Added in version 0.7.0.
The YAML format maps directly to the internal SemanticViewDefinition structure. Field names follow serde conventions, which differ from SQL clause names in some cases:
SQL Clause |
YAML Key |
Notes |
|---|---|---|
|
|
|
|
|
Different name — YAML uses the internal |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
View-level comment |
Complete Example¶
A comprehensive YAML definition covering all supported features:
tables:
- alias: o
table: orders
pk_columns:
- id
comment: Order transactions
synonyms:
- order_facts
- alias: c
table: customers
pk_columns:
- id
joins:
- table: c
from_alias: o
fk_columns:
- customer_id
name: order_customer
cardinality: ManyToOne
facts:
- name: net_price
expr: o.extended_price * (1 - o.discount)
source_table: o
comment: Price after discount
synonyms:
- discounted_price
dimensions:
- name: region
expr: o.region
source_table: o
comment: Sales territory
- name: customer_name
expr: c.name
source_table: c
metrics:
- name: revenue
expr: SUM(o.amount)
source_table: o
comment: Total revenue
synonyms:
- total_revenue
- name: order_count
expr: COUNT(*)
source_table: o
- name: avg_order
expr: revenue / order_count
- name: balance
expr: SUM(o.amount)
source_table: o
non_additive_by:
- dimension: report_date
order: Desc
nulls: First
materializations:
- name: region_agg
table: daily_revenue_by_region
dimensions:
- region
metrics:
- revenue
- order_count
comment: Revenue analytics view
A minimal definition requires only tables, and at least one of dimensions or metrics:
tables:
- alias: o
table: orders
pk_columns:
- id
dimensions:
- name: region
expr: o.region
source_table: o
metrics:
- name: revenue
expr: SUM(o.amount)
source_table: o
Top-Level Keys¶
Key |
Type |
Required |
Description |
|---|---|---|---|
|
list of Table |
Yes |
Physical tables available to the view. |
|
list of Dimension |
Yes * |
Named grouping expressions. |
|
list of Metric |
Yes * |
Named aggregation or derived expressions. |
|
list of Join |
No |
FK/PK relationships between tables. Maps to the SQL |
|
list of Fact |
No |
Named row-level expressions (no aggregates). |
|
list of Materialization |
No |
Pre-aggregated table mappings for query routing. |
|
string |
No |
View-level human-readable description. |
* At least one of dimensions or metrics must be non-empty.
Table¶
Each entry in the tables list declares a physical table with an alias. The first table is the base table (root of the relationship graph).
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Short name used to reference this table in all other sections. |
|
|
string |
Yes |
Physical table name. Supports catalog-qualified names ( |
|
|
list of string |
No |
|
Primary key column names. Used for JOIN synthesis and cardinality inference. |
|
list of list of string |
No |
|
UNIQUE constraint column lists. Each inner list is one constraint. Used for cardinality inference. |
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
tables:
- alias: o
table: orders
pk_columns:
- id
unique_constraints:
- - email
comment: Order transactions
synonyms:
- order_facts
Dimension¶
Each entry in the dimensions list declares a named grouping expression.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Dimension name. |
|
|
string |
Yes |
SQL expression. Can be a column reference ( |
|
|
string |
No |
null |
Table alias this dimension comes from. Used for join dependency resolution. |
|
string |
No |
null |
Override output column type. Wraps the expression in |
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
dimensions:
- name: region
expr: o.region
source_table: o
comment: Sales territory
- name: order_month
expr: date_trunc('month', o.ordered_at)
source_table: o
output_type: DATE
Metric¶
Each entry in the metrics list declares a named aggregation, derived metric, semi-additive metric, or window metric.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Metric name. |
|
|
string |
Yes |
For base metrics: aggregate expression ( |
|
|
string |
No |
null |
Table alias. Present for base metrics, absent for derived metrics. |
|
string |
No |
null |
Override output column type. |
|
list of string |
No |
|
Named relationships this metric traverses (for role-playing dimensions). |
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
|
string |
No |
|
Access modifier: |
|
list of NonAdditiveDim |
No |
|
Semi-additive dimensions for snapshot selection. Mutually exclusive with |
|
No |
null |
Window function specification. Mutually exclusive with |
Base metric (with source_table and aggregate expression):
metrics:
- name: revenue
expr: SUM(o.amount)
source_table: o
Derived metric (no source_table, references other metrics):
metrics:
- name: profit
expr: revenue - cost
Private metric:
metrics:
- name: raw_total
expr: SUM(o.amount)
source_table: o
access: Private
Semi-additive metric:
metrics:
- name: total_balance
expr: SUM(a.balance)
source_table: a
non_additive_by:
- dimension: report_date
order: Desc
nulls: First
Window metric:
metrics:
- name: rolling_avg
expr: AVG(total_qty)
source_table: s
window_spec:
window_function: AVG
inner_metric: total_qty
excluding_dims:
- date
order_by:
- expr: date
order: Asc
nulls: Last
frame_clause: "RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW"
Fact¶
Each entry in the facts list declares a named row-level expression. Facts can reference other facts (resolved in topological order). Aggregate functions are not allowed.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Fact name. |
|
|
string |
Yes |
Row-level SQL expression. Must not contain aggregate functions. |
|
|
string |
No |
null |
Table alias this fact is scoped to. |
|
string |
No |
null |
Output type hint for |
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
|
string |
No |
|
Access modifier: |
facts:
- name: net_price
expr: o.extended_price * (1 - o.discount)
source_table: o
comment: Price after discount
synonyms:
- discounted_price
- name: tax_amount
expr: o.net_price * o.tax_rate
source_table: o
Join¶
Each entry in the joins list declares a FK/PK relationship between tables. This maps to the SQL RELATIONSHIPS clause.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Target table alias (the table being joined to). |
|
|
string |
No |
|
Source table alias containing the FK columns. |
|
list of string |
No |
|
FK column names on the source table. |
|
list of string |
No |
|
Referenced columns on the target table. Defaults to the target’s primary key if omitted. |
|
string |
No |
null |
Relationship name. Required for role-playing dimensions ( |
|
string |
No |
|
|
joins:
- table: c
from_alias: o
fk_columns:
- customer_id
name: order_customer
cardinality: ManyToOne
- table: a
from_alias: f
fk_columns:
- departure_code
ref_columns:
- airport_code
name: dep_airport
Materialization¶
Each entry in the materializations list maps a pre-aggregated table to the dimensions and metrics it covers.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Materialization name (unique within the view). |
|
|
string |
Yes |
Pre-aggregated table name. Supports catalog-qualified names. Not validated for existence at define time. |
|
|
list of string |
No |
|
Dimension names covered. Must match declared dimension names. |
|
list of string |
No |
|
Metric names covered. Must match declared metric names. |
At least one of dimensions or metrics must be specified.
materializations:
- name: region_agg
table: daily_revenue_by_region
dimensions:
- region
metrics:
- revenue
- order_count
- name: global_agg
table: global_totals
metrics:
- revenue
NonAdditiveDim¶
Used within a metric’s non_additive_by list to specify snapshot selection behavior.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Dimension name for snapshot selection. |
|
|
string |
No |
|
Sort direction: |
|
string |
No |
|
NULLS placement: |
non_additive_by:
- dimension: report_date
order: Desc
nulls: First
WindowSpec¶
Used within a metric’s window_spec field to declare a window function metric.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Window function name ( |
|
|
string |
Yes |
Name of the metric to wrap in the window function. |
|
|
list of string |
No |
|
Additional function arguments (e.g., |
|
list of string |
No |
|
Dimensions to exclude from the partition set ( |
|
list of string |
No |
|
Explicit partition dimensions ( |
|
list of WindowOrderBy |
No |
|
ORDER BY entries for the window frame. |
|
string |
No |
null |
Raw SQL frame clause (e.g., |
window_spec:
window_function: AVG
inner_metric: total_qty
excluding_dims:
- date
order_by:
- expr: date
order: Asc
nulls: Last
frame_clause: "RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW"
WindowOrderBy¶
Used within a window spec’s order_by list.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Dimension name or expression to order by. |
|
|
string |
No |
|
Sort direction: |
|
string |
No |
|
NULLS placement: |
Size Limit¶
YAML definitions are capped at 1 MiB (1,048,576 bytes). Definitions exceeding this limit are rejected before parsing. Large definitions should be split into multiple semantic views.