Skip to main content

mz_catalog/
builtin.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Built-in catalog items.
11//!
12//! Builtins exist in the `mz_catalog` ambient schema. They are automatically
13//! installed into the catalog when it is opened. Their definitions are not
14//! persisted in the catalog, but hardcoded in this module. This makes it easy
15//! to add new builtins, or change the definition of existing builtins, in new
16//! versions of Materialize.
17//!
18//! Builtin's names, columns, and types are part of the stable API of
19//! Materialize. Be careful to maintain backwards compatibility when changing
20//! definitions of existing builtins!
21//!
22//! More information about builtin system tables and types can be found in
23//! <https://materialize.com/docs/sql/system-catalog/>.
24
25mod builtin;
26pub mod notice;
27mod ontology;
28mod pg_catalog;
29pub use pg_catalog::*;
30mod mz_catalog;
31pub use mz_catalog::*;
32mod mz_internal;
33pub use mz_internal::*;
34mod mz_introspection;
35pub use mz_introspection::*;
36mod information_schema;
37pub use information_schema::*;
38
39use std::collections::BTreeMap;
40use std::hash::Hash;
41use std::string::ToString;
42use std::sync::LazyLock;
43
44use mz_compute_client::logging::LogVariant;
45use mz_ore::collections::HashMap;
46use mz_pgrepr::oid;
47use mz_repr::adt::mz_acl_item::{AclMode, MzAclItem};
48use mz_repr::namespaces::{
49    INFORMATION_SCHEMA, MZ_CATALOG_SCHEMA, MZ_INTERNAL_SCHEMA, MZ_UNSAFE_SCHEMA, PG_CATALOG_SCHEMA,
50};
51use mz_repr::role_id::RoleId;
52use mz_repr::{RelationDesc, SemanticType, SqlRelationType};
53use mz_sql::catalog::RoleAttributesRaw;
54use mz_sql::catalog::{
55    CatalogItemType, CatalogTypeDetails, NameReference, ObjectType, SystemObjectType, TypeReference,
56};
57use mz_sql::rbac;
58use mz_sql::session::user::{
59    ANALYTICS_USER_NAME, JWT_SYNC_ROLE_NAME, MZ_ANALYTICS_ROLE_ID, MZ_JWT_SYNC_ROLE_ID,
60    MZ_MONITOR_REDACTED_ROLE_ID, MZ_MONITOR_ROLE_ID, MZ_SUPPORT_ROLE_ID, MZ_SYSTEM_ROLE_ID,
61    SUPPORT_USER_NAME, SYSTEM_USER_NAME,
62};
63use serde::Serialize;
64
65use crate::durable::objects::SystemObjectDescription;
66use crate::memory::objects::DataSourceDesc;
67
68pub const BUILTIN_PREFIXES: &[&str] = &["mz_", "pg_", "external_"];
69const BUILTIN_CLUSTER_REPLICA_NAME: &str = "r1";
70
71/// A sentinel used in place of a fingerprint that indicates that a builtin
72/// object is runtime alterable. Runtime alterable objects don't have meaningful
73/// fingerprints because they may have been intentionally changed by the user
74/// after creation.
75// NOTE(benesch): ideally we'd use a fingerprint type that used a sum type
76// rather than a loosely typed string to represent the runtime alterable
77// state like so:
78//
79//     enum Fingerprint {
80//         SqlText(String),
81//         RuntimeAlterable,
82//     }
83//
84// However, that would entail a complicated migration for the existing system object
85// mapping collection stored on disk.
86pub const RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL: &str = "<RUNTIME-ALTERABLE>";
87
88#[derive(Clone, Debug)]
89pub enum Builtin<T: 'static + TypeReference> {
90    Log(&'static BuiltinLog),
91    Table(&'static BuiltinTable),
92    View(&'static BuiltinView),
93    MaterializedView(&'static BuiltinMaterializedView),
94    Type(&'static BuiltinType<T>),
95    Func(BuiltinFunc),
96    Source(&'static BuiltinSource),
97    Index(&'static BuiltinIndex),
98    Connection(&'static BuiltinConnection),
99}
100
101impl<T: TypeReference> Builtin<T> {
102    pub fn name(&self) -> &'static str {
103        match self {
104            Builtin::Log(log) => log.name,
105            Builtin::Table(table) => table.name,
106            Builtin::View(view) => view.name,
107            Builtin::MaterializedView(mv) => mv.name,
108            Builtin::Type(typ) => typ.name,
109            Builtin::Func(func) => func.name,
110            Builtin::Source(coll) => coll.name,
111            Builtin::Index(index) => index.name,
112            Builtin::Connection(connection) => connection.name,
113        }
114    }
115
116    pub fn schema(&self) -> &'static str {
117        match self {
118            Builtin::Log(log) => log.schema,
119            Builtin::Table(table) => table.schema,
120            Builtin::View(view) => view.schema,
121            Builtin::MaterializedView(mv) => mv.schema,
122            Builtin::Type(typ) => typ.schema,
123            Builtin::Func(func) => func.schema,
124            Builtin::Source(coll) => coll.schema,
125            Builtin::Index(index) => index.schema,
126            Builtin::Connection(connection) => connection.schema,
127        }
128    }
129
130    pub fn catalog_item_type(&self) -> CatalogItemType {
131        match self {
132            Builtin::Log(_) => CatalogItemType::Source,
133            Builtin::Source(_) => CatalogItemType::Source,
134            Builtin::Table(_) => CatalogItemType::Table,
135            Builtin::View(_) => CatalogItemType::View,
136            Builtin::MaterializedView(_) => CatalogItemType::MaterializedView,
137            Builtin::Type(_) => CatalogItemType::Type,
138            Builtin::Func(_) => CatalogItemType::Func,
139            Builtin::Index(_) => CatalogItemType::Index,
140            Builtin::Connection(_) => CatalogItemType::Connection,
141        }
142    }
143
144    /// Whether the object can be altered at runtime by its owner.
145    pub fn runtime_alterable(&self) -> bool {
146        match self {
147            Builtin::Connection(c) => c.runtime_alterable,
148            _ => false,
149        }
150    }
151}
152
153#[derive(Clone, Debug, Hash, Serialize)]
154pub struct BuiltinLog {
155    pub variant: LogVariant,
156    pub name: &'static str,
157    pub schema: &'static str,
158    pub oid: u32,
159    /// ACL items to apply to the object
160    pub access: Vec<MzAclItem>,
161    #[serde(default)]
162    pub ontology: Option<Ontology>,
163}
164
165/// Ontology metadata for a builtin catalog object.
166///
167/// When present on a builtin, it marks it as an ontology entity with an explicit
168/// `entity_name`, `description`, and optional per-column semantic type annotations.
169///
170/// ## Why `column_semantic_types` lives here and not in `RelationDesc`
171///
172/// Semantic types are pure catalog-level metadata: they annotate what an ID
173/// column *means* (e.g. "this is a ClusterId") without affecting the Arrow
174/// data type used for encoding. Keeping them in `RelationDesc` would cause
175/// persist schema mismatches during zero-downtime upgrades: the old binary
176/// registers a schema without semantic types, the new binary tries to register
177/// a schema with them, and `register_schema` returns `None` because the schemas
178/// are not `PartialEq`. Since the only consumers of semantic types are the
179/// ontology views (which already have access to `Ontology`), storing them here
180/// is both correct and avoids the schema-evolution problem entirely.
181#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize)]
182pub struct Ontology {
183    /// The ontology entity name (e.g., "database", "table", "mv"). Names a
184    /// single row of this relation, so prefer singular event/object nouns
185    /// (e.g., "replica_status_event" not "replica_status_history").
186    pub entity_name: &'static str,
187    /// One-line description of this entity.
188    pub description: &'static str,
189    /// Relationships originating from this entity (foreign keys, unions,
190    /// mappings, dependencies, metrics).
191    pub links: &'static [OntologyLink],
192    /// Per-column semantic type annotations: `(column_name, SemanticType)`.
193    /// Only columns that carry a meaningful semantic type need to appear here.
194    pub column_semantic_types: &'static [(&'static str, SemanticType)],
195}
196
197/// Cardinality of an ontology link.
198#[derive(
199    Clone,
200    Copy,
201    Debug,
202    Hash,
203    PartialEq,
204    Eq,
205    serde::Serialize,
206    serde::Deserialize
207)]
208#[serde(rename_all = "snake_case")]
209pub enum Cardinality {
210    OneToOne,
211    ManyToOne,
212}
213
214/// Helper used by serde to skip serializing `false` boolean fields.
215fn is_false(v: &bool) -> bool {
216    !v
217}
218
219/// Typed properties for an ontology link. Serialized to the `properties` JSONB
220/// column in `mz_ontology_link_types`. The `kind` field is inlined from the
221/// enum variant name via `#[serde(tag = "kind")]`.
222///
223/// Choosing the right variant matters:
224///
225/// - [`LinkProperties::ForeignKey`]: the source entity has a column whose
226///   value is an ID that directly references a row in the target entity.
227///   Use this when there is an explicit FK column (e.g. `schema_id` ->
228///   `schema`).
229/// - [`LinkProperties::DependsOn`]: this entity logically depends on the
230///   target entity via a graph-edge table (e.g. `mz_compute_dependencies`
231///   records that a compute object depends on another object). The
232///   `source_column` is the column **in this entity** that holds the
233///   dependent's ID; `target_column` is the column in the target entity
234///   being depended upon. Use this for dependency-graph tables, **not**
235///   `ForeignKey`.
236/// - [`LinkProperties::Union`]: the source entity is a superset view that
237///   contains the target entity as a subset, optionally filtered by a
238///   discriminator column.
239/// - [`LinkProperties::MapsTo`]: the source entity provides an ID translation
240///   to the target entity, possibly via an intermediate table or across ID
241///   namespaces.
242/// - [`LinkProperties::Measures`]: the source entity records metric
243///   measurements about the target entity.
244#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, serde::Serialize)]
245#[serde(tag = "kind", rename_all = "snake_case")]
246pub enum LinkProperties {
247    /// A foreign-key relationship: `source_column` in the source entity
248    /// references `target_column` in the target entity.
249    ForeignKey {
250        /// Column in the source entity that holds the reference.
251        source_column: &'static str,
252        /// Column in the target entity being referenced (usually `id`).
253        target_column: &'static str,
254        /// How many source rows may reference a single target row.
255        cardinality: Cardinality,
256        /// Semantic type of the source column, if it carries an ID that
257        /// requires type-aware resolution (e.g. `CatalogItemId`, `GlobalId`).
258        #[serde(skip_serializing_if = "Option::is_none")]
259        source_id_type: Option<mz_repr::SemanticType>,
260        /// Intermediate mapping relation needed when `source_id_type` does not
261        /// directly match the target entity's ID type (e.g.
262        /// `mz_internal.mz_object_global_ids` to go from `GlobalId` to catalog
263        /// object).
264        #[serde(skip_serializing_if = "Option::is_none")]
265        requires_mapping: Option<&'static str>,
266        /// True when the source column may be NULL (the reference is optional).
267        #[serde(default, skip_serializing_if = "is_false")]
268        nullable: bool,
269        /// Free-form annotation for cases that need extra context.
270        #[serde(skip_serializing_if = "Option::is_none")]
271        note: Option<&'static str>,
272        /// Additional `(source_column, target_column)` pairs that together with
273        /// `source_column`/`target_column` form a composite join key. Used for
274        /// `_per_worker` entities whose primary key is `(id, worker_id)`, and
275        /// for message-count raw relations whose join key includes worker IDs.
276        /// Serialized as an array; omitted when `None`.
277        #[serde(skip_serializing_if = "Option::is_none")]
278        extra_key_columns: Option<&'static [(&'static str, &'static str)]>,
279    },
280    /// A union relationship: the source entity is a superset view that includes
281    /// the target entity, optionally filtered by a discriminator column/value.
282    Union {
283        /// Column used to discriminate between subtypes (e.g. `type`).
284        #[serde(skip_serializing_if = "Option::is_none")]
285        discriminator_column: Option<&'static str>,
286        /// Value of `discriminator_column` that selects the target entity.
287        #[serde(skip_serializing_if = "Option::is_none")]
288        discriminator_value: Option<&'static str>,
289        /// Free-form annotation for cases that need extra context.
290        #[serde(skip_serializing_if = "Option::is_none")]
291        note: Option<&'static str>,
292    },
293    /// A mapping relationship: the source entity maps to the target entity,
294    /// optionally via an intermediate table and/or with an ID-type conversion.
295    MapsTo {
296        /// Column in the source entity that holds the ID to map from.
297        source_column: &'static str,
298        /// Column in the target entity being mapped to.
299        target_column: &'static str,
300        /// Intermediate relation used to perform the mapping.
301        #[serde(skip_serializing_if = "Option::is_none")]
302        via: Option<&'static str>,
303        /// Semantic type of the source ID before mapping.
304        #[serde(skip_serializing_if = "Option::is_none")]
305        from_type: Option<mz_repr::SemanticType>,
306        /// Semantic type of the target ID after mapping.
307        #[serde(skip_serializing_if = "Option::is_none")]
308        to_type: Option<mz_repr::SemanticType>,
309        /// Free-form annotation for cases that need extra context.
310        #[serde(skip_serializing_if = "Option::is_none")]
311        note: Option<&'static str>,
312    },
313    /// A dependency relationship: this entity directly depends on the
314    /// target entity (e.g. a materialization that references an object).
315    DependsOn {
316        /// Column in this entity that holds the dependency ID.
317        source_column: &'static str,
318        /// Column in the target entity being depended upon (usually `id`).
319        target_column: &'static str,
320        /// Semantic type of the source column.
321        #[serde(skip_serializing_if = "Option::is_none")]
322        source_id_type: Option<mz_repr::SemanticType>,
323        /// Intermediate mapping relation needed when `source_id_type` does not
324        /// directly match the target entity's ID type (e.g. GlobalId →
325        /// `mz_internal.mz_object_global_ids` to reach a catalog object).
326        #[serde(skip_serializing_if = "Option::is_none")]
327        requires_mapping: Option<&'static str>,
328    },
329    /// A metric relationship: the source entity records measurements of a named
330    /// metric on the target entity.
331    Measures {
332        /// Column in the source entity that references the target entity.
333        source_column: &'static str,
334        /// Column in the target entity being measured (usually `id`).
335        target_column: &'static str,
336        /// Name of the metric being measured (e.g. `cpu_time_ns`).
337        metric: &'static str,
338        /// Semantic type of the source column, if ID-type resolution is needed.
339        #[serde(skip_serializing_if = "Option::is_none")]
340        source_id_type: Option<mz_repr::SemanticType>,
341        /// Intermediate mapping relation needed when the source ID type differs
342        /// from the target entity's ID type.
343        #[serde(skip_serializing_if = "Option::is_none")]
344        requires_mapping: Option<&'static str>,
345        /// Free-form annotation for cases that need extra context.
346        #[serde(skip_serializing_if = "Option::is_none")]
347        note: Option<&'static str>,
348        /// Additional `(source_column, target_column)` pairs that together with
349        /// `source_column`/`target_column` form a composite join key.
350        #[serde(skip_serializing_if = "Option::is_none")]
351        extra_key_columns: Option<&'static [(&'static str, &'static str)]>,
352    },
353}
354
355impl LinkProperties {
356    /// Basic foreign-key link with no optional fields set.
357    pub const fn fk(
358        source_column: &'static str,
359        target_column: &'static str,
360        cardinality: Cardinality,
361    ) -> Self {
362        Self::ForeignKey {
363            source_column,
364            target_column,
365            cardinality,
366            source_id_type: None,
367            requires_mapping: None,
368            nullable: false,
369            note: None,
370            extra_key_columns: None,
371        }
372    }
373
374    /// Foreign-key link where the source column may be NULL.
375    pub const fn fk_nullable(
376        source_column: &'static str,
377        target_column: &'static str,
378        cardinality: Cardinality,
379    ) -> Self {
380        Self::ForeignKey {
381            source_column,
382            target_column,
383            cardinality,
384            source_id_type: None,
385            requires_mapping: None,
386            nullable: true,
387            note: None,
388            extra_key_columns: None,
389        }
390    }
391
392    /// Foreign-key link whose source column carries a typed ID (e.g.
393    /// `CatalogItemId`) but does not require an intermediate mapping table.
394    pub const fn fk_typed(
395        source_column: &'static str,
396        target_column: &'static str,
397        cardinality: Cardinality,
398        source_id_type: mz_repr::SemanticType,
399    ) -> Self {
400        Self::ForeignKey {
401            source_column,
402            target_column,
403            cardinality,
404            source_id_type: Some(source_id_type),
405            requires_mapping: None,
406            nullable: false,
407            note: None,
408            extra_key_columns: None,
409        }
410    }
411
412    /// Foreign-key link whose source column carries a typed ID that requires
413    /// an intermediate mapping table to resolve (e.g. `GlobalId` →
414    /// `mz_internal.mz_object_global_ids`).
415    pub const fn fk_mapped(
416        source_column: &'static str,
417        target_column: &'static str,
418        cardinality: Cardinality,
419        source_id_type: mz_repr::SemanticType,
420        requires_mapping: &'static str,
421    ) -> Self {
422        Self::ForeignKey {
423            source_column,
424            target_column,
425            cardinality,
426            source_id_type: Some(source_id_type),
427            requires_mapping: Some(requires_mapping),
428            nullable: false,
429            note: None,
430            extra_key_columns: None,
431        }
432    }
433
434    /// Foreign-key link with a composite join key. `extra_key_columns` lists
435    /// additional `(source_column, target_column)` pairs beyond the primary
436    /// `source_column`/`target_column` pair. Examples:
437    /// - `&[("worker_id", "worker_id")]` for `_per_worker` entities
438    /// - `&[("from_worker_id", "worker_id")]` for message-count raw relations
439    pub const fn fk_composite(
440        source_column: &'static str,
441        target_column: &'static str,
442        cardinality: Cardinality,
443        extra_key_columns: &'static [(&'static str, &'static str)],
444    ) -> Self {
445        Self::ForeignKey {
446            source_column,
447            target_column,
448            cardinality,
449            source_id_type: None,
450            requires_mapping: None,
451            nullable: false,
452            note: None,
453            extra_key_columns: Some(extra_key_columns),
454        }
455    }
456
457    /// Union link filtered by a discriminator column/value pair.
458    pub const fn union_disc(
459        discriminator_column: &'static str,
460        discriminator_value: &'static str,
461    ) -> Self {
462        Self::Union {
463            discriminator_column: Some(discriminator_column),
464            discriminator_value: Some(discriminator_value),
465            note: None,
466        }
467    }
468
469    /// Basic measures link with no optional fields set.
470    pub const fn measures(
471        source_column: &'static str,
472        target_column: &'static str,
473        metric: &'static str,
474    ) -> Self {
475        Self::Measures {
476            source_column,
477            target_column,
478            metric,
479            source_id_type: None,
480            requires_mapping: None,
481            note: None,
482            extra_key_columns: None,
483        }
484    }
485
486    /// Measures link with a composite join key.
487    pub const fn measures_composite(
488        source_column: &'static str,
489        target_column: &'static str,
490        metric: &'static str,
491        extra_key_columns: &'static [(&'static str, &'static str)],
492    ) -> Self {
493        Self::Measures {
494            source_column,
495            target_column,
496            metric,
497            source_id_type: None,
498            requires_mapping: None,
499            note: None,
500            extra_key_columns: Some(extra_key_columns),
501        }
502    }
503
504    /// Measures link whose source ID requires an intermediate mapping table.
505    pub const fn measures_mapped(
506        source_column: &'static str,
507        target_column: &'static str,
508        metric: &'static str,
509        source_id_type: mz_repr::SemanticType,
510        requires_mapping: &'static str,
511    ) -> Self {
512        Self::Measures {
513            source_column,
514            target_column,
515            metric,
516            source_id_type: Some(source_id_type),
517            requires_mapping: Some(requires_mapping),
518            note: None,
519            extra_key_columns: None,
520        }
521    }
522}
523
524/// A directed relationship from one ontology entity to another.
525///
526/// Each link has a `name` (the relationship label, e.g. `"owned_by"`), a
527/// `target` entity name, and a [`LinkProperties`] variant that captures the
528/// kind of relationship.
529#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize)]
530pub struct OntologyLink {
531    /// Relationship name describing the relationship FROM this entity TO the
532    /// target (e.g., `"owned_by"` means "this entity is owned by the target",
533    /// `"depends_on"` means "this entity depends on the target"). When the
534    /// same name appears on multiple links of the same entity, all links
535    /// share that relationship role (e.g., several `"union_includes"` links).
536    pub name: &'static str,
537    /// Target entity name (e.g., "role", "schema").
538    pub target: &'static str,
539    /// Typed properties for the `properties` JSONB column.
540    pub properties: LinkProperties,
541}
542
543#[derive(Clone, Hash, Debug, PartialEq, Eq)]
544pub struct BuiltinTable {
545    pub name: &'static str,
546    pub schema: &'static str,
547    pub oid: u32,
548    pub desc: RelationDesc,
549    pub column_comments: BTreeMap<&'static str, &'static str>,
550    /// Whether the table's retention policy is controlled by
551    /// the system variable `METRICS_RETENTION`
552    pub is_retained_metrics_object: bool,
553    /// ACL items to apply to the object
554    pub access: Vec<MzAclItem>,
555    /// Ontology metadata. None means this builtin is not an ontology entity.
556    pub ontology: Option<Ontology>,
557}
558
559#[derive(Clone, Debug, PartialEq, Eq)]
560pub struct BuiltinSource {
561    pub name: &'static str,
562    pub schema: &'static str,
563    pub oid: u32,
564    pub desc: RelationDesc,
565    pub column_comments: BTreeMap<&'static str, &'static str>,
566    pub data_source: DataSourceDesc,
567    /// Whether the source's retention policy is controlled by
568    /// the system variable `METRICS_RETENTION`
569    pub is_retained_metrics_object: bool,
570    /// ACL items to apply to the object
571    pub access: Vec<MzAclItem>,
572    /// Ontology metadata. None means this builtin is not an ontology entity.
573    pub ontology: Option<Ontology>,
574}
575
576#[derive(Hash, Debug)]
577pub struct BuiltinView {
578    pub name: &'static str,
579    pub schema: &'static str,
580    pub oid: u32,
581    pub desc: RelationDesc,
582    pub column_comments: BTreeMap<&'static str, &'static str>,
583    pub sql: &'static str,
584    /// ACL items to apply to the object
585    pub access: Vec<MzAclItem>,
586    /// Ontology metadata. None means this builtin is not an ontology entity.
587    pub ontology: Option<Ontology>,
588}
589
590impl BuiltinView {
591    pub fn create_sql(&self) -> String {
592        format!("CREATE VIEW {}.{} AS {}", self.schema, self.name, self.sql)
593    }
594}
595
596#[derive(Hash, Debug)]
597pub struct BuiltinMaterializedView {
598    pub name: &'static str,
599    pub schema: &'static str,
600    pub oid: u32,
601    pub desc: RelationDesc,
602    pub column_comments: BTreeMap<&'static str, &'static str>,
603    /// SQL fragment for the MV, following `CREATE MATERIALIZED VIEW [name]`
604    ///
605    /// Format: `IN CLUSTER [cluster_name] AS [query]`
606    pub sql: &'static str,
607    /// Whether the MV's retention policy is controlled by
608    /// the system variable `METRICS_RETENTION`
609    pub is_retained_metrics_object: bool,
610    /// ACL items to apply to the object
611    pub access: Vec<MzAclItem>,
612    /// Ontology metadata. None means this builtin is not an ontology entity.
613    pub ontology: Option<Ontology>,
614}
615
616impl BuiltinMaterializedView {
617    pub fn create_sql(&self) -> String {
618        format!(
619            "CREATE MATERIALIZED VIEW {}.{} {}",
620            self.schema, self.name, self.sql
621        )
622    }
623}
624
625#[derive(Debug)]
626pub struct BuiltinType<T: TypeReference> {
627    pub name: &'static str,
628    pub schema: &'static str,
629    pub oid: u32,
630    pub details: CatalogTypeDetails<T>,
631}
632
633#[derive(Clone, Debug)]
634pub struct BuiltinFunc {
635    pub schema: &'static str,
636    pub name: &'static str,
637    pub inner: &'static mz_sql::func::Func,
638}
639
640/// Note: When creating a built-in index, it's usually best to choose a key that has only one
641/// component. For example, if you created an index
642/// `ON mz_internal.mz_object_lifetimes (id, object_type)`, then this index couldn't be used for a
643/// lookup for `WHERE object_type = ...`, and neither for joins keyed on just `id`.
644/// See <https://materialize.com/docs/transform-data/optimization/#matching-multi-column-indexes-to-multi-column-where-clauses>
645#[derive(Debug)]
646pub struct BuiltinIndex {
647    pub name: &'static str,
648    pub schema: &'static str,
649    pub oid: u32,
650    /// SQL fragment for the index, following `CREATE INDEX [name]`
651    ///
652    /// Format: `IN CLUSTER [cluster_name] ON [table_name] ([column_exprs])`
653    pub sql: &'static str,
654    pub is_retained_metrics_object: bool,
655}
656
657impl BuiltinIndex {
658    pub fn create_sql(&self) -> String {
659        format!("CREATE INDEX {}\n{}", self.name, self.sql)
660    }
661}
662
663#[derive(Hash, Debug)]
664pub struct BuiltinConnection {
665    pub name: &'static str,
666    pub schema: &'static str,
667    pub oid: u32,
668    pub sql: &'static str,
669    pub access: &'static [MzAclItem],
670    pub owner_id: &'static RoleId,
671    /// Whether the object can be altered at runtime by its owner.
672    ///
673    /// Note that when `runtime_alterable` is true, changing the `sql` in future
674    /// versions does not trigger a migration.
675    pub runtime_alterable: bool,
676}
677
678#[derive(Clone, Debug)]
679pub struct BuiltinRole {
680    pub id: RoleId,
681    /// Name of the builtin role.
682    ///
683    /// IMPORTANT: Must start with a prefix from [`BUILTIN_PREFIXES`].
684    pub name: &'static str,
685    pub oid: u32,
686    pub attributes: RoleAttributesRaw,
687}
688
689#[derive(Clone, Debug)]
690pub struct BuiltinCluster {
691    /// Name of the cluster.
692    ///
693    /// IMPORTANT: Must start with a prefix from [`BUILTIN_PREFIXES`].
694    pub name: &'static str,
695    pub privileges: &'static [MzAclItem],
696    pub owner_id: &'static RoleId,
697}
698
699#[derive(Clone, Debug, PartialEq, Eq)]
700pub struct BuiltinClusterReplica {
701    /// Name of the compute replica.
702    pub name: &'static str,
703    /// Name of the cluster that this replica belongs to.
704    pub cluster_name: &'static str,
705}
706
707/// Uniquely identifies the definition of a builtin object.
708pub trait Fingerprint {
709    fn fingerprint(&self) -> String;
710}
711
712impl<T: TypeReference> Fingerprint for &Builtin<T> {
713    fn fingerprint(&self) -> String {
714        match self {
715            Builtin::Log(log) => log.fingerprint(),
716            Builtin::Table(table) => table.fingerprint(),
717            Builtin::View(view) => view.fingerprint(),
718            Builtin::MaterializedView(mv) => mv.fingerprint(),
719            Builtin::Type(typ) => typ.fingerprint(),
720            Builtin::Func(func) => func.fingerprint(),
721            Builtin::Source(coll) => coll.fingerprint(),
722            Builtin::Index(index) => index.fingerprint(),
723            Builtin::Connection(connection) => connection.fingerprint(),
724        }
725    }
726}
727
728// Types and Funcs never change fingerprints so we just return constant 0
729impl<T: TypeReference> Fingerprint for &BuiltinType<T> {
730    fn fingerprint(&self) -> String {
731        "".to_string()
732    }
733}
734
735impl Fingerprint for &BuiltinFunc {
736    fn fingerprint(&self) -> String {
737        "".to_string()
738    }
739}
740
741impl Fingerprint for &BuiltinLog {
742    fn fingerprint(&self) -> String {
743        self.variant.desc().fingerprint()
744    }
745}
746
747impl Fingerprint for &BuiltinTable {
748    fn fingerprint(&self) -> String {
749        self.desc.fingerprint()
750    }
751}
752
753impl Fingerprint for &BuiltinView {
754    fn fingerprint(&self) -> String {
755        self.sql.to_string()
756    }
757}
758
759impl Fingerprint for &BuiltinSource {
760    fn fingerprint(&self) -> String {
761        self.desc.fingerprint()
762    }
763}
764
765impl Fingerprint for &BuiltinMaterializedView {
766    fn fingerprint(&self) -> String {
767        self.create_sql()
768    }
769}
770
771impl Fingerprint for &BuiltinIndex {
772    fn fingerprint(&self) -> String {
773        self.create_sql()
774    }
775}
776
777impl Fingerprint for &BuiltinConnection {
778    fn fingerprint(&self) -> String {
779        self.sql.to_string()
780    }
781}
782
783impl Fingerprint for RelationDesc {
784    fn fingerprint(&self) -> String {
785        self.typ().fingerprint()
786    }
787}
788
789impl Fingerprint for SqlRelationType {
790    fn fingerprint(&self) -> String {
791        serde_json::to_string(self).expect("serialization cannot fail")
792    }
793}
794
795pub(super) const PUBLIC_SELECT: MzAclItem = MzAclItem {
796    grantee: RoleId::Public,
797    grantor: MZ_SYSTEM_ROLE_ID,
798    acl_mode: AclMode::SELECT,
799};
800
801pub(super) const SUPPORT_SELECT: MzAclItem = MzAclItem {
802    grantee: MZ_SUPPORT_ROLE_ID,
803    grantor: MZ_SYSTEM_ROLE_ID,
804    acl_mode: AclMode::SELECT,
805};
806
807pub(super) const ANALYTICS_SELECT: MzAclItem = MzAclItem {
808    grantee: MZ_ANALYTICS_ROLE_ID,
809    grantor: MZ_SYSTEM_ROLE_ID,
810    acl_mode: AclMode::SELECT,
811};
812
813pub(super) const MONITOR_SELECT: MzAclItem = MzAclItem {
814    grantee: MZ_MONITOR_ROLE_ID,
815    grantor: MZ_SYSTEM_ROLE_ID,
816    acl_mode: AclMode::SELECT,
817};
818
819pub(super) const MONITOR_REDACTED_SELECT: MzAclItem = MzAclItem {
820    grantee: MZ_MONITOR_REDACTED_ROLE_ID,
821    grantor: MZ_SYSTEM_ROLE_ID,
822    acl_mode: AclMode::SELECT,
823};
824
825pub static MZ_CATALOG_RAW_DESCRIPTION: LazyLock<SystemObjectDescription> =
826    LazyLock::new(|| SystemObjectDescription {
827        schema_name: MZ_CATALOG_RAW.schema.to_string(),
828        object_type: CatalogItemType::Source,
829        object_name: MZ_CATALOG_RAW.name.to_string(),
830    });
831
832pub static MZ_STORAGE_USAGE_BY_SHARD_DESCRIPTION: LazyLock<SystemObjectDescription> =
833    LazyLock::new(|| SystemObjectDescription {
834        schema_name: MZ_STORAGE_USAGE_BY_SHARD.schema.to_string(),
835        object_type: CatalogItemType::Table,
836        object_name: MZ_STORAGE_USAGE_BY_SHARD.name.to_string(),
837    });
838
839/// Identifies [`MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY`] for the schema-migration
840/// guard in `builtin_schema_migration.rs`, which forbids migrating this table
841/// because its startup pruner assumes it is the only source of retractions.
842pub static MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY_DESCRIPTION: LazyLock<SystemObjectDescription> =
843    LazyLock::new(|| SystemObjectDescription {
844        schema_name: MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY.schema.to_string(),
845        object_type: CatalogItemType::Table,
846        object_name: MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY.name.to_string(),
847    });
848pub const MZ_SYSTEM_ROLE: BuiltinRole = BuiltinRole {
849    id: MZ_SYSTEM_ROLE_ID,
850    name: SYSTEM_USER_NAME,
851    oid: oid::ROLE_MZ_SYSTEM_OID,
852    attributes: RoleAttributesRaw::new().with_all(),
853};
854
855pub const MZ_SUPPORT_ROLE: BuiltinRole = BuiltinRole {
856    id: MZ_SUPPORT_ROLE_ID,
857    name: SUPPORT_USER_NAME,
858    oid: oid::ROLE_MZ_SUPPORT_OID,
859    attributes: RoleAttributesRaw::new(),
860};
861
862pub const MZ_ANALYTICS_ROLE: BuiltinRole = BuiltinRole {
863    id: MZ_ANALYTICS_ROLE_ID,
864    name: ANALYTICS_USER_NAME,
865    oid: oid::ROLE_MZ_ANALYTICS_OID,
866    attributes: RoleAttributesRaw::new(),
867};
868
869/// This role can `SELECT` from various query history objects,
870/// e.g. `mz_prepared_statement_history`.
871pub const MZ_MONITOR_ROLE: BuiltinRole = BuiltinRole {
872    id: MZ_MONITOR_ROLE_ID,
873    name: "mz_monitor",
874    oid: oid::ROLE_MZ_MONITOR_OID,
875    attributes: RoleAttributesRaw::new(),
876};
877
878/// This role is like [`MZ_MONITOR_ROLE`], but can only query
879/// the redacted versions of the objects.
880pub const MZ_MONITOR_REDACTED: BuiltinRole = BuiltinRole {
881    id: MZ_MONITOR_REDACTED_ROLE_ID,
882    name: "mz_monitor_redacted",
883    oid: oid::ROLE_MZ_MONITOR_REDACTED_OID,
884    attributes: RoleAttributesRaw::new(),
885};
886
887/// Sentinel role used as the grantor for JWT group-sync-managed
888/// role memberships. Never logged into directly.
889pub const MZ_JWT_SYNC_ROLE: BuiltinRole = BuiltinRole {
890    id: MZ_JWT_SYNC_ROLE_ID,
891    name: JWT_SYNC_ROLE_NAME,
892    oid: oid::ROLE_MZ_JWT_SYNC_OID,
893    attributes: RoleAttributesRaw::new(),
894};
895
896pub const MZ_SYSTEM_CLUSTER: BuiltinCluster = BuiltinCluster {
897    name: SYSTEM_USER_NAME,
898    owner_id: &MZ_SYSTEM_ROLE_ID,
899    privileges: &[
900        MzAclItem {
901            grantee: MZ_SUPPORT_ROLE_ID,
902            grantor: MZ_SYSTEM_ROLE_ID,
903            acl_mode: AclMode::USAGE,
904        },
905        rbac::owner_privilege(ObjectType::Cluster, MZ_SYSTEM_ROLE_ID),
906    ],
907};
908
909pub const MZ_SYSTEM_CLUSTER_REPLICA: BuiltinClusterReplica = BuiltinClusterReplica {
910    name: BUILTIN_CLUSTER_REPLICA_NAME,
911    cluster_name: MZ_SYSTEM_CLUSTER.name,
912};
913
914pub const MZ_CATALOG_SERVER_CLUSTER: BuiltinCluster = BuiltinCluster {
915    name: "mz_catalog_server",
916    owner_id: &MZ_SYSTEM_ROLE_ID,
917    privileges: &[
918        MzAclItem {
919            grantee: RoleId::Public,
920            grantor: MZ_SYSTEM_ROLE_ID,
921            acl_mode: AclMode::USAGE,
922        },
923        MzAclItem {
924            grantee: MZ_SUPPORT_ROLE_ID,
925            grantor: MZ_SYSTEM_ROLE_ID,
926            acl_mode: AclMode::USAGE.union(AclMode::CREATE),
927        },
928        rbac::owner_privilege(ObjectType::Cluster, MZ_SYSTEM_ROLE_ID),
929    ],
930};
931
932pub const MZ_CATALOG_SERVER_CLUSTER_REPLICA: BuiltinClusterReplica = BuiltinClusterReplica {
933    name: BUILTIN_CLUSTER_REPLICA_NAME,
934    cluster_name: MZ_CATALOG_SERVER_CLUSTER.name,
935};
936
937pub const MZ_PROBE_CLUSTER: BuiltinCluster = BuiltinCluster {
938    name: "mz_probe",
939    owner_id: &MZ_SYSTEM_ROLE_ID,
940    privileges: &[
941        MzAclItem {
942            grantee: MZ_SUPPORT_ROLE_ID,
943            grantor: MZ_SYSTEM_ROLE_ID,
944            acl_mode: AclMode::USAGE,
945        },
946        MzAclItem {
947            grantee: MZ_MONITOR_ROLE_ID,
948            grantor: MZ_SYSTEM_ROLE_ID,
949            acl_mode: AclMode::USAGE,
950        },
951        rbac::owner_privilege(ObjectType::Cluster, MZ_SYSTEM_ROLE_ID),
952    ],
953};
954pub const MZ_PROBE_CLUSTER_REPLICA: BuiltinClusterReplica = BuiltinClusterReplica {
955    name: BUILTIN_CLUSTER_REPLICA_NAME,
956    cluster_name: MZ_PROBE_CLUSTER.name,
957};
958
959pub const MZ_SUPPORT_CLUSTER: BuiltinCluster = BuiltinCluster {
960    name: "mz_support",
961    owner_id: &MZ_SUPPORT_ROLE_ID,
962    privileges: &[
963        MzAclItem {
964            grantee: MZ_SYSTEM_ROLE_ID,
965            grantor: MZ_SUPPORT_ROLE_ID,
966            acl_mode: rbac::all_object_privileges(SystemObjectType::Object(ObjectType::Cluster)),
967        },
968        rbac::owner_privilege(ObjectType::Cluster, MZ_SUPPORT_ROLE_ID),
969    ],
970};
971
972pub const MZ_ANALYTICS_CLUSTER: BuiltinCluster = BuiltinCluster {
973    name: "mz_analytics",
974    owner_id: &MZ_ANALYTICS_ROLE_ID,
975    privileges: &[
976        MzAclItem {
977            grantee: MZ_SYSTEM_ROLE_ID,
978            grantor: MZ_ANALYTICS_ROLE_ID,
979            acl_mode: rbac::all_object_privileges(SystemObjectType::Object(ObjectType::Cluster)),
980        },
981        rbac::owner_privilege(ObjectType::Cluster, MZ_ANALYTICS_ROLE_ID),
982    ],
983};
984
985/// List of all builtin objects sorted topologically by dependency.
986pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::new(|| {
987    let mut builtin_types = vec![
988        Builtin::Type(&TYPE_ANY),
989        Builtin::Type(&TYPE_ANYARRAY),
990        Builtin::Type(&TYPE_ANYELEMENT),
991        Builtin::Type(&TYPE_ANYNONARRAY),
992        Builtin::Type(&TYPE_ANYRANGE),
993        Builtin::Type(&TYPE_BOOL),
994        Builtin::Type(&TYPE_BOOL_ARRAY),
995        Builtin::Type(&TYPE_BYTEA),
996        Builtin::Type(&TYPE_BYTEA_ARRAY),
997        Builtin::Type(&TYPE_BPCHAR),
998        Builtin::Type(&TYPE_BPCHAR_ARRAY),
999        Builtin::Type(&TYPE_CHAR),
1000        Builtin::Type(&TYPE_CHAR_ARRAY),
1001        Builtin::Type(&TYPE_DATE),
1002        Builtin::Type(&TYPE_DATE_ARRAY),
1003        Builtin::Type(&TYPE_FLOAT4),
1004        Builtin::Type(&TYPE_FLOAT4_ARRAY),
1005        Builtin::Type(&TYPE_FLOAT8),
1006        Builtin::Type(&TYPE_FLOAT8_ARRAY),
1007        Builtin::Type(&TYPE_INT4),
1008        Builtin::Type(&TYPE_INT4_ARRAY),
1009        Builtin::Type(&TYPE_INT8),
1010        Builtin::Type(&TYPE_INT8_ARRAY),
1011        Builtin::Type(&TYPE_INTERVAL),
1012        Builtin::Type(&TYPE_INTERVAL_ARRAY),
1013        Builtin::Type(&TYPE_JSONB),
1014        Builtin::Type(&TYPE_JSONB_ARRAY),
1015        Builtin::Type(&TYPE_LIST),
1016        Builtin::Type(&TYPE_MAP),
1017        Builtin::Type(&TYPE_NAME),
1018        Builtin::Type(&TYPE_NAME_ARRAY),
1019        Builtin::Type(&TYPE_NUMERIC),
1020        Builtin::Type(&TYPE_NUMERIC_ARRAY),
1021        Builtin::Type(&TYPE_OID),
1022        Builtin::Type(&TYPE_OID_ARRAY),
1023        Builtin::Type(&TYPE_RECORD),
1024        Builtin::Type(&TYPE_RECORD_ARRAY),
1025        Builtin::Type(&TYPE_REGCLASS),
1026        Builtin::Type(&TYPE_REGCLASS_ARRAY),
1027        Builtin::Type(&TYPE_REGPROC),
1028        Builtin::Type(&TYPE_REGPROC_ARRAY),
1029        Builtin::Type(&TYPE_REGTYPE),
1030        Builtin::Type(&TYPE_REGTYPE_ARRAY),
1031        Builtin::Type(&TYPE_INT2),
1032        Builtin::Type(&TYPE_INT2_ARRAY),
1033        Builtin::Type(&TYPE_TEXT),
1034        Builtin::Type(&TYPE_TEXT_ARRAY),
1035        Builtin::Type(&TYPE_TIME),
1036        Builtin::Type(&TYPE_TIME_ARRAY),
1037        Builtin::Type(&TYPE_TIMESTAMP),
1038        Builtin::Type(&TYPE_TIMESTAMP_ARRAY),
1039        Builtin::Type(&TYPE_TIMESTAMPTZ),
1040        Builtin::Type(&TYPE_TIMESTAMPTZ_ARRAY),
1041        Builtin::Type(&TYPE_UUID),
1042        Builtin::Type(&TYPE_UUID_ARRAY),
1043        Builtin::Type(&TYPE_VARCHAR),
1044        Builtin::Type(&TYPE_VARCHAR_ARRAY),
1045        Builtin::Type(&TYPE_INT2_VECTOR),
1046        Builtin::Type(&TYPE_INT2_VECTOR_ARRAY),
1047        Builtin::Type(&TYPE_ANYCOMPATIBLE),
1048        Builtin::Type(&TYPE_ANYCOMPATIBLEARRAY),
1049        Builtin::Type(&TYPE_ANYCOMPATIBLENONARRAY),
1050        Builtin::Type(&TYPE_ANYCOMPATIBLELIST),
1051        Builtin::Type(&TYPE_ANYCOMPATIBLEMAP),
1052        Builtin::Type(&TYPE_ANYCOMPATIBLERANGE),
1053        Builtin::Type(&TYPE_UINT2),
1054        Builtin::Type(&TYPE_UINT2_ARRAY),
1055        Builtin::Type(&TYPE_UINT4),
1056        Builtin::Type(&TYPE_UINT4_ARRAY),
1057        Builtin::Type(&TYPE_UINT8),
1058        Builtin::Type(&TYPE_UINT8_ARRAY),
1059        Builtin::Type(&TYPE_MZ_TIMESTAMP),
1060        Builtin::Type(&TYPE_MZ_TIMESTAMP_ARRAY),
1061        Builtin::Type(&TYPE_INT4_RANGE),
1062        Builtin::Type(&TYPE_INT4_RANGE_ARRAY),
1063        Builtin::Type(&TYPE_INT8_RANGE),
1064        Builtin::Type(&TYPE_INT8_RANGE_ARRAY),
1065        Builtin::Type(&TYPE_DATE_RANGE),
1066        Builtin::Type(&TYPE_DATE_RANGE_ARRAY),
1067        Builtin::Type(&TYPE_NUM_RANGE),
1068        Builtin::Type(&TYPE_NUM_RANGE_ARRAY),
1069        Builtin::Type(&TYPE_TS_RANGE),
1070        Builtin::Type(&TYPE_TS_RANGE_ARRAY),
1071        Builtin::Type(&TYPE_TSTZ_RANGE),
1072        Builtin::Type(&TYPE_TSTZ_RANGE_ARRAY),
1073        Builtin::Type(&TYPE_MZ_ACL_ITEM),
1074        Builtin::Type(&TYPE_MZ_ACL_ITEM_ARRAY),
1075        Builtin::Type(&TYPE_ACL_ITEM),
1076        Builtin::Type(&TYPE_ACL_ITEM_ARRAY),
1077        Builtin::Type(&TYPE_INTERNAL),
1078    ];
1079
1080    let mut builtin_funcs = Vec::new();
1081    for (schema, funcs) in &[
1082        (PG_CATALOG_SCHEMA, &*mz_sql::func::PG_CATALOG_BUILTINS),
1083        (
1084            INFORMATION_SCHEMA,
1085            &*mz_sql::func::INFORMATION_SCHEMA_BUILTINS,
1086        ),
1087        (MZ_CATALOG_SCHEMA, &*mz_sql::func::MZ_CATALOG_BUILTINS),
1088        (MZ_INTERNAL_SCHEMA, &*mz_sql::func::MZ_INTERNAL_BUILTINS),
1089        (MZ_UNSAFE_SCHEMA, &*mz_sql::func::MZ_UNSAFE_BUILTINS),
1090    ] {
1091        for (name, func) in funcs.iter() {
1092            builtin_funcs.push(Builtin::Func(BuiltinFunc {
1093                name,
1094                schema,
1095                inner: func,
1096            }));
1097        }
1098    }
1099
1100    let mut builtin_items = vec![
1101        Builtin::Source(&MZ_CATALOG_RAW),
1102        Builtin::Log(&MZ_ARRANGEMENT_SHARING_RAW),
1103        Builtin::Log(&MZ_ARRANGEMENT_BATCHES_RAW),
1104        Builtin::Log(&MZ_ARRANGEMENT_RECORDS_RAW),
1105        Builtin::Log(&MZ_ARRANGEMENT_BATCHER_RECORDS_RAW),
1106        Builtin::Log(&MZ_ARRANGEMENT_BATCHER_SIZE_RAW),
1107        Builtin::Log(&MZ_ARRANGEMENT_BATCHER_CAPACITY_RAW),
1108        Builtin::Log(&MZ_ARRANGEMENT_BATCHER_ALLOCATIONS_RAW),
1109        Builtin::Log(&MZ_DATAFLOW_CHANNELS_PER_WORKER),
1110        Builtin::Log(&MZ_DATAFLOW_OPERATORS_PER_WORKER),
1111        Builtin::Log(&MZ_DATAFLOW_ADDRESSES_PER_WORKER),
1112        Builtin::Log(&MZ_DATAFLOW_OPERATOR_REACHABILITY_RAW),
1113        Builtin::Log(&MZ_COMPUTE_EXPORTS_PER_WORKER),
1114        Builtin::Log(&MZ_COMPUTE_DATAFLOW_GLOBAL_IDS_PER_WORKER),
1115        Builtin::Log(&MZ_CLUSTER_PROMETHEUS_METRICS),
1116        Builtin::Log(&MZ_MESSAGE_COUNTS_RECEIVED_RAW),
1117        Builtin::Log(&MZ_MESSAGE_COUNTS_SENT_RAW),
1118        Builtin::Log(&MZ_MESSAGE_BATCH_COUNTS_RECEIVED_RAW),
1119        Builtin::Log(&MZ_MESSAGE_BATCH_COUNTS_SENT_RAW),
1120        Builtin::Log(&MZ_ACTIVE_PEEKS_PER_WORKER),
1121        Builtin::Log(&MZ_PEEK_DURATIONS_HISTOGRAM_RAW),
1122        Builtin::Log(&MZ_ARRANGEMENT_HEAP_CAPACITY_RAW),
1123        Builtin::Log(&MZ_ARRANGEMENT_HEAP_ALLOCATIONS_RAW),
1124        Builtin::Log(&MZ_ARRANGEMENT_HEAP_SIZE_RAW),
1125        Builtin::Log(&MZ_SCHEDULING_ELAPSED_RAW),
1126        Builtin::Log(&MZ_COMPUTE_OPERATOR_DURATIONS_HISTOGRAM_RAW),
1127        Builtin::Log(&MZ_SCHEDULING_PARKS_HISTOGRAM_RAW),
1128        Builtin::Log(&MZ_COMPUTE_FRONTIERS_PER_WORKER),
1129        Builtin::Log(&MZ_COMPUTE_IMPORT_FRONTIERS_PER_WORKER),
1130        Builtin::Log(&MZ_COMPUTE_ERROR_COUNTS_RAW),
1131        Builtin::Log(&MZ_COMPUTE_HYDRATION_TIMES_PER_WORKER),
1132        Builtin::Log(&MZ_COMPUTE_OPERATOR_HYDRATION_STATUSES_PER_WORKER),
1133        Builtin::Table(&MZ_KAFKA_SINKS),
1134        Builtin::Table(&MZ_KAFKA_CONNECTIONS),
1135        Builtin::Table(&MZ_KAFKA_SOURCES),
1136        Builtin::Table(&MZ_OBJECT_DEPENDENCIES),
1137        Builtin::Table(&MZ_ICEBERG_SINKS),
1138        Builtin::MaterializedView(&MZ_DATABASES),
1139        Builtin::MaterializedView(&MZ_SCHEMAS),
1140        Builtin::Table(&MZ_COLUMNS),
1141        Builtin::Table(&MZ_INDEXES),
1142        Builtin::Table(&MZ_INDEX_COLUMNS),
1143        Builtin::Table(&MZ_TABLES),
1144        Builtin::Table(&MZ_SOURCES),
1145        Builtin::Table(&MZ_SOURCE_REFERENCES),
1146        Builtin::Table(&MZ_POSTGRES_SOURCES),
1147        Builtin::Table(&MZ_POSTGRES_SOURCE_TABLES),
1148        Builtin::Table(&MZ_MYSQL_SOURCE_TABLES),
1149        Builtin::Table(&MZ_SQL_SERVER_SOURCE_TABLES),
1150        Builtin::Table(&MZ_KAFKA_SOURCE_TABLES),
1151        Builtin::Table(&MZ_SINKS),
1152        Builtin::Table(&MZ_VIEWS),
1153        Builtin::Table(&MZ_TYPES),
1154        Builtin::Table(&MZ_TYPE_PG_METADATA),
1155        Builtin::Table(&MZ_ARRAY_TYPES),
1156        Builtin::Table(&MZ_BASE_TYPES),
1157        Builtin::Table(&MZ_LIST_TYPES),
1158        Builtin::Table(&MZ_MAP_TYPES),
1159        Builtin::Table(&MZ_ROLES),
1160        Builtin::Table(&MZ_ROLE_AUTH),
1161        Builtin::MaterializedView(&MZ_ROLE_MEMBERS),
1162        Builtin::Table(&MZ_ROLE_PARAMETERS),
1163        Builtin::Table(&MZ_PSEUDO_TYPES),
1164        Builtin::Table(&MZ_FUNCTIONS),
1165        Builtin::Table(&MZ_OPERATORS),
1166        Builtin::Table(&MZ_AGGREGATES),
1167        Builtin::Table(&MZ_CLUSTERS),
1168        Builtin::MaterializedView(&MZ_CLUSTER_WORKLOAD_CLASSES),
1169        Builtin::Table(&MZ_CLUSTER_SCHEDULES),
1170        Builtin::MaterializedView(&MZ_SECRETS),
1171        Builtin::MaterializedView(&MZ_CONNECTIONS),
1172        Builtin::Table(&MZ_SSH_TUNNEL_CONNECTIONS),
1173        Builtin::Table(&MZ_CLUSTER_REPLICAS),
1174        Builtin::Source(&MZ_CLUSTER_REPLICA_METRICS_HISTORY),
1175        Builtin::View(&MZ_CLUSTER_REPLICA_METRICS),
1176        Builtin::Table(&MZ_CLUSTER_REPLICA_SIZES),
1177        Builtin::Source(&MZ_CLUSTER_REPLICA_STATUS_HISTORY),
1178        Builtin::View(&MZ_CLUSTER_REPLICA_STATUSES),
1179        Builtin::MaterializedView(&MZ_INTERNAL_CLUSTER_REPLICAS),
1180        Builtin::MaterializedView(&MZ_PENDING_CLUSTER_REPLICAS),
1181        Builtin::Table(&MZ_AUDIT_EVENTS),
1182        Builtin::Table(&MZ_STORAGE_USAGE_BY_SHARD),
1183        Builtin::Table(&MZ_EGRESS_IPS),
1184        Builtin::Table(&MZ_AWS_PRIVATELINK_CONNECTIONS),
1185        Builtin::Table(&MZ_AWS_CONNECTIONS),
1186        Builtin::Table(&MZ_SUBSCRIPTIONS),
1187        Builtin::Table(&MZ_SESSIONS),
1188        Builtin::Table(&MZ_DEFAULT_PRIVILEGES),
1189        Builtin::Table(&MZ_SYSTEM_PRIVILEGES),
1190        Builtin::Table(&MZ_COMMENTS),
1191        Builtin::Table(&MZ_WEBHOOKS_SOURCES),
1192        Builtin::Table(&MZ_HISTORY_RETENTION_STRATEGIES),
1193        Builtin::MaterializedView(&MZ_MATERIALIZED_VIEWS),
1194        Builtin::Table(&MZ_MATERIALIZED_VIEW_REFRESH_STRATEGIES),
1195        Builtin::MaterializedView(&MZ_NETWORK_POLICIES),
1196        Builtin::MaterializedView(&MZ_NETWORK_POLICY_RULES),
1197        Builtin::Table(&MZ_LICENSE_KEYS),
1198        Builtin::Table(&MZ_REPLACEMENTS),
1199        Builtin::View(&MZ_RELATIONS),
1200        Builtin::View(&MZ_OBJECT_OID_ALIAS),
1201        Builtin::View(&MZ_OBJECTS),
1202        Builtin::View(&MZ_OBJECT_FULLY_QUALIFIED_NAMES),
1203        Builtin::View(&MZ_OBJECTS_ID_NAMESPACE_TYPES),
1204        Builtin::View(&MZ_OBJECT_HISTORY),
1205        Builtin::View(&MZ_OBJECT_LIFETIMES),
1206        Builtin::Table(&MZ_OBJECT_GLOBAL_IDS),
1207        Builtin::View(&MZ_ARRANGEMENT_SHARING_PER_WORKER),
1208        Builtin::View(&MZ_ARRANGEMENT_SHARING),
1209        Builtin::View(&MZ_ARRANGEMENT_SIZES_PER_WORKER),
1210        Builtin::View(&MZ_ARRANGEMENT_SIZES),
1211        Builtin::View(&MZ_DATAFLOWS_PER_WORKER),
1212        Builtin::View(&MZ_DATAFLOWS),
1213        Builtin::View(&MZ_DATAFLOW_ADDRESSES),
1214        Builtin::View(&MZ_DATAFLOW_CHANNELS),
1215        Builtin::View(&MZ_DATAFLOW_OPERATORS),
1216        Builtin::View(&MZ_DATAFLOW_GLOBAL_IDS),
1217        Builtin::View(&MZ_COMPUTE_EXPORTS),
1218        Builtin::View(&MZ_MAPPABLE_OBJECTS),
1219        Builtin::View(&MZ_DATAFLOW_OPERATOR_DATAFLOWS_PER_WORKER),
1220        Builtin::View(&MZ_DATAFLOW_OPERATOR_DATAFLOWS),
1221        Builtin::View(&MZ_OBJECT_TRANSITIVE_DEPENDENCIES),
1222        Builtin::View(&MZ_DATAFLOW_OPERATOR_REACHABILITY_PER_WORKER),
1223        Builtin::View(&MZ_DATAFLOW_OPERATOR_REACHABILITY),
1224        Builtin::View(&MZ_CLUSTER_REPLICA_UTILIZATION),
1225        Builtin::View(&MZ_CLUSTER_REPLICA_UTILIZATION_HISTORY),
1226        Builtin::View(&MZ_DATAFLOW_OPERATOR_PARENTS_PER_WORKER),
1227        Builtin::View(&MZ_DATAFLOW_OPERATOR_PARENTS),
1228        Builtin::View(&MZ_DATAFLOW_ARRANGEMENT_SIZES),
1229        Builtin::View(&MZ_EXPECTED_GROUP_SIZE_ADVICE),
1230        Builtin::View(&MZ_COMPUTE_FRONTIERS),
1231        Builtin::View(&MZ_DATAFLOW_CHANNEL_OPERATORS_PER_WORKER),
1232        Builtin::View(&MZ_DATAFLOW_CHANNEL_OPERATORS),
1233        Builtin::View(&MZ_COMPUTE_IMPORT_FRONTIERS),
1234        Builtin::View(&MZ_MESSAGE_COUNTS_PER_WORKER),
1235        Builtin::View(&MZ_MESSAGE_COUNTS),
1236        Builtin::View(&MZ_ACTIVE_PEEKS),
1237        Builtin::View(&MZ_COMPUTE_OPERATOR_DURATIONS_HISTOGRAM_PER_WORKER),
1238        Builtin::View(&MZ_COMPUTE_OPERATOR_DURATIONS_HISTOGRAM),
1239        Builtin::View(&MZ_RECORDS_PER_DATAFLOW_OPERATOR_PER_WORKER),
1240        Builtin::View(&MZ_RECORDS_PER_DATAFLOW_OPERATOR),
1241        Builtin::View(&MZ_RECORDS_PER_DATAFLOW_PER_WORKER),
1242        Builtin::View(&MZ_RECORDS_PER_DATAFLOW),
1243        Builtin::View(&MZ_PEEK_DURATIONS_HISTOGRAM_PER_WORKER),
1244        Builtin::View(&MZ_PEEK_DURATIONS_HISTOGRAM),
1245        Builtin::View(&MZ_SCHEDULING_ELAPSED_PER_WORKER),
1246        Builtin::View(&MZ_SCHEDULING_ELAPSED),
1247        Builtin::View(&MZ_SCHEDULING_PARKS_HISTOGRAM_PER_WORKER),
1248        Builtin::View(&MZ_SCHEDULING_PARKS_HISTOGRAM),
1249        Builtin::View(&MZ_SHOW_ALL_OBJECTS),
1250        Builtin::View(&MZ_SHOW_COLUMNS),
1251        Builtin::View(&MZ_SHOW_CLUSTERS),
1252        Builtin::View(&MZ_SHOW_SECRETS),
1253        Builtin::View(&MZ_SHOW_DATABASES),
1254        Builtin::View(&MZ_SHOW_SCHEMAS),
1255        Builtin::View(&MZ_SHOW_TABLES),
1256        Builtin::View(&MZ_SHOW_VIEWS),
1257        Builtin::View(&MZ_SHOW_TYPES),
1258        Builtin::View(&MZ_SHOW_ROLES),
1259        Builtin::View(&MZ_SHOW_CONNECTIONS),
1260        Builtin::View(&MZ_SHOW_SOURCES),
1261        Builtin::View(&MZ_SHOW_SINKS),
1262        Builtin::View(&MZ_SHOW_MATERIALIZED_VIEWS),
1263        Builtin::View(&MZ_SHOW_INDEXES),
1264        Builtin::View(&MZ_CLUSTER_REPLICA_HISTORY),
1265        Builtin::View(&MZ_CLUSTER_REPLICA_NAME_HISTORY),
1266        Builtin::View(&MZ_TIMEZONE_NAMES),
1267        Builtin::View(&MZ_TIMEZONE_ABBREVIATIONS),
1268        Builtin::View(&PG_NAMESPACE_ALL_DATABASES),
1269        Builtin::Index(&PG_NAMESPACE_ALL_DATABASES_IND),
1270        Builtin::View(&PG_NAMESPACE),
1271        Builtin::View(&PG_CLASS_ALL_DATABASES),
1272        Builtin::Index(&PG_CLASS_ALL_DATABASES_IND),
1273        Builtin::View(&PG_CLASS),
1274        Builtin::View(&PG_DEPEND),
1275        Builtin::View(&PG_DATABASE),
1276        Builtin::View(&PG_INDEX),
1277        Builtin::View(&PG_TYPE_ALL_DATABASES),
1278        Builtin::Index(&PG_TYPE_ALL_DATABASES_IND),
1279        Builtin::View(&PG_TYPE),
1280        Builtin::View(&PG_DESCRIPTION_ALL_DATABASES),
1281        Builtin::Index(&PG_DESCRIPTION_ALL_DATABASES_IND),
1282        Builtin::View(&PG_DESCRIPTION),
1283        Builtin::View(&PG_ATTRIBUTE_ALL_DATABASES),
1284        Builtin::Index(&PG_ATTRIBUTE_ALL_DATABASES_IND),
1285        Builtin::View(&PG_ATTRIBUTE),
1286        Builtin::View(&PG_PROC),
1287        Builtin::View(&PG_OPERATOR),
1288        Builtin::View(&PG_RANGE),
1289        Builtin::View(&PG_ENUM),
1290        Builtin::View(&PG_ATTRDEF_ALL_DATABASES),
1291        Builtin::Index(&PG_ATTRDEF_ALL_DATABASES_IND),
1292        Builtin::View(&PG_ATTRDEF),
1293        Builtin::View(&PG_SETTINGS),
1294        Builtin::View(&PG_AUTH_MEMBERS),
1295        Builtin::View(&PG_CONSTRAINT),
1296        Builtin::View(&PG_TABLES),
1297        Builtin::View(&PG_TABLESPACE),
1298        Builtin::View(&PG_ACCESS_METHODS),
1299        Builtin::View(&PG_LOCKS),
1300        Builtin::View(&PG_AUTHID_CORE),
1301        Builtin::Index(&PG_AUTHID_CORE_IND),
1302        Builtin::View(&PG_AUTHID),
1303        Builtin::View(&PG_ROLES),
1304        Builtin::View(&PG_USER),
1305        Builtin::View(&PG_VIEWS),
1306        Builtin::View(&PG_MATVIEWS),
1307        Builtin::View(&PG_COLLATION),
1308        Builtin::View(&PG_POLICY),
1309        Builtin::View(&PG_INHERITS),
1310        Builtin::View(&PG_AGGREGATE),
1311        Builtin::View(&PG_TRIGGER),
1312        Builtin::View(&PG_REWRITE),
1313        Builtin::View(&PG_EXTENSION),
1314        Builtin::View(&PG_EVENT_TRIGGER),
1315        Builtin::View(&PG_LANGUAGE),
1316        Builtin::View(&PG_SHDESCRIPTION),
1317        Builtin::View(&PG_INDEXES),
1318        Builtin::View(&PG_TIMEZONE_ABBREVS),
1319        Builtin::View(&PG_TIMEZONE_NAMES),
1320        Builtin::View(&INFORMATION_SCHEMA_APPLICABLE_ROLES),
1321        Builtin::View(&INFORMATION_SCHEMA_COLUMNS),
1322        Builtin::View(&INFORMATION_SCHEMA_ENABLED_ROLES),
1323        Builtin::View(&INFORMATION_SCHEMA_KEY_COLUMN_USAGE),
1324        Builtin::View(&INFORMATION_SCHEMA_REFERENTIAL_CONSTRAINTS),
1325        Builtin::View(&INFORMATION_SCHEMA_ROUTINES),
1326        Builtin::View(&INFORMATION_SCHEMA_SCHEMATA),
1327        Builtin::View(&INFORMATION_SCHEMA_TABLES),
1328        Builtin::View(&INFORMATION_SCHEMA_TABLE_CONSTRAINTS),
1329        Builtin::View(&INFORMATION_SCHEMA_TABLE_PRIVILEGES),
1330        Builtin::View(&INFORMATION_SCHEMA_ROLE_TABLE_GRANTS),
1331        Builtin::View(&INFORMATION_SCHEMA_TRIGGERS),
1332        Builtin::View(&INFORMATION_SCHEMA_VIEWS),
1333        Builtin::View(&INFORMATION_SCHEMA_CHARACTER_SETS),
1334        Builtin::View(&MZ_SHOW_ROLE_MEMBERS),
1335        Builtin::View(&MZ_SHOW_MY_ROLE_MEMBERS),
1336        Builtin::View(&MZ_SHOW_SYSTEM_PRIVILEGES),
1337        Builtin::View(&MZ_SHOW_MY_SYSTEM_PRIVILEGES),
1338        Builtin::View(&MZ_SHOW_CLUSTER_PRIVILEGES),
1339        Builtin::View(&MZ_SHOW_MY_CLUSTER_PRIVILEGES),
1340        Builtin::View(&MZ_SHOW_DATABASE_PRIVILEGES),
1341        Builtin::View(&MZ_SHOW_MY_DATABASE_PRIVILEGES),
1342        Builtin::View(&MZ_SHOW_SCHEMA_PRIVILEGES),
1343        Builtin::View(&MZ_SHOW_MY_SCHEMA_PRIVILEGES),
1344        Builtin::View(&MZ_SHOW_OBJECT_PRIVILEGES),
1345        Builtin::View(&MZ_SHOW_MY_OBJECT_PRIVILEGES),
1346        Builtin::View(&MZ_SHOW_ALL_PRIVILEGES),
1347        Builtin::View(&MZ_SHOW_ALL_MY_PRIVILEGES),
1348        Builtin::View(&MZ_SHOW_DEFAULT_PRIVILEGES),
1349        Builtin::View(&MZ_SHOW_MY_DEFAULT_PRIVILEGES),
1350        Builtin::Source(&MZ_SINK_STATUS_HISTORY),
1351        Builtin::View(&MZ_SINK_STATUSES),
1352        Builtin::Source(&MZ_SOURCE_STATUS_HISTORY),
1353        Builtin::Source(&MZ_AWS_PRIVATELINK_CONNECTION_STATUS_HISTORY),
1354        Builtin::View(&MZ_AWS_PRIVATELINK_CONNECTION_STATUSES),
1355        Builtin::Source(&MZ_STATEMENT_EXECUTION_HISTORY),
1356        Builtin::View(&MZ_STATEMENT_EXECUTION_HISTORY_REDACTED),
1357        Builtin::Source(&MZ_PREPARED_STATEMENT_HISTORY),
1358        Builtin::Source(&MZ_SESSION_HISTORY),
1359        Builtin::Source(&MZ_SQL_TEXT),
1360        Builtin::View(&MZ_SQL_TEXT_REDACTED),
1361        Builtin::View(&MZ_RECENT_SQL_TEXT),
1362        Builtin::View(&MZ_RECENT_SQL_TEXT_REDACTED),
1363        Builtin::Index(&MZ_RECENT_SQL_TEXT_IND),
1364        Builtin::View(&MZ_ACTIVITY_LOG_THINNED),
1365        Builtin::View(&MZ_RECENT_ACTIVITY_LOG_THINNED),
1366        Builtin::View(&MZ_RECENT_ACTIVITY_LOG),
1367        Builtin::View(&MZ_RECENT_ACTIVITY_LOG_REDACTED),
1368        Builtin::Index(&MZ_RECENT_ACTIVITY_LOG_THINNED_IND),
1369        Builtin::View(&MZ_SOURCE_STATUSES),
1370        Builtin::Source(&MZ_STATEMENT_LIFECYCLE_HISTORY),
1371        Builtin::Source(&MZ_STORAGE_SHARDS),
1372        Builtin::Source(&MZ_SOURCE_STATISTICS_RAW),
1373        Builtin::Source(&MZ_SINK_STATISTICS_RAW),
1374        Builtin::View(&MZ_SOURCE_STATISTICS_WITH_HISTORY),
1375        Builtin::Index(&MZ_SOURCE_STATISTICS_WITH_HISTORY_IND),
1376        Builtin::View(&MZ_SOURCE_STATISTICS),
1377        Builtin::Index(&MZ_SOURCE_STATISTICS_IND),
1378        Builtin::View(&MZ_SINK_STATISTICS),
1379        Builtin::Index(&MZ_SINK_STATISTICS_IND),
1380        Builtin::View(&MZ_STORAGE_USAGE),
1381        Builtin::Source(&MZ_FRONTIERS),
1382        Builtin::View(&MZ_GLOBAL_FRONTIERS),
1383        Builtin::Source(&MZ_WALLCLOCK_LAG_HISTORY),
1384        Builtin::View(&MZ_WALLCLOCK_GLOBAL_LAG_HISTORY),
1385        Builtin::View(&MZ_WALLCLOCK_GLOBAL_LAG_RECENT_HISTORY),
1386        Builtin::View(&MZ_WALLCLOCK_GLOBAL_LAG),
1387        Builtin::Source(&MZ_WALLCLOCK_GLOBAL_LAG_HISTOGRAM_RAW),
1388        Builtin::View(&MZ_WALLCLOCK_GLOBAL_LAG_HISTOGRAM),
1389        Builtin::Source(&MZ_MATERIALIZED_VIEW_REFRESHES),
1390        Builtin::Source(&MZ_COMPUTE_DEPENDENCIES),
1391        Builtin::View(&MZ_MATERIALIZATION_DEPENDENCIES),
1392        Builtin::View(&MZ_MATERIALIZATION_LAG),
1393        Builtin::View(&MZ_CONSOLE_CLUSTER_UTILIZATION_OVERVIEW),
1394        Builtin::View(&MZ_COMPUTE_ERROR_COUNTS_PER_WORKER),
1395        Builtin::View(&MZ_COMPUTE_ERROR_COUNTS),
1396        Builtin::Source(&MZ_COMPUTE_ERROR_COUNTS_RAW_UNIFIED),
1397        Builtin::Source(&MZ_COMPUTE_HYDRATION_TIMES),
1398        Builtin::Source(&MZ_OBJECT_ARRANGEMENT_SIZES_UNIFIED),
1399        Builtin::Index(&MZ_OBJECT_ARRANGEMENT_SIZES_IND),
1400        Builtin::Table(&MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY),
1401        Builtin::Index(&MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY_OBJECT_IND),
1402        Builtin::Index(&MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY_TS_IND),
1403        Builtin::Log(&MZ_COMPUTE_LIR_MAPPING_PER_WORKER),
1404        Builtin::View(&MZ_LIR_MAPPING),
1405        Builtin::Source(&MZ_COMPUTE_OPERATOR_HYDRATION_STATUSES),
1406        Builtin::Source(&MZ_CLUSTER_REPLICA_FRONTIERS),
1407        Builtin::View(&MZ_COMPUTE_HYDRATION_STATUSES),
1408        Builtin::View(&MZ_HYDRATION_STATUSES),
1409        Builtin::Index(&MZ_HYDRATION_STATUSES_IND),
1410        Builtin::View(&MZ_SHOW_CLUSTER_REPLICAS),
1411        Builtin::View(&MZ_SHOW_NETWORK_POLICIES),
1412        Builtin::View(&MZ_CLUSTER_DEPLOYMENT_LINEAGE),
1413        Builtin::Index(&MZ_SHOW_DATABASES_IND),
1414        Builtin::Index(&MZ_SHOW_SCHEMAS_IND),
1415        Builtin::Index(&MZ_SHOW_CONNECTIONS_IND),
1416        Builtin::Index(&MZ_SHOW_TABLES_IND),
1417        Builtin::Index(&MZ_SHOW_SOURCES_IND),
1418        Builtin::Index(&MZ_SHOW_VIEWS_IND),
1419        Builtin::Index(&MZ_SHOW_MATERIALIZED_VIEWS_IND),
1420        Builtin::Index(&MZ_SHOW_SINKS_IND),
1421        Builtin::Index(&MZ_SHOW_TYPES_IND),
1422        Builtin::Index(&MZ_SHOW_ALL_OBJECTS_IND),
1423        Builtin::Index(&MZ_SHOW_INDEXES_IND),
1424        Builtin::Index(&MZ_SHOW_COLUMNS_IND),
1425        Builtin::Index(&MZ_SHOW_CLUSTERS_IND),
1426        Builtin::Index(&MZ_SHOW_CLUSTER_REPLICAS_IND),
1427        Builtin::Index(&MZ_SHOW_SECRETS_IND),
1428        Builtin::Index(&MZ_SHOW_ROLES_IND),
1429        Builtin::Index(&MZ_CLUSTERS_IND),
1430        Builtin::Index(&MZ_INDEXES_IND),
1431        Builtin::Index(&MZ_ROLES_IND),
1432        Builtin::Index(&MZ_SOURCES_IND),
1433        Builtin::Index(&MZ_SINKS_IND),
1434        Builtin::Index(&MZ_MATERIALIZED_VIEWS_IND),
1435        Builtin::Index(&MZ_SOURCE_STATUSES_IND),
1436        Builtin::Index(&MZ_SOURCE_STATUS_HISTORY_IND),
1437        Builtin::Index(&MZ_SINK_STATUSES_IND),
1438        Builtin::Index(&MZ_SINK_STATUS_HISTORY_IND),
1439        Builtin::Index(&MZ_CLUSTER_REPLICAS_IND),
1440        Builtin::Index(&MZ_CLUSTER_REPLICA_SIZES_IND),
1441        Builtin::Index(&MZ_CLUSTER_REPLICA_STATUSES_IND),
1442        Builtin::Index(&MZ_CLUSTER_REPLICA_STATUS_HISTORY_IND),
1443        Builtin::Index(&MZ_CLUSTER_REPLICA_METRICS_IND),
1444        Builtin::Index(&MZ_CLUSTER_REPLICA_METRICS_HISTORY_IND),
1445        Builtin::Index(&MZ_CLUSTER_REPLICA_HISTORY_IND),
1446        Builtin::Index(&MZ_CLUSTER_REPLICA_NAME_HISTORY_IND),
1447        Builtin::Index(&MZ_OBJECT_LIFETIMES_IND),
1448        Builtin::Index(&MZ_OBJECT_HISTORY_IND),
1449        Builtin::Index(&MZ_OBJECT_DEPENDENCIES_IND),
1450        Builtin::Index(&MZ_COMPUTE_DEPENDENCIES_IND),
1451        Builtin::Index(&MZ_OBJECT_TRANSITIVE_DEPENDENCIES_IND),
1452        Builtin::Index(&MZ_FRONTIERS_IND),
1453        Builtin::Index(&MZ_WALLCLOCK_GLOBAL_LAG_RECENT_HISTORY_IND),
1454        Builtin::Index(&MZ_KAFKA_SOURCES_IND),
1455        Builtin::Index(&MZ_WEBHOOK_SOURCES_IND),
1456        Builtin::Index(&MZ_COMMENTS_IND),
1457        Builtin::Index(&MZ_DATABASES_IND),
1458        Builtin::Index(&MZ_SCHEMAS_IND),
1459        Builtin::Index(&MZ_CONNECTIONS_IND),
1460        Builtin::Index(&MZ_TABLES_IND),
1461        Builtin::Index(&MZ_TYPES_IND),
1462        Builtin::Index(&MZ_OBJECTS_IND),
1463        Builtin::Index(&MZ_COLUMNS_IND),
1464        Builtin::Index(&MZ_SECRETS_IND),
1465        Builtin::Index(&MZ_VIEWS_IND),
1466        Builtin::Index(&MZ_CONSOLE_CLUSTER_UTILIZATION_OVERVIEW_IND),
1467        Builtin::Index(&MZ_CLUSTER_DEPLOYMENT_LINEAGE_IND),
1468        Builtin::Index(&MZ_CLUSTER_REPLICA_FRONTIERS_IND),
1469        Builtin::Index(&MZ_COMPUTE_HYDRATION_TIMES_IND),
1470        Builtin::View(&MZ_RECENT_STORAGE_USAGE),
1471        Builtin::Index(&MZ_RECENT_STORAGE_USAGE_IND),
1472        Builtin::Connection(&MZ_ANALYTICS),
1473        Builtin::View(&MZ_INDEX_ADVICE),
1474        Builtin::View(&MZ_MCP_DATA_PRODUCTS),
1475        Builtin::View(&MZ_MCP_DATA_PRODUCT_DETAILS),
1476    ];
1477
1478    builtin_items.extend(notice::builtins());
1479
1480    // Generate ontology views by enumerating existing builtins.
1481    builtin_items.extend(ontology::generate_views(&builtin_items));
1482
1483    // Generate builtin relations reporting builtin objects last, since they need a complete view
1484    // of all other builtins.
1485    let mut builtin_builtins = builtin::builtins(&builtin_items).collect();
1486
1487    // Construct the full list of builtins, retaining dependency order.
1488    let mut builtins = Vec::new();
1489    builtins.append(&mut builtin_types);
1490    builtins.append(&mut builtin_funcs);
1491    builtins.append(&mut builtin_builtins);
1492    builtins.append(&mut builtin_items);
1493
1494    builtins
1495});
1496pub const BUILTIN_ROLES: &[&BuiltinRole] = &[
1497    &MZ_SYSTEM_ROLE,
1498    &MZ_SUPPORT_ROLE,
1499    &MZ_ANALYTICS_ROLE,
1500    &MZ_MONITOR_ROLE,
1501    &MZ_MONITOR_REDACTED,
1502    &MZ_JWT_SYNC_ROLE,
1503];
1504pub const BUILTIN_CLUSTERS: &[&BuiltinCluster] = &[
1505    &MZ_SYSTEM_CLUSTER,
1506    &MZ_CATALOG_SERVER_CLUSTER,
1507    &MZ_PROBE_CLUSTER,
1508    &MZ_SUPPORT_CLUSTER,
1509    &MZ_ANALYTICS_CLUSTER,
1510];
1511pub const BUILTIN_CLUSTER_REPLICAS: &[&BuiltinClusterReplica] = &[
1512    &MZ_SYSTEM_CLUSTER_REPLICA,
1513    &MZ_CATALOG_SERVER_CLUSTER_REPLICA,
1514    &MZ_PROBE_CLUSTER_REPLICA,
1515];
1516
1517#[allow(non_snake_case)]
1518pub mod BUILTINS {
1519    use super::*;
1520
1521    pub fn logs() -> impl Iterator<Item = &'static BuiltinLog> {
1522        BUILTINS_STATIC.iter().filter_map(|b| match b {
1523            Builtin::Log(log) => Some(*log),
1524            _ => None,
1525        })
1526    }
1527
1528    pub fn types() -> impl Iterator<Item = &'static BuiltinType<NameReference>> {
1529        BUILTINS_STATIC.iter().filter_map(|b| match b {
1530            Builtin::Type(typ) => Some(*typ),
1531            _ => None,
1532        })
1533    }
1534
1535    pub fn views() -> impl Iterator<Item = &'static BuiltinView> {
1536        BUILTINS_STATIC.iter().filter_map(|b| match b {
1537            Builtin::View(view) => Some(*view),
1538            _ => None,
1539        })
1540    }
1541
1542    pub fn materialized_views() -> impl Iterator<Item = &'static BuiltinMaterializedView> {
1543        BUILTINS_STATIC.iter().filter_map(|b| match b {
1544            Builtin::MaterializedView(mv) => Some(*mv),
1545            _ => None,
1546        })
1547    }
1548
1549    pub fn funcs() -> impl Iterator<Item = &'static BuiltinFunc> {
1550        BUILTINS_STATIC.iter().filter_map(|b| match b {
1551            Builtin::Func(func) => Some(func),
1552            _ => None,
1553        })
1554    }
1555
1556    pub fn iter() -> impl Iterator<Item = &'static Builtin<NameReference>> {
1557        BUILTINS_STATIC.iter()
1558    }
1559}
1560
1561pub static BUILTIN_LOG_LOOKUP: LazyLock<HashMap<&'static str, &'static BuiltinLog>> =
1562    LazyLock::new(|| BUILTINS::logs().map(|log| (log.name, log)).collect());
1563/// Keys are builtin object description, values are the builtin index when sorted by dependency and
1564/// the builtin itself.
1565pub static BUILTIN_LOOKUP: LazyLock<
1566    HashMap<SystemObjectDescription, (usize, &'static Builtin<NameReference>)>,
1567> = LazyLock::new(|| {
1568    BUILTINS_STATIC
1569        .iter()
1570        .enumerate()
1571        .map(|(idx, builtin)| {
1572            (
1573                SystemObjectDescription {
1574                    schema_name: builtin.schema().to_string(),
1575                    object_type: builtin.catalog_item_type(),
1576                    object_name: builtin.name().to_string(),
1577                },
1578                (idx, builtin),
1579            )
1580        })
1581        .collect()
1582});
1583
1584#[cfg(test)]
1585mod tests {
1586    use std::collections::{BTreeMap, BTreeSet};
1587
1588    use mz_pgrepr::oid::FIRST_MATERIALIZE_OID;
1589    use mz_sql_parser::ast::visit::{self, Visit};
1590    use mz_sql_parser::ast::{Raw, RawItemName, UnresolvedItemName};
1591
1592    use super::*;
1593
1594    #[mz_ore::test]
1595    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `rust_psm_stack_pointer` on OS `linux`
1596    fn test_builtin_type_schema() {
1597        for typ in BUILTINS::types() {
1598            if typ.oid < FIRST_MATERIALIZE_OID {
1599                assert_eq!(
1600                    typ.schema, PG_CATALOG_SCHEMA,
1601                    "{typ:?} should be in {PG_CATALOG_SCHEMA} schema"
1602                );
1603            } else {
1604                // `mz_pgrepr::Type` resolution relies on all non-PG types existing in the
1605                // mz_catalog schema.
1606                assert_eq!(
1607                    typ.schema, MZ_CATALOG_SCHEMA,
1608                    "{typ:?} should be in {MZ_CATALOG_SCHEMA} schema"
1609                );
1610            }
1611        }
1612    }
1613
1614    /// Visitor that collects the last component of all referenced
1615    /// item names from a SQL AST.
1616    struct ItemNameCollector {
1617        names: BTreeSet<String>,
1618    }
1619
1620    impl<'ast> Visit<'ast, Raw> for ItemNameCollector {
1621        fn visit_item_name(&mut self, name: &'ast <Raw as mz_sql_parser::ast::AstInfo>::ItemName) {
1622            let unresolved: &UnresolvedItemName = match name {
1623                RawItemName::Name(n) | RawItemName::Id(_, n, _) => n,
1624            };
1625            let parts = &unresolved.0;
1626            if !parts.is_empty() {
1627                let obj_name = parts[parts.len() - 1].as_str().to_string();
1628                self.names.insert(obj_name);
1629            }
1630            visit::visit_item_name(self, name);
1631        }
1632    }
1633
1634    /// Tests that `BUILTINS_STATIC` is ordered respecting dependencies:
1635    /// if builtin A references builtin B in its SQL, then B must appear
1636    /// before A in the list. (This ordering is assumed by, e.g.,
1637    /// `sort_updates` during catalog migrations.)
1638    #[mz_ore::test]
1639    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `rust_psm_stack_pointer` on OS `linux`
1640    fn test_builtins_static_dependency_order() {
1641        // Build a map from name -> (schema, index) for all builtins.
1642        // We look up by just the name (last component) to catch
1643        // unqualified references in SQL.
1644        let mut builtin_by_name: BTreeMap<&str, (&str, usize)> = BTreeMap::new();
1645        let mut duplicate_names = Vec::new();
1646        for (idx, builtin) in BUILTINS_STATIC.iter().enumerate() {
1647            if let Some((prev_schema, prev_idx)) =
1648                builtin_by_name.insert(builtin.name(), (builtin.schema(), idx))
1649            {
1650                // Only flag duplicates across different schemas.
1651                // Same-schema duplicates (e.g., range types that
1652                // appear as both Type and Func) are fine because
1653                // they resolve to the same schema.
1654                if prev_schema != builtin.schema() {
1655                    duplicate_names.push(format!(
1656                        "name {:?} appears in both {}.{} (index \
1657                         {}) and {}.{} (index {})",
1658                        builtin.name(),
1659                        prev_schema,
1660                        builtin.name(),
1661                        prev_idx,
1662                        builtin.schema(),
1663                        builtin.name(),
1664                        idx,
1665                    ));
1666                }
1667            }
1668        }
1669        assert!(
1670            duplicate_names.is_empty(),
1671            "BUILTINS_STATIC has duplicate names across different \
1672             schemas (this test needs adjustment if such duplicates \
1673             are intentional):\n{}",
1674            duplicate_names.join("\n"),
1675        );
1676
1677        // Get the `CREATE ...` SQL for builtins that have it.
1678        let get_create_sql = |builtin: &Builtin<NameReference>| -> Option<String> {
1679            match builtin {
1680                Builtin::View(v) => Some(v.create_sql()),
1681                Builtin::MaterializedView(mv) => Some(mv.create_sql()),
1682                Builtin::Index(idx) => Some(idx.create_sql()),
1683                _ => None,
1684            }
1685        };
1686
1687        // For each SQL-bearing builtin, parse its SQL, walk the AST to
1688        // find referenced item names, and check that all referenced
1689        // builtins appear earlier in BUILTINS_STATIC.
1690        let mut violations = Vec::new();
1691        for (idx, builtin) in BUILTINS_STATIC.iter().enumerate() {
1692            let create_sql = match get_create_sql(builtin) {
1693                Some(sql) => sql,
1694                None => continue,
1695            };
1696
1697            let stmts = mz_sql_parser::parser::parse_statements(&create_sql).unwrap_or_else(|e| {
1698                panic!(
1699                    "failed to parse SQL for {}.{}: \
1700                         {e}\nSQL: {create_sql}",
1701                    builtin.schema(),
1702                    builtin.name(),
1703                )
1704            });
1705
1706            let mut collector = ItemNameCollector {
1707                names: BTreeSet::new(),
1708            };
1709            for stmt in &stmts {
1710                collector.visit_statement(&stmt.ast);
1711            }
1712
1713            for ref_name in &collector.names {
1714                if let Some(&(ref_schema, dep_idx)) = builtin_by_name.get(ref_name.as_str()) {
1715                    if dep_idx > idx {
1716                        violations.push(format!(
1717                            "{}.{} (index {}) references \
1718                             {}.{} (index {}), but the \
1719                             dependency appears later in \
1720                             BUILTINS_STATIC",
1721                            builtin.schema(),
1722                            builtin.name(),
1723                            idx,
1724                            ref_schema,
1725                            ref_name,
1726                            dep_idx,
1727                        ));
1728                    }
1729                }
1730            }
1731        }
1732
1733        assert!(
1734            violations.is_empty(),
1735            "BUILTINS_STATIC has dependency ordering violations:\n{}",
1736            violations.join("\n"),
1737        );
1738    }
1739
1740    /// Validates ontology metadata consistency:
1741    /// - Every link target references an entity that exists.
1742    /// - No duplicate entity names.
1743    /// - Every annotated builtin has a non-empty entity_name and description.
1744    #[mz_ore::test]
1745    #[cfg_attr(miri, ignore)]
1746    fn test_ontology_consistency() {
1747        // Collect all entity names from builtins with ontology annotations.
1748        let mut entity_names: BTreeSet<String> = BTreeSet::new();
1749        let mut duplicate_entities = Vec::new();
1750
1751        for builtin in BUILTINS_STATIC.iter() {
1752            let ontology = match builtin {
1753                Builtin::Table(t) => t.ontology.as_ref(),
1754                Builtin::View(v) => v.ontology.as_ref(),
1755                Builtin::MaterializedView(mv) => mv.ontology.as_ref(),
1756                Builtin::Source(s) => s.ontology.as_ref(),
1757                Builtin::Log(l) => l.ontology.as_ref(),
1758                _ => None,
1759            };
1760            if let Some(ont) = ontology {
1761                assert!(
1762                    !ont.entity_name.is_empty(),
1763                    "builtin {} has empty ontology entity_name",
1764                    builtin.name()
1765                );
1766                assert!(
1767                    !ont.description.is_empty(),
1768                    "builtin {} ({}) has empty ontology description",
1769                    builtin.name(),
1770                    ont.entity_name
1771                );
1772                if !entity_names.insert(ont.entity_name.to_string()) {
1773                    duplicate_entities.push(format!(
1774                        "duplicate entity_name {:?} on builtin {}",
1775                        ont.entity_name,
1776                        builtin.name()
1777                    ));
1778                }
1779            }
1780        }
1781        assert!(
1782            duplicate_entities.is_empty(),
1783            "ontology has duplicate entity names:\n{}",
1784            duplicate_entities.join("\n"),
1785        );
1786
1787        // Validate link targets reference existing entities.
1788        let mut bad_targets = Vec::new();
1789        for builtin in BUILTINS_STATIC.iter() {
1790            let ontology = match builtin {
1791                Builtin::Table(t) => t.ontology.as_ref(),
1792                Builtin::View(v) => v.ontology.as_ref(),
1793                Builtin::MaterializedView(mv) => mv.ontology.as_ref(),
1794                Builtin::Source(s) => s.ontology.as_ref(),
1795                Builtin::Log(l) => l.ontology.as_ref(),
1796                _ => None,
1797            };
1798            if let Some(ont) = ontology {
1799                for link in ont.links {
1800                    if !entity_names.contains(link.target) {
1801                        bad_targets.push(format!(
1802                            "entity {:?} link {:?} targets {:?} which is not a known entity",
1803                            ont.entity_name, link.name, link.target
1804                        ));
1805                    }
1806                }
1807            }
1808        }
1809        assert!(
1810            bad_targets.is_empty(),
1811            "ontology has links targeting unknown entities:\n{}",
1812            bad_targets.join("\n"),
1813        );
1814
1815        // Semantic type annotations are typed (SemanticType enum), so validity
1816        // is guaranteed at compile time — no runtime check needed.
1817
1818        // Validate that every "reference" column (one whose semantic type implies
1819        // a FK relationship) is covered by an OntologyLink on entities that
1820        // already have at least one FK-style link.
1821        //
1822        // Scope: only entities that have started FK annotation (at least one
1823        // link with a source_column). Entities with only union/maps_to links,
1824        // or no links at all, are not yet fully annotated and are skipped to
1825        // avoid noise.
1826        //
1827        // Exemptions:
1828        // - Column at index 0 named "id": almost always the entity's own PK,
1829        //   not a FK (e.g. mz_objects.id, mz_functions.id).
1830        // - Columns in the relation's declared key set.
1831        //
1832        // "Reference" types are ID types that imply a FK. Discriminators
1833        // (ObjectType, ConnectionType, SourceType), OID, and metric types
1834        // (ByteCount, etc.) are excluded.
1835        let reference_sem_types: BTreeSet<SemanticType> = BTreeSet::from([
1836            SemanticType::CatalogItemId,
1837            SemanticType::GlobalId,
1838            SemanticType::ClusterId,
1839            SemanticType::ReplicaId,
1840            SemanticType::SchemaId,
1841            SemanticType::DatabaseId,
1842            SemanticType::RoleId,
1843            SemanticType::NetworkPolicyId,
1844        ]);
1845
1846        let mut uncovered_fk_cols = Vec::new();
1847        for builtin in BUILTINS_STATIC.iter() {
1848            let desc_storage;
1849            let (name, desc, ontology): (&str, &RelationDesc, Option<&Ontology>) = match builtin {
1850                Builtin::Table(t) => (t.name, &t.desc, t.ontology.as_ref()),
1851                Builtin::View(v) => (v.name, &v.desc, v.ontology.as_ref()),
1852                Builtin::MaterializedView(mv) => (mv.name, &mv.desc, mv.ontology.as_ref()),
1853                Builtin::Source(s) => (s.name, &s.desc, s.ontology.as_ref()),
1854                Builtin::Log(l) => {
1855                    desc_storage = l.variant.desc();
1856                    (l.name, &desc_storage, l.ontology.as_ref())
1857                }
1858                _ => continue,
1859            };
1860            let Some(ont) = ontology else { continue };
1861
1862            // Collect all source_column values declared by existing links.
1863            let linked_cols: BTreeSet<&str> = ont
1864                .links
1865                .iter()
1866                .filter_map(|link| match &link.properties {
1867                    LinkProperties::ForeignKey { source_column, .. } => Some(*source_column),
1868                    LinkProperties::Measures { source_column, .. } => Some(*source_column),
1869                    LinkProperties::DependsOn { source_column, .. } => Some(*source_column),
1870                    LinkProperties::MapsTo { source_column, .. } => Some(*source_column),
1871                    LinkProperties::Union { .. } => None,
1872                })
1873                .collect();
1874
1875            // Skip entities that have no FK-style links yet — they are either
1876            // unannotated or use only union/maps_to links. Only enforce
1877            // coverage on entities that have started FK annotation.
1878            if linked_cols.is_empty() {
1879                continue;
1880            }
1881
1882            // Column indices that are part of the declared key set.
1883            let pk_indices: BTreeSet<usize> = desc.typ().keys.iter().flatten().copied().collect();
1884
1885            for (col_name, sem) in ont.column_semantic_types {
1886                if !reference_sem_types.contains(sem) {
1887                    continue;
1888                }
1889                let Some(idx) = desc.iter_names().position(|n| n.as_str() == *col_name) else {
1890                    continue;
1891                };
1892                // Exempt the entity's own primary identifier: column 0 named
1893                // "id" is by convention the entity's own PK (not a FK), even
1894                // when no explicit with_key() is declared on the relation.
1895                if idx == 0 && *col_name == "id" {
1896                    continue;
1897                }
1898                if pk_indices.contains(&idx) {
1899                    continue;
1900                }
1901                if linked_cols.contains(*col_name) {
1902                    continue;
1903                }
1904                uncovered_fk_cols.push(format!(
1905                    "entity {:?} (builtin {}) column {:?} has semantic type {:?} but no OntologyLink covers it (add a link with source_column: {:?})",
1906                    ont.entity_name, name, col_name, sem, col_name
1907                ));
1908            }
1909        }
1910        assert!(
1911            uncovered_fk_cols.is_empty(),
1912            "ontology entities have FK-typed columns with no OntologyLink:\n{}",
1913            uncovered_fk_cols.join("\n"),
1914        );
1915
1916        // Validate that every source_column in a link actually names a column
1917        // in the entity's RelationDesc. This catches stale annotations after
1918        // column renames or removals. With typed LinkProperties this is mostly
1919        // belt-and-suspenders since the type system enforces field presence, but
1920        // we still need to verify the string value matches a real column.
1921        let mut bad_source_cols = Vec::new();
1922        for builtin in BUILTINS_STATIC.iter() {
1923            let desc_storage;
1924            let (name, desc, ontology): (&str, &RelationDesc, Option<&Ontology>) = match builtin {
1925                Builtin::Table(t) => (t.name, &t.desc, t.ontology.as_ref()),
1926                Builtin::View(v) => (v.name, &v.desc, v.ontology.as_ref()),
1927                Builtin::MaterializedView(mv) => (mv.name, &mv.desc, mv.ontology.as_ref()),
1928                Builtin::Source(s) => (s.name, &s.desc, s.ontology.as_ref()),
1929                Builtin::Log(l) => {
1930                    desc_storage = l.variant.desc();
1931                    (l.name, &desc_storage, l.ontology.as_ref())
1932                }
1933                _ => continue,
1934            };
1935            let Some(ont) = ontology else { continue };
1936
1937            let col_names: BTreeSet<&str> = desc.iter_names().map(|c| c.as_str()).collect();
1938
1939            for link in ont.links {
1940                let source_col = match &link.properties {
1941                    LinkProperties::ForeignKey { source_column, .. } => Some(*source_column),
1942                    LinkProperties::Measures { source_column, .. } => Some(*source_column),
1943                    LinkProperties::DependsOn { source_column, .. } => Some(*source_column),
1944                    LinkProperties::MapsTo { source_column, .. } => Some(*source_column),
1945                    LinkProperties::Union { .. } => None,
1946                };
1947                let Some(col) = source_col else { continue };
1948                if !col_names.contains(col) {
1949                    bad_source_cols.push(format!(
1950                        "entity {:?} (builtin {}) link {:?} references source_column {:?} which does not exist in the relation",
1951                        ont.entity_name, name, link.name, col
1952                    ));
1953                }
1954                let extra_key_columns = match &link.properties {
1955                    LinkProperties::ForeignKey {
1956                        extra_key_columns: Some(extras),
1957                        ..
1958                    } => Some(*extras),
1959                    LinkProperties::Measures {
1960                        extra_key_columns: Some(extras),
1961                        ..
1962                    } => Some(*extras),
1963                    _ => None,
1964                };
1965                if let Some(extras) = extra_key_columns {
1966                    for (src_col, _) in extras {
1967                        if !col_names.contains(*src_col) {
1968                            bad_source_cols.push(format!(
1969                                "entity {:?} (builtin {}) link {:?} extra_key_columns references {:?} which does not exist in the relation",
1970                                ont.entity_name, name, link.name, src_col
1971                            ));
1972                        }
1973                    }
1974                }
1975            }
1976        }
1977        assert!(
1978            bad_source_cols.is_empty(),
1979            "ontology links reference non-existent source_columns:\n{}",
1980            bad_source_cols.join("\n"),
1981        );
1982
1983        // Sanity check: we have a reasonable number of annotated entities.
1984        assert!(
1985            entity_names.len() > 120,
1986            "expected > 120 ontology entities, found {}",
1987            entity_names.len()
1988        );
1989    }
1990
1991    /// Verify that `LinkProperties` serializes to the same JSON that the old
1992    /// hand-written `properties_json` strings contained. One representative
1993    /// case per constructor/variant is enough — the important thing is that
1994    /// field names, enum tag values, and skip-if-None/false behaviour are all
1995    /// correct.
1996    #[mz_ore::test]
1997    fn test_link_properties_serialization() {
1998        let check = |props: LinkProperties, expected: &str| {
1999            let got = serde_json::to_string(&props).expect("serialize");
2000            let got_val: serde_json::Value = serde_json::from_str(&got).expect("parse got");
2001            let exp_val: serde_json::Value =
2002                serde_json::from_str(expected).expect("parse expected");
2003            assert_eq!(got_val, exp_val, "mismatch for {expected}");
2004        };
2005
2006        // fk — basic, no optional fields
2007        check(
2008            LinkProperties::fk("owner_id", "id", Cardinality::ManyToOne),
2009            r#"{"kind":"foreign_key","source_column":"owner_id","target_column":"id","cardinality":"many_to_one"}"#,
2010        );
2011        // fk_composite — extra_key_columns present
2012        check(
2013            LinkProperties::fk_composite(
2014                "operator_id",
2015                "id",
2016                Cardinality::ManyToOne,
2017                &[("worker_id", "worker_id")],
2018            ),
2019            r#"{"kind":"foreign_key","source_column":"operator_id","target_column":"id","cardinality":"many_to_one","extra_key_columns":[["worker_id","worker_id"]]}"#,
2020        );
2021        // fk — one_to_one cardinality
2022        check(
2023            LinkProperties::fk("id", "id", Cardinality::OneToOne),
2024            r#"{"kind":"foreign_key","source_column":"id","target_column":"id","cardinality":"one_to_one"}"#,
2025        );
2026        // fk_nullable — nullable field present and true
2027        check(
2028            LinkProperties::fk_nullable("database_id", "id", Cardinality::ManyToOne),
2029            r#"{"kind":"foreign_key","source_column":"database_id","target_column":"id","cardinality":"many_to_one","nullable":true}"#,
2030        );
2031        // fk_typed — source_id_type present, requires_mapping absent
2032        check(
2033            LinkProperties::fk_typed(
2034                "replica_id",
2035                "id",
2036                Cardinality::ManyToOne,
2037                mz_repr::SemanticType::CatalogItemId,
2038            ),
2039            r#"{"kind":"foreign_key","source_column":"replica_id","target_column":"id","cardinality":"many_to_one","source_id_type":"CatalogItemId"}"#,
2040        );
2041        // fk_mapped — source_id_type + requires_mapping both present
2042        check(
2043            LinkProperties::fk_mapped(
2044                "object_id",
2045                "id",
2046                Cardinality::ManyToOne,
2047                mz_repr::SemanticType::GlobalId,
2048                "mz_internal.mz_object_global_ids",
2049            ),
2050            r#"{"kind":"foreign_key","source_column":"object_id","target_column":"id","cardinality":"many_to_one","source_id_type":"GlobalId","requires_mapping":"mz_internal.mz_object_global_ids"}"#,
2051        );
2052        // union_disc — discriminator fields present, note absent
2053        check(
2054            LinkProperties::union_disc("type", "table"),
2055            r#"{"kind":"union","discriminator_column":"type","discriminator_value":"table"}"#,
2056        );
2057        // Union — note only, discriminator absent
2058        check(
2059            LinkProperties::Union {
2060                discriminator_column: None,
2061                discriminator_value: None,
2062                note: Some("example note"),
2063            },
2064            r#"{"kind":"union","note":"example note"}"#,
2065        );
2066        // measures — basic
2067        check(
2068            LinkProperties::measures("id", "id", "cpu_time_ns"),
2069            r#"{"kind":"measures","source_column":"id","target_column":"id","metric":"cpu_time_ns"}"#,
2070        );
2071        // measures_composite — extra_key_columns present
2072        check(
2073            LinkProperties::measures_composite(
2074                "export_id",
2075                "export_id",
2076                "time_ns",
2077                &[("worker_id", "worker_id")],
2078            ),
2079            r#"{"kind":"measures","source_column":"export_id","target_column":"export_id","metric":"time_ns","extra_key_columns":[["worker_id","worker_id"]]}"#,
2080        );
2081        // measures_mapped — source_id_type + requires_mapping present
2082        check(
2083            LinkProperties::measures_mapped(
2084                "object_id",
2085                "id",
2086                "wallclock_lag",
2087                mz_repr::SemanticType::GlobalId,
2088                "mz_internal.mz_object_global_ids",
2089            ),
2090            r#"{"kind":"measures","source_column":"object_id","target_column":"id","metric":"wallclock_lag","source_id_type":"GlobalId","requires_mapping":"mz_internal.mz_object_global_ids"}"#,
2091        );
2092        // DependsOn — with mapping
2093        check(
2094            LinkProperties::DependsOn {
2095                source_column: "object_id",
2096                target_column: "id",
2097                source_id_type: Some(mz_repr::SemanticType::GlobalId),
2098                requires_mapping: Some("mz_internal.mz_object_global_ids"),
2099            },
2100            r#"{"kind":"depends_on","source_column":"object_id","target_column":"id","source_id_type":"GlobalId","requires_mapping":"mz_internal.mz_object_global_ids"}"#,
2101        );
2102        // DependsOn — direct (CatalogItemId, no mapping)
2103        check(
2104            LinkProperties::DependsOn {
2105                source_column: "object_id",
2106                target_column: "id",
2107                source_id_type: Some(mz_repr::SemanticType::CatalogItemId),
2108                requires_mapping: None,
2109            },
2110            r#"{"kind":"depends_on","source_column":"object_id","target_column":"id","source_id_type":"CatalogItemId"}"#,
2111        );
2112        // MapsTo — via + from_type + to_type
2113        check(
2114            LinkProperties::MapsTo {
2115                source_column: "id",
2116                target_column: "global_id",
2117                via: Some("mz_internal.mz_object_global_ids"),
2118                from_type: Some(mz_repr::SemanticType::CatalogItemId),
2119                to_type: Some(mz_repr::SemanticType::GlobalId),
2120                note: None,
2121            },
2122            r#"{"kind":"maps_to","source_column":"id","target_column":"global_id","via":"mz_internal.mz_object_global_ids","from_type":"CatalogItemId","to_type":"GlobalId"}"#,
2123        );
2124    }
2125}