Data access and semantic queries
Nao connects a project to one or more warehouses, turns governed requests into SQL, and returns query results that downstream tools can reuse. It keeps semantic definitions separate from warehouse execution so metric meaning, access rules, and database credentials remain under project control.
Data source configuration
The project configuration defines the databases that Nao can use and the semantic layer that describes governed metrics. A database has a project-visible name and a provider-specific connection; a semantic layer points to a compiled manifest and selects the database that runs semantic queries. See example/nao_config.yaml for the relationship between these settings.
Warehouse credentials enter through a provider-specific connection form rather than through chat. The form collects only the fields needed by the selected provider, supports the provider's authentication choices, and keeps entered values while a user corrects a failed check. Nao validates the connection before setup, reports the fields that need attention, and does not expose credential values in public job state. References: apps/…/warehouse-provider-config.ts• L47-125, apps/…/warehouse-credentials.ts• L61-243, apps/…/warehouse-provisioning.ts• L178-239
After validation, Nao prepares a project configuration with environment references instead of plain-text secrets. It runs project setup, checks the connection, syncs warehouse metadata, and registers the project. It then waits for business context before it generates onboarding rules, publishes the project, and marks the connection ready. Setup can be cancelled, retried, or recovered after a worker stops; a failed setup rolls back resources that it created. Stored credentials are encrypted and later removed after the retry window. References: apps/…/warehouse_provisioning.py• L148-219, apps/…/warehouse_provisioning.py• L291-359, apps/…/warehouse-provisioning.ts• L349-420, apps/…/warehouse-provisioning.ts• L695-863, apps/…/warehouse-provisioning.ts• L1064-1220
The setup path is summarized below.
flowchart LR
A[Provider form] --> B[Connection validation]
B --> C[Project setup]
C --> D[Warehouse metadata sync]
D --> E[Project registration]
E --> F[Business context]
F --> G[Onboarding rules and publication]
G --> H[Governed project ready]
C -. failure or cancel .-> I[Rollback]
D -. failure or cancel .-> I
E -. failure or cancel .-> I
G -. failure or cancel .-> I
Database selection
The data service resolves a request to a configured database before it opens a connection. A project with one database uses it by default. A project with several databases must name the target, and semantic queries use the database named by semantic_layer.database or the only configured database. References: apps/…/main.py• L342-420, apps/…/main.py• L773-810, apps/…/nao-config.ts• L195-234
Warehouse query execution
Warehouse execution is the shared path for SQL that reaches an external database. Nao loads the selected connection, validates the query against project access rules, runs it, and returns rows, columns, a row count, and a database dialect. The same result shape gives charts, stories, and later local queries a common interface. See SQL and warehouse execution for the child page on this path. References: apps/…/execute-sql.ts• L32-112, apps/…/main.py• L576-630, apps/…/warehouse-sql.service.ts• L41-110
For warehouses that support user-identity authentication, enterprise SSO can provide the signed-in user's identity-provider token. Nao checks that the project has the SSO feature before it requests an Azure or OIDC token, then passes an available token with the warehouse query. The open-source edition sends no such token. This allows a supported warehouse to apply the user's own identity without changing the shared query result contract. References: apps/…/warehouse-access-token.service.ts• L7-16, apps/…/warehouse-sql.service.ts• L77-110
Before execution, Nao can restrict tables, hide excluded columns, and enforce row-level rules. These checks apply at the warehouse boundary, so a query cannot bypass them by coming from the semantic layer. Raw SQL can be disabled for a project while semantic queries still reach the configured warehouse. References: apps/…/main.py• L197-250, apps/…/main.py• L423-476, apps/…/execute-sql.ts• L219-227
When warehouse access is not needed, Nao uses its local DuckDB for files and earlier results. In semantics-only mode this local path remains available for reshaping a governed result, while direct warehouse SQL is limited to the semantic query path. References: apps/…/execute-sql.ts• L75-138, apps/…/execute-sql.ts• L229-233
The database sync path builds the context that helps agents use a warehouse safely. It connects to each configured database, discovers schemas and tables, and produces selected metadata such as columns, previews, profiles, and query history. Provider-specific rules can change how this context is gathered; for example, BigQuery previews and counts account for partition-filter requirements instead of forcing an unsafe full-table read. References: cli/…/provider.py• L269-360, cli/…/provider.py• L367-451, cli/…/context.py• L26-34, cli/…/context.py• L131-162, cli/…/bigquery.py• L125-181
The request and result path is summarized below.
flowchart LR
A[Agent request] --> B{Request type}
B -->|Raw SQL| C[SQL checks]
B -->|Metric query| D[Semantic layer compile]
D --> C
C --> E[Database selection]
E --> F[Access and row rules]
F --> G[Warehouse execution]
G --> H[Query result]
H --> I[Charts, stories, or local follow-up]
Semantic layer queries
The semantic layer is a governed vocabulary for metrics and dimensions. A metric query names measures, optional grouping dimensions, filters, sort order, row limit, and a time range instead of writing joins and aggregations directly. The catalog exposes the valid metrics and dimensions for discovery and query editors. See Semantic-layer workflows for the child page on semantic definitions and query handling. References: apps/…/execute-semantic-query.ts• L5-29, apps/…/execute-semantic-query.ts• L47-67, cli/…/provider.py• L78-103, cli/…/provider.py• L230-272
The semantic sync step copies the source manifest into the project runtime and creates a readable catalog of metrics and dimensions. Runtime queries use that project-local definition, so the deployed project does not need the original dbt project to remain present. A stale or missing manifest prevents governed definitions from loading. References: cli/…/provider.py• L78-103, cli/…/metricflow.py• L50-61, cli/…/metricflow.py• L120-132
Nao compiles the metric query against the project manifest and the dialect of its selected database. The semantic engine only produces SQL. Nao then sends that SQL through the normal warehouse execution path, which keeps connection handling and access checks in one place. Semantic queries work only for database types with a supported semantic SQL dialect; unsupported types fail before warehouse execution. References: cli/…/metricflow.py• L37-71, cli/…/metricflow.py• L142-165, cli/…/metricflow.py• L190-236, apps/…/execute-semantic-query.ts• L24-36
The semantic query lifecycle has three stages:
- Nao discovers the configured semantic layer and its query mode.
- The semantic engine resolves metric and dimension names into SQL for the target database.
- The shared SQL path validates and runs the SQL, then records the result with its semantic source.
The sequence below shows how a metric request reaches the warehouse and returns as a governed result.
sequenceDiagram
participant A as Agent
participant S as Semantic layer
participant Q as SQL executor
participant W as Warehouse
A->>S: Metrics, dimensions, filters, time range
S-->>A: Compiled SQL and database identity
A->>Q: SQL with semantic source
Q->>W: Validated SQL
W-->>Q: Rows and columns
Q-->>A: Governed query result
Semantic-layer mode controls which route the agent may use. With no configured layer, the semantic tool is unavailable. In prioritized mode both semantic queries and warehouse SQL are available. In exclusive mode the warehouse is reachable through semantic queries while raw SQL remains local-only. References: apps/…/semantic-layer.service.ts• L28-49, apps/…/execute-sql.ts• L219-225
Query result reuse
A successful warehouse or semantic query receives a query ID and remains available to later tools. Semantic results can feed charts, stories, page results, and local DuckDB work. A semantic result cannot be edited as raw SQL; the agent must issue another semantic request when it needs different metrics, groups, or filters. References: apps/…/execute-semantic-query.ts• L10-14, apps/…/execute-sql.ts• L92-100, apps/…/execute-sql.ts• L167-217
Access modes
Nao separates the meaning of a request from the permission to read data. The semantic layer defines what a metric means, while the warehouse request applies table, column, and row controls to the resulting SQL.
| Mode | Semantic queries | Raw SQL on warehouse | Local SQL |
|---|---|---|---|
| No semantic layer | Unavailable | Available if permitted | Available |
| Prioritized | Available | Available if permitted | Available |
| Exclusive | Available | Not available | Available |
References: apps/…/semantic-layer.service.ts• L28-49 apps/…/execute-sql.ts• L219-225
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the provider form fields and credential payload names aligned with backend normalization and validation. A camelCase-to-snake_case mismatch can make a valid form fail before connection setup.
- Keep authentication choices aligned across the form, server-side credential rules, and derived provider settings. The server must remain the authority for authentication modes that affect access.
- Keep user-identity warehouse authentication aligned with the SSO feature gate, Azure and OIDC token sources, and the optional token field sent to the warehouse boundary.
- Keep prepared environment references aligned with project config loading and stored project credentials. Secrets must not enter generated config or command logs.
- Keep provisioning status and rollback behavior aligned with project registration, metadata sync, business context, rule generation, publication, and retry handling. A partial setup must not appear as a connected project.
- Keep database sync metadata rules aligned with provider limits such as partition filters and with the context used by templates and agents.
- Keep the copied semantic manifest, generated metric catalog, supported SQL dialects, and semantic database selection aligned. A definition can be discoverable yet still fail at compile or execution time.
- Keep the semantic query result contract aligned with the SQL result contract so charts, stories, and page results can consume both.
- Keep warehouse access and row-security inputs flowing into both raw SQL and compiled semantic SQL.
- Keep semantic-layer mode rules aligned with tool exposure and the local-only SQL description.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Several databases exist without database_id | Warehouse execution cannot choose a connection | Require an explicit database name |
semantic_layer.database names an unknown database | Semantic compilation cannot select its execution target | Validate the name against configured databases |
| Provider form fields do not match the backend credential contract | Validation fails before Nao can test the warehouse | Keep provider settings, payload conversion, and server field rules in sync |
| Required credentials are missing or an unsupported authentication field is sent | Setup is rejected without a connection test | Validate allowed fields and provider-specific authentication requirements |
| A supported user-identity warehouse needs a token but SSO is disabled or the identity token is unavailable | The warehouse cannot apply the signed-in user's identity and may deny the query | Keep the SSO feature gate, Azure or OIDC token sources, and optional warehouse token field aligned |
| Metric or dimension is unknown or its filter is invalid | Semantic compilation returns a query error and no warehouse query runs | Use the semantic catalog and documented references |
| The semantic manifest is missing, stale, or unsupported for the selected database type | The semantic layer cannot load or compile the governed definitions | Run nao sync after the source project produces the manifest and use a supported database type |
| A query violates table, column, or row rules | The warehouse rejects the request before or during execution | Keep the shared SQL guard path enabled |
| A partitioned BigQuery table requires a filter and metadata context cannot supply one | Preview or profiling cannot safely read the table | Use the provider-aware metadata path and configured partition filters |
| A project uses exclusive mode and sends warehouse SQL directly | Raw SQL is rejected even when the connection works | Use the semantic query route or local DuckDB |
| Setup stops after project registration, rule generation, or publishing | A partial project or managed repository can remain unless rollback runs | Keep cancellation, lease recovery, and rollback tied to the provisioning job |
| Setup workers stop repeatedly before finishing | The setup fails after the retry limit instead of remaining active forever | Use job leases, reconciliation, and bounded recovery attempts |
Key files
| File | Why to read it |
|---|---|
| apps/…/warehouse-provisioning.ts | Shows the user-facing warehouse connection lifecycle, retries, recovery, rollback, and project readiness. |
| apps/…/warehouse_provisioning.py | Shows provider credential validation and conversion into secret-free project configuration. |
| apps/…/warehouse-credentials-form.tsx | Shows how users provide provider-specific connection details and receive validation feedback. |
| apps/…/warehouse-credentials.ts | Shows how form values map to the backend warehouse credential contract. |
| apps/…/warehouse-provider-config.ts | Defines the provider-level connection fields and defaults presented to users. |
| cli/…/provider.py | Shows how warehouse schemas and tables become agent-facing project context. |
| cli/…/context.py | Shows the common metadata, preview, and profiling context available during database sync. |
| cli/…/bigquery.py | Shows how a provider-specific warehouse constraint changes safe metadata access. |
| apps/…/semantic-layer.service.ts | Defines semantic-layer discovery, modes, catalog access, and compilation. |
| apps/…/execute-semantic-query.ts | Shows how a metric request becomes a normal governed query result. |
| apps/…/execute-sql.ts | Shows the shared routing between local data, application data, and warehouses. |
| apps/…/warehouse-access-token.service.ts | Shows how enterprise SSO can provide a signed-in user's token for supported warehouse authentication. |
| apps/…/warehouse-sql.service.ts | Shows the boundary between the backend and the warehouse service. |
| apps/…/main.py | Shows database resolution, access checks, execution, and semantic endpoints. |
| cli/…/provider.py | Shows how the manifest becomes the project’s metric and dimension catalog. |
| cli/…/metricflow.py | Explains the compile-only role of the semantic engine. |
| apps/…/warehouse-credentials.ts | Shows how project warehouse credentials are stored and supplied without exposing values. |
| example/nao_config.yaml | Gives a small example of database and semantic-layer configuration. |