Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 3 additions & 52 deletions src/adapter/src/catalog/builtin_table_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use mz_catalog::builtin::{
MZ_POSTGRES_SOURCES, MZ_PSEUDO_TYPES, MZ_REPLACEMENTS, MZ_ROLE_AUTH, MZ_ROLE_PARAMETERS,
MZ_ROLES, MZ_SECRETS, MZ_SESSIONS, MZ_SINKS, MZ_SOURCE_REFERENCES, MZ_SOURCES,
MZ_SQL_SERVER_SOURCE_TABLES, MZ_SSH_TUNNEL_CONNECTIONS, MZ_STORAGE_USAGE_BY_SHARD,
MZ_SUBSCRIPTIONS, MZ_SYSTEM_PRIVILEGES, MZ_TABLES, MZ_TYPE_PG_METADATA, MZ_TYPES, MZ_VIEWS,
MZ_SUBSCRIPTIONS, MZ_SYSTEM_PRIVILEGES, MZ_TABLES, MZ_TYPE_PG_METADATA, MZ_TYPES,
MZ_WEBHOOKS_SOURCES,
};
use mz_catalog::config::AwsPrincipalContext;
use mz_catalog::durable::SourceReferences;
use mz_catalog::memory::error::{Error, ErrorKind};
use mz_catalog::memory::objects::{
CatalogEntry, CatalogItem, ClusterVariant, Connection, ContinualTask, DataSourceDesc, Func,
Index, MaterializedView, Sink, Table, TableDataSource, Type, View,
Index, MaterializedView, Sink, Table, TableDataSource, Type,
};
use mz_controller::clusters::{
ManagedReplicaAvailabilityZones, ManagedReplicaLocation, ReplicaLocation,
Expand Down Expand Up @@ -633,9 +633,6 @@ impl CatalogState {

updates
}
CatalogItem::View(view) => {
self.pack_view_update(id, oid, schema_id, name, owner_id, privileges, view, diff)
}
CatalogItem::MaterializedView(mview) => self.pack_materialized_view_update(
id, oid, schema_id, name, owner_id, privileges, mview, diff,
),
Expand All @@ -657,6 +654,7 @@ impl CatalogState {
CatalogItem::ContinualTask(ct) => self.pack_continual_task_update(
id, oid, schema_id, name, owner_id, privileges, ct, diff,
),
CatalogItem::View(_) => Vec::new(),
};

if !entry.item().is_temporary() {
Expand Down Expand Up @@ -1221,53 +1219,6 @@ impl CatalogState {
Ok(BuiltinTableUpdate::row(id, row, diff))
}

fn pack_view_update(
&self,
id: CatalogItemId,
oid: u32,
schema_id: &SchemaSpecifier,
name: &str,
owner_id: &RoleId,
privileges: Datum,
view: &View,
diff: Diff,
) -> Vec<BuiltinTableUpdate<&'static BuiltinTable>> {
let create_stmt = mz_sql::parse::parse(&view.create_sql)
.unwrap_or_else(|e| {
panic!(
"create_sql cannot be invalid: `{}` --- error: `{}`",
view.create_sql, e
)
})
.into_element()
.ast;
let query = match &create_stmt {
Statement::CreateView(stmt) => &stmt.definition.query,
_ => unreachable!(),
};

let mut query_string = query.to_ast_string_stable();
// PostgreSQL appends a semicolon in `pg_views.definition`, we
// do the same for compatibility's sake.
query_string.push(';');

vec![BuiltinTableUpdate::row(
&*MZ_VIEWS,
Row::pack_slice(&[
Datum::String(&id.to_string()),
Datum::UInt32(oid),
Datum::String(&schema_id.to_string()),
Datum::String(name),
Datum::String(&query_string),
Datum::String(&owner_id.to_string()),
privileges,
Datum::String(&view.create_sql),
Datum::String(&create_stmt.to_ast_string_redacted()),
]),
diff,
)]
}

fn pack_materialized_view_update(
&self,
id: CatalogItemId,
Expand Down
6 changes: 6 additions & 0 deletions src/adapter/src/catalog/open/builtin_schema_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ static MIGRATIONS: LazyLock<Vec<MigrationStep>> = LazyLock::new(|| {
MZ_INTERNAL_SCHEMA,
"mz_pending_cluster_replicas",
),
MigrationStep::replacement(
"26.19.0-dev.0",
CatalogItemType::MaterializedView,
MZ_CATALOG_SCHEMA,
"mz_views",
),
]
});

Expand Down
171 changes: 123 additions & 48 deletions src/catalog/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! More information about builtin system tables and types can be found in
//! <https://materialize.com/docs/sql/system-catalog/>.

mod builtin;
pub mod notice;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -2831,48 +2832,108 @@ pub static MZ_SINKS: LazyLock<BuiltinTable> = LazyLock::new(|| {
access: vec![PUBLIC_SELECT],
}
});
pub static MZ_VIEWS: LazyLock<BuiltinTable> = LazyLock::new(|| BuiltinTable {
name: "mz_views",
schema: MZ_CATALOG_SCHEMA,
oid: oid::TABLE_MZ_VIEWS_OID,
desc: RelationDesc::builder()
.with_column("id", SqlScalarType::String.nullable(false))
.with_column("oid", SqlScalarType::Oid.nullable(false))
.with_column("schema_id", SqlScalarType::String.nullable(false))
.with_column("name", SqlScalarType::String.nullable(false))
.with_column("definition", SqlScalarType::String.nullable(false))
.with_column("owner_id", SqlScalarType::String.nullable(false))
.with_column(
"privileges",
SqlScalarType::Array(Box::new(SqlScalarType::MzAclItem)).nullable(false),
)
.with_column("create_sql", SqlScalarType::String.nullable(false))
.with_column("redacted_create_sql", SqlScalarType::String.nullable(false))
.with_key(vec![0])
.with_key(vec![1])
.finish(),
column_comments: BTreeMap::from_iter([
("id", "Materialize's unique ID for the view."),
("oid", "A PostgreSQL-compatible OID for the view."),
(
"schema_id",
"The ID of the schema to which the view belongs. Corresponds to `mz_schemas.id`.",
),
("name", "The name of the view."),
("definition", "The view definition (a `SELECT` query)."),
(
"owner_id",
"The role ID of the owner of the view. Corresponds to `mz_roles.id`.",
),
("privileges", "The privileges belonging to the view."),
("create_sql", "The `CREATE` SQL statement for the view."),
(
"redacted_create_sql",
"The redacted `CREATE` SQL statement for the view.",
),
]),
is_retained_metrics_object: false,
access: vec![PUBLIC_SELECT],
pub static MZ_VIEWS: LazyLock<BuiltinMaterializedView> = LazyLock::new(|| {
BuiltinMaterializedView {
name: "mz_views",
schema: MZ_CATALOG_SCHEMA,
oid: oid::MV_MZ_VIEWS_OID,
desc: RelationDesc::builder()
.with_column("id", SqlScalarType::String.nullable(false))
.with_column("oid", SqlScalarType::Oid.nullable(false))
.with_column("schema_id", SqlScalarType::String.nullable(false))
.with_column("name", SqlScalarType::String.nullable(false))
.with_column("definition", SqlScalarType::String.nullable(false))
.with_column("owner_id", SqlScalarType::String.nullable(false))
.with_column(
"privileges",
SqlScalarType::Array(Box::new(SqlScalarType::MzAclItem)).nullable(false),
)
.with_column("create_sql", SqlScalarType::String.nullable(false))
.with_column("redacted_create_sql", SqlScalarType::String.nullable(false))
.with_key(vec![0])
.with_key(vec![1])
.finish(),
column_comments: BTreeMap::from_iter([
("id", "Materialize's unique ID for the view."),
("oid", "A PostgreSQL-compatible OID for the view."),
(
"schema_id",
"The ID of the schema to which the view belongs. Corresponds to `mz_schemas.id`.",
),
("name", "The name of the view."),
("definition", "The view definition (a `SELECT` query)."),
(
"owner_id",
"The role ID of the owner of the view. Corresponds to `mz_roles.id`.",
),
("privileges", "The privileges belonging to the view."),
("create_sql", "The `CREATE` SQL statement for the view."),
(
"redacted_create_sql",
"The redacted `CREATE` SQL statement for the view.",
),
]),
sql: "
IN CLUSTER mz_catalog_server
WITH (
ASSERT NOT NULL id,
ASSERT NOT NULL oid,
ASSERT NOT NULL schema_id,
ASSERT NOT NULL name,
ASSERT NOT NULL definition,
ASSERT NOT NULL owner_id,
ASSERT NOT NULL privileges,
ASSERT NOT NULL create_sql,
ASSERT NOT NULL redacted_create_sql
) AS
WITH
user_views AS (
SELECT
mz_internal.parse_catalog_id(data->'key'->'gid') AS id,
(data->'value'->>'oid')::oid AS oid,
mz_internal.parse_catalog_id(data->'value'->'schema_id') AS schema_id,
data->'value'->>'name' AS name,
(mz_internal.parse_catalog_create_sql(data->'value'->'definition'->'V1'->>'create_sql'))->>'definition' AS definition,
mz_internal.parse_catalog_id(data->'value'->'owner_id') AS owner_id,
mz_internal.parse_catalog_privileges(data->'value'->'privileges') AS privileges,
data->'value'->'definition'->'V1'->>'create_sql' AS create_sql,
mz_internal.redact_sql(data->'value'->'definition'->'V1'->>'create_sql') AS redacted_create_sql
FROM mz_internal.mz_catalog_raw
WHERE
data->>'kind' = 'Item' AND
mz_internal.parse_catalog_create_sql(data->'value'->'definition'->'V1'->>'create_sql')->>'type' = 'view'
),
builtin_mappings AS (
SELECT
data->'key'->>'schema_name' AS schema_name,
data->'key'->>'object_name' AS name,
's' || (data->'value'->>'catalog_id') AS id
FROM mz_internal.mz_catalog_raw
WHERE
data->>'kind' = 'GidMapping' AND
data->'key'->>'object_type' = '4'
),
builtin_views AS (
SELECT
m.id,
v.oid,
s.id AS schema_id,
v.name,
v.definition,
's1' AS owner_id,
v.privileges,
v.create_sql,
mz_internal.redact_sql(v.create_sql) AS redacted_create_sql
FROM mz_internal.mz_builtin_views v
JOIN builtin_mappings m USING (schema_name, name)
JOIN mz_schemas s ON s.name = v.schema_name
WHERE s.database_id IS NULL
)
SELECT * FROM user_views
UNION ALL
SELECT * FROM builtin_views",
access: vec![PUBLIC_SELECT],
}
});
pub static MZ_MATERIALIZED_VIEWS: LazyLock<BuiltinTable> = LazyLock::new(|| BuiltinTable {
name: "mz_materialized_views",
Expand Down Expand Up @@ -14068,7 +14129,7 @@ pub const MZ_ANALYTICS_CLUSTER: BuiltinCluster = BuiltinCluster {

/// List of all builtin objects sorted topologically by dependency.
pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::new(|| {
let mut builtins = vec![
let mut builtin_types = vec![
Builtin::Type(&TYPE_ANY),
Builtin::Type(&TYPE_ANYARRAY),
Builtin::Type(&TYPE_ANYELEMENT),
Expand Down Expand Up @@ -14160,6 +14221,8 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
Builtin::Type(&TYPE_ACL_ITEM_ARRAY),
Builtin::Type(&TYPE_INTERNAL),
];

let mut builtin_funcs = Vec::new();
for (schema, funcs) in &[
(PG_CATALOG_SCHEMA, &*mz_sql::func::PG_CATALOG_BUILTINS),
(
Expand All @@ -14171,14 +14234,15 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
(MZ_UNSAFE_SCHEMA, &*mz_sql::func::MZ_UNSAFE_BUILTINS),
] {
for (name, func) in funcs.iter() {
builtins.push(Builtin::Func(BuiltinFunc {
builtin_funcs.push(Builtin::Func(BuiltinFunc {
name,
schema,
inner: func,
}));
}
}
builtins.append(&mut vec![

let mut builtin_items = vec![
Builtin::Source(&MZ_CATALOG_RAW),
Builtin::Log(&MZ_ARRANGEMENT_SHARING_RAW),
Builtin::Log(&MZ_ARRANGEMENT_BATCHES_RAW),
Expand Down Expand Up @@ -14230,7 +14294,7 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
Builtin::Table(&MZ_SQL_SERVER_SOURCE_TABLES),
Builtin::Table(&MZ_KAFKA_SOURCE_TABLES),
Builtin::Table(&MZ_SINKS),
Builtin::Table(&MZ_VIEWS),
Builtin::MaterializedView(&MZ_VIEWS),
Builtin::Table(&MZ_MATERIALIZED_VIEWS),
Builtin::Table(&MZ_MATERIALIZED_VIEW_REFRESH_STRATEGIES),
Builtin::Table(&MZ_TYPES),
Expand Down Expand Up @@ -14555,9 +14619,20 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
Builtin::View(&MZ_INDEX_ADVICE),
Builtin::View(&MZ_MCP_DATA_PRODUCTS),
Builtin::View(&MZ_MCP_DATA_PRODUCT_DETAILS),
]);
];

builtin_items.extend(notice::builtins());

// Generate builtin relations reporting builtin objects last, since they need a complete view
// of all other builtins.
let mut builtin_builtins = builtin::builtins(&builtin_items).collect();

builtins.extend(notice::builtins());
// Construct the full list of builtins, retaining dependency order.
let mut builtins = Vec::new();
builtins.append(&mut builtin_types);
builtins.append(&mut builtin_funcs);
builtins.append(&mut builtin_builtins);
builtins.append(&mut builtin_items);

builtins
});
Expand Down
Loading
Loading