Writing and Checking SQL
Lint a query against the compiled schema and the dialect's rules, probe it read-only on the live database, or have EKOS's agent write it.
Most wrong SQL runs fine and looks plausible. On BIRD, the errors were in meaning: a value spelled differently in the data, rows repeated by a join, a sentinel 0 averaged in, integer division, NULLs sorted first. EKOS checks a query for exactly those against what it compiled (tables, columns, keys, foreign keys, descriptions) and against the engine's own rules. New in v1.3.0 (RFC 0242, 0243).
| You want | Use | Model call |
|---|---|---|
| Check a query you wrote | ekos sql check / MCP ekos_sql_check |
no |
| The engine's rules before you write | ekos_sql_check with only dialect |
no |
| SQL for a question | ekos sql generate / MCP ekos_sql_generate |
yes |
Check a query
ekos sql check q.sql # static: the ledger's schema and the dialect's rules
echo "SELECT …" | ekos sql check - # or from stdin
ekos sql check q.sql --dialect oracle # default: the database's kind, else the project's first dialect rule, else generic
ekos sql check q.sql --database shop # also run read-only probes on a [[databases]] entry
ekos sql check q.sql --sqlite bird.sqlite # … or on a SQLite file
ekos sql check q.sql --json
Dialects: sqlite, postgres, mysql, mssql, snowflake, databricks, clickhouse, trino, duckdb, oracle, generic. Every dialect plugin states its engine's facts and notes for writing SQL for it; the check prints those notes too.
Static findings
| Code | Finding |
|---|---|
L001 |
a table or column that does not exist, with the nearest real names |
L002 |
a join on columns that are not a foreign key, where neither side is a key, so rows repeat. Since v1.6.0 also through functions and casts: DATE_TRUNC('month', a.m) = DATE_TRUNC('month', b.m) |
L003 |
= on a column that holds comma-separated lists; it needs LIKE |
L004 |
a literal the column does not hold, by the values EKOS knows, with the spellings it does hold |
L005 |
integer division that truncates a ratio (7 / 2 = 3) |
L006 |
an aggregate over a column that uses a value for "unknown" |
L007 |
ORDER BY col … LIMIT ascending, where NULL or empty values sort first |
L008 |
a date literal in another format than the column stores |
L009 |
the wrong row-limit form for the dialect: LIMIT on Oracle or SQL Server, TOP elsewhere |
L010 |
ILIKE on an engine without it |
L011 |
identifiers quoted the dialect's wrong way: backticks on PostgreSQL, "name" on MySQL, which reads it as a string |
L012 |
an output column named after a metric a semantic model defines, computed another way |
L013 |
a constant under a column's name ('Unknown' AS customer): a made-up value where the data is expected (new in v1.6.0) |
L014 |
quoted text where a date function needs a date (DATE_TRUNC('year', '2026-01-01')); write DATE '…' or CURRENT_DATE (new in v1.6.0) |
E001 |
the query does not parse under the dialect's parser. A warning only: the parser's coverage of a dialect is incomplete, and the engine has the last word when a probe runs |
An empty list is not proof the query is right.
Probes on the database
With --database, --sqlite or --duckdb, the check also runs the query and a few side queries, read-only:
| Code | Finding |
|---|---|
E000 |
the query fails on the database |
P001 |
it returns no rows |
P002 |
it returns a single NULL |
P003 |
col = 'Germany' matches nothing, while a case-insensitive or containing match does ('Berlin, Germany') |
P004 |
an aggregate over a join that repeats rows of the first table |
P005 |
ORDER BY … LIMIT 1 where the first two rows tie |
Before anything is sent, a guard must accept the text as exactly one query: no second statement, no SELECT … INTO, no FOR UPDATE. The query is then wrapped so at most the asked rows come back. Each engine adds its own protection:
| Engine | Read-only by |
|---|---|
| PostgreSQL | a read-only session with a statement timeout, and a role that cannot write. A writable role is not probed; the command says why and checks statically. --allow-writable-role probes anyway, with the session still read-only |
| ClickHouse | readonly=1 on every request |
| Oracle | SET TRANSACTION READ ONLY |
| SQL Server, Informix | the read-only sessions described in Indexing a Database |
| SQLite | the file opened read-only, no ATTACH |
| DuckDB | the file opened read-only, external access off, the configuration locked; needs a build with the duckdb feature |
Generate SQL
ekos sql generate "monthly revenue per customer in 2025" --database shop
ekos sql generate "…" --sqlite bird.sqlite --max-turns 6 --json
ekos sql generate "…" --allow-metered # any model route that is not a local Ollama model
sql generate is EKOS's own text-to-SQL agent, the one measured on BIRD. Its prompt holds the compiled schema (tables, columns, keys, descriptions, foreign keys) and the dialect's notes. Every draft is checked with the rules above and, with a database, probed read-only, before it is final. It prints the SQL, the turns, the tools used and any findings left open. It returns SQL, never data.
The model comes from the nl-sql task route ([llm.tasks.nl-sql]). A route other than a local Ollama model needs --allow-metered. --max-turns (default 6, the setting BIRD was measured with) counts the model's replies, the first draft included.
For agents (MCP)
ekos_sql_check is read-only and always listed. Called with only dialect, it returns that engine's writing rules, for an agent about to write SQL. Called with sql, it checks the draft statically. The MCP server never opens a database for it; use ekos sql check --database for probes.
ekos_sql_generate (new in v1.4.1) runs the agent for an MCP client. It is the first MCP tool that calls a model, so it is off by default:
[sql-generate]
mcp = true # list and serve ekos_sql_generate
allow-metered = false # true: a provider other than Ollama may be called
daily-calls = 50 # generations per UTC day for the project (default)
daily-tokens = 200000 # model tokens per UTC day (default)
max-turns = 6 # agent turns per question (default)
- Arguments:
question, optionaldatabase(default: the project's only[[databases]]entry, if it has exactly one),dialect, andtables. tables(new in v1.4.4) is the caller's allow-list, 1–500 names: the agent sees only those tables and the joins among them. Names EKOS has not compiled come back intables_unknown.statusisok,open_findings,no_sql,limit(the daily cap is spent; no model is called) orrefused.- It never returns SQL that writes (since v1.4.5). A
DELETE,UPDATEor any other write, parsed or recognised by its verbs when the parser cannot read it, is answeredrefusedwith a reason and no SQL. The call still counts toward the cap. - A call is counted before the model is called, so a failing call cannot be retried past the cap. Through the web console, the tenant must allow metered calls and each person or token has its own daily allowance (Connecting Other MCP Agents).
The MCP server builds the SQL schema once per store state and read scope, so a repeated check or generation does not re-read the store (since v1.4.2).
In Claude Code, the text-to-sql skill under .claude/skills/ gives the same procedure over MCP: learn the engine's rules, find the tables and their foreign keys, check every literal against the data, decide the grain, draft, and run ekos_sql_check before answering.
Measured
On BIRD Mini-Dev (302 hold-out questions over 11 databases), the same model, Claude Haiku 5.5, scored 53.3% execution accuracy on raw DDL and 76.2% with EKOS's compiled knowledge and the agent's checks. On 100 tune questions, Claude Sonnet 5.5 with the agent scored 87.0%. On the full BIRD dev set (1,534 questions) the agent on Haiku 5.5 scored 65.84%. Every question, its SQL, tokens and cost: BIRD results.
Not yet: live probes on MySQL, Snowflake and Databricks (no live plugin exists for them; they get notes, facts and static checks), and write statements of any kind.