How Orion writes SQL
How Orion answers data questions semantic-layer-first โ reveals before raw SQL, read-only guardrails, the Luminary-only raw-SQL refusal, and the SQL reviewer.
When to use it โ Any time you ask Orion a data question and want to understand (or trust) where the numbers came from.
Where to find it โ Ask Orion anything in the Orion chat panel. The behavior described here is built into how Orion answers โ there is nothing to turn on.
Who can use it โ Everyone can ask Orion questions (Lens, Tracer, Weaver, Luminary). Raw, ungoverned SQL is restricted to Luminary.
When you ask Orion a data question, it does not just guess a SELECT and run it. Orion is built semantic-layer-first: it always checks your governed reveals before writing any SQL, prefers running a reveal when one matches, and falls back to raw SQL only when nothing in the semantic layer covers your question.
This page explains exactly what Orion does under the hood, the guardrails that keep its queries safe and governed, and the two supporting features โ the optional SQL reviewer and the "Explain the SQL" helper.
Overview
Every analytical answer Orion produces follows a fixed routing order:
- Call
get_revealsfirst โ before writing any SQL, for every analytical question, Orion searches your reveals for one that covers the concept you asked about (revenue, orders, customers, returns, products, inventory, and so on). - Run the reveal if one matches โ when a relevant reveal exists, Orion calls
run_revealinstead of writing SQL. Reveals carry the correct metric definitions, joins, and warehouse SQL dialect, so the answer matches your governed numbers. - Raw SQL only as a last resort โ if and only if no reveal covers the question, Orion writes raw SQL with
run_sql. When it does, it tells you so plainly with a line like "No reveal covers this โ using raw SQL."
This order is not a suggestion to the model โ it is enforced by hard guardrails described below. Raw SQL is read-only, and ungoverned raw SQL is restricted to Luminary so your row- and field-level security still applies to everyone else.
When to use it
You do not invoke this behavior directly โ it runs every time Orion answers a data question. Understanding it helps when you want to:
- Trust a number. Read Orion's provenance footer to see whether the answer came from a governed reveal or from raw SQL.
- Diagnose a "wrong" answer. If a figure looks off, knowing whether it was reveal-backed tells you whether to fix a reveal definition or whether Orion fell back to ad-hoc SQL.
- Understand a permission message. If Orion tells you raw SQL is restricted to admins, this page explains why and what to do instead.
Concepts
| Term | What it means |
|---|---|
| Reveal | A governed query saved in your semantic layer โ it encodes the correct metric definitions, joins, and dialect. Orion runs reveals with run_reveal. See Reveals. |
get_reveals | The tool Orion calls first to search your workspace for reveals matching the question. |
run_reveal | The tool Orion uses to execute a matching reveal โ the preferred path over raw SQL. |
run_sql | The last-resort tool that runs raw read-only SQL against your warehouse. Restricted to Luminary. |
| Reveal-backed | Whether an answer was produced via the semantic layer. Orion's provenance footer makes this visible, and it is logged so semantic-layer coverage is measurable. |
| SQL reviewer | An optional second-pass Haiku agent that checks Orion's raw SQL for correctness before the answer is sent. |
Getting started
Nothing to set up. Open the Orion chat panel and ask a data question โ for example, "What was revenue last week?" Orion handles reveal lookup, execution, and provenance automatically.
A few things determine what Orion can do:
- Reveals must exist for Orion to answer from the semantic layer. The more of your key metrics are modeled as reveals, the more answers stay governed. See Reveals and Accuracy.
- Your role determines whether Orion can fall back to raw SQL on your behalf. Only Luminary can run ungoverned raw SQL.
- The SQL reviewer is off by default and is enabled per workspace by a Luminary.
Step-by-step
Orion checks reveals first
For every analytical question, Orion calls get_reveals to search your workspace for a reveal that covers the concept โ revenue, orders, customers, returns, products, inventory, events, and so on. It never skips this step, even when it thinks it already knows the answer.
If a reveal matches, Orion runs it
When get_reveals returns a relevant reveal, Orion calls run_reveal with the domain name and reveal name. The reveal supplies the correct metric definitions, joins, and SQL dialect for your warehouse. Once a reveal returns rows, that data is Orion's complete answer source โ it does not run extra SQL to re-filter or re-aggregate it.
If no reveal covers it, Orion falls back to raw SQL
Only when nothing in the semantic layer matches does Orion write raw SQL with run_sql. It states this honestly in its answer โ for example, "No reveal covers this โ using raw SQL." Before running, the read-only guardrail validates the query.
Optional: the reviewer double-checks the SQL
If your workspace has the adversarial SQL reviewer enabled, Orion's raw SQL is sent to a fast second-pass reviewer that checks metric definitions, grain, date filters, and missing exclusions before the result reaches you.
Orion shows where the number came from
Every data answer ends with a provenance line naming the source (the reveal name, or "raw SQL โ
| Symptom | Cause | Fix |
|---|---|---|
| "Raw SQL is restricted to admins. Use a governed reveal (run_reveal) so row- and field-level policies apply." | You are not a Luminary, so Orion will not run ungoverned raw SQL on your behalf โ this keeps row- and field-level security intact. | Ask the question in a way a reveal covers, or have a Weaver build a reveal for it. See Reveals. |
| "Orion can only execute read queries. ' | The generated SQL started with something other than SELECT, WITH, EXPLAIN, SHOW, or DESCRIBE. | Rephrase as a read-only question โ Orion never modifies the warehouse. |
| "Multiple SQL statements are not permitted โ send a single read query." | The query contained more than one statement (e.g. a smuggled second statement after a semicolon). | Ask for one thing at a time so Orion produces a single read query. |
| "No active connection found in this workspace." | There is no active warehouse connection for Orion to query. | Connect or activate a warehouse. See Warehouses. |
| Orion answered "No reveal covers this โ using raw SQL" for something you expected to be governed | No reveal in the workspace matched the concept, so Orion fell back to ungoverned SQL. | Build a reveal for that metric so future answers stay governed. See Reveals and Accuracy. |
| "Reviewer unavailable" note on an answer | The optional SQL reviewer timed out or failed; it is non-fatal, so Orion continued with the original SQL. | Manually verify the query, or retry. Reviewer failures never block an answer. |
Related pages
- Reveals โ the governed queries Orion runs first
- Semantic understanding โ how Orion maps your question onto the model
- Accuracy โ improving the correctness of Orion's answers
- Roles โ who can run raw SQL and how policies are enforced
- Discover query IDs โ tracing the queries Orion runs