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::CreateCluster(_)
85 | Plan::CreateClusterReplica(_)
86 | Plan::CreateSource(_)
87 | Plan::CreateSources(_)
88 | Plan::CreateSecret(_)
89 | Plan::CreateSink(_)
90 | Plan::CreateTable(_)
91 | Plan::CreateView(_)
92 | Plan::CreateMaterializedView(_)
93 | Plan::CreateIndex(_)
94 | Plan::CreateType(_)
95 | Plan::Comment(_)
96 | Plan::DiscardTemp
97 | Plan::DiscardAll
98 | Plan::DropObjects(_)
99 | Plan::DropOwned(_)
100 | Plan::EmptyQuery
101 | Plan::ShowAllVariables
102 | Plan::ShowCreate(_)
103 | Plan::ShowVariable(_)
104 | Plan::InspectShard(_)
105 | Plan::SetVariable(_)
106 | Plan::ResetVariable(_)
107 | Plan::SetTransaction(_)
108 | Plan::StartTransaction(_)
109 | Plan::CommitTransaction(_)
110 | Plan::AbortTransaction(_)
111 | Plan::CopyFrom(_)
112 | Plan::CopyTo(_)
113 | Plan::ExplainPlan(ExplainPlanPlan {
114 explainee:
115 Explainee::Statement(
116 ExplaineeStatement::CreateView { .. }
119 | ExplaineeStatement::CreateMaterializedView { .. }
120 | ExplaineeStatement::CreateIndex { .. },
121 ),
122 ..
123 })
124 | Plan::ExplainPlan(ExplainPlanPlan { explainee: _, .. })
125 | Plan::ExplainPushdown(_)
126 | Plan::ExplainSinkSchema(_)
127 | Plan::Insert(_)
128 | Plan::AlterNetworkPolicy(_)
129 | Plan::AlterNoop(_)
130 | Plan::AlterClusterRename(_)
131 | Plan::AlterClusterSwap(_)
132 | Plan::AlterClusterReplicaRename(_)
133 | Plan::AlterCluster(_)
134 | Plan::AlterConnection(_)
135 | Plan::AlterSource(_)
136 | Plan::AlterSetCluster(_)
137 | Plan::AlterItemRename(_)
138 | Plan::AlterRetainHistory(_)
139 | Plan::AlterSourceTimestampInterval(_)
140 | Plan::AlterSchemaRename(_)
141 | Plan::AlterSchemaSwap(_)
142 | Plan::AlterSecret(_)
143 | Plan::AlterSink(_)
144 | Plan::AlterSystemSet(_)
145 | Plan::AlterSystemReset(_)
146 | Plan::AlterSystemResetAll(_)
147 | Plan::AlterRole(_)
148 | Plan::AlterOwner(_)
149 | Plan::AlterTableAddColumn(_)
150 | Plan::AlterMaterializedViewApplyReplacement(_)
151 | Plan::Declare(_)
152 | Plan::Fetch(_)
153 | Plan::Close(_)
154 | Plan::ReadThenWrite(_)
155 | Plan::Prepare(_)
156 | Plan::Execute(_)
157 | Plan::Deallocate(_)
158 | Plan::Raise(_)
159 | Plan::GrantRole(_)
160 | Plan::RevokeRole(_)
161 | Plan::GrantPrivileges(_)
162 | Plan::RevokePrivileges(_)
163 | Plan::AlterDefaultPrivileges(_)
164 | Plan::ReassignOwned(_)
165 | Plan::ValidateConnection(_)
166 | Plan::SideEffectingFunc(_) => return TargetCluster::Active,
167 };
168
169 if !session.vars().auto_route_catalog_queries() {
171 return TargetCluster::Active;
172 }
173
174 if session.vars().cluster_replica().is_some() {
176 return TargetCluster::Active;
177 }
178
179 let mut depends_on = depends_on
181 .into_iter()
182 .map(|gid| catalog.resolve_item_id(&gid))
183 .peekable();
184 let has_dependencies = depends_on.peek().is_some();
185
186 let valid_dependencies = depends_on.all(|id| {
189 let entry = catalog.state().get_entry(&id);
190 let schema = entry.name().qualifiers.schema_spec;
191
192 let system_only = catalog.state().is_system_schema_specifier(schema);
193 let non_replica = catalog.state().introspection_dependencies(id).is_empty();
194
195 system_only && non_replica
196 });
197
198 if (has_dependencies && valid_dependencies)
199 || (!has_dependencies && !could_run_expensive_function)
200 {
201 let intros_cluster = catalog
202 .state()
203 .resolve_builtin_cluster(&MZ_CATALOG_SERVER_CLUSTER);
204 tracing::debug!("Running on '{}' cluster", MZ_CATALOG_SERVER_CLUSTER.name);
205
206 if intros_cluster.name != session.vars().cluster() {
208 session.add_notice(AdapterNotice::AutoRunOnCatalogServerCluster);
209 }
210 TargetCluster::CatalogServer
211 } else {
212 TargetCluster::Active
213 }
214}
215
216pub fn check_cluster_restrictions(
219 cluster: &str,
220 catalog: &impl SessionCatalog,
221 plan: &Plan,
222) -> Result<(), AdapterError> {
223 if cluster != MZ_CATALOG_SERVER_CLUSTER.name {
225 return Ok(());
226 }
227
228 let depends_on: Box<dyn Iterator<Item = GlobalId>> = match plan {
236 Plan::ReadThenWrite(plan) => Box::new(plan.selection.depends_on().into_iter()),
237 Plan::Insert(plan) => Box::new(plan.values.depends_on().into_iter()),
244 Plan::Subscribe(plan) => match plan.from {
245 SubscribeFrom::Id(id) => Box::new(std::iter::once(id)),
246 SubscribeFrom::Query { ref expr, .. } => Box::new(expr.depends_on().into_iter()),
247 },
248 Plan::Select(plan) => Box::new(plan.source.depends_on().into_iter()),
249 Plan::CopyTo(plan) => Box::new(plan.select_plan.source.depends_on().into_iter()),
253 _ => return Ok(()),
254 };
255
256 let unallowed_dependents: SmallVec<[String; 2]> = depends_on
262 .filter_map(|id| {
263 let item = catalog.get_item_by_global_id(&id);
264 let schema_spec = item.name().qualifiers.schema_spec;
265
266 if !catalog.is_system_schema_specifier(schema_spec) {
267 let full_name = catalog.resolve_full_name(item.name());
268 Some(full_name.to_string())
269 } else {
270 None
271 }
272 })
273 .collect();
274
275 if !unallowed_dependents.is_empty() {
277 Err(AdapterError::UnallowedOnCluster {
278 depends_on: unallowed_dependents,
279 cluster: MZ_CATALOG_SERVER_CLUSTER.name.to_string(),
280 })
281 } else {
282 Ok(())
283 }
284}