# couchbase/query architecture

> Architecture diagram of the GitHub repository couchbase/query, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/couchbase/query
- Repository: https://github.com/couchbase/query

## Overview

Couchbase Query is a N1QL query engine. Its main workflow accepts a query request, parses it into algebra, prepares an executable plan, runs that plan against the datastore, and returns results. The repository also contains natural-language query support, document inference, full-text search planning, user-defined functions, and prepared-plan handling. The graph follows documented stages and relationships visible in the README or sampled source references; unsampled implementation details are not expanded into speculative links.

Main entry points: [`algebra`](https://github.com/couchbase/query/tree/master/algebra) (Statement algebra), [`natural/nlquery.go`](https://github.com/couchbase/query/blob/master/natural/nlquery.go) (Natural-language queries), [`server/server.go`](https://github.com/couchbase/query/blob/master/server/server.go) (Query server), [`datastore/datastore.go`](https://github.com/couchbase/query/blob/master/datastore/datastore.go) (Datastore interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_request["Request handling"]
  node_http_endpoint["HTTP endpoint"]
  node_request_handler["Request parsing<br/>[service_request.go]"]
  node_response["Response encoding"]
end

subgraph group_query["Query processing"]
  node_parser["N1QL parser<br/>[n1ql.go]"]
  node_algebra["Statement algebra"]
  node_expressions["Expressions"]
  node_semantics["Semantic analysis"]
  node_rewrite["Query rewrite"]
  node_planner["Planner optimizer"]
  node_plan["Executable plans"]
  node_execution["Plan execution<br/>[context.go]"]
end

subgraph group_data["Data and extensions"]
  node_datastore["Datastore interface<br/>[datastore.go]"]
  node_indexes["Index access<br/>[index.go]"]
  node_inference["Document inference"]
  node_fts["Full-text search<br/>[search_ftsindex.go]"]
  node_natural["Natural-language queries<br/>[nlquery.go]"]
  node_ai_gateway["AI provider gateway<br/>[gateway.go]"]
  node_udf_storage[("UDF storage<br/>[storage.go]")]
end

subgraph group_runtime["Engine runtime"]
  node_engine["Query server<br/>[server.go]"]
  node_prepared["Prepared plans<br/>[prepareds.go]"]
  node_service_manager["Cluster service manager<br/>[service_manager.go]"]
  node_engine_main["Engine bootstrap<br/>[main.go]"]
end

node_user(("Query client"))

node_user -->|"submits query"| node_http_endpoint
node_http_endpoint -->|"dispatches to"| node_request_handler
node_request_handler -->|"submits request"| node_engine
node_request_handler -->|"routes natural query"| node_natural
node_engine -->|"parses query"| node_parser
node_parser -->|"produces algebra"| node_algebra
node_algebra -->|"prepares statement"| node_planner
node_planner -->|"builds plan"| node_plan
node_plan -->|"runs as"| node_execution
node_execution -->|"accesses data"| node_datastore
node_datastore -->|"exposes indexes"| node_indexes
node_execution -->|"returns results"| node_response
node_algebra -->|"uses expressions"| node_expressions
node_engine -->|"uses prepared plans"| node_prepared
node_planner -->|"plans search scans"| node_fts
node_natural -->|"requests completions"| node_ai_gateway
node_natural -->|"builds query algebra"| node_algebra
node_natural -->|"reads query context"| node_datastore
node_inference -->|"retrieves documents"| node_datastore
node_engine_main -->|"starts server"| node_engine
node_engine_main -->|"initializes functions"| node_udf_storage
node_engine_main -.->|"starts services"| node_service_manager
node_semantics -.->|"analyzes statements"| node_algebra
node_rewrite -.->|"rewrites statements"| node_algebra

click node_http_endpoint "https://github.com/couchbase/query/blob/master/server/http/service_endpoint.go"
click node_request_handler "https://github.com/couchbase/query/blob/master/server/http/service_request.go"
click node_response "https://github.com/couchbase/query/blob/master/server/http/service_response.go"
click node_engine "https://github.com/couchbase/query/blob/master/server/server.go"
click node_parser "https://github.com/couchbase/query/blob/master/parser/n1ql/n1ql.go"
click node_algebra "https://github.com/couchbase/query/tree/master/algebra"
click node_expressions "https://github.com/couchbase/query/tree/master/expression"
click node_semantics "https://github.com/couchbase/query/tree/master/semantics"
click node_rewrite "https://github.com/couchbase/query/tree/master/rewrite"
click node_planner "https://github.com/couchbase/query/tree/master/planner"
click node_plan "https://github.com/couchbase/query/tree/master/plan"
click node_execution "https://github.com/couchbase/query/blob/master/execution/context.go"
click node_datastore "https://github.com/couchbase/query/blob/master/datastore/datastore.go"
click node_indexes "https://github.com/couchbase/query/blob/master/datastore/index.go"
click node_inference "https://github.com/couchbase/query/blob/master/inferencer/document_retrievers.go"
click node_fts "https://github.com/couchbase/query/blob/master/execution/search_ftsindex.go"
click node_natural "https://github.com/couchbase/query/blob/master/natural/nlquery.go"
click node_ai_gateway "https://github.com/couchbase/query/blob/master/natural/ai_gateway/gateway.go"
click node_udf_storage "https://github.com/couchbase/query/blob/master/functions/storage/storage.go"
click node_prepared "https://github.com/couchbase/query/blob/master/prepareds/prepareds.go"
click node_service_manager "https://github.com/couchbase/query/blob/master/server/control/couchbase/service_manager.go"
click node_engine_main "https://github.com/couchbase/query/blob/master/server/cbq-engine/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_http_endpoint,node_request_handler,node_response,node_user toneBlue
class node_parser,node_algebra,node_expressions,node_semantics,node_rewrite,node_planner,node_plan,node_execution toneAmber
class node_datastore,node_indexes,node_inference,node_fts,node_natural,node_ai_gateway,node_udf_storage toneMint
class node_engine,node_prepared,node_service_manager,node_engine_main toneRose
```

## Components

### Request handling

- [HTTP endpoint](https://github.com/couchbase/query/blob/master/server/http/service_endpoint.go): `server/http/service_endpoint.go`
- [Request parsing](https://github.com/couchbase/query/blob/master/server/http/service_request.go): `server/http/service_request.go`
- [Response encoding](https://github.com/couchbase/query/blob/master/server/http/service_response.go): `server/http/service_response.go`

### Query processing

- [N1QL parser](https://github.com/couchbase/query/blob/master/parser/n1ql/n1ql.go): `parser/n1ql/n1ql.go`
- [Statement algebra](https://github.com/couchbase/query/tree/master/algebra): `algebra`
- [Expressions](https://github.com/couchbase/query/tree/master/expression): `expression`
- [Semantic analysis](https://github.com/couchbase/query/tree/master/semantics): `semantics`
- [Query rewrite](https://github.com/couchbase/query/tree/master/rewrite): `rewrite`
- [Planner optimizer](https://github.com/couchbase/query/tree/master/planner): `planner`
- [Executable plans](https://github.com/couchbase/query/tree/master/plan): `plan`
- [Plan execution](https://github.com/couchbase/query/blob/master/execution/context.go): `execution/context.go`

### Data and extensions

- [Datastore interface](https://github.com/couchbase/query/blob/master/datastore/datastore.go): `datastore/datastore.go`
- [Index access](https://github.com/couchbase/query/blob/master/datastore/index.go): `datastore/index.go`
- [Document inference](https://github.com/couchbase/query/blob/master/inferencer/document_retrievers.go): `inferencer/document_retrievers.go`
- [Full-text search](https://github.com/couchbase/query/blob/master/execution/search_ftsindex.go): `execution/search_ftsindex.go`
- [Natural-language queries](https://github.com/couchbase/query/blob/master/natural/nlquery.go): `natural/nlquery.go`
- [AI provider gateway](https://github.com/couchbase/query/blob/master/natural/ai_gateway/gateway.go): `natural/ai_gateway/gateway.go`
- [UDF storage](https://github.com/couchbase/query/blob/master/functions/storage/storage.go): `functions/storage/storage.go`

### Engine runtime

- [Query server](https://github.com/couchbase/query/blob/master/server/server.go): `server/server.go`
- [Prepared plans](https://github.com/couchbase/query/blob/master/prepareds/prepareds.go): `prepareds/prepareds.go`
- [Cluster service manager](https://github.com/couchbase/query/blob/master/server/control/couchbase/service_manager.go): `server/control/couchbase/service_manager.go`
- [Engine bootstrap](https://github.com/couchbase/query/blob/master/server/cbq-engine/main.go): `server/cbq-engine/main.go`

### Other components

- **Query client**

## Connections

- Query client → HTTP endpoint: submits query
- HTTP endpoint → Request parsing: dispatches to (evidence: `server/http/service_endpoint.go`)
- Request parsing → Query server: submits request (evidence: `server/http/service_request.go`)
- Request parsing → Natural-language queries: routes natural query (evidence: `server/http/service_request.go`)
- Query server → N1QL parser: parses query (evidence: `server/server.go`)
- N1QL parser → Statement algebra: produces algebra
- Statement algebra → Planner optimizer: prepares statement (evidence: `planner/build_scan_search.go`)
- Planner optimizer → Executable plans: builds plan (evidence: `planner/build_scan_search.go`)
- Executable plans → Plan execution: runs as (evidence: `execution/context.go`)
- Plan execution → Datastore interface: accesses data (evidence: `execution/context.go`)
- Datastore interface → Index access: exposes indexes
- Plan execution → Response encoding: returns results (evidence: `server/http/service_response.go`)
- Statement algebra → Expressions: uses expressions (evidence: `algebra/subquery.go`)
- Query server → Prepared plans: uses prepared plans
- Planner optimizer → Full-text search: plans search scans (evidence: `planner/build_scan_search.go`)
- Natural-language queries → AI provider gateway: requests completions (evidence: `natural/nlquery.go`)
- Natural-language queries → Statement algebra: builds query algebra (evidence: `natural/nlquery.go`)
- Natural-language queries → Datastore interface: reads query context (evidence: `natural/nlquery.go`)
- Document inference → Datastore interface: retrieves documents (evidence: `inferencer/document_retrievers.go`)
- Engine bootstrap → Query server: starts server (evidence: `server/cbq-engine/main.go`)
- Engine bootstrap → UDF storage: initializes functions (evidence: `server/cbq-engine/main.go`)
- Engine bootstrap → Cluster service manager: starts services
- Semantic analysis → Statement algebra: analyzes statements
- Query rewrite → Statement algebra: rewrites statements
