blog · · 4 min read
A knowledge graph that lets an agent answer data questions correctly
ecoai, the internal AI platform for Slack and desktop that I built at Visma e-conomic, gets a lot of data questions. The warehouse behind them has hundreds of tables and views, many with thin descriptions, and what a view means often depends on the report it was built for. Better descriptions helped, but not enough. The agent would pick a view that looked right and count the wrong population, or read a missing row as a fact about the customer.
So I built a knowledge graph of the company’s data from the evidence in the repositories, put reviewed meaning on top of it, and changed how the agent writes SQL so that it starts from the graph. The eight clips below follow that from the product schema to a query that runs. Names, the question and the SQL in them are illustrative. The structure, the motion and the rounded counts follow the real system.
01 / 08
It starts with the product’s own schema
The product repository has the schema definition of the production database: about 600 tables, 5,500 columns and 650 foreign keys. That is the ground truth for what exists and how tables relate, so it is the first layer of the graph.
In the clip each table’s height follows its number of columns, and the arcs are foreign keys. The few tables that most others point to light up.
02 / 08
Pipelines copy product tables into the warehouse
About 20 extract pipelines copy product tables into the data warehouse, one warehouse table per source table. The graph links each warehouse table to the product table it came from, so a warehouse column can be traced back to the product field that fills it.
03 / 08
Five layers of SQL, with lineage from two sides
On top of the extracts, SQL transformations build five layers: extract, staging, warehouse, metrics and reports. About 350 transformation models declare about 1,000 references to each other. Reading the SQL of the deployed views adds about 1,500 dependencies that the declarations don’t show. Together they give the lineage of a report column back to a product table.
Reading the SQL also finds behaviour that descriptions miss. A login view, for example, counts distinct logins per day and agreement, so a user who logs in to the same agreement several times in a day counts once.
04 / 08
Reviewed meaning goes on top of the evidence
Some things the code can’t tell you: what one row represents, which date to use, what has been filtered out, and which joins are safe. These are curated facts: grain, time policy, warnings, analysis contracts, reviewed joins, context links between related sources, and routes from common questions to the right source. There are about 200 curated targets, 80 reviewed joins, 90 context links and 40 routes.
Every fact records its evidence and its status, so a reviewed business rule can be told apart from a description inferred from SQL, and gaps stay visible instead of being filled with guesses.
05 / 08
Only sources the agent can actually query are published
At build time the graph has about 2,000 nodes. Before anything ships, each target is proven with a zero-row query through ecoai’s own read-only connection, and about 40 targets are excluded after review. What is left is a runtime graph of about 300 nodes, stored as a content-addressed snapshot.
Each written summary also stores a hash of the definition it was written from. If that definition changes, the build stops until someone reviews the summary again. That doesn’t catch a change in business meaning, which still needs a person, but it does catch the common case of SQL changing under a description.
06 / 08
A question finds its sources
At runtime the model pulls the concepts out of the question, and search ranks graph nodes by them: exact aliases first, then aliases inside longer terms, then full text. Names such as apps are matched against the labels that actually exist in the data, so one name finds every spelling the warehouse uses.
In the clip, an illustrative question about current customers with the Acme integration ranks an active-integrations report first and the customer metric second. Those scores are from replaying the real search code.
07 / 08
Live schema meets reviewed facts
Before any SQL is written, the agent prepares each source it chose. Preparing combines the live fields from the warehouse with the facts and warnings from the graph, and resolves the time policy against live data, for example by finding the latest available snapshot date.
Label filters are fixed to the exact values found during search, and the prepared context is pinned to the graph snapshot it came from.
08 / 08
The SQL is written from that context, then checked before it runs
The model writes SQL from the prepared context, so the time window, the label scope and the joins come from the graph instead of from guessing.
Before the query runs, it is parsed to make sure it only reads, checked against the user’s own access, checked to use the snapshot it was prepared against, and dry-run against a cost limit. Only then does it execute.