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        // mz_sources is generated dynamically below with inlined builtin VALUES.
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 mz_sources with builtin source/log entries inlined as VALUES so
1481    // that its SQL fingerprint changes whenever a builtin source is added or
1482    // removed, forcing an explicit MigrationStep::replacement.
1483    //
1484    // Must happen BEFORE ontology::generate_views so that mz_sources's ontology
1485    // annotation (entity_name = "source") is visible to the ontology index views.
1486    // All sources/logs are already present in builtin_items at this point.
1487    {
1488        let source_iter = builtin_items.iter().filter_map(|b| match b {
1489            Builtin::Source(x) => Some(*x),
1490            _ => None,
1491        });
1492        let log_iter = builtin_items.iter().filter_map(|b| match b {
1493            Builtin::Log(x) => Some(*x),
1494            _ => None,
1495        });
1496        let mz_sources = builtin::make_mz_sources(source_iter, log_iter);
1497        let mz_sources_ref: &'static BuiltinMaterializedView = Box::leak(Box::new(mz_sources));
1498        // Insert at the original position of the old static MZ_SOURCES —
1499        // right before mz_source_references — to preserve stable IDs for
1500        // all items that follow it in the list.
1501        let insert_pos = builtin_items
1502            .iter()
1503            .position(|b| matches!(b, Builtin::Table(t) if t.name == "mz_source_references"))
1504            .expect("mz_source_references must be present in builtin_items");
1505        builtin_items.insert(insert_pos, Builtin::MaterializedView(mz_sources_ref));
1506    }
1507
1508    // Generate ontology views by enumerating existing builtins.
1509    builtin_items.extend(ontology::generate_views(&builtin_items));
1510
1511    // Generate builtin relations reporting builtin objects last, since they need a complete view
1512    // of all other builtins.
1513    let mut builtin_builtins = builtin::builtins(&builtin_items).collect();
1514
1515    // Construct the full list of builtins, retaining dependency order.
1516    let mut builtins = Vec::new();
1517    builtins.append(&mut builtin_types);
1518    builtins.append(&mut builtin_funcs);
1519    builtins.append(&mut builtin_builtins);
1520    builtins.append(&mut builtin_items);
1521
1522    builtins
1523});
1524pub const BUILTIN_ROLES: &[&BuiltinRole] = &[
1525    &MZ_SYSTEM_ROLE,
1526    &MZ_SUPPORT_ROLE,
1527    &MZ_ANALYTICS_ROLE,
1528    &MZ_MONITOR_ROLE,
1529    &MZ_MONITOR_REDACTED,
1530    &MZ_JWT_SYNC_ROLE,
1531];
1532pub const BUILTIN_CLUSTERS: &[&BuiltinCluster] = &[
1533    &MZ_SYSTEM_CLUSTER,
1534    &MZ_CATALOG_SERVER_CLUSTER,
1535    &MZ_PROBE_CLUSTER,
1536    &MZ_SUPPORT_CLUSTER,
1537    &MZ_ANALYTICS_CLUSTER,
1538];
1539pub const BUILTIN_CLUSTER_REPLICAS: &[&BuiltinClusterReplica] = &[
1540    &MZ_SYSTEM_CLUSTER_REPLICA,
1541    &MZ_CATALOG_SERVER_CLUSTER_REPLICA,
1542    &MZ_PROBE_CLUSTER_REPLICA,
1543];
1544
1545#[allow(non_snake_case)]
1546pub mod BUILTINS {
1547    use super::*;
1548
1549    pub fn logs() -> impl Iterator<Item = &'static BuiltinLog> {
1550        BUILTINS_STATIC.iter().filter_map(|b| match b {
1551            Builtin::Log(log) => Some(*log),
1552            _ => None,
1553        })
1554    }
1555
1556    pub fn types() -> impl Iterator<Item = &'static BuiltinType<NameReference>> {
1557        BUILTINS_STATIC.iter().filter_map(|b| match b {
1558            Builtin::Type(typ) => Some(*typ),
1559            _ => None,
1560        })
1561    }
1562
1563    pub fn views() -> impl Iterator<Item = &'static BuiltinView> {
1564        BUILTINS_STATIC.iter().filter_map(|b| match b {
1565            Builtin::View(view) => Some(*view),
1566            _ => None,
1567        })
1568    }
1569
1570    pub fn materialized_views() -> impl Iterator<Item = &'static BuiltinMaterializedView> {
1571        BUILTINS_STATIC.iter().filter_map(|b| match b {
1572            Builtin::MaterializedView(mv) => Some(*mv),
1573            _ => None,
1574        })
1575    }
1576
1577    pub fn funcs() -> impl Iterator<Item = &'static BuiltinFunc> {
1578        BUILTINS_STATIC.iter().filter_map(|b| match b {
1579            Builtin::Func(func) => Some(func),
1580            _ => None,
1581        })
1582    }
1583
1584    pub fn iter() -> impl Iterator<Item = &'static Builtin<NameReference>> {
1585        BUILTINS_STATIC.iter()
1586    }
1587}
1588
1589pub static BUILTIN_LOG_LOOKUP: LazyLock<HashMap<&'static str, &'static BuiltinLog>> =
1590    LazyLock::new(|| BUILTINS::logs().map(|log| (log.name, log)).collect());
1591/// Keys are builtin object description, values are the builtin index when sorted by dependency and
1592/// the builtin itself.
1593pub static BUILTIN_LOOKUP: LazyLock<
1594    HashMap<SystemObjectDescription, (usize, &'static Builtin<NameReference>)>,
1595> = LazyLock::new(|| {
1596    BUILTINS_STATIC
1597        .iter()
1598        .enumerate()
1599        .map(|(idx, builtin)| {
1600            (
1601                SystemObjectDescription {
1602                    schema_name: builtin.schema().to_string(),
1603                    object_type: builtin.catalog_item_type(),
1604                    object_name: builtin.name().to_string(),
1605                },
1606                (idx, builtin),
1607            )
1608        })
1609        .collect()
1610});
1611
1612#[cfg(test)]
1613mod tests {
1614    use std::collections::{BTreeMap, BTreeSet};
1615
1616    use mz_pgrepr::oid::FIRST_MATERIALIZE_OID;
1617    use mz_sql_parser::ast::visit::{self, Visit};
1618    use mz_sql_parser::ast::{Raw, RawItemName, UnresolvedItemName};
1619
1620    use super::*;
1621
1622    #[mz_ore::test]
1623    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `rust_psm_stack_pointer` on OS `linux`
1624    fn test_builtin_type_schema() {
1625        for typ in BUILTINS::types() {
1626            if typ.oid < FIRST_MATERIALIZE_OID {
1627                assert_eq!(
1628                    typ.schema, PG_CATALOG_SCHEMA,
1629                    "{typ:?} should be in {PG_CATALOG_SCHEMA} schema"
1630                );
1631            } else {
1632                // `mz_pgrepr::Type` resolution relies on all non-PG types existing in the
1633                // mz_catalog schema.
1634                assert_eq!(
1635                    typ.schema, MZ_CATALOG_SCHEMA,
1636                    "{typ:?} should be in {MZ_CATALOG_SCHEMA} schema"
1637                );
1638            }
1639        }
1640    }
1641
1642    /// Visitor that collects the last component of all referenced
1643    /// item names from a SQL AST.
1644    struct ItemNameCollector {
1645        names: BTreeSet<String>,
1646    }
1647
1648    impl<'ast> Visit<'ast, Raw> for ItemNameCollector {
1649        fn visit_item_name(&mut self, name: &'ast <Raw as mz_sql_parser::ast::AstInfo>::ItemName) {
1650            let unresolved: &UnresolvedItemName = match name {
1651                RawItemName::Name(n) | RawItemName::Id(_, n, _) => n,
1652            };
1653            let parts = &unresolved.0;
1654            if !parts.is_empty() {
1655                let obj_name = parts[parts.len() - 1].as_str().to_string();
1656                self.names.insert(obj_name);
1657            }
1658            visit::visit_item_name(self, name);
1659        }
1660    }
1661
1662    /// Tests that `BUILTINS_STATIC` is ordered respecting dependencies:
1663    /// if builtin A references builtin B in its SQL, then B must appear
1664    /// before A in the list. (This ordering is assumed by, e.g.,
1665    /// `sort_updates` during catalog migrations.)
1666    #[mz_ore::test]
1667    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `rust_psm_stack_pointer` on OS `linux`
1668    fn test_builtins_static_dependency_order() {
1669        // Build a map from name -> (schema, index) for all builtins.
1670        // We look up by just the name (last component) to catch
1671        // unqualified references in SQL.
1672        let mut builtin_by_name: BTreeMap<&str, (&str, usize)> = BTreeMap::new();
1673        let mut duplicate_names = Vec::new();
1674        for (idx, builtin) in BUILTINS_STATIC.iter().enumerate() {
1675            if let Some((prev_schema, prev_idx)) =
1676                builtin_by_name.insert(builtin.name(), (builtin.schema(), idx))
1677            {
1678                // Only flag duplicates across different schemas.
1679                // Same-schema duplicates (e.g., range types that
1680                // appear as both Type and Func) are fine because
1681                // they resolve to the same schema.
1682                if prev_schema != builtin.schema() {
1683                    duplicate_names.push(format!(
1684                        "name {:?} appears in both {}.{} (index \
1685                         {}) and {}.{} (index {})",
1686                        builtin.name(),
1687                        prev_schema,
1688                        builtin.name(),
1689                        prev_idx,
1690                        builtin.schema(),
1691                        builtin.name(),
1692                        idx,
1693                    ));
1694                }
1695            }
1696        }
1697        assert!(
1698            duplicate_names.is_empty(),
1699            "BUILTINS_STATIC has duplicate names across different \
1700             schemas (this test needs adjustment if such duplicates \
1701             are intentional):\n{}",
1702            duplicate_names.join("\n"),
1703        );
1704
1705        // Get the `CREATE ...` SQL for builtins that have it.
1706        let get_create_sql = |builtin: &Builtin<NameReference>| -> Option<String> {
1707            match builtin {
1708                Builtin::View(v) => Some(v.create_sql()),
1709                Builtin::MaterializedView(mv) => Some(mv.create_sql()),
1710                Builtin::Index(idx) => Some(idx.create_sql()),
1711                _ => None,
1712            }
1713        };
1714
1715        // For each SQL-bearing builtin, parse its SQL, walk the AST to
1716        // find referenced item names, and check that all referenced
1717        // builtins appear earlier in BUILTINS_STATIC.
1718        let mut violations = Vec::new();
1719        for (idx, builtin) in BUILTINS_STATIC.iter().enumerate() {
1720            let create_sql = match get_create_sql(builtin) {
1721                Some(sql) => sql,
1722                None => continue,
1723            };
1724
1725            let stmts = mz_sql_parser::parser::parse_statements(&create_sql).unwrap_or_else(|e| {
1726                panic!(
1727                    "failed to parse SQL for {}.{}: \
1728                         {e}\nSQL: {create_sql}",
1729                    builtin.schema(),
1730                    builtin.name(),
1731                )
1732            });
1733
1734            let mut collector = ItemNameCollector {
1735                names: BTreeSet::new(),
1736            };
1737            for stmt in &stmts {
1738                collector.visit_statement(&stmt.ast);
1739            }
1740
1741            for ref_name in &collector.names {
1742                if let Some(&(ref_schema, dep_idx)) = builtin_by_name.get(ref_name.as_str()) {
1743                    if dep_idx > idx {
1744                        violations.push(format!(
1745                            "{}.{} (index {}) references \
1746                             {}.{} (index {}), but the \
1747                             dependency appears later in \
1748                             BUILTINS_STATIC",
1749                            builtin.schema(),
1750                            builtin.name(),
1751                            idx,
1752                            ref_schema,
1753                            ref_name,
1754                            dep_idx,
1755                        ));
1756                    }
1757                }
1758            }
1759        }
1760
1761        assert!(
1762            violations.is_empty(),
1763            "BUILTINS_STATIC has dependency ordering violations:\n{}",
1764            violations.join("\n"),
1765        );
1766    }
1767
1768    /// Validates ontology metadata consistency:
1769    /// - Every link target references an entity that exists.
1770    /// - No duplicate entity names.
1771    /// - Every annotated builtin has a non-empty entity_name and description.
1772    #[mz_ore::test]
1773    #[cfg_attr(miri, ignore)]
1774    fn test_ontology_consistency() {
1775        // Collect all entity names from builtins with ontology annotations.
1776        let mut entity_names: BTreeSet<String> = BTreeSet::new();
1777        let mut duplicate_entities = Vec::new();
1778
1779        for builtin in BUILTINS_STATIC.iter() {
1780            let ontology = match builtin {
1781                Builtin::Table(t) => t.ontology.as_ref(),
1782                Builtin::View(v) => v.ontology.as_ref(),
1783                Builtin::MaterializedView(mv) => mv.ontology.as_ref(),
1784                Builtin::Source(s) => s.ontology.as_ref(),
1785                Builtin::Log(l) => l.ontology.as_ref(),
1786                _ => None,
1787            };
1788            if let Some(ont) = ontology {
1789                assert!(
1790                    !ont.entity_name.is_empty(),
1791                    "builtin {} has empty ontology entity_name",
1792                    builtin.name()
1793                );
1794                assert!(
1795                    !ont.description.is_empty(),
1796                    "builtin {} ({}) has empty ontology description",
1797                    builtin.name(),
1798                    ont.entity_name
1799                );
1800                if !entity_names.insert(ont.entity_name.to_string()) {
1801                    duplicate_entities.push(format!(
1802                        "duplicate entity_name {:?} on builtin {}",
1803                        ont.entity_name,
1804                        builtin.name()
1805                    ));
1806                }
1807            }
1808        }
1809        assert!(
1810            duplicate_entities.is_empty(),
1811            "ontology has duplicate entity names:\n{}",
1812            duplicate_entities.join("\n"),
1813        );
1814
1815        // Validate link targets reference existing entities.
1816        let mut bad_targets = Vec::new();
1817        for builtin in BUILTINS_STATIC.iter() {
1818            let ontology = match builtin {
1819                Builtin::Table(t) => t.ontology.as_ref(),
1820                Builtin::View(v) => v.ontology.as_ref(),
1821                Builtin::MaterializedView(mv) => mv.ontology.as_ref(),
1822                Builtin::Source(s) => s.ontology.as_ref(),
1823                Builtin::Log(l) => l.ontology.as_ref(),
1824                _ => None,
1825            };
1826            if let Some(ont) = ontology {
1827                for link in ont.links {
1828                    if !entity_names.contains(link.target) {
1829                        bad_targets.push(format!(
1830                            "entity {:?} link {:?} targets {:?} which is not a known entity",
1831                            ont.entity_name, link.name, link.target
1832                        ));
1833                    }
1834                }
1835            }
1836        }
1837        assert!(
1838            bad_targets.is_empty(),
1839            "ontology has links targeting unknown entities:\n{}",
1840            bad_targets.join("\n"),
1841        );
1842
1843        // Semantic type annotations are typed (SemanticType enum), so validity
1844        // is guaranteed at compile time — no runtime check needed.
1845
1846        // Validate that every "reference" column (one whose semantic type implies
1847        // a FK relationship) is covered by an OntologyLink on entities that
1848        // already have at least one FK-style link.
1849        //
1850        // Scope: only entities that have started FK annotation (at least one
1851        // link with a source_column). Entities with only union/maps_to links,
1852        // or no links at all, are not yet fully annotated and are skipped to
1853        // avoid noise.
1854        //
1855        // Exemptions:
1856        // - Column at index 0 named "id": almost always the entity's own PK,
1857        //   not a FK (e.g. mz_objects.id, mz_functions.id).
1858        // - Columns in the relation's declared key set.
1859        //
1860        // "Reference" types are ID types that imply a FK. Discriminators
1861        // (ObjectType, ConnectionType, SourceType), OID, and metric types
1862        // (ByteCount, etc.) are excluded.
1863        let reference_sem_types: BTreeSet<SemanticType> = BTreeSet::from([
1864            SemanticType::CatalogItemId,
1865            SemanticType::GlobalId,
1866            SemanticType::ClusterId,
1867            SemanticType::ReplicaId,
1868            SemanticType::SchemaId,
1869            SemanticType::DatabaseId,
1870            SemanticType::RoleId,
1871            SemanticType::NetworkPolicyId,
1872        ]);
1873
1874        let mut uncovered_fk_cols = Vec::new();
1875        for builtin in BUILTINS_STATIC.iter() {
1876            let desc_storage;
1877            let (name, desc, ontology): (&str, &RelationDesc, Option<&Ontology>) = match builtin {
1878                Builtin::Table(t) => (t.name, &t.desc, t.ontology.as_ref()),
1879                Builtin::View(v) => (v.name, &v.desc, v.ontology.as_ref()),
1880                Builtin::MaterializedView(mv) => (mv.name, &mv.desc, mv.ontology.as_ref()),
1881                Builtin::Source(s) => (s.name, &s.desc, s.ontology.as_ref()),
1882                Builtin::Log(l) => {
1883                    desc_storage = l.variant.desc();
1884                    (l.name, &desc_storage, l.ontology.as_ref())
1885                }
1886                _ => continue,
1887            };
1888            let Some(ont) = ontology else { continue };
1889
1890            // Collect all source_column values declared by existing links.
1891            let linked_cols: BTreeSet<&str> = ont
1892                .links
1893                .iter()
1894                .filter_map(|link| match &link.properties {
1895                    LinkProperties::ForeignKey { source_column, .. } => Some(*source_column),
1896                    LinkProperties::Measures { source_column, .. } => Some(*source_column),
1897                    LinkProperties::DependsOn { source_column, .. } => Some(*source_column),
1898                    LinkProperties::MapsTo { source_column, .. } => Some(*source_column),
1899                    LinkProperties::Union { .. } => None,
1900                })
1901                .collect();
1902
1903            // Skip entities that have no FK-style links yet — they are either
1904            // unannotated or use only union/maps_to links. Only enforce
1905            // coverage on entities that have started FK annotation.
1906            if linked_cols.is_empty() {
1907                continue;
1908            }
1909
1910            // Column indices that are part of the declared key set.
1911            let pk_indices: BTreeSet<usize> = desc.typ().keys.iter().flatten().copied().collect();
1912
1913            for (col_name, sem) in ont.column_semantic_types {
1914                if !reference_sem_types.contains(sem) {
1915                    continue;
1916                }
1917                let Some(idx) = desc.iter_names().position(|n| n.as_str() == *col_name) else {
1918                    continue;
1919                };
1920                // Exempt the entity's own primary identifier: column 0 named
1921                // "id" is by convention the entity's own PK (not a FK), even
1922                // when no explicit with_key() is declared on the relation.
1923                if idx == 0 && *col_name == "id" {
1924                    continue;
1925                }
1926                if pk_indices.contains(&idx) {
1927                    continue;
1928                }
1929                if linked_cols.contains(*col_name) {
1930                    continue;
1931                }
1932                uncovered_fk_cols.push(format!(
1933                    "entity {:?} (builtin {}) column {:?} has semantic type {:?} but no OntologyLink covers it (add a link with source_column: {:?})",
1934                    ont.entity_name, name, col_name, sem, col_name
1935                ));
1936            }
1937        }
1938        assert!(
1939            uncovered_fk_cols.is_empty(),
1940            "ontology entities have FK-typed columns with no OntologyLink:\n{}",
1941            uncovered_fk_cols.join("\n"),
1942        );
1943
1944        // Validate that every source_column in a link actually names a column
1945        // in the entity's RelationDesc. This catches stale annotations after
1946        // column renames or removals. With typed LinkProperties this is mostly
1947        // belt-and-suspenders since the type system enforces field presence, but
1948        // we still need to verify the string value matches a real column.
1949        let mut bad_source_cols = Vec::new();
1950        for builtin in BUILTINS_STATIC.iter() {
1951            let desc_storage;
1952            let (name, desc, ontology): (&str, &RelationDesc, Option<&Ontology>) = match builtin {
1953                Builtin::Table(t) => (t.name, &t.desc, t.ontology.as_ref()),
1954                Builtin::View(v) => (v.name, &v.desc, v.ontology.as_ref()),
1955                Builtin::MaterializedView(mv) => (mv.name, &mv.desc, mv.ontology.as_ref()),
1956                Builtin::Source(s) => (s.name, &s.desc, s.ontology.as_ref()),
1957                Builtin::Log(l) => {
1958                    desc_storage = l.variant.desc();
1959                    (l.name, &desc_storage, l.ontology.as_ref())
1960                }
1961                _ => continue,
1962            };
1963            let Some(ont) = ontology else { continue };
1964
1965            let col_names: BTreeSet<&str> = desc.iter_names().map(|c| c.as_str()).collect();
1966
1967            for link in ont.links {
1968                let source_col = match &link.properties {
1969                    LinkProperties::ForeignKey { source_column, .. } => Some(*source_column),
1970                    LinkProperties::Measures { source_column, .. } => Some(*source_column),
1971                    LinkProperties::DependsOn { source_column, .. } => Some(*source_column),
1972                    LinkProperties::MapsTo { source_column, .. } => Some(*source_column),
1973                    LinkProperties::Union { .. } => None,
1974                };
1975                let Some(col) = source_col else { continue };
1976                if !col_names.contains(col) {
1977                    bad_source_cols.push(format!(
1978                        "entity {:?} (builtin {}) link {:?} references source_column {:?} which does not exist in the relation",
1979                        ont.entity_name, name, link.name, col
1980                    ));
1981                }
1982                let extra_key_columns = match &link.properties {
1983                    LinkProperties::ForeignKey {
1984                        extra_key_columns: Some(extras),
1985                        ..
1986                    } => Some(*extras),
1987                    LinkProperties::Measures {
1988                        extra_key_columns: Some(extras),
1989                        ..
1990                    } => Some(*extras),
1991                    _ => None,
1992                };
1993                if let Some(extras) = extra_key_columns {
1994                    for (src_col, _) in extras {
1995                        if !col_names.contains(*src_col) {
1996                            bad_source_cols.push(format!(
1997                                "entity {:?} (builtin {}) link {:?} extra_key_columns references {:?} which does not exist in the relation",
1998                                ont.entity_name, name, link.name, src_col
1999                            ));
2000                        }
2001                    }
2002                }
2003            }
2004        }
2005        assert!(
2006            bad_source_cols.is_empty(),
2007            "ontology links reference non-existent source_columns:\n{}",
2008            bad_source_cols.join("\n"),
2009        );
2010
2011        // Sanity check: we have a reasonable number of annotated entities.
2012        assert!(
2013            entity_names.len() > 120,
2014            "expected > 120 ontology entities, found {}",
2015            entity_names.len()
2016        );
2017    }
2018
2019    /// Verify that `LinkProperties` serializes to the same JSON that the old
2020    /// hand-written `properties_json` strings contained. One representative
2021    /// case per constructor/variant is enough — the important thing is that
2022    /// field names, enum tag values, and skip-if-None/false behaviour are all
2023    /// correct.
2024    #[mz_ore::test]
2025    fn test_link_properties_serialization() {
2026        let check = |props: LinkProperties, expected: &str| {
2027            let got = serde_json::to_string(&props).expect("serialize");
2028            let got_val: serde_json::Value = serde_json::from_str(&got).expect("parse got");
2029            let exp_val: serde_json::Value =
2030                serde_json::from_str(expected).expect("parse expected");
2031            assert_eq!(got_val, exp_val, "mismatch for {expected}");
2032        };
2033
2034        // fk — basic, no optional fields
2035        check(
2036            LinkProperties::fk("owner_id", "id", Cardinality::ManyToOne),
2037            r#"{"kind":"foreign_key","source_column":"owner_id","target_column":"id","cardinality":"many_to_one"}"#,
2038        );
2039        // fk_composite — extra_key_columns present
2040        check(
2041            LinkProperties::fk_composite(
2042                "operator_id",
2043                "id",
2044                Cardinality::ManyToOne,
2045                &[("worker_id", "worker_id")],
2046            ),
2047            r#"{"kind":"foreign_key","source_column":"operator_id","target_column":"id","cardinality":"many_to_one","extra_key_columns":[["worker_id","worker_id"]]}"#,
2048        );
2049        // fk — one_to_one cardinality
2050        check(
2051            LinkProperties::fk("id", "id", Cardinality::OneToOne),
2052            r#"{"kind":"foreign_key","source_column":"id","target_column":"id","cardinality":"one_to_one"}"#,
2053        );
2054        // fk_nullable — nullable field present and true
2055        check(
2056            LinkProperties::fk_nullable("database_id", "id", Cardinality::ManyToOne),
2057            r#"{"kind":"foreign_key","source_column":"database_id","target_column":"id","cardinality":"many_to_one","nullable":true}"#,
2058        );
2059        // fk_typed — source_id_type present, requires_mapping absent
2060        check(
2061            LinkProperties::fk_typed(
2062                "replica_id",
2063                "id",
2064                Cardinality::ManyToOne,
2065                mz_repr::SemanticType::CatalogItemId,
2066            ),
2067            r#"{"kind":"foreign_key","source_column":"replica_id","target_column":"id","cardinality":"many_to_one","source_id_type":"CatalogItemId"}"#,
2068        );
2069        // fk_mapped — source_id_type + requires_mapping both present
2070        check(
2071            LinkProperties::fk_mapped(
2072                "object_id",
2073                "id",
2074                Cardinality::ManyToOne,
2075                mz_repr::SemanticType::GlobalId,
2076                "mz_internal.mz_object_global_ids",
2077            ),
2078            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"}"#,
2079        );
2080        // union_disc — discriminator fields present, note absent
2081        check(
2082            LinkProperties::union_disc("type", "table"),
2083            r#"{"kind":"union","discriminator_column":"type","discriminator_value":"table"}"#,
2084        );
2085        // Union — note only, discriminator absent
2086        check(
2087            LinkProperties::Union {
2088                discriminator_column: None,
2089                discriminator_value: None,
2090                note: Some("example note"),
2091            },
2092            r#"{"kind":"union","note":"example note"}"#,
2093        );
2094        // measures — basic
2095        check(
2096            LinkProperties::measures("id", "id", "cpu_time_ns"),
2097            r#"{"kind":"measures","source_column":"id","target_column":"id","metric":"cpu_time_ns"}"#,
2098        );
2099        // measures_composite — extra_key_columns present
2100        check(
2101            LinkProperties::measures_composite(
2102                "export_id",
2103                "export_id",
2104                "time_ns",
2105                &[("worker_id", "worker_id")],
2106            ),
2107            r#"{"kind":"measures","source_column":"export_id","target_column":"export_id","metric":"time_ns","extra_key_columns":[["worker_id","worker_id"]]}"#,
2108        );
2109        // measures_mapped — source_id_type + requires_mapping present
2110        check(
2111            LinkProperties::measures_mapped(
2112                "object_id",
2113                "id",
2114                "wallclock_lag",
2115                mz_repr::SemanticType::GlobalId,
2116                "mz_internal.mz_object_global_ids",
2117            ),
2118            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"}"#,
2119        );
2120        // DependsOn — with mapping
2121        check(
2122            LinkProperties::DependsOn {
2123                source_column: "object_id",
2124                target_column: "id",
2125                source_id_type: Some(mz_repr::SemanticType::GlobalId),
2126                requires_mapping: Some("mz_internal.mz_object_global_ids"),
2127            },
2128            r#"{"kind":"depends_on","source_column":"object_id","target_column":"id","source_id_type":"GlobalId","requires_mapping":"mz_internal.mz_object_global_ids"}"#,
2129        );
2130        // DependsOn — direct (CatalogItemId, no mapping)
2131        check(
2132            LinkProperties::DependsOn {
2133                source_column: "object_id",
2134                target_column: "id",
2135                source_id_type: Some(mz_repr::SemanticType::CatalogItemId),
2136                requires_mapping: None,
2137            },
2138            r#"{"kind":"depends_on","source_column":"object_id","target_column":"id","source_id_type":"CatalogItemId"}"#,
2139        );
2140        // MapsTo — via + from_type + to_type
2141        check(
2142            LinkProperties::MapsTo {
2143                source_column: "id",
2144                target_column: "global_id",
2145                via: Some("mz_internal.mz_object_global_ids"),
2146                from_type: Some(mz_repr::SemanticType::CatalogItemId),
2147                to_type: Some(mz_repr::SemanticType::GlobalId),
2148                note: None,
2149            },
2150            r#"{"kind":"maps_to","source_column":"id","target_column":"global_id","via":"mz_internal.mz_object_global_ids","from_type":"CatalogItemId","to_type":"GlobalId"}"#,
2151        );
2152    }
2153
2154    /// Verifies that the `mz_sources` materialized view fingerprint changes
2155    /// whenever a new builtin source or log is added.
2156    ///
2157    /// This is the correctness property that `make_mz_sources` provides: by
2158    /// inlining the full set of builtin sources/logs as VALUES in its SQL, any
2159    /// change to those sets is reflected in `fingerprint()`. A stale fingerprint
2160    /// would prevent the catalog migration from replacing `mz_sources`, leaving
2161    /// it with out-of-date data, silently serving stale builtin source rows.
2162    #[mz_ore::test]
2163    #[cfg_attr(miri, ignore)]
2164    fn test_mz_sources_fingerprint_changes_with_new_builtin_source() {
2165        let sources: Vec<&'static BuiltinSource> = BUILTINS_STATIC
2166            .iter()
2167            .filter_map(|b| match b {
2168                Builtin::Source(x) => Some(*x),
2169                _ => None,
2170            })
2171            .collect();
2172        let logs: Vec<&'static BuiltinLog> = BUILTINS_STATIC
2173            .iter()
2174            .filter_map(|b| match b {
2175                Builtin::Log(x) => Some(*x),
2176                _ => None,
2177            })
2178            .collect();
2179
2180        // The fingerprint from make_mz_sources must match the live BUILTINS_STATIC entry.
2181        let mv_base = builtin::make_mz_sources(sources.iter().copied(), logs.iter().copied());
2182        let fp_base = Fingerprint::fingerprint(&&mv_base);
2183
2184        let mz_sources_static = BUILTINS_STATIC
2185            .iter()
2186            .find_map(|b| match b {
2187                Builtin::MaterializedView(mv) if mv.name == "mz_sources" => Some(*mv),
2188                _ => None,
2189            })
2190            .expect("mz_sources must be present in BUILTINS_STATIC");
2191        assert_eq!(
2192            fp_base,
2193            Fingerprint::fingerprint(&mz_sources_static),
2194            "make_mz_sources fingerprint must match the BUILTINS_STATIC mz_sources fingerprint"
2195        );
2196
2197        // Adding an extra source must change the fingerprint, proving that
2198        // make_mz_sources inlines the source list into its SQL.
2199        let extra_source = sources[0];
2200        let mv_extra = builtin::make_mz_sources(
2201            sources.iter().copied().chain(std::iter::once(extra_source)),
2202            logs.iter().copied(),
2203        );
2204        assert_ne!(
2205            fp_base,
2206            Fingerprint::fingerprint(&&mv_extra),
2207            "mz_sources fingerprint must change when a builtin source is added"
2208        );
2209    }
2210}