QueriesOverview

Queries Overview

A query is a named, parameterized read against one or more projection tables. You declare queries in .queries.causet files. The compiler converts them to parameterized SQL. At runtime, the query service binds inputs and executes against the fork’s projection schema.

Callers do not write SQL. They call a query by name with typed parameters.


How queries fit into the read path

Write path: Intents emit events → projection worker UPSERTs rows.

Read path: Named queries compile to SQL over those rows → query service returns JSON.

Projections and queries are eventually consistent with the ledger. There is a lag between a write and when it appears in query results.


Execution flow

  1. Client calls POST .../queries/{query_name} with a JSON params body.
  2. Query service resolves the active IR version for the fork.
  3. Compiled SQL template loads from the IR; parameters bind.
  4. Optional Redis cache check (if cache_ttl_seconds is configured).
  5. SQL executes in the tenant schema {platform}_{application}_{fork}.
  6. Results return as a JSON array.

Declaring queries

# app.causet
includes:
  queries: [./queries/**/*.queries.causet]

Each query names a from projection table, optional input params, filters, joins, and limits. The compiler validates table references and parameter types at build time.


In this section

PageWhat it covers
ConceptWhy queries exist, read vs write path, eventual consistency
Defining Queriesfrom, input, fields, top-level schema
Filters & JoinsWHERE operators, joins, order, limit
Aggregatesgroup_by, aggregate, coalesce_zero
CachingRedis TTL and staleness trade-offs
Best PracticesCommon mistakes and production guidance
ExamplesConcert-app query catalog

Next steps