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
Illustration of the copilot workspace: the Indonesian question 'Berapa penjualan bersih per cabang bulan lalu?' with its generated SQL and read-only, LIMIT and branch-filter guardrail chips, beside a demo bar chart of net sales by branch for Surabaya, Bandung, Medan and Makassar.
Original interface illustration · Synthetic demo data.View larger

At a glance

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 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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
  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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.

talk to us about your data

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.

Discuss your project