query_nested
Run multi-stage queries as a DAG where each stage can reference prior outputs as source models. Solves chained analytics where one query's result feeds the next.
Instructions
Run a multi-stage query as a DAG. Use this when one stage depends on the output of another.
queries is a list of query dicts forming a DAG. Each entry has the
same shape as the regular query tool's arguments
(source_model, measures, dimensions, filters,
time_dimensions, order, limit, offset,
whole_periods_only) plus an optional name. Stages reference
each other by name via source_model: "<sibling_name>" or
joins.target_model.
Order doesn't matter — the engine auto-sorts so every stage
appears after the siblings it references. The last entry of
the input is always the entry point / DAG root (its result is
what's returned); only the non-final entries are reordered.
Every non-final entry must have a name. Cycles,
self-references, and a non-final stage referencing the root are
rejected with a clear error. Stages that aren't reachable from
the root are accepted as utility sub-queries — they're silently
dropped from the emitted SQL.
Args:
queries: Ordered list of stage dicts. Earlier stages must be
named; the last stage is the one whose rows return.
variables: Variable values for {var} placeholder
substitution in filters. Runtime kwarg precedence:
runtime > stage.variables > outer query.variables > model.query_variables.
show_sql: When true, include the generated SQL in the response.
dry_run: When true, generate the SQL without executing it.
explain: When true, run EXPLAIN ANALYZE and return the plan.
format: markdown (default), json, or csv.
Example: queries=[ {"name": "monthly", "source_model": "orders", "measures": [{"formula": ":count"}, {"formula": "revenue:sum"}], "time_dimensions": [{"dimension": "created_at", "granularity": "month"}]}, {"source_model": "monthly", "measures": [{"formula": ":count"}]} ]
For a single-stage query, prefer the regular query tool — its
typed arguments give a more discoverable schema.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | markdown | |
| dry_run | No | ||
| explain | No | ||
| queries | Yes | ||
| show_sql | No | ||
| variables | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |