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.
YAML keys differ from the SQL clause names in some cases:
SQL Clause |
YAML Key |
Notes |
|---|---|---|
|
|
|
|
|
Different name – YAML uses |
|
|
A list of |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
View-level comment |
|
|
A list of |
Complete Example¶
A YAML definition covering all supported features:
tables:
- alias: o
table: orders
pk_columns:
- id
comment: Order transactions
synonyms:
- order_facts
- alias: paid
definition: |
SELECT * FROM orders WHERE amount > 0
pk_columns:
- id
- alias: c
table: customers
pk_columns:
- id
joins:
- table: c
from_alias: o
fk_columns:
- customer_id
name: order_customer
cardinality: ManyToOne
variables:
- name: min_amount
data_type: DECIMAL(10,2)
default: '100'
comment: Smallest order counted as large
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
tags:
- name: {tag: S_LAYER_KIND}
value: dimension
- name: customer_name
expr: c.name
source_table: c
- name: report_date
expr: o.report_date
source_table: o
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: revenue_by_region
dimensions:
- region
metrics:
- revenue
- order_count
comment: Revenue analytics view
tags:
- name: {database: analytics, schema: governance, tag: owner}
value: finance
Minimal Definition¶
A minimal definition has tables, dimensions, and metrics. Both the dimensions and metrics keys must be present, and at least one of them must be non-empty. Write [] for the one you do not need:
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
A definition with metrics only:
tables:
- alias: o
table: orders
pk_columns:
- id
dimensions: []
metrics:
- name: revenue
expr: SUM(o.amount)
source_table: o
Note
tables must list at least one table. A definition that omits tables, or sets it to [], is rejected by CREATE SEMANTIC VIEW ... FROM YAML.
Changed in version 0.16.0: Earlier versions accepted such a definition, and the view failed at its first query.
Top-Level Keys¶
Key |
Type |
Required |
Description |
|---|---|---|---|
|
list of Table |
Yes |
Physical tables available to the view. Must list at least one table: a definition without |
|
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 Variable |
No |
View variables that member expressions and queries read. Maps to the SQL Added in version 0.18.0. |
|
list of Materialization |
No |
Pre-aggregated table mappings for query routing. |
|
string |
No |
View-level human-readable description. |
|
list of Tag |
No |
View-level tags. |
|
integer or string |
No |
|
|
mapping |
No |
Cortex Analyst instructions: |
|
list of VerifiedQuery |
No |
The YAML form of |
* Both keys must be present – a definition without a metrics key is rejected with a “missing field” error – and at least one of the two must be non-empty. Use [] for the empty one.
Table¶
Each entry in the tables list declares a physical table, or a table defined by a query, 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 |
One of these two |
Physical table name. Supports catalog-qualified names ( |
|
|
string |
One of these two |
The query of a table defined by a query, without its parentheses. It must be one query: its brackets nest, nothing in it is left open, and it holds no Added in version 0.18.0. |
|
|
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. |
|
list of Tag |
No |
|
Name/value metadata tags. |
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. Must be an alias declared in |
|
string |
No |
null |
No longer accepted. Write the cast into the expression instead –
|
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
|
list of Tag |
No |
|
Name/value metadata tags. |
|
boolean |
No |
|
|
|
list of string |
No |
|
The YAML form of |
|
boolean |
No |
|
The YAML form of |
|
mapping |
No |
null |
The YAML form of |
dimensions:
- name: region
expr: o.region
source_table: o
comment: Sales territory
- name: order_month
expr: CAST(date_trunc('month', o.ordered_at) AS DATE)
source_table: o
- name: is_eu
expr: o.region = 'EU'
source_table: o
is_filter: true
- name: warehouse
expr: o.warehouse
source_table: o
sample_values: [SMALL, LARGE]
is_enum: true
cortex_search_service:
service: warehouses
schema: search
literal_column: warehouse_name
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. Must be an alias declared in |
|
string |
No |
null |
No longer accepted. Write the cast into the expression instead (see the dimension table above). |
|
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. |
|
list of Tag |
No |
|
Name/value metadata tags. |
|
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 |
No longer accepted. Write the cast into the expression instead (see the dimension table above). SHOW SEMANTIC FACTS reports the expression’s actual type without it. |
|
string |
No |
null |
Human-readable description. |
|
list of string |
No |
|
Alternative names for discoverability. |
|
list of Tag |
No |
|
Name/value metadata tags. |
|
string |
No |
|
Access modifier: |
|
boolean |
No |
|
|
|
list of string |
No |
|
The YAML form of |
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 |
Yes |
Source table alias containing the FK columns. |
|
|
list of string |
Yes |
FK column names on the source table. At least one is required; |
|
|
list of string |
No |
|
Referenced columns on the target table. Defaults to the target’s primary key if omitted. |
|
string |
Yes |
Relationship name, referenced by |
|
|
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
Variable¶
Added in version 0.18.0.
Each entry in the variables list declares a view variable. This maps to the SQL VARIABLES clause; see VARIABLES for what a variable does and the rules it follows.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Variable name: one identifier. It cannot be the name of a fact, dimension, metric or other variable. |
|
|
string |
No |
null |
A DuckDB type that each value is cast to ( |
|
string |
No |
null |
The default expression, used when a query does not set the variable ( |
|
string |
No |
null |
Human-readable description. |
variables:
- name: min_amount
data_type: INTEGER
default: '1000'
- name: multiplier
data_type: DOUBLE
default: |
1.0
- name: target_region
data_type: VARCHAR
comment: the region to report on
data_type and default are stored trimmed, so a | block scalar, which ends in a line break, stores 1.0. Because an export has to re-create the same view through GET_DDL, CREATE SEMANTIC VIEW ... FROM YAML also refuses an entry that the SQL form could not write back:
a
data_typeordefaultholding a SQL comment marker (--or/*), whichCREATEwould drop:default: 2 -- a noteis refused;a
data_typeordefaultholding aDEFAULT,:=orCOMMENTof its own outside brackets, which would read back as a different variable.
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: 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: |
Tag¶
Added in version 0.17.0.
Used within the tags list of the view, a table, a fact, a dimension or a metric. The shape follows Snowflake’s semantic-view YAML: name is a mapping with the tag’s name parts.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
mapping |
Yes |
The tag name: |
|
|
string |
Yes |
The tag value, at most 256 characters. |
tags:
- name: {tag: S_LAYER_KIND}
value: metric
- name: {schema: governance, tag: pii}
value: none
A tag list may not name the same tag twice. Tags the SQL form could not write (an empty name part, a database without a schema, a value over 256 characters, a duplicate) are rejected by CREATE SEMANTIC VIEW ... FROM YAML.
VerifiedQuery¶
Added in version 0.18.0.
Used within the top-level verified_queries list. The field names are Snowflake’s semantic-view YAML names. See Cortex Analyst metadata.
Field |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
string |
Yes |
Entry name. One identifier, written as it would appear in |
|
|
string |
Yes |
The question the query answers. |
|
|
string |
Yes |
The SQL that answers it. Stored, not checked. |
|
|
integer |
No |
null |
When the query was verified, in seconds since the Unix epoch. |
|
string |
No |
null |
Who verified it. |
|
boolean |
No |
|
The YAML form of |
max_staleness: 3600
module_custom_instructions:
sql_generation: Round all numeric columns to 2 decimal places.
verified_queries:
- name: revenue_by_region
question: What is the revenue for each region?
sql: SELECT region, revenue FROM SEMANTIC_VIEW(sales DIMENSIONS region METRICS revenue)
use_as_onboarding_question: true
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.