Skip to content

Commit 5e1fc18

Browse files
committed
catalog: add mz_replacements system relation
This commit adds a new `mz_replacements` builtin table, exposing the replacement MV relationship. It also adds a `replacing` column to the `SHOW MATERIALIZED VIEWS` output, fueled by the new `mz_replacements` table.
1 parent 4d63825 commit 5e1fc18

File tree

5 files changed

+90
-61
lines changed

5 files changed

+90
-61
lines changed

src/adapter/src/catalog/builtin_table_updates.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,7 @@ use mz_adapter_types::dyncfgs::ENABLE_PASSWORD_AUTH;
1616
use mz_audit_log::{EventDetails, EventType, ObjectType, VersionedEvent, VersionedStorageUsage};
1717
use mz_catalog::SYSTEM_CONN_ID;
1818
use mz_catalog::builtin::{
19-
BuiltinTable, MZ_AGGREGATES, MZ_ARRAY_TYPES, MZ_AUDIT_EVENTS, MZ_AWS_CONNECTIONS,
20-
MZ_AWS_PRIVATELINK_CONNECTIONS, MZ_BASE_TYPES, MZ_CLUSTER_REPLICA_SIZES, MZ_CLUSTER_REPLICAS,
21-
MZ_CLUSTER_SCHEDULES, MZ_CLUSTER_WORKLOAD_CLASSES, MZ_CLUSTERS, MZ_COLUMNS, MZ_COMMENTS,
22-
MZ_CONNECTIONS, MZ_CONTINUAL_TASKS, MZ_DATABASES, MZ_DEFAULT_PRIVILEGES, MZ_EGRESS_IPS,
23-
MZ_FUNCTIONS, MZ_HISTORY_RETENTION_STRATEGIES, MZ_ICEBERG_SINKS, MZ_INDEX_COLUMNS, MZ_INDEXES,
24-
MZ_INTERNAL_CLUSTER_REPLICAS, MZ_KAFKA_CONNECTIONS, MZ_KAFKA_SINKS, MZ_KAFKA_SOURCE_TABLES,
25-
MZ_KAFKA_SOURCES, MZ_LICENSE_KEYS, MZ_LIST_TYPES, MZ_MAP_TYPES,
26-
MZ_MATERIALIZED_VIEW_REFRESH_STRATEGIES, MZ_MATERIALIZED_VIEWS, MZ_MYSQL_SOURCE_TABLES,
27-
MZ_NETWORK_POLICIES, MZ_NETWORK_POLICY_RULES, MZ_OBJECT_DEPENDENCIES, MZ_OBJECT_GLOBAL_IDS,
28-
MZ_OPERATORS, MZ_PENDING_CLUSTER_REPLICAS, MZ_POSTGRES_SOURCE_TABLES, MZ_POSTGRES_SOURCES,
29-
MZ_PSEUDO_TYPES, MZ_ROLE_AUTH, MZ_ROLE_MEMBERS, MZ_ROLE_PARAMETERS, MZ_ROLES, MZ_SCHEMAS,
30-
MZ_SECRETS, MZ_SESSIONS, MZ_SINKS, MZ_SOURCE_REFERENCES, MZ_SOURCES,
31-
MZ_SQL_SERVER_SOURCE_TABLES, MZ_SSH_TUNNEL_CONNECTIONS, MZ_STORAGE_USAGE_BY_SHARD,
32-
MZ_SUBSCRIPTIONS, MZ_SYSTEM_PRIVILEGES, MZ_TABLES, MZ_TYPE_PG_METADATA, MZ_TYPES, MZ_VIEWS,
33-
MZ_WEBHOOKS_SOURCES,
19+
BuiltinTable, MZ_AGGREGATES, MZ_ARRAY_TYPES, MZ_AUDIT_EVENTS, MZ_AWS_CONNECTIONS, MZ_AWS_PRIVATELINK_CONNECTIONS, MZ_BASE_TYPES, MZ_CLUSTERS, MZ_CLUSTER_REPLICAS, MZ_CLUSTER_REPLICA_SIZES, MZ_CLUSTER_SCHEDULES, MZ_CLUSTER_WORKLOAD_CLASSES, MZ_COLUMNS, MZ_COMMENTS, MZ_CONNECTIONS, MZ_CONTINUAL_TASKS, MZ_DATABASES, MZ_DEFAULT_PRIVILEGES, MZ_EGRESS_IPS, MZ_FUNCTIONS, MZ_HISTORY_RETENTION_STRATEGIES, MZ_ICEBERG_SINKS, MZ_INDEXES, MZ_INDEX_COLUMNS, MZ_INTERNAL_CLUSTER_REPLICAS, MZ_KAFKA_CONNECTIONS, MZ_KAFKA_SINKS, MZ_KAFKA_SOURCES, MZ_KAFKA_SOURCE_TABLES, MZ_LICENSE_KEYS, MZ_LIST_TYPES, MZ_MAP_TYPES, MZ_MATERIALIZED_VIEWS, MZ_MATERIALIZED_VIEW_REFRESH_STRATEGIES, MZ_MYSQL_SOURCE_TABLES, MZ_NETWORK_POLICIES, MZ_NETWORK_POLICY_RULES, MZ_OBJECT_DEPENDENCIES, MZ_OBJECT_GLOBAL_IDS, MZ_OPERATORS, MZ_PENDING_CLUSTER_REPLICAS, MZ_POSTGRES_SOURCES, MZ_POSTGRES_SOURCE_TABLES, MZ_PSEUDO_TYPES, MZ_REPLACEMENTS, MZ_ROLES, MZ_ROLE_AUTH, MZ_ROLE_MEMBERS, MZ_ROLE_PARAMETERS, MZ_SCHEMAS, MZ_SECRETS, MZ_SESSIONS, MZ_SINKS, MZ_SOURCES, MZ_SOURCE_REFERENCES, MZ_SQL_SERVER_SOURCE_TABLES, MZ_SSH_TUNNEL_CONNECTIONS, MZ_STORAGE_USAGE_BY_SHARD, MZ_SUBSCRIPTIONS, MZ_SYSTEM_PRIVILEGES, MZ_TABLES, MZ_TYPES, MZ_TYPE_PG_METADATA, MZ_VIEWS, MZ_WEBHOOKS_SOURCES
3420
};
3521
use mz_catalog::config::AwsPrincipalContext;
3622
use mz_catalog::durable::SourceReferences;
@@ -1538,6 +1524,17 @@ impl CatalogState {
15381524
));
15391525
}
15401526

1527+
if let Some(target_id) = mview.replacement_target {
1528+
updates.push(BuiltinTableUpdate::row(
1529+
&*MZ_REPLACEMENTS,
1530+
Row::pack_slice(&[
1531+
Datum::String(&id.to_string()),
1532+
Datum::String(&target_id.to_string()),
1533+
]),
1534+
diff,
1535+
));
1536+
}
1537+
15411538
updates
15421539
}
15431540

src/catalog/src/builtin.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5545,6 +5545,28 @@ pub static MZ_LICENSE_KEYS: LazyLock<BuiltinTable> = LazyLock::new(|| BuiltinTab
55455545
access: vec![PUBLIC_SELECT],
55465546
});
55475547

5548+
pub static MZ_REPLACEMENTS: LazyLock<BuiltinTable> = LazyLock::new(|| BuiltinTable {
5549+
name: "mz_replacements",
5550+
schema: MZ_INTERNAL_SCHEMA,
5551+
oid: oid::TABLE_MZ_REPLACEMENTS_OID,
5552+
desc: RelationDesc::builder()
5553+
.with_column("id", SqlScalarType::String.nullable(false))
5554+
.with_column("target_id", SqlScalarType::String.nullable(false))
5555+
.finish(),
5556+
column_comments: BTreeMap::from_iter([
5557+
(
5558+
"id",
5559+
"The ID of the replacement object. Corresponds to `mz_objects.id`.",
5560+
),
5561+
(
5562+
"target_id",
5563+
"The ID of the replacement target. Corresponds to `mz_objects.id`.",
5564+
),
5565+
]),
5566+
is_retained_metrics_object: false,
5567+
access: vec![PUBLIC_SELECT],
5568+
});
5569+
55485570
// These will be replaced with per-replica tables once source/sink multiplexing on
55495571
// a single cluster is supported.
55505572
pub static MZ_SOURCE_STATISTICS_RAW: LazyLock<BuiltinSource> = LazyLock::new(|| BuiltinSource {
@@ -11108,22 +11130,30 @@ pub static MZ_SHOW_MATERIALIZED_VIEWS: LazyLock<BuiltinView> = LazyLock::new(||
1110811130
.finish(),
1110911131
column_comments: BTreeMap::new(),
1111011132
sql: "
11111-
WITH comments AS (
11112-
SELECT id, comment
11113-
FROM mz_internal.mz_comments
11114-
WHERE object_type = 'materialized-view' AND object_sub_id IS NULL
11115-
)
11133+
WITH
11134+
comments AS (
11135+
SELECT id, comment
11136+
FROM mz_internal.mz_comments
11137+
WHERE object_type = 'materialized-view' AND object_sub_id IS NULL
11138+
),
11139+
replacements AS (
11140+
SELECT r.id, mv.name AS target_name
11141+
FROM mz_internal.mz_replacements r
11142+
JOIN mz_materialized_views mv ON r.target_id = mv.id
11143+
)
1111611144
SELECT
1111711145
mviews.id as id,
1111811146
mviews.name,
1111911147
clusters.name AS cluster,
1112011148
schema_id,
1112111149
cluster_id,
11122-
COALESCE(comments.comment, '') as comment
11150+
COALESCE(comments.comment, '') as comment,
11151+
replacements.target_name as replacing
1112311152
FROM
1112411153
mz_catalog.mz_materialized_views AS mviews
1112511154
JOIN mz_catalog.mz_clusters AS clusters ON clusters.id = mviews.cluster_id
11126-
LEFT JOIN comments ON mviews.id = comments.id",
11155+
LEFT JOIN comments ON mviews.id = comments.id
11156+
LEFT JOIN replacements ON mviews.id = replacements.id",
1112711157
access: vec![PUBLIC_SELECT],
1112811158
});
1112911159

@@ -13839,6 +13869,7 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
1383913869
Builtin::Table(&MZ_NETWORK_POLICIES),
1384013870
Builtin::Table(&MZ_NETWORK_POLICY_RULES),
1384113871
Builtin::Table(&MZ_LICENSE_KEYS),
13872+
Builtin::Table(&MZ_REPLACEMENTS),
1384213873
Builtin::View(&MZ_RELATIONS),
1384313874
Builtin::View(&MZ_OBJECT_OID_ALIAS),
1384413875
Builtin::View(&MZ_OBJECTS),

src/pgrepr-consts/src/oid.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ pub const VIEW_MZ_STORAGE_USAGE_OID: u32 = 16771;
497497
pub const VIEW_MZ_RELATIONS_OID: u32 = 16772;
498498
pub const VIEW_MZ_OBJECT_OID_ALIAS_OID: u32 = 16773;
499499
pub const VIEW_MZ_OBJECTS_OID: u32 = 16774;
500-
pub const VIEW_MZ_OBJECT_GLOBAL_IDS_OID: u32 = 17061;
501500
pub const VIEW_MZ_OBJECT_FULLY_QUALIFIED_NAMES_OID: u32 = 16775;
502501
pub const VIEW_MZ_OBJECT_LIFETIMES_OID: u32 = 16776;
503502
pub const VIEW_MZ_DATAFLOWS_PER_WORKER_OID: u32 = 16777;
@@ -782,3 +781,5 @@ pub const TABLE_MZ_SQL_SERVER_SOURCE_TABLES_OID: u32 = 17057;
782781
pub const TABLE_MZ_LICENSE_KEYS_OID: u32 = 17058;
783782
pub const TABLE_MZ_ROLE_AUTH_OID: u32 = 17059;
784783
pub const TABLE_MZ_ICEBERG_SINKS_OID: u32 = 17060;
784+
pub const VIEW_MZ_OBJECT_GLOBAL_IDS_OID: u32 = 17061;
785+
pub const TABLE_MZ_REPLACEMENTS_OID: u32 = 17062;

src/sql/src/plan/statement/show.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use crate::plan::statement::{StatementContext, StatementDesc, dml};
4747
use crate::plan::{
4848
HirRelationExpr, Params, Plan, PlanError, ShowColumnsPlan, ShowCreatePlan, query, transform_ast,
4949
};
50+
use crate::session::vars::ENABLE_REPLACEMENT_MATERIALIZED_VIEWS;
5051

5152
pub fn describe_show_create_view(
5253
_: &StatementContext,
@@ -576,18 +577,17 @@ fn show_materialized_views<'a>(
576577
}
577578

578579
let query = format!(
579-
"SELECT name, cluster, comment
580-
FROM mz_internal.mz_show_materialized_views
581-
WHERE {where_clause}"
580+
"SELECT name, cluster, comment, replacing
581+
FROM mz_internal.mz_show_materialized_views
582+
WHERE {where_clause}"
582583
);
583584

584-
ShowSelect::new(
585-
scx,
586-
query,
587-
filter,
588-
None,
589-
Some(&["name", "cluster", "comment"]),
590-
)
585+
let mut projection = vec!["name", "cluster", "comment"];
586+
if scx.is_feature_flag_enabled(&ENABLE_REPLACEMENT_MATERIALIZED_VIEWS) {
587+
projection.push("replacing");
588+
}
589+
590+
ShowSelect::new(scx, query, filter, None, Some(&projection))
591591
}
592592

593593
fn show_sinks<'a>(

test/sqllogictest/replacement-materialized-views.slt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ SELECT * FROM mv
4343
3 4
4444
5 6
4545

46-
query TTT colnames,rowsort
46+
query TTTT colnames,rowsort
4747
SHOW MATERIALIZED VIEWS
4848
----
49-
name cluster comment
50-
mv quickstart (empty)
51-
rp quickstart (empty)
49+
name cluster comment replacing
50+
mv quickstart (empty) NULL
51+
rp quickstart (empty) mv
5252

5353
statement ok
5454
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
5555

56-
query TTT colnames,rowsort
56+
query TTTT colnames,rowsort
5757
SHOW MATERIALIZED VIEWS
5858
----
59-
name cluster comment
60-
mv quickstart (empty)
59+
name cluster comment replacing
60+
mv quickstart (empty) NULL
6161

6262
query II
6363
SELECT * FROM mv
@@ -79,21 +79,21 @@ SELECT * FROM mv
7979
7 4
8080
11 6
8181

82-
query TTT colnames,rowsort
82+
query TTTT colnames,rowsort
8383
SHOW MATERIALIZED VIEWS
8484
----
85-
name cluster comment
86-
mv quickstart (empty)
87-
rp quickstart (empty)
85+
name cluster comment replacing
86+
mv quickstart (empty) NULL
87+
rp quickstart (empty) mv
8888

8989
statement ok
9090
DROP MATERIALIZED VIEW rp
9191

92-
query TTT colnames,rowsort
92+
query TTTT colnames,rowsort
9393
SHOW MATERIALIZED VIEWS
9494
----
95-
name cluster comment
96-
mv quickstart (empty)
95+
name cluster comment replacing
96+
mv quickstart (empty) NULL
9797

9898
query II
9999
SELECT * FROM mv
@@ -108,43 +108,43 @@ SELECT * FROM mv
108108
statement ok
109109
CREATE MATERIALIZED VIEW rp REPLACING mv IN CLUSTER other AS SELECT a, b FROM t;
110110

111-
query TTT colnames,rowsort
111+
query TTTT colnames,rowsort
112112
SHOW MATERIALIZED VIEWS
113113
----
114-
name cluster comment
115-
mv quickstart (empty)
116-
rp other (empty)
114+
name cluster comment replacing
115+
mv quickstart (empty) NULL
116+
rp other (empty) mv
117117

118118
statement ok
119119
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
120120

121-
query TTT colnames,rowsort
121+
query TTTT colnames,rowsort
122122
SHOW MATERIALIZED VIEWS
123123
----
124-
name cluster comment
125-
mv other (empty)
124+
name cluster comment replacing
125+
mv other (empty) NULL
126126

127127

128128
# Test: replacement can have the same query
129129

130130
statement ok
131131
CREATE MATERIALIZED VIEW rp REPLACING mv IN CLUSTER other AS SELECT a, b FROM t;
132132

133-
query TTT colnames,rowsort
133+
query TTTT colnames,rowsort
134134
SHOW MATERIALIZED VIEWS
135135
----
136-
name cluster comment
137-
mv other (empty)
138-
rp other (empty)
136+
name cluster comment replacing
137+
mv other (empty) NULL
138+
rp other (empty) mv
139139

140140
statement ok
141141
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
142142

143-
query TTT colnames,rowsort
143+
query TTTT colnames,rowsort
144144
SHOW MATERIALIZED VIEWS
145145
----
146-
name cluster comment
147-
mv other (empty)
146+
name cluster comment replacing
147+
mv other (empty) NULL
148148

149149

150150
# Test: usage errors

0 commit comments

Comments
 (0)