AlgoViz

SQL, graphs and passages

medium 6 min

The representation determines which questions an answer can support.

Video lesson · Chapter 1 of 4

How many orders, and why did they fail?

0:00 / 3:36

Theory

Read or listen first. About 2 min.

What it is

The representation determines which questions an answer can support. A passage retriever selects a bounded sample of relevant text. A structured query can aggregate every eligible row under specified joins and filters. A graph organizes entities and relationships so a query can follow links or summarize groups. Each design needs a distinct ground truth and a record of which data and permissions were used.

Why it exists

Ask whether the user wants a total, an explanation or a relationship. Define metrics and join keys in a governed semantic layer. Constrain analytical execution to authorized read-only data and bound its cost. For graph retrieval, keep links back to evidence: entity resolution can merge unrelated records, and community summaries can omit exceptions. The answer may combine a verified numeric result with separately sourced narrative.

Count all orders and explain the shared fault

Question planner → Governed query (aggregate request). Question planner → Entity graph (relationship query). Governed query → Verified answer (verified count). Entity graph → Verified answer (sourced explanation)
Question planner → Governed query (aggregate request). Question planner → Entity graph (relationship query). Governed query → Verified answer (verified count). Entity graph → Verified answer (sourced explanation)

Inside this chapter

Chapter sources
Text-to-SQL and execution verification
Resolve business terms into measures, units and time windows before creating a query. Validate the tables, filters and joins, then check the result with known fixtures. A query that executes successfully can still double-count orders after joining line items.

Text-to-SQL has two halves: mapping business words to the right tables, measures and filters, and checking that the executed query means what the user asked. Joins are the classic trap, because joining orders to line items multiplies rows. Validate against small fixtures with known answers, restrict execution to read-only views the user may see, and cap cost with timeouts and row limits.

Example. One order has three items. Counting joined rows returns three. Verify the expected order-level grain before publishing the count.

Another example. "Active customers last month" might mean logged in, paid, or not churned. Resolving it through a defined metric (say, active_customer = paid invoice in period) before generating SQL avoids three different numbers on three different days.

Counting orders at the wrong grain

QueryRows countedResultCorrect?
COUNT(*) over orders JOIN items3 item rows for order 5013No, line-item grain
COUNT(DISTINCT order_id)1 order1Yes, order grain
A query that runs without error can still answer a different question.
Knowledge graphs and global synthesis
Extract entities and relationships with provenance, resolve aliases, and retain uncertainty. Local graph queries answer neighborhood questions; corpus-level summaries can aggregate evidence across clusters. Measure extraction and synthesis errors independently.

Graph retrieval extracts entities and relationships from text, usually with a model, and stores them with links back to the source passage. Entity resolution then merges aliases, which is where errors compound: two things with one name become one node and invent relationships. Local queries walk a node's neighbourhood; global queries summarise clusters of the graph. Summaries are convenient but can drop exceptions, so keep the path back to passages.

Example. Two reports name “Mercury.” One refers to a payment service and one to a project. Merging them creates a false dependency.

A false merge on the name Mercury

Keep type and provenance on every entity so merges can be checked.
Selecting evidence by question
Use passage retrieval for a quoted exception, SQL for a complete governed aggregate, and graph exploration for relationships across reports. Explain the coverage and freshness of the selected source to the reader.

Passage retrieval returns a sample, so it can illustrate but cannot count. SQL over a governed table can count everything eligible but cannot explain why. A graph can follow relationships across reports. Many real questions need two of these, and the answer should cite each part to its own source and say how fresh each source is.

Example. Ten retrieved incident reports can illustrate a recurring fault but cannot establish that exactly ten orders were affected.

Route by what the question needs

Governed semantic layer
A shared, versioned set of metric and dimension definitions (what counts as revenue, an active user, an order) that text-to-SQL and dashboards both compile from, so the same question gets the same number everywhere.

Letting a model write raw SQL against hundreds of tables invites subtle disagreements: different join paths, different status filters, different time zones. A semantic layer narrows the model's job to picking a defined metric, dimensions and filters, which is easier to validate and to permission. It also gives the answer something precise to cite: the metric name and the snapshot it ran on.

Example. The metric orders_shipped is defined once as distinct order IDs with status shipped and a ship date in the period. The agent asks for that metric by name instead of writing its own joins.

From words to a governed query
  1. 1User: orders shipped in Maybusiness language
  2. 2Match metricorders_shipped
  3. 3Apply filtersperiod = May, tenant = A
  4. 4Compile SQLfrom the layer's definition
  5. 5Run read-onlywith row and time limits

The model chooses metrics and filters; the layer owns the joins.

How it works

Step through the idea, or press Play.

Step 1 of 4

Question planner. Split count from explanation

Understand it

Worked example

Count all orders and explain the shared fault

  1. 1

    Split count from explanation

    The question asks for a complete order count and the shared cause described by engineers.

  2. 2

    Count distinct eligible orders

    Validate date, status and grain against a fixture containing repeated line items.

  3. 3

    Link reports to the faulty service

    Retrieve connected reports and confirm source passages for each claimed relationship.

  4. 4

    Return two separately supported claims

    Cite the query snapshot for the count and the original reports for the explanation.

Follow the handoffs

Ask whether the user wants a total, an explanation or a relationship. Define metrics and join keys in a governed semantic layer. Constrain analytical execution to authorized read-only data and bound its cost. For graph retrieval, keep links back to evidence: entity resolution can merge unrelated records, and community summaries can omit exceptions. The answer may combine a verified numeric result with separately sourced narrative.
Splitting one question into two evidence paths
  1. 1Plancount vs explanation
  2. 2SQLCOUNT DISTINCT eligible orders
  3. 3Fixture checkrepeated items counted once
  4. 4Graph walkreports linked to the service
  5. 5Composetwo claims, two citations

Answer built correctly

  • Count cites the query snapshot
  • Cause cites original incident reports
  • Each claim says where it came from

Common shortcut

  • Count inferred from 10 retrieved reports
  • Cause taken from a graph summary alone
  • One citation covers both claims

Summary

The whole topic in one minute.

Top-k retrieval ranks a sample. It does not enumerate every record satisfying a predicate.

Ask whether the user wants a total, an explanation or a relationship. Define metrics and join keys in a governed semantic layer. Constrain analytical execution to authorized read-only data and bound its cost. For graph retrieval, keep links back to evidence: entity resolution can merge unrelated records, and community summaries can omit exceptions. The answer may combine a verified numeric result with separately sourced narrative.

Sources