Semantic layer workflows
The semantic layer turns governed metric questions into warehouse queries. Project configuration points to the layer's definitions and the database that runs its SQL. Agents and clients use the semantic query contract to discover metrics, compile queries, execute results, and reuse those results in charts and stories.
Semantic layer configuration
The semantic layer is enabled when a project declares semantic_layer in the fully qualified path nao_config.yaml. The declaration identifies the semantic engine, its manifest, and the database that executes compiled SQL. A project with one database can omit the database name; a project with several databases must name the target explicitly. example/nao_config.yaml• L1-21, cli/…/__init__.py• L15-45
The manifest is the source of the metric and dimension definitions. The sync workflow reads that source, copies a runtime manifest under .meta/semantic_layer/, and publishes readable metric and dimension context for agents. The generated context is refreshed by sync and is not an authoring surface. Metric pages can also carry business metadata such as ownership, expected ranges, and actions for unusual values. cli/…/provider.py• L78-116, cli/…/provider.py• L119-157
Runtime compilation prefers the synced manifest and falls back to the configured source when no synced copy exists. This lets runtime use a stable project artifact after the source build has completed. A failed later sync does not replace the last good copied manifest. cli/…/metricflow.py• L50-60, cli/…/test_sync_provider.py• L104-115
Semantic query lifecycle
The semantic query flow keeps definition, SQL generation, execution, and result reuse as separate stages. This lets the semantic layer govern the meaning of a metric while the normal SQL path keeps control of database access and result storage. MetricFlow acts as a compiler only; it does not connect to the warehouse. apps/…/semantic-layer.service.ts• L51-70, cli/…/metricflow.py• L1-5
The main flow is:
flowchart LR
A[Metric query] --> B[Semantic layer]
B --> C[Compiled SQL and target database]
C --> D[Standard query execution]
D --> E[Query result and query ID]
E --> F[Agent, chart, story, or client]
- The agent or client supplies metrics and optional grouping, filters, sorting, limits, and time bounds.
- The semantic layer resolves those names against the current definitions and compiles them for the target database dialect.
- The standard SQL executor runs the compiled SQL. The semantic engine does not connect to the warehouse itself.
- The result keeps the semantic query, compiled SQL, database ID, rows, and a reusable query ID.
- Agents and clients pass that query ID to result views such as charts, stories, or paged result displays. apps/…/execute-semantic-query.ts• L24-36, apps/…/execute-semantic-query.ts• L32-45
Query definition and catalog
The semantic query contract describes business terms instead of table joins. It requires at least one metric and supports dimensions, template-based filters, sort order, row limits, and inclusive time bounds. A catalog describes which metrics and dimensions are available so an editor or agent can select valid names. apps/…/execute-semantic-query.ts• L5-29, apps/…/execute-semantic-query.ts• L47-67
The sync output makes the catalog usable without opening the source project. Each metric page gives its type, dimensions, and query examples. The dimensions index gives the names and basic meaning of every exposed dimension. The output can include nested metric metadata so agents can use business context with the governed definitions. cli/…/provider.py• L119-157, cli/…/provider.py• L203-227
The catalog supports guided editing rather than replacing the semantic definitions. The client loads it for a stored query and uses it for completion and validation hints. apps/…/use-semantic-layer-catalog.ts• L6-12, apps/…/semantic-query-editor.tsx• L308-326
Query updates and freshness
The query editor treats a semantic result as an editable definition with a separate compiled result. A preview compiles and runs the draft without changing the stored query. Save and run compiles the draft, updates the stored tool result, and publishes the new result back to the chat. apps/…/update-semantic-query.ts• L35-62, apps/…/semantic-query-editor.tsx• L124-160
Definitions can change after a query runs. The client therefore compiles a saved semantic query again without executing it. It marks the result current when the new SQL matches, outdated when the SQL differs, and broken when the query no longer compiles. A later save and run refreshes the stored result. apps/…/use-semantic-query-freshness.ts• L19-60
The compiler reloads a manifest when its file changes and keeps separate compiled engines for different SQL dialects. This makes a refreshed manifest available to later queries without mixing definitions or dialect output across projects. cli/…/metricflow.py• L109-132
Agent and client access
Semantic access depends on both project configuration and an administrator-selected mode. The project must declare a usable layer before any mode can enable it. The mode then controls whether the semantic query tool is exposed and whether direct warehouse SQL remains available. apps/…/semantic-layer.service.ts• L23-49
| Mode | Semantic queries | Direct warehouse SQL |
|---|---|---|
disabled | Not exposed | Available |
prioritized | Exposed and preferred for governed metrics | Available as a fallback |
exclusive | Exposed for warehouse questions | Limited to the local database and prior results |
The same access policy applies to the context-layer MCP tools. When enabled, an MCP client receives the semantic query tool and a matching result contract. Tool schemas are sent without an explicit JSON Schema dialect marker so clients that expect the newer schema default can accept them. apps/…/context-layer.ts• L56-64, apps/…/context-layer.ts• L125-136, apps/…/tool-schema-dialect.ts• L11-22
Dialect rules
Dialect handling has two related roles. The semantic compiler renders SQL for the configured database type. Agent guidance also adds warehouse-specific SQL and tool-use rules when a matching connection is present. This keeps generated SQL aligned with the target warehouse and helps direct SQL fallback avoid known unsupported forms. cli/…/metricflow.py• L37-71, apps/…/dialect-rules.tsx• L8-16
The compiler supports a defined set of database types. MotherDuck uses the DuckDB renderer. An unsupported type blocks semantic compilation rather than silently using a different SQL dialect. Active guidance is selected from the connection types, so adding or changing a warehouse connection can change the rules included in the agent prompt. cli/…/metricflow.py• L37-71, apps/…/dialect-rules.tsx• L164-175
The sync catalog does not depend on the target warehouse dialect because listing definitions does not render query SQL. Query compilation still uses the configured database dialect. This keeps generated documentation stable while preserving database-specific SQL at execution time. cli/…/provider.py• L90-108, cli/…/metricflow.py• L142-165
Validation and failure handling
Validation occurs before warehouse execution. The semantic service sends the project folder and required environment values to the local semantic service. That service loads the configured layer, resolves the target database, and returns either compiled SQL or a catalog. Query errors are distinct from setup or service errors so clients can show a useful correction or availability message. apps/…/semantic-layer.service.ts• L73-89, apps/…/main.py• L697-731, apps/…/main.py• L734-791
The compiler rejects empty metric selections, invalid dates, unknown metrics or dimensions, invalid filters, missing manifests, and unsupported database types before SQL execution. A missing MetricFlow package is reported as an unavailable semantic layer rather than as a query error. Stored-query updates convert query errors into client-facing bad requests, while missing queries, wrong owners, and non-semantic query IDs are rejected before compilation. cli/…/metricflow.py• L23-35, cli/…/metricflow.py• L63-71, cli/…/metricflow.py• L142-165, apps/…/update-semantic-query.ts• L74-105
Related execution path
The semantic workflow ends at the shared SQL execution path rather than creating a second warehouse access path. Read SQL and warehouse execution for the rules that govern direct SQL, database access, and result handling after compilation. Read the parent page, Data and semantic queries, for the broader query model.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the semantic query input and output schemas aligned with the agent tool, MCP registration, stored-query updates, and editor fields.
- When changing manifest sync or runtime manifest resolution, check both generated agent context and the manifest used for compilation. A stale
.metacopy can hide a newer source manifest. - When adding a database type, update both the semantic compiler dialect map and the database-specific agent guidance.
- When changing semantic-layer modes, check tool exposure, direct SQL restrictions, system-prompt guidance, and MCP schemas together.
- When changing compiled-result fields, preserve the query ID and semantic query metadata used by charts, stories, freshness checks, and local result access.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The manifest path is missing or the manifest is not available | The semantic layer cannot load or sync its definitions | Validate semantic_layer.manifest_path and run sync after the source manifest is built |
| A source manifest becomes invalid during sync | Sync fails and the existing copied runtime manifest remains in place | Fix the source manifest before the next sync; treat the sync result as authoritative |
Several databases are configured without semantic_layer.database | The target database is ambiguous and compilation stops | Set the database name in project configuration |
| A metric, dimension, filter, or date is invalid | Compilation returns a query error and no warehouse query runs | Use the catalog and generated metric or dimension context before editing a query |
| The database type has no supported semantic renderer | The layer cannot produce target-dialect SQL | Use a supported database type or add its renderer and guidance as one change |
| Definitions change after a result is saved | The saved result can be outdated or stop compiling | Recompile on freshness checks and save and run to refresh it |
| Exclusive mode receives direct warehouse SQL | The request is rejected while local DuckDB queries remain available | Route governed warehouse questions through execute_semantic_query |
Key files
| File | Why to read it |
|---|---|
| example/nao_config.yaml | Shows the project-level semantic layer declaration and database link. |
| cli/…/__init__.py | Defines the configuration contract and manifest path behavior. |
| cli/…/provider.py | Explains how definitions become a runtime manifest and agent-readable metric and dimension context. |
| cli/…/metricflow.py | Describes the compiler-only boundary, manifest selection, validation, caching, and supported SQL dialect mapping. |
| apps/…/main.py | Shows the compile and catalog service boundary and validation outcomes. |
| apps/…/semantic-layer.service.ts | Shows routing modes and the backend boundary to the semantic service. |
| apps/…/execute-semantic-query.ts | Defines the shared semantic query and result contract. |
| apps/…/execute-semantic-query.ts | Shows how compiled SQL enters standard query execution and becomes reusable output. |
| apps/…/update-semantic-query.ts | Shows preview, save, catalog, and stored-query validation behavior. |
| apps/…/use-semantic-query-freshness.ts | Shows how clients detect definition changes without rerunning warehouse work. |
| apps/…/context-layer.ts | Shows how agents and MCP clients receive semantic query access. |
| apps/…/dialect-rules.tsx | Shows how active warehouse connections shape agent SQL guidance. |