SELECT statement.
Style 1 — SEMANTIC_VIEW() relation function
Use SEMANTIC_VIEW(...) in the FROM clause to specify exactly which metrics
and dimensions to retrieve, plus optional filtering and sorting. The database
inlines the semantic view as a derived subquery with the requested aggregation.
Syntax
SEMANTIC_VIEW(...) are unordered and optional, with
the exception of the view name which must appear first.
Parameters
Inlining behaviour
The database transforms Style-1 syntax into an equivalent SQL query:LEFT JOIN is used. Set
enable_semantic_view_inner_join = true to switch to INNER JOIN.
Only the tables the query needs appear in FROM. See
Table pruning below.
Examples
Detail queries: FACTS
FACTS asks for the rows themselves instead of an aggregate. At row level a dimension is
just a fact the model also groups by, and a window metric has one value per row, so FACTS
accepts all three: declared facts, dimensions, and window metrics.
GROUP BY, so the
result keeps the grain of the root table:
FACTScannot appear together withMETRICSorDIMENSIONS: raw rows and aggregated rows have different grains, so a single query cannot ask for both.HAVINGis rejected — there is no aggregation to filter. Put the predicate inWHERE.- An aggregate metric is rejected: it has no row-level value. Request it with
METRICS. A window metric is accepted. - The view-level
FILTERstill applies,AND-ed with the query’s ownWHERE.
SEMANTIC_VIEW(...) inlines to its own sub-query, and the outer query joins them on the
facts they expose.
Validating the join topology: EXPLAIN SNOWFLAKE
EXPLAIN SNOWFLAKE <query> checks that a query joins its tables as a single snowflake —
every join edge pointing from the foreign-key side to a side whose join columns cover a unique
key, one root table, and every other table reachable from it. That is the condition under which
the row grain of the root table survives every join, so metrics cannot be silently inflated by a
fan-out.
unique_constraints table property, and — for a sub-query, CTE or
logical view — the GROUP BY, SELECT DISTINCT or ROW_NUMBER() OVER (PARTITION BY ...) = 1
that establishes uniqueness inside it.
The statement raises an error when the topology cannot be a snowflake: neither side of an equal
join is unique (the row grain would be lost), the query spans several independent snowflakes,
an ON clause carries no join key, or a single-inequality (ASOF-shaped) edge points at a side
that is not unique. Cases that metadata cannot decide are reported as explicit assumptions
rather than assumed away — a range join notes that it relies on the intervals never overlapping,
and an ASOF JOIN notes that the winner of a tie is unspecified.
Reading a query’s substrate: EXPLAIN LINEAGE
EXPLAIN LINEAGE [JSON | TEXT] <query> reports the facts about a query that a tool needs in
order to reason about it — the same join topology EXPLAIN SNOWFLAKE validates, plus what a
semantic-view recommendation depends on. The query is analysed and planned, never executed.
Column statistics are deliberately left out: large, and nothing in a recommendation depends on
them. A table whose DDL cannot be rendered is listed by name with the reason, rather than
dropping the whole section.
The TEXT rendering walks whatever the substrate contains rather than naming fields, so the two
serializations cannot drift apart: a key added later appears in both at once.
Every value stays on one bullet, however long it is: a plan, a
CREATE TABLE or the statement
itself is a single fact, so the line breaks inside it are escaped as \n exactly as JSON escapes
them, rather than being spread over sibling bullets. Undoing the escape gives back the original
text character for character.
Style 2 — plain table reference
Reference the semantic view as if it were a regular table. The database inlines the view’s columns into a wide subquery and exposes them with flat aliases of the form<tableAlias>__<columnName>.
Note
Facts are projected only by a view that declares no METRICS. When a view
declares metrics, the inlined subquery is grouped at dimension grain, so a raw
fact column has no well-defined value there: projecting it would either be
invalid SQL or, if it were added to the grouping, change the grain and therefore
every metric value. Expose such a column as a dimension if you need to select it
alongside metrics.
Syntax
Flat column aliases
Each column from the semantic view is surfaced as<tableAlias>__<columnName>. For example, dimension orders.order_date is
exposed as orders__order_date, metric orders.total_price as
orders__total_price.
Example
Given:Several semantic view accesses in one query
A single query may reference as many semantic views as it needs, in either style and in any mix. Each reference is inlined independently, so period-over-period reports — the usual reason to want more than one — are written the same way as against ordinary tables:db.view.column) does not reach into the sub-query; use the alias instead.
Table pruning: only the tables a query needs
A semantic view models a whole snowflake, but a single query rarely reads all of it. The inlined query joins the smallest set of tables that covers the tables its projections (METRICS / DIMENSIONS / FACTS) and its predicates (WHERE, HAVING, ORDER BY, and
the view-level FILTER) reference — plus whatever tables are needed to connect them.
COUNT(power_station.ps_id) returned the number of devices, not the number of stations.
Pruning is skipped, and every declared table joined, when a predicate names a base-table
column directly (WHERE o_orderstatus = 'F'), since the model alone does not say which
table owns that column. Reference the fact or dimension instead
(WHERE orders.order_status = 'F') to keep the query pruned.
Session variables: table pruning
INNER JOIN is not inert the way a LEFT JOIN is. A
LEFT JOIN to a table nothing reads neither drops nor duplicates rows, so removing it cannot
change the answer. An INNER JOIN also drops the rows that fail to match, so a model that
relies on that filtering — treating the join as a data-quality gate rather than as a lookup —
can keep it. Pruning stays on by default in both modes: a relationship declares a foreign key,
and “only the tables the query needs” is what the model means either way.
Session variable: enable_semantic_view_inner_join
Inspecting the inline SQL: EXPLAIN INLINE
EXPLAIN INLINE <query> prints the inline SQL — the equivalent query where every
semantic-view reference has been expanded into its underlying base-table SQL (joins +
aggregation). The printed SQL is legal and directly executable: all identifiers are
quoted so the aliases remain valid.
This works for both query styles, and for semantic views used anywhere in the query
(including inside sub-queries and CTEs).
EXPLAIN INLINE returns the query unchanged.
The same expansion is available over HTTP via the semantic view inline REST endpoint,
POST /api/v2/catalogs/{catalog}/databases/{db}/semantic_views/inline.