-
Notifications
You must be signed in to change notification settings - Fork 513
edu-34: add ontology docs #37439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kay-kim
wants to merge
1
commit into
MaterializeInc:main
Choose a base branch
from
kay-kim:edu-34-ontology-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−8
Open
edu-34: add ontology docs #37439
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| --- | ||
| title: "Use an ontology table" | ||
| description: "Create an ontology table that helps agents write correct joins." | ||
| menu: | ||
| main: | ||
| parent: architecture-patterns | ||
| weight: 10 | ||
| --- | ||
|
|
||
| The ontology table is a curated catalog of join relationships between tables in | ||
| your database. Each row describes a single join: the columns in one table that | ||
| reference columns in another. | ||
|
|
||
| Through the Materialize [MCP server](/integrations/mcp-server/)'s `query` tool, | ||
| an agent can query the ontology table before writing multi-table SQL. | ||
|
|
||
| {{< note >}} | ||
| This pattern relies on the MCP server's `query` tool, which is enabled by | ||
| default starting in v26.27 for the agent MCP server and v26.30 for the developer | ||
| MCP server. | ||
| {{</ note >}} | ||
|
|
||
| ```sql | ||
| CREATE TABLE ontology ( | ||
| table_name text NOT NULL, | ||
| columns text[] NOT NULL, | ||
| referenced_table text NOT NULL, | ||
| referenced_columns text[] NOT NULL | ||
| ); | ||
|
|
||
| COMMENT ON TABLE ontology IS | ||
| 'Defines the join relationships between tables in the database. Each row | ||
| describes a single join: the columns in table_name that reference | ||
| referenced_columns in referenced_table. ALWAYS query this table before | ||
| writing any multi-table query. Use it to confirm exact join keys rather | ||
| than guessing column names. Filter by table_name OR referenced_table to | ||
| find all relationships involving a given table.'; | ||
|
|
||
| COMMENT ON COLUMN ontology.table_name IS | ||
| 'The dependent table, the one that holds the foreign key.'; | ||
| COMMENT ON COLUMN ontology.columns IS | ||
| 'The FK columns in table_name, in order. Pair positionally with referenced_columns.'; | ||
| COMMENT ON COLUMN ontology.referenced_table IS | ||
| 'The parent table, the one being pointed to.'; | ||
| COMMENT ON COLUMN ontology.referenced_columns IS | ||
| 'The PK or unique columns in referenced_table, in order matching columns.'; | ||
|
|
||
| CREATE DEFAULT INDEX ON ontology; | ||
| ``` | ||
|
|
||
| ## Agent system prompt | ||
|
|
||
| Add the following to the agent's system prompt to enforce the intended behavior: | ||
|
|
||
| ```text | ||
| Before writing or executing any joins, query the ontology table for the involved table names. Use the returned join keys verbatim. | ||
| ``` | ||
|
|
||
| ## Example: e-commerce schema | ||
|
|
||
| Given the following tables and join-relevant columns: | ||
|
|
||
| | Table | Key columns | | ||
| | --- | --- | | ||
| | `customers` | `id`, `email` | | ||
| | `addresses` | `id`, `customer_id` | | ||
| | `orders` | `id`, `customer_id`, `shipping_address_id` | | ||
| | `order_items` | `id`, `order_id`, `product_id` | | ||
| | `products` | `id`, `category_id` | | ||
| | `categories` | `id` | | ||
| | `support_tickets` | `id`, `customer_email` *(implicit join, no FK)* | | ||
|
|
||
| The ontology table is populated as: | ||
|
|
||
| ```sql | ||
| INSERT INTO ontology (table_name, columns, referenced_table, referenced_columns) VALUES | ||
| ('addresses', ARRAY['customer_id'], 'customers', ARRAY['id']), | ||
| ('orders', ARRAY['customer_id'], 'customers', ARRAY['id']), | ||
| ('orders', ARRAY['shipping_address_id'], 'addresses', ARRAY['id']), | ||
| ('order_items', ARRAY['order_id'], 'orders', ARRAY['id']), | ||
| ('order_items', ARRAY['product_id'], 'products', ARRAY['id']), | ||
| ('products', ARRAY['category_id'], 'categories', ARRAY['id']), | ||
| ('support_tickets', ARRAY['customer_email'], 'customers', ARRAY['email']); | ||
| ``` | ||
|
|
||
| Tables with multiple relationships, like `orders`, contribute one row per | ||
| relationship. Implicit joins, such as `support_tickets` → `customers`, are | ||
| documented exactly like the declared foreign-key relationships. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left it out but do we want to distinguish this from the internal
mz_internal.mz_ontology_...views?