mz_adapter/coord/
catalog_serving.rs1use mz_expr::CollectionPlan;
22use mz_repr::GlobalId;
23use mz_sql::catalog::SessionCatalog;
24use mz_sql::plan::{
25 ExplainPlanPlan, ExplainTimestampPlan, Explainee, ExplaineeStatement, Plan, SubscribeFrom,
26 SubscribePlan,
27};
28use smallvec::SmallVec;
29
30use crate::AdapterError;
31use crate::catalog::ConnCatalog;
32use crate::coord::TargetCluster;
33use crate::notice::AdapterNotice;
34use crate::session::Session;
35use mz_catalog::builtin::MZ_CATALOG_SERVER_CLUSTER;
36
37pub fn auto_run_on_catalog_server<'a, 's, 'p>(
40 catalog: &'a ConnCatalog<'a>,
41 session: &'s Session,
42 plan: &'p Plan,
43) -> TargetCluster {
44 let inspect_subscribe = |plan: &SubscribePlan| {
45 (
46 plan.from.depends_on(),
47 match &plan.from {
48 SubscribeFrom::Id(_) => false,
49 SubscribeFrom::Query { expr, desc: _ } => expr.could_run_expensive_function(),
50 },
51 )
52 };
53
54 let (depends_on, could_run_expensive_function) = match plan {
55 Plan::Select(plan) => (
56 plan.source.depends_on(),
57 plan.source.could_run_expensive_function(),
58 ),
59 Plan::ShowColumns(plan) => (
60 plan.select_plan.source.depends_on(),
61 plan.select_plan.source.could_run_expensive_function(),
62 ),
63 Plan::Subscribe(plan) => inspect_subscribe(plan),
64 Plan::ExplainPlan(ExplainPlanPlan {
65 explainee: Explainee::Statement(ExplaineeStatement::Select { plan, .. }),
66 ..
67 }) => (
68 plan.source.depends_on(),
69 plan.source.could_run_expensive_function(),
70 ),
71 Plan::ExplainPlan(ExplainPlanPlan {
72 explainee: Explainee::Statement(ExplaineeStatement::Subscribe { plan, .. }),
73 ..
74 }) => inspect_subscribe(plan),
75 Plan::ExplainTimestamp(ExplainTimestampPlan { raw_plan, .. }) => (
76 raw_plan.depends_on(),
77 raw_plan.could_run_expensive_function(),
78 ),
79 Plan::CreateConnection(_)
80 | Plan::CreateDatabase(_)
81 | Plan::CreateSchema(_)
82 | Plan::CreateRole(_)
83 | Plan::CreateNetworkPolicy(_)
84 | Plan::CreateQueryPolicy(_)
85 | Plan::CreateCluster(_)
86 | Plan::CreateClusterReplica(_)
87 | Plan::CreateSource(_)
88 | Plan::CreateSources(_)
89 | Plan::CreateSecret(_)
90 | Plan::CreateSink(_)
91 | Plan::CreateTable(_)
92 | Plan::CreateView(_)
93 | Plan::CreateMaterializedView(_)
94 | Plan::CreateIndex(_)
95 | Plan::CreateMetricSink(_)
96 | Plan::CreateType(_)
97 | Plan::Comment(_)
98 | Plan::DiscardTemp
99 | Plan::DiscardAll
100 | Plan::DropObjects(_)
101 | Plan::DropOwned(_)
102 | Plan::EmptyQuery
103 | Plan::ShowAllVariables
104 | Plan::ShowCreate(_)
105 | Plan::ShowVariable(_)
106 | Plan::InspectShard(_)
107 | Plan::SetVariable(_)
108 | Plan::ResetVariable(_)
109 | Plan::SetTransaction(_)
110 | Plan::StartTransaction(_)
111 | Plan::CommitTransaction(_)
112 | Plan::AbortTransaction(_)
113 | Plan::CopyFrom(_)
114 | Plan::CopyTo(_)
115 | Plan::ExplainPlan(ExplainPlanPlan {
116 explainee:
117 Explainee::Statement(
118 ExplaineeStatement::CreateView { .. }
121 | ExplaineeStatement::CreateMaterializedView { .. }
122 | ExplaineeStatement::CreateIndex { .. },
123 ),
124 ..
125 })
126 | Plan::ExplainPlan(ExplainPlanPlan { explainee: _, .. })
127 | Plan::ExplainPushdown(_)
128 | Plan::ExplainSinkSchema(_)
129 | Plan::Insert(_)
130 | Plan::AlterNetworkPolicy(_)
131 | Plan::AlterQueryPolicy(_)
132 | Plan::AlterNoop(_)
133 | Plan::AlterClusterRename(_)
134 | Plan::AlterClusterSwap(_)
135 | Plan::AlterClusterReplicaRename(_)
136 | Plan::AlterCluster(_)
137 | Plan::AlterConnection(_)
138 | Plan::AlterSource(_)
139 | Plan::AlterSetCluster(_)
140 | Plan::AlterItemRename(_)
141 | Plan::AlterRetainHistory(_)
142 | Plan::AlterSourceTimestampInterval(_)
143 | Plan::AlterSchemaRename(_)
144 | Plan::AlterSchemaSwap(_)
145 | Plan::AlterSecret(_)
146 | Plan::AlterSink(_)
147 | Plan::AlterSystemSet(_)
148 | Plan::AlterSystemReset(_)
149 | Plan::AlterSystemResetAll(_)
150 | Plan::AlterRole(_)
151 | Plan::AlterOwner(_)
152 | Plan::AlterTableAddColumn(_)
153 | Plan::AlterMaterializedViewApplyReplacement(_)
154 | Plan::Declare(_)
155 | Plan::Fetch(_)
156 | Plan::Close(_)
157 | Plan::ReadThenWrite(_)
158 | Plan::Prepare(_)
159 | Plan::Execute(_)
160 | Plan::Deallocate(_)
161 | Plan::Raise(_)
162 | Plan::GrantRole(_)
163 | Plan::RevokeRole(_)
164 | Plan::GrantPrivileges(_)
165 | Plan::RevokePrivileges(_)
166 | Plan::AlterDefaultPrivileges(_)
167 | Plan::ReassignOwned(_)
168 | Plan::ValidateConnection(_)
169 | Plan::SideEffectingFunc(_) => return TargetCluster::Active,
170 };
171
172 if !session.vars().auto_route_catalog_queries() {
174 return TargetCluster::Active;
175 }
176
177 if session.vars().cluster_replica().is_some() {
179 return TargetCluster::Active;
180 }
181
182 let mut depends_on = depends_on
184 .into_iter()
185 .map(|gid| catalog.resolve_item_id(&gid))
186 .peekable();
187 let has_dependencies = depends_on.peek().is_some();
188
189 let valid_dependencies = depends_on.all(|id| {
192 let entry = catalog.state().get_entry(&id);
193 let schema = entry.name().qualifiers.schema_spec;
194
195 let system_only = catalog.state().is_system_schema_specifier(schema);
196 let non_replica = catalog.state().introspection_dependencies(id).is_empty();
197
198 system_only && non_replica
199 });
200
201 if (has_dependencies && valid_dependencies)
202 || (!has_dependencies && !could_run_expensive_function)
203 {
204 let intros_cluster = catalog
205 .state()
206 .resolve_builtin_cluster(&MZ_CATALOG_SERVER_CLUSTER);
207 tracing::debug!("Running on '{}' cluster", MZ_CATALOG_SERVER_CLUSTER.name);
208
209 if intros_cluster.name != session.vars().cluster() {
211 session.add_notice(AdapterNotice::AutoRunOnCatalogServerCluster);
212 }
213 TargetCluster::CatalogServer
214 } else {
215 TargetCluster::Active
216 }
217}
218
219pub fn check_cluster_restrictions(
222 cluster: &str,
223 catalog: &impl SessionCatalog,
224 plan: &Plan,
225) -> Result<(), AdapterError> {
226 if cluster != MZ_CATALOG_SERVER_CLUSTER.name {
228 return Ok(());
229 }
230
231 let depends_on: Box<dyn Iterator<Item = GlobalId>> = match plan {
239 Plan::ReadThenWrite(plan) => Box::new(plan.selection.depends_on().into_iter()),
240 Plan::Insert(plan) => Box::new(plan.values.depends_on().into_iter()),
247 Plan::Subscribe(plan) => match plan.from {
248 SubscribeFrom::Id(id) => Box::new(std::iter::once(id)),
249 SubscribeFrom::Query { ref expr, .. } => Box::new(expr.depends_on().into_iter()),
250 },
251 Plan::Select(plan) => Box::new(plan.source.depends_on().into_iter()),
252 Plan::CopyTo(plan) => Box::new(plan.select_plan.source.depends_on().into_iter()),
256 _ => return Ok(()),
257 };
258
259 let unallowed_dependents: SmallVec<[String; 2]> = depends_on
265 .filter_map(|id| {
266 let item = catalog.get_item_by_global_id(&id);
267 let schema_spec = item.name().qualifiers.schema_spec;
268
269 if !catalog.is_system_schema_specifier(schema_spec) {
270 let full_name = catalog.resolve_full_name(item.name());
271 Some(full_name.to_string())
272 } else {
273 None
274 }
275 })
276 .collect();
277
278 if !unallowed_dependents.is_empty() {
280 Err(AdapterError::UnallowedOnCluster {
281 depends_on: unallowed_dependents,
282 cluster: MZ_CATALOG_SERVER_CLUSTER.name.to_string(),
283 })
284 } else {
285 Ok(())
286 }
287}