mz_adapter/
query_policy.rs1use mz_compute_types::dataflows::DataflowDescription;
13use mz_controller_types::ClusterId;
14use mz_ore::str::StrExt;
15use mz_sql::plan::{QueryPlanFeature, QueryPolicyMode};
16
17use crate::catalog::Catalog;
18use crate::coord::peek::{FastPathPlan, PeekPlan};
19use crate::session::Session;
20use crate::{AdapterError, AdapterNotice};
21
22pub(crate) struct QueryPlanFeatures {
24 temporary_dataflow: bool,
25 storage_read: bool,
26}
27
28impl From<&PeekPlan> for QueryPlanFeatures {
29 fn from(plan: &PeekPlan) -> Self {
30 match plan {
31 PeekPlan::SlowPath(dataflow) => Self::from(&dataflow.desc),
32 PeekPlan::FastPath(plan) => Self {
33 temporary_dataflow: false,
34 storage_read: matches!(plan, FastPathPlan::PeekPersist(..)),
35 },
36 }
37 }
38}
39
40impl<P, S, O> From<&DataflowDescription<P, S, O>> for QueryPlanFeatures {
41 fn from(dataflow: &DataflowDescription<P, S, O>) -> Self {
42 Self {
43 temporary_dataflow: true,
46 storage_read: !dataflow.source_imports.is_empty(),
47 }
48 }
49}
50
51#[derive(Clone, Debug)]
53pub struct QueryPolicyViolation {
54 pub policy: String,
55 pub rule: String,
56 pub cluster: String,
57 pub feature: QueryPlanFeature,
58}
59
60impl QueryPolicyViolation {
61 pub fn reason(&self) -> &'static str {
62 match self.feature {
63 QueryPlanFeature::TemporaryDataflow => "this query would build a temporary dataflow",
64 QueryPlanFeature::StorageRead => "this query would read from object storage",
65 }
66 }
67
68 pub fn detail(&self) -> String {
69 format!(
70 "Query policy {} rule {} applies to this query on cluster {}.",
71 self.policy.quoted(),
72 self.rule.quoted(),
73 self.cluster.quoted(),
74 )
75 }
76
77 pub fn hint(&self) -> &'static str {
78 "Run EXPLAIN to inspect the query plan. Create an index that serves the query on this \
79 cluster, or use a cluster whose query policy permits this plan."
80 }
81}
82
83pub(crate) fn check_query_policies(
88 catalog: &Catalog,
89 session: &Session,
90 cluster_id: ClusterId,
91 plan: impl Into<QueryPlanFeatures>,
92) -> Result<(), AdapterError> {
93 if !catalog.system_config().enable_query_policy_enforcement() {
94 return Ok(());
95 }
96
97 let plan = plan.into();
98 let includes = |feature| match feature {
99 QueryPlanFeature::TemporaryDataflow => plan.temporary_dataflow,
100 QueryPlanFeature::StorageRead => plan.storage_read,
101 };
102
103 let mut rejection = None;
104 let mut warned = false;
105 if let Some(policy) = catalog.state().query_policy_for(cluster_id) {
106 for rule in &policy.rules {
107 if !includes(rule.value) {
108 continue;
109 }
110 let violation = QueryPolicyViolation {
111 policy: policy.name.clone(),
112 rule: rule.name.clone(),
113 cluster: catalog.get_cluster(cluster_id).name.clone(),
114 feature: rule.value,
115 };
116 match policy.mode {
117 QueryPolicyMode::Warn => {
118 warned = true;
119 session.add_notice(AdapterNotice::QueryPolicyWarning(violation));
120 }
121 QueryPolicyMode::Enforce => {
122 rejection.get_or_insert(violation);
123 }
124 }
125 }
126 }
127 if let Some(violation) = rejection {
128 session.metrics().query_policy_queries("rejected").inc();
129 Err(AdapterError::QueryPolicyRejected(violation))
130 } else {
131 if warned {
132 session.metrics().query_policy_queries("warned").inc();
133 }
134 Ok(())
135 }
136}