Celiq
v0.12Open app โ†—
Docs/Orion AI/How Orion writes SQL

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.

TracerWeaverLuminaryUpdated June 2026 ยท 7 min read
โœฆ
In this section
Part of Celiq's semantic layer platform. Connect your warehouse, model your data once, query it everywhere.
๐Ÿ’ก
Tip
What it is โ€” The rules Orion follows whenever a question needs to query your warehouse: it reaches for a governed reveal first, and only writes raw SQL as a last resort.
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:

  1. Call get_reveals first โ€” 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).
  2. Run the reveal if one matches โ€” when a relevant reveal exists, Orion calls run_reveal instead of writing SQL. Reveals carry the correct metric definitions, joins, and warehouse SQL dialect, so the answer matches your governed numbers.
  3. 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

TermWhat it means
RevealA 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_revealsThe tool Orion calls first to search your workspace for reveals matching the question.
run_revealThe tool Orion uses to execute a matching reveal โ€” the preferred path over raw SQL.
run_sqlThe last-resort tool that runs raw read-only SQL against your warehouse. Restricted to Luminary.
Reveal-backedWhether 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 reviewerAn 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

1

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.

2

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.

3

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.

4

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.

5

Orion shows where the number came from

Every data answer ends with a provenance line naming the source (the reveal name, or "raw SQL โ€”

"), the freshness (the latest date in the data), and a confidence level โ€” so you can always tell whether an answer was governed.

Examples

A question a reveal covers. You ask "What's our revenue last week?" Orion calls get_reveals, finds an orders/revenue reveal, and runs it โ€” it does not write SQL:
get_reveals({ search: "revenue" })
โ†’ run_reveal({ domain_name: "bigquery_ecommerce", reveal_name: "orders_analysis", date_from: "...", date_to: "..." })

> Source: orders_analysis (semantic layer) ยท Freshness: 2024-12-22 ยท Confidence: high
A question no reveal covers. You ask something niche that the semantic layer does not model. Orion searches reveals, finds nothing relevant, and falls back to raw SQL โ€” saying so explicitly. The query must be a single read-only statement:
sql
-- Orion: "No reveal covers this โ€” using raw SQL."
SELECT status, COUNT(*) AS n
FROM orders
WHERE created_at > (SELECT MAX(created_at) FROM orders) - INTERVAL 7 DAY
GROUP BY status

Note that Orion anchors relative time windows ("last 7 days") to the table's latest date rather than the real calendar date, because many datasets end in the past.

A blocked write. Orion's raw SQL is read-only by design. If a query is anything other than a read, the executor refuses it rather than touching your warehouse:
Orion can only execute read queries. "UPDATE" statements are not permitted.

Best practices

  • Model your key metrics as reveals. The more reveals you have, the more of Orion's answers stay governed and consistent with your official numbers. Gaps are where Orion has to fall back to raw SQL.
  • Read the provenance footer. It is the fastest way to confirm whether an answer is reveal-backed before you act on it.
  • Treat a raw-SQL fallback as a signal. If Orion repeatedly falls back to raw SQL for an important question, that is a cue to build a reveal for it so the answer becomes governed and reusable.
  • Enable the SQL reviewer for high-stakes workspaces. The extra correctness pass is worth the small latency cost when ungoverned SQL is being trusted by the business.

Tips

๐Ÿ’ก
Tip

Want to know exactly what a query does? Use Explain the SQL โ€” Orion produces a plain-English, under-150-word explanation of what each clause does, written for a business analyst. This is available to Tracer and above and is a quick way to sanity-check an ad-hoc query before you rely on it.

Common mistakes

โš ๏ธ
Warning
Assuming every answer is governed. Not every answer comes from a reveal. When Orion says "No reveal covers this โ€” using raw SQL," the number is ungoverned and may not match your official metric definition. Check the provenance line.

Expecting Orion to run raw SQL for non-admins. If you are not a Luminary, Orion will not run ungoverned raw SQL for you โ€” this is intentional so row- and field-level policies still apply. Ask via a reveal instead, or have a Weaver build one.

Asking Orion to modify data. Orion's SQL is read-only. It cannot INSERT, UPDATE, DELETE, or run any non-read statement, and it rejects multiple statements in one query.

Troubleshooting

The read-only guardrail allows only single SELECT, WITH, EXPLAIN, SHOW, and DESCRIBE statements. Everything else is refused at the application layer, regardless of the connection's credentials.

SymptomCauseFix
"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. '' statements are not permitted."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 governedNo 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 answerThe 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.
  • 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