The Semantic Layer, From ScratchChapter 1
Chapter 1

One question, five systems

Why a simple business question is hard to answer, and what a semantic layer does about it.

On Monday morning someone in marketing sends you a message: "Who are our top spenders in Wisconsin? We want to invite them to a loyalty event." It's a one-line question, and every engineer knows it's not a one-line answer. The customers live in one system, the orders in another, the product catalog in a third, the loyalty accounts in a fourth, and the definition of "top spender" in somebody's spreadsheet. This chapter is about that gap, and about the idea this book builds: a layer that closes it by describing what the data means rather than copying it somewhere new.

1.1The question and the five systems

In a typical company, answering "top spenders in Wisconsin" needs:

  1. The customer database, usually an operational Postgres or MySQL behind the app or CRM: names, emails, and which address each customer has.
  2. The data warehouse, where orders land for analytics: every purchase, with a customer id, a date, a quantity and a price.
  3. The product catalog, if the question grows to "top spenders on sports gear in Wisconsin".
  4. The loyalty or rewards system, because marketing will immediately ask "and which of them already have a rewards account?"
  5. The definitions: whatever tool holds the agreed meaning of "top spender". Total spend or order count? Lifetime or this year? Usually a BI model, a dbt file, or a spreadsheet, in several slightly different versions.
"Who are our top spenders in Wisconsin?" 1 · Customer database our lab: Aurora (customer, address) 2 · Data warehouse our lab: Redshift (purchase) 3 · Product catalog our lab: Redshift (product, …) 4 · Rewards system our lab: Aurora (rewards_account) 5 · The definitions our lab: Stardog rules (ch10)

5. Finally, what does "top spender" even mean? Somebody's spreadsheet knows. In our lab, a Stardog rule will (Chapter 10).

Figure 1.1: One question, five systems. In our lab, two AWS databases stand in for the first four, and the fifth, the definitions, moves into Stardog.

Our lab is a small, honest version of this: two real AWS databases play the first four roles, and the fifth, the definitions, moves into the semantic layer as rules (Chapter 10).

1.2The usual answers, and what they cost

You know the standard ways to answer a cross-system question. They all work, and each has a cost that grows with the number of questions and systems.

ApproachHow it worksWhat it costs
ETL into one warehouseCopy customers and addresses from Aurora into Redshift every night, then JOIN.A pipeline to build, schedule, monitor and fix. Data is as stale as the last run. Personal data lives in two places, with two sets of grants.
Federated SQLA query engine (Trino, Athena federated queries, Redshift's federated query to Postgres) reads both sources in one SQL statement.Solves the plumbing, not the meaning. The author still needs every table and join key, and "top spender" is still defined inside each query.
A BI semantic modelLookML, dbt metrics or a Power BI model defines "revenue" once for that tool.Good within one tool, but tied to it (and usually one warehouse), and awkward for other programs or an AI agent to use.

Notice the pattern. Getting rows from two places is solvable in several ways. The hard, recurring problem is meaning: which table holds customers, how orders connect to customers, what "top spender" means, who may see a Social Security number. Every approach above re-solves that in each pipeline, query or dashboard.

1.3What a semantic layer is, and what it isn't

A knowledge graph is a way of representing information as a network of things (customers, orders, products) connected by named relationships (an order was purchased by a customer), together with a machine-readable description of what those things and relationships are. A semantic layer is a job, not a technology: a layer between people or programs and the physical data, where they ask questions in business terms and the layer works out which data answers them. A knowledge graph is one way to do that job (there are others; see "Other ways to build a semantic layer" below). This book builds the semantic layer using a knowledge graph.

Here the semantic layer is Stardog, with five parts, each of which you will build.

PartWhat it doesNearest SQL idea
OntologyThe business vocabulary: kinds of things (Customer, Order) and how they relateA logical data model, but one the database can read and reason with
MappingsHow each concept corresponds to tables and columns in Aurora and RedshiftThe SELECT behind a view
RulesDerived facts: "a customer with a large order is a big spender"A view or a computed column, shared by every query
SecurityWho may see which parts of the dataGRANTs and row/column security, in one place for all sources
Query engineTakes one query, splits it by source, runs SQL, joins the resultsA federated query planner

Just as important is what it isn't:

Other ways to build a semantic layer

A knowledge graph isn't the only way to build a semantic layer, and the relationship runs one way only: some semantic layers aren't knowledge graphs, and some knowledge graphs aren't semantic layers (Google's Knowledge Graph powers search results; others detect fraud or drive recommendations). There are six families in use today, and big companies often combine two of them.

ApproachExamplesHow it worksGood atWeaker at
Metrics layer ("headless BI")dbt Semantic Layer (MetricFlow), Cube, AtScale, Looker's LookML, Snowflake semantic views, Databricks metric viewsYAML or code defines entities, dimensions and measures ("revenue = sum(price × qty)") over a warehouse, then generates SQLOne consistent definition of every metric across dashboards; the most common choice todayUsually one warehouse; few relationships; no reasoning
BI-tool semantic modelPower BI semantic models (DAX), Tableau data models, SAP BusinessObjects "universes" (the original semantic layer, from the 1990s)A business model built inside the reporting toolFast for analystsLocked into that tool; other apps and agents can't reuse it
Data virtualization with business viewsDenodo, Dremio, Trino/Starburst with viewsOne SQL query across engines, with views renamed into business termsCross-engine, no ETLThe "meaning" is only view and column names; no shared identity or rules
Knowledge graph (this book)Stardog, Ontotext GraphDB, Amazon Neptune (RDF); Neo4j (property graph)An ontology plus mappings or loaded data, queried with SPARQL (or Cypher)Relationships, one identity for the same customer across sources, rules and reasoning, federation (Stardog)Steeper learning curve; slower than a warehouse at heavy aggregation
Operational ontology platformPalantir Foundry OntologyBusiness "object types", links and actions on top of datasetsCovers both reading and doing (workflows)Proprietary; the data typically lives inside the platform
API or service layerGraphQL (Apollo federation), domain microservices, even a JPA domain modelBusiness entities exposed as types or endpoints over many sourcesGreat for applicationsHand-coded; ad-hoc analytical questions are hard

Which one when. One warehouse and mostly KPIs and dashboards: a metrics layer, the cheapest and most mainstream option. Many systems, lots of relationships, the same customer scattered across them, and business rules like "big spender" that should be defined once: a knowledge graph. That is the customer-360 situation in this book, and the reason the AWS blog chose Stardog. For AI agents, every family is now adding an "AI-ready" semantic model (Snowflake Cortex Analyst's semantic model files, Databricks Genie, dbt's MCP server). A knowledge graph's edge there is that the ontology and its relationships give the model more meaning to work with.

A semantic layer is a job, not a technology: an ETL-free business view over many databases, where the ontology supplies the meaning and the mappings supply the connections.

Example: This book builds it with a knowledge graph on Stardog; metrics layers, BI models and data virtualization are other ways.

1.4The three layers from the blog

The AWS blog describes an agentic system in three layers, separating three jobs that often get blurred together.

LAYER IN THE BLOG IN THIS BOOK Model the LLM: reads the question, writes queries, phrases answers Claude Sonnet 4.6 on Amazon Bedrock OpenAI model or via OpenRouter Meaning the semantic layer: ontology, mappings, rules, security Stardog Stardog Cloud Free tier Runtime where the agent runs: sessions, tool calls, credentials Bedrock AgentCore Python on your laptop Aurora PostgreSQL Redshift Serverless
Figure 1.2: The blog's three layers, with the blog's choices and ours. The data stays below, untouched.

The model layer is the language model: good at language, bad at knowing your data. Given only table names, it will cheerfully invent columns and guess join keys. The meaning layer fixes that: it gives the model a small, precise vocabulary ("an Order is purchased by a Customer; a Customer has address an Address") and a single query interface to everything. The model writes a query in business terms; the semantic layer turns it into correct SQL on the right systems. The word for this is grounding: the answer is tied to real data through a well-defined interface rather than to the model's guesses. The runtime layer runs the agent: holding the conversation, calling tools, keeping credentials safe.

Most of this book is about the middle layer. The model and runtime are interchangeable, which is why swapping Bedrock for OpenAI costs us nothing in the chapters that matter.

1.5Our use case: customer 360, split across two engines

"Customer 360" means a complete view of each customer: who they are, where they live, what they bought, how they pay, whether they're in the rewards program. Our data is a Stardog training dataset (the c360 knowledge kit), split the way it might be in a real company: operational data in Aurora, transactional history in the warehouse.

EngineDatabase.schemaTableRowsKey columns
Aurora PostgreSQLc360.c360customer500cid (PK), location → address.id
address500id (a UUID string), city, state, zip
credit_card500id, cid, card_num, card_type
rewards_account500id, cid, account_id, create_date
Redshift Serverlessdev.c360purchase2,500id, pid, cid, date, time, quantity, price
product580id, name, brand, price, dept
category31id, dept_name, parent
vendor418id, vendor_name, industry
Aurora PostgreSQL Redshift Serverless customercid = 225 · Carolyn Inworth address · Milwaukee, Wisconsin credit_card rewards_account purchasecid = 225 · many rows product category vendor same cid no FK, no shared engine, no JOIN
Figure 1.3: The split. The only thing connecting the two engines is a value: the customer id cid appears in both. Chapter 9 turns that shared value into a shared name.

Aurora's customer.cid is a primary key; Redshift's purchase.cid is a plain integer column with the same values. There can be no foreign key across engines. Carolyn Inworth is cid 225 in Aurora, and her purchases carry cid 225 in Redshift. Hold on to that: Chapters 3 and 9 turn that shared value into a shared name, the whole trick behind joining the two databases without ETL.

Competency questions

Before designing any vocabulary, the blog recommends writing down competency questions: a short list of real questions the ontology must be "competent" to answer. They keep the model small and honest; anything nobody asks about stays out. Ours come from the blog, plus the one we started with:

#QuestionNeeds
1List ten customers and their state.Aurora only
2Top ten products by units sold.Redshift only
3Top ten customer spenders, with their state.Both (federated)
4How many big spenders per state?Both, plus a rule (reasoning)
5Who are our top spenders in Wisconsin?Both, plus a filter

These five come back in Chapter 14 as the test for the finished agent. You could answer the first two in SQL today, the third only with extra plumbing. The fourth needs a shared definition of "big spender" that doesn't exist yet.

1.6The questions this book answers

Competency questions are what the system must answer. Your own questions as a skeptical database engineer (how the agent knows the ontology, who knows which data lives where, where SPARQL becomes SQL, how two engines' results get joined) are listed with their chapters in Section P.2. The short version: the mappings know where data lives, and Stardog's query engine splits the query, runs SQL on each source and joins the results itself, storing nothing (Chapter 9).

The chapters follow the build order: foundations (ch2–4: triples, names, SPARQL), the model (ch5–6: the ontology), the data (ch7–9: virtual graphs, mappings, federation), meaning and control (ch10–12: rules, SHACL, security) and the agent (ch13–14).

1.7The terms from this chapter

TermWhat it isWhy it existsTiny example
Knowledge graphData stored as things connected by named relationships, plus a description of what they mean.Meaning travels with the data, not in people's heads.Order 0e7e… purchased by customer 225.
Semantic layerA knowledge graph used as a layer between consumers and physical databases.Ask in business terms once, instead of re-encoding meaning in every query.Ask for "Customers in Wisconsin", not customer JOIN address.
Virtual graphA view-like graph whose facts are produced from SQL at query time.Query data where it lives, with no ETL.aurora_c360_safe over Aurora's tables.
GroundingTying an LLM's answer to real data through a well-defined interface.Models invent columns and joins when they guess.The agent must answer from run_sparql results.
Competency questionA real question the finished system must answer.Keeps the model small and gives you an acceptance test."How many big spenders per state?"

Check yourself

What's the difference between the "plumbing" problem and the "meaning" problem?

Plumbing is getting rows from several systems into one place (ETL, federated SQL). Meaning is knowing which tables represent a customer or an order, how they connect, and what terms like "top spender" mean. Most tools solve plumbing; the semantic layer is mainly about meaning, and brings its own plumbing (virtual graphs).

Does Stardog store copies of the customers and orders in our design?

No. The rows stay in Aurora and Redshift. Stardog holds the ontology, mappings, rules and security, and reads the data at query time through virtual graphs.

Which of the blog's three layers does this book mostly teach, and which two do we swap?

The meaning layer (Stardog). We swap the model (Claude on Bedrock becomes an OpenAI or OpenRouter model) and the runtime (AgentCore becomes Python on your laptop).

What connects Carolyn's Aurora row to her Redshift purchases?

Only a value: cid = 225 appears in both, with no foreign key and no shared engine. Chapters 3 and 9 turn it into a shared name, which makes the federated join work.

What is a competency question, and why write them first?

A real question the finished system must answer. Writing three to five first keeps the ontology small: you model what the questions need and nothing else. They also become the agent's acceptance test.

Chapter recap