PROJECT STUDY
Text-to-SQL Copilot
Plain questions. Governed SQL. Every query shown.
An analytics copilot that answers Indonesian or English business questions from a governed semantic layer, checks every SQL statement on its syntax tree and shows the SQL behind every number.
Explore the build
At a glance
- Industry
- Retail and distribution
- Capabilities
- 8
- Technologies
- 12
- Year
- 2026
The challenge
Business users wait days for an analyst to answer routine questions.
A branch manager who asks "Berapa penjualan bersih per cabang bulan lalu?" (What were net sales per branch last month?) joins a queue, even though the data already sits in the warehouse.
Letting a language model write SQL directly is risky. A wrong join can double-count sales, the same metric can mean something different each time it is asked, a user can see rows or columns they should not see, and an unbounded query can scan the whole warehouse. A useful copilot has to be fast and governed at once, with one definition per metric and the query behind every number open to inspection.
What we built
Text-to-SQL Copilot is an analytics workspace that answers business questions from a governed semantic layer, with DuckDB as the warehouse and every safety rule enforced in code on the SQL itself.
A governed semantic layer
Entities with the only columns the copilot may see, many-to-one joins, 11 measures with owners and Indonesian and English definitions, dimensions with members and synonyms, time grains, relative periods, roles and row policies live in version-controlled YAML, checked against the warehouse catalogue at start-up
A planner that needs no model
Common metric questions in Indonesian or English, with abbreviations, suffixes, explicit and relative periods, member filters and rankings such as "5 besar", compile straight to SQL from the definitions. The planner answers only when every word is explained and otherwise hands over with a named reason
A model that sees only a slice
For a question the planner hands over, BM25 retrieval picks the measures, dimensions and tables it needs, and a language model behind an OpenAI-compatible adapter sees only that slice, as the user's role may see it, and never receives result rows. A scripted adapter replays canned replies for tests and the demo
Guardrails on the syntax tree
Every statement, whoever wrote it, must be one SELECT over allow-listed tables, columns and functions, with no writes, file access or settings anywhere in the tree, every column read by name and no joins that link nothing. A mandatory LIMIT, a cost check through EXPLAIN and a timeout follow
Access as a rewrite
Every table a statement reads is rewritten into a subquery with only the role's visible columns and, for a branch or regional manager, a filter to their branches. The lines the guardrails added are highlighted in the SQL as executed
Answers that show their work
Each answer brings a table, a chart suggestion, a headline and explanation written by code in the question's language, the SQL as executed and as written, the definitions and owners behind it, and the pipeline trace. An editor checks or runs edited SQL through the same pipeline as the selected persona
A workspace in two languages
Pages for the semantic model with its ERD, the guardrail policy with live checks and the evaluation report sit beside the workspace, in Indonesian or English, from 390 px phones to 1440 px desktops
Execution-accuracy evaluation
66 synthetic questions with hand-written gold SQL and 43 guardrail attack cases are scored by comparing result sets, with row scope as a hard gate reported apart from the score
A governed semantic layer
Entities with the only columns the copilot may see, many-to-one joins, 11 measures with owners and Indonesian and English definitions, dimensions with members and synonyms, time grains, relative periods, roles and row policies live in version-controlled YAML, checked against the warehouse catalogue at start-up
A planner that needs no model
Common metric questions in Indonesian or English, with abbreviations, suffixes, explicit and relative periods, member filters and rankings such as "5 besar", compile straight to SQL from the definitions. The planner answers only when every word is explained and otherwise hands over with a named reason
A model that sees only a slice
For a question the planner hands over, BM25 retrieval picks the measures, dimensions and tables it needs, and a language model behind an OpenAI-compatible adapter sees only that slice, as the user's role may see it, and never receives result rows. A scripted adapter replays canned replies for tests and the demo
Guardrails on the syntax tree
Every statement, whoever wrote it, must be one SELECT over allow-listed tables, columns and functions, with no writes, file access or settings anywhere in the tree, every column read by name and no joins that link nothing. A mandatory LIMIT, a cost check through EXPLAIN and a timeout follow
Access as a rewrite
Every table a statement reads is rewritten into a subquery with only the role's visible columns and, for a branch or regional manager, a filter to their branches. The lines the guardrails added are highlighted in the SQL as executed
Answers that show their work
Each answer brings a table, a chart suggestion, a headline and explanation written by code in the question's language, the SQL as executed and as written, the definitions and owners behind it, and the pipeline trace. An editor checks or runs edited SQL through the same pipeline as the selected persona
A workspace in two languages
Pages for the semantic model with its ERD, the guardrail policy with live checks and the evaluation report sit beside the workspace, in Indonesian or English, from 390 px phones to 1440 px desktops
Execution-accuracy evaluation
66 synthetic questions with hand-written gold SQL and 43 guardrail attack cases are scored by comparing result sets, with row scope as a hard gate reported apart from the score
How it works
Normalise and plan
Each question is normalised for case, accents, Indonesian abbreviations such as brp and bln and the suffixes -nya, -lah and -kah, and its periods are resolved against the warehouse's as-of date. The planner segments the rest by dynamic programming, choosing the reading with the fewest unexplained words, and builds either a plan or a handover that names its reason.
Compile from definitions
A plan compiles to a SQLGlot tree from the semantic layer: each fact is summed on its own business date in its own CTE, the totals meet in a FULL OUTER JOIN, ratios divide by NULLIF and joins follow many-to-one edges only. Member values and dates enter the tree as literals, never as text.
Hand over with a slice
A handed-over question is ranked against the layer with BM25, and the slice is closed over join paths and filtered by role before the model writes SQL from it. Only syntax, unknown-reference and database errors go back to the model for correction, at most twice.
Guard every statement
Each statement is parsed in DuckDB's dialect and walked whole for writes, DDL, settings and table functions, then checked against the role's tables, columns and functions. Every table reference is rewritten to the role's visible columns and, for a scoped manager, a branch filter, and the statement is capped at 500 rows.
Price, run and explain
The final statement is priced from DuckDB's EXPLAIN estimates against a 5,000,000-row budget, then runs on a read-only, locked-down connection and is interrupted if it is still running after 5 seconds. The answer returns with its table, chart suggestion, explanation, lineage with owners and formulas, guardrail notices and the pipeline trace.
Engineering insights
Every safety property lives in code that can be tested, not in instructions to a model.
Stack by layer
- Frontend
- React
- TypeScript
- Vite
- TanStack Query
- React Router
- ts-pattern
- ts-belt
- CSS Modules
- SVG
- Backend
- Python
- FastAPI
- Uvicorn
- Pydantic
- SQLGlot
- PyYAML
- Data
- DuckDB
- YAML semantic layer
- AI
- BM25
- Rule-based planner
- OpenAI-compatible LLM adapter
- Execution-accuracy evaluation
- Infrastructure
- uv
- pytest
- Vitest
- Testing Library
- Playwright
- Ruff
- mypy
- Biome
- Docker Compose
- GitHub Actions
Measures meet only after aggregation
Joining sales lines to returns before summing counts a sale once per matching return, and an inner join drops a branch with no returns. The compiler sums each fact on its own date in its own CTE and joins the totals with a FULL OUTER JOIN, and the synthetic warehouse plants a branch with sales and no returns to check it
The planner answers only what it fully read
A word the segmentation cannot explain hands the question over with a reason instead of a guess. On 65 answerable synthetic questions the planner compiled 59, all matching the hand-written gold rows, though the 56 development questions shaped its vocabulary. Ten held-out questions compiled 8 on their first run, all correct, and all 10 after a matcher fix
Guardrails read the syntax tree, not the text
String checks are fooled by comments, quoting and dialect quirks, so every statement is parsed and walked whole: a DELETE inside a CTE, SELECT INTO and read_csv are all refused. DuckDB can read a column without naming it, through a whole-row struct, a column position or a column pattern, so each of those is refused too. All 43 synthetic attack cases are refused with the expected rule
Access is a rewrite, not a prompt
After validation, each table reference becomes a subquery under the same alias with only the role's visible columns the query uses and, for a scoped manager, a branch filter. Unions, self-joins and subqueries are rewritten too, a LEFT JOIN stays a LEFT JOIN, order lines are scoped through their order, and scope never appears in a prompt
A LIMIT caps rows returned, not rows read
The mandatory LIMIT protects the browser, while a cost guard on DuckDB's EXPLAIN estimates protects the warehouse: on the demo warehouse, a fan-out join estimated at about 54.6 million rows is refused before it runs. Estimates can be wrong, so a 5-second interrupt backs them up, because DuckDB has no statement timeout of its own
A refusal is final, so a model cannot probe
Guardrail and cost refusals end the attempt, and only syntax, unknown-reference and database errors return to the model, with nothing but the error text. Explanations are written by code from the plan or the SQL's lineage, so result rows are never sent to a model provider
Design system
Analyst paper is quiet and data-first, like an analyst's working paper: warm paper and ink keep the table, chart and SQL in front, indigo marks actions and answers compiled from governed definitions, and amber is kept for the moments a guardrail scoped, capped or refused a query.
Colour palette
- Paper
#fbfaf7Page background behind white panels - Ink
#1f2328Text, figures and SQL identifiers - Muted ink
#59636eSecondary text, axis labels, lineage notes and SQL comments - Accent indigo
#4338caPrimary actions, focus, the active navigation item, the planner badge and SQL keywords - Guardrail amber
#b45309Guardrail notices, scoped answers, refusals, restricted columns and the rule beside SQL lines the guardrails added - Error red
#b42318Failed queries and errors, always with a text label - Pass green
#17703fPassed checks and correct evaluation cases, always with a text label - Okabe-Ito blue
#0072b2Bars and the first chart series, from the colour-blind-safe Okabe-Ito palette
Colour
Paper, ink and muted ink carry the content. Accent indigo marks actions and the planner badge, guardrail amber means a guardrail acted, error red means a query failed and pass green marks a passed check, each with a text label so no status rests on colour alone
Charts
Plain SVG bar and line charts are laid out at the container's measured width, with every bar and the last point of every line labelled with its value, in the colour-blind-safe Okabe-Ito palette. Only the series that keep 3:1 against white are used for marks
Typography
IBM Plex Sans for the interface and explanations and IBM Plex Mono for SQL, column names, table figures and KPI values, both bundled through @fontsource with no font CDN, with tabular, right-aligned numbers in tables
Components
An in-house set over CSS custom properties and CSS modules: an answer summary with a provenance badge, a result table with a sticky header, highlighted SQL with the guardrails' additions marked, an SQL editor with Check and Run, a refusal card, an ERD with row-scope marks and locked columns, and evaluation tiles
Accessibility
All 22 text pairs the interface uses meet WCAG AA and are tested in code, and amber text never sits on the code background, where it would fall short. Charts and the ERD carry accessible names, and layouts run from 390 px phones to 1440 px desktops without horizontal page scroll
Where this fits
Teams that wait on analysts for routine numbers can start from the same pattern: governed metric definitions, guardrails enforced in code and answers that show their SQL. Explore our AI solutions, analytics dashboards and retail work, or talk to us about your data.
Inside the project
Screens from the running workspace at 1440 by 900 on a synthetic distributor's warehouse, in English and Bahasa Indonesia; questions the planner hands over are answered here by a scripted demo model.
On the synthetic warehouse, an Indonesian question compiled from the semantic layer without a model, with its chart and the SQL as executed.View larger Viewed as a regional manager, the answer covers only the four synthetic branches in scope, and the injected branch filter is marked in the SQL.View larger A query from the scripted demo model, refused before it ran, naming the rule, the column and how the question got there.View larger The synthetic warehouse's semantic model as an ERD, drawn from the same YAML the planner and the guardrails read.View larger
KEEP EXPLORING
Another problem. A different approach.

Store & Workforce Vision
A privacy-aware store operations platform for occupancy, queue and zone events, with configurable alerts, manager review and aggregate reporting.

Mining Haul Dispatch
Haul dispatch for open-pit mines: a linear program plans haul flows inside the coal-quality band, and each truck goes to the excavator furthest behind plan, with the reason logged.
Let’s connect the dots
Let's put these ideas to work.
Share a process, product, or problem you want to improve. We will work through the possibilities together.