Skip to content

catalog: infer relationships between relations from view definitions - #40832

Draft
jubrad wants to merge 4 commits into
MaterializeInc:mainfrom
jubrad:inferred-relationships
Draft

jubrad wants to merge 4 commits into
MaterializeInc:mainfrom
jubrad:inferred-relationships

Conversation

@jubrad

@jubrad jubrad commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Adds two builtins that read the relationships between user relations out of their view and materialized view definitions, with no declared keys or ontology table:

  • mz_internal.mz_inferred_relationships, a materialized view on mz_catalog_server holding direct edges.
  • mz_internal.mz_mcp_relationships, a view for agents, shaped like an ontology table, filtered to what the session's role can read, and on the restrict_to_user_objects allow-list.

It's the backend counterpart to the Console ontology explorer (#40712), which applies the same rules in the browser.

How it works

mz_internal.parse_catalog_view_joins(create_sql) follows the existing parse_catalog_* pattern in mz-catalog-decode. It walks a persisted create_sql's resolved AST and returns JSON with three lists:

  • the column pairs the view equates (ON, WHERE, USING, and IN (SELECT ...) semijoins);
  • its USING joins, with the relations joined before them;
  • its output columns that are input columns unchanged.

Qualifiers resolve through a stack of SELECT scopes. Anything that isn't bound to a catalog relation is opaque, so the function errs toward missing a relationship rather than inventing one:

  • a CTE;
  • a subquery;
  • a table function;
  • an alias that renames columns.

mz_internal.mz_inferred_relationships holds only direct edges. Every row comes from a single view's definition, so the view grows linearly with the number of views. The kind column says which of three it is:

kind Meaning Example
join A view equates the two columns. The referenced side is a column that alone keys an index, then a column named id, then the side not named *_id. A join none of these settle has is_directed = false. bids.auction_id → auctions.id
key A view passes a key through unchanged. SELECT o.id AS org_id gives org_spend.org_id → organizations.id
passthrough A view passes any other column through unchanged. Not a relationship, only the link mz_mcp_relationships follows to derive references. winning_bids.buyer → bids.buyer

Relationships across several views are paths through these rows. Nothing is derived in the view: an earlier revision derived them with WITH MUTUALLY RECURSIVE, and maintaining that fixpoint was most of its cost (numbers below).

mz_internal.mz_mcp_relationships is what agents query, with the same columns as the ontology table pattern (table_name, columns, referenced_table, referenced_columns, plus kind and is_directed). Besides the join and key rows, it lists derived references. A column holding the same values as a referencing column, through a chain of views passing it, references the same key. So in a dbt-style project every layer gets the foreign key directly: stg_orders.customer_id, int_orders.customer_id and fct_orders.customer_id each → customers.id, even when only one layer joins customers. Agents get parity with a hand-written ontology table without writing recursive queries.

  • The chains are walked on read, by pointer doubling over the passthrough rows. Maintaining them in the materialized view measured 3 to 5.5 times its cost per replaced view (table below). On read, the view takes about 300 ms with 4,000 views.
  • Privileges: like mz_mcp_data_products, rows are limited to relations the role holds SELECT on, through mz_show_my_object_privileges, so the view can be allow-listed for restrict_to_user_objects sessions without describing relations the role can't see. A derived row between two readable relations can depend on a chain through views the role can't read. Those views' names never appear, only the relationship they imply.

No feature flag: builtins are created whatever the flags say, so a flag could only gate the parse function, not the cost.

Validation against the Console

On the auction demo, with six extra views covering a CTE, alias shadowing, USING, a semijoin and expression joins, I pointed the Console from #40712 at a local environmentd running this branch and compared by kind.

  • join and key rows match the Console's inferred joins, with two expected differences:
    • The undirected USING (org_id) pair. Its order is arbitrary, and the two sides pick differently.
    • One relationship only this view finds, correctly. The view is SELECT a.id FROM auctions a JOIN (SELECT a.id FROM bids a) s ON .... The Console's token scan sees a bound twice and gives up. The AST walk scopes the inner a to its subquery.
  • passthrough rows: this view stores 14. The Console never draws them, only the references derived through them.

Coverage without derivation. Two columns count as related when a path of rows connects them.

  • All 16 relationships the fixpoint version derived are connected: 13 by a single row, 3 by a path.
  • So are all 11 resolvable rows of the demo's hand-written ontology table: 7 by a single row, 4 by a path. The fixpoint version recovered 9 of them.

Cost (local, optimized build, mz_catalog_server with 1 worker, about 3,000 views)

CPU is each dataflow's root operator in mz_scheduling_elapsed. Summing every operator double-counts nested scopes, which inflated the figures in this PR's first revision.

Fixpoint (first revision) Direct edges mz_object_dependencies
CPU per replaced view, 300 CREATE OR REPLACE in one burst 1.66 ms 0.68 ms 0.57 ms
Dataflow operators 842 641 236
Arrangement memory 8.2 MB 5.0 MB 0.9 MB
Hydration after restart, wall time 4.8 s 3.7 s, same as mz_views 1.0 s

Where derived references are computed, with about 4,200 views (the chain plus a dbt-style workload and a 300-deep stack):

CPU per replaced view Correct at any depth Cost on read
Maintained with WITH MUTUALLY RECURSIVE 3.77 ms yes none
Maintained with unrolled pointer doubling (8 joins) 2.01 ms up to 256 levels none
On read in mz_mcp_relationships (shipped) 0.68 ms (direct edges only) yes ~300 ms
  • A 1,000-view dbt deploy costs about 0.7 s of catalog server CPU for this view.
  • Latency: new relationships appear within 15 ms of CREATE VIEW returning, as fast as mz_object_dependencies.
  • Deep view stacks are linear by construction: a stack of 300 views gives 300 rows. Under the fixpoint, before keys stopped propagating, it gave 45,150.

Tests

  • view_joins.rs unit tests: extraction from real create_sql, including what must be skipped.
  • mz_inferred_relationships.slt:
    • the auction schema end to end, by kind;
    • evidence;
    • index-key direction;
    • retraction on DROP VIEW;
    • derived references in mz_mcp_relationships;
    • a restrict_to_user_objects role that sees only the relations it can read, and is refused mz_inferred_relationships.
  • Regenerated: the function registry and LIR snapshots, regproc, system-parameters.json, the autogenerated catalog SLT, and the catalog_server EXPLAIN, index accounting, OID and information_schema SLTs (builtin row counts only).

Open questions

  • Is the per-environment cost acceptable with no flag? About 5 MB and 0.68 ms of catalog server CPU per replaced view at 3,000 views, close to mz_object_dependencies.
  • Should the MCP server point agents at mz_mcp_relationships, for example in its instructions or a tool, the way the ontology table docs give agents a system prompt?

Release notes

None: both views are in mz_internal, which is unstable.

🤖 Generated with Claude Code

Add mz_internal.mz_inferred_relationships, a builtin materialized view on
mz_catalog_server that reads the relationships between user relations out
of the environment's view and materialized view definitions, with no
declared keys.

A new scalar function, mz_internal.parse_catalog_view_joins, walks a
persisted create_sql's resolved AST and returns the column pairs the view
equates (ON, WHERE, USING, and IN (SELECT ...) semijoins) and its output
columns that are input columns unchanged. Qualifiers resolve through a
stack of SELECT scopes, and anything not bound to a catalog relation (a
CTE, a subquery, a table function, an alias that renames columns) is
opaque, so the function errs toward missing a relationship.

The view orients each join toward a column that alone keys an index, then
a column named id, then away from a column named *_id, and keeps a join
nothing settles undirected. A passed-through key references its source,
and a passed-through non-key column shares its references with its
source, to a fixed point with WITH MUTUALLY RECURSIVE. Propagating through
keys too would build the transitive closure of a view stack, which grows
with the square of its depth.

The function is gated by a new enable_inferred_relationships feature flag,
default off, on in CI. The builtin is planned with item parsing flags on,
so it is maintained regardless of the flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jubrad added a commit to jubrad/materialize that referenced this pull request Nov 16, 2026
A passed key now gets only its direct reference. Propagating through it
too built the transitive closure of a view stack, which grows with the
square of its depth: a stack of 300 views passing `id` produced 45,150
relationships. This keeps the explorer in agreement with
mz_internal.mz_inferred_relationships (MaterializeInc#40832).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jubrad added a commit to jubrad/materialize that referenced this pull request Nov 17, 2026
Inference no longer derives references to a fixed point. Every inferred
edge now comes from a single view's definition: a join, a passed key, or
a new "same values" edge for any other passed-through column. This
matches mz_internal.mz_inferred_relationships (MaterializeInc#40832), where the fixpoint
cost about twice the maintenance per replaced view and the derived edges
added nothing the paths lack. Declared joins are confirmed by a path of
inferred edges, and only same-values edges on a path to a join are drawn,
so passed-through names and amounts do not bury the relationships. On the
auction demo every declared join that resolves is connected, against 9 of
11 with the fixpoint.

Above 500 entities the canvas is no longer drawn: dagre's recursive
ranking overflowed the stack on a chain of about 3,000 views. A large
environment now opens on the ontology table's schemas, or the one schema
with the most inferred joins, rather than every schema an inferred join
touches.

Also restores the license header of inferredJoins.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the WITH MUTUALLY RECURSIVE fixpoint. Every row now comes from a
single view's definition, with a new `kind` column: `join` for columns a
view equates, `key` for a key passed through, and `same_values` for any
other column passed through. Relationships across several views are the
paths through these rows.

Maintaining the fixpoint was nearly all of the view's cost. Replacing 300
views with 3,000 present cost 1.66 ms of catalog server CPU per view with
it and 0.68 ms without, against 0.57 ms for mz_object_dependencies, and
hydration no longer outlasts mz_views. Paths lose nothing: on the auction
demo, every relationship the fixpoint derived is connected, and so is
every resolvable row of a hand-written ontology table.

Also drops the column existence joins, since the planner validated every
column a persisted definition names, and the pass that dropped undirected
duplicates, since direction depends only on the two columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jubrad and others added 2 commits November 16, 2026 21:28
Add mz_internal.mz_mcp_relationships, a builtin view that lists the
relationships between relations the current role can read, shaped like an
ontology table (table_name, columns, referenced_table,
referenced_columns), so agents can find how to join two relations without
one. It is on the restrict_to_user_objects allow-list, and like
mz_mcp_data_products lists only relations the role holds SELECT on.

Besides the join and key rows of mz_inferred_relationships, it lists
derived references: a column holding the same values as a referencing
column, through a chain of views passing it, references the same key, so
fct_orders.customer_id references customers.id even when only an
intermediate view joins them. The chains are walked on read, by pointer
doubling over the same_values rows. Maintaining them in the materialized
view instead measured 3 to 5.5 times its cost per replaced view; on read
the view takes about 300 ms with 4,000 views.

Also drop the enable_inferred_relationships flag. Builtins are created
regardless of flags, so it gated only the parse function, not the cost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A passed-through column is not a relationship. It is the link
mz_mcp_relationships follows to derive references, so name it for what it
records and document that agents should read mz_mcp_relationships.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant