Introducing a new way to review your PRs.

Read the announcement

getnao / nao

👾 nao is the #1 open source agentic BI. (1) Create context with nao-core cli, (2) deploy nao chat interface for everyone

View on GitHub

23 documents · Updated Nov 23, 2026

docs / data-and-semantic-queries

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:

  1. Nao discovers the configured semantic layer and its query mode.
  2. The semantic engine resolves metric and dimension names into SQL for the target database.
  3. 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.

ModeSemantic queriesRaw SQL on warehouseLocal SQL
No semantic layerUnavailableAvailable if permittedAvailable
PrioritizedAvailableAvailable if permittedAvailable
ExclusiveAvailableNot availableAvailable

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

TriggerConsequenceGuard
Several databases exist without database_idWarehouse execution cannot choose a connectionRequire an explicit database name
semantic_layer.database names an unknown databaseSemantic compilation cannot select its execution targetValidate the name against configured databases
Provider form fields do not match the backend credential contractValidation fails before Nao can test the warehouseKeep provider settings, payload conversion, and server field rules in sync
Required credentials are missing or an unsupported authentication field is sentSetup is rejected without a connection testValidate allowed fields and provider-specific authentication requirements
A supported user-identity warehouse needs a token but SSO is disabled or the identity token is unavailableThe warehouse cannot apply the signed-in user's identity and may deny the queryKeep the SSO feature gate, Azure or OIDC token sources, and optional warehouse token field aligned
Metric or dimension is unknown or its filter is invalidSemantic compilation returns a query error and no warehouse query runsUse the semantic catalog and documented references
The semantic manifest is missing, stale, or unsupported for the selected database typeThe semantic layer cannot load or compile the governed definitionsRun nao sync after the source project produces the manifest and use a supported database type
A query violates table, column, or row rulesThe warehouse rejects the request before or during executionKeep the shared SQL guard path enabled
A partitioned BigQuery table requires a filter and metadata context cannot supply onePreview or profiling cannot safely read the tableUse the provider-aware metadata path and configured partition filters
A project uses exclusive mode and sends warehouse SQL directlyRaw SQL is rejected even when the connection worksUse the semantic query route or local DuckDB
Setup stops after project registration, rule generation, or publishingA partial project or managed repository can remain unless rollback runsKeep cancellation, lease recovery, and rollback tied to the provisioning job
Setup workers stop repeatedly before finishingThe setup fails after the retry limit instead of remaining active foreverUse job leases, reconciliation, and bounded recovery attempts

Key files

FileWhy to read it
apps/…/warehouse-provisioning.tsShows the user-facing warehouse connection lifecycle, retries, recovery, rollback, and project readiness.
apps/…/warehouse_provisioning.pyShows provider credential validation and conversion into secret-free project configuration.
apps/…/warehouse-credentials-form.tsxShows how users provide provider-specific connection details and receive validation feedback.
apps/…/warehouse-credentials.tsShows how form values map to the backend warehouse credential contract.
apps/…/warehouse-provider-config.tsDefines the provider-level connection fields and defaults presented to users.
cli/…/provider.pyShows how warehouse schemas and tables become agent-facing project context.
cli/…/context.pyShows the common metadata, preview, and profiling context available during database sync.
cli/…/bigquery.pyShows how a provider-specific warehouse constraint changes safe metadata access.
apps/…/semantic-layer.service.tsDefines semantic-layer discovery, modes, catalog access, and compilation.
apps/…/execute-semantic-query.tsShows how a metric request becomes a normal governed query result.
apps/…/execute-sql.tsShows the shared routing between local data, application data, and warehouses.
apps/…/warehouse-access-token.service.tsShows how enterprise SSO can provide a signed-in user's token for supported warehouse authentication.
apps/…/warehouse-sql.service.tsShows the boundary between the backend and the warehouse service.
apps/…/main.pyShows database resolution, access checks, execution, and semantic endpoints.
cli/…/provider.pyShows how the manifest becomes the project’s metric and dimension catalog.
cli/…/metricflow.pyExplains the compile-only role of the semantic engine.
apps/…/warehouse-credentials.tsShows how project warehouse credentials are stored and supplied without exposing values.
example/nao_config.yamlGives a small example of database and semantic-layer configuration.