|
| 1 | +//! AST-based DDL dispatch — typed fast path. |
| 2 | +//! |
| 3 | +//! Runs before the legacy string-prefix routers. Handles |
| 4 | +//! `IF [NOT] EXISTS` at the dispatch level so individual handlers |
| 5 | +//! don't need to check. Falls through to legacy dispatch for |
| 6 | +//! `Other` variants and for statements where the typed path |
| 7 | +//! delegates to the existing handler (via `raw_sql`). |
| 8 | +
|
| 9 | +use pgwire::api::results::{Response, Tag}; |
| 10 | +use pgwire::error::PgWireResult; |
| 11 | + |
| 12 | +use nodedb_sql::ddl_ast::NodedbStatement; |
| 13 | + |
| 14 | +use crate::control::security::identity::AuthenticatedIdentity; |
| 15 | +use crate::control::state::SharedState; |
| 16 | + |
| 17 | +/// Try to dispatch a parsed `NodedbStatement`. Returns `Some` if |
| 18 | +/// fully handled, `None` if the statement should fall through to |
| 19 | +/// the legacy dispatch. |
| 20 | +pub(super) fn try_dispatch( |
| 21 | + state: &SharedState, |
| 22 | + identity: &AuthenticatedIdentity, |
| 23 | + stmt: &NodedbStatement, |
| 24 | +) -> Option<PgWireResult<Vec<Response>>> { |
| 25 | + match stmt { |
| 26 | + // ── IF NOT EXISTS: swallow duplicate-creation errors ────── |
| 27 | + NodedbStatement::CreateCollection { |
| 28 | + name, |
| 29 | + if_not_exists: true, |
| 30 | + .. |
| 31 | + } => { |
| 32 | + if collection_exists(state, identity, name) { |
| 33 | + return Some(Ok(vec![Response::Execution(Tag::new("CREATE COLLECTION"))])); |
| 34 | + } |
| 35 | + None // fall through to legacy CREATE handler |
| 36 | + } |
| 37 | + |
| 38 | + NodedbStatement::CreateSequence { |
| 39 | + name, |
| 40 | + if_not_exists: true, |
| 41 | + .. |
| 42 | + } => { |
| 43 | + if sequence_exists(state, identity, name) { |
| 44 | + return Some(Ok(vec![Response::Execution(Tag::new("CREATE SEQUENCE"))])); |
| 45 | + } |
| 46 | + None |
| 47 | + } |
| 48 | + |
| 49 | + // ── IF EXISTS: swallow not-found errors on DROP ────────── |
| 50 | + NodedbStatement::DropCollection { |
| 51 | + name, |
| 52 | + if_exists: true, |
| 53 | + } => { |
| 54 | + if !collection_exists(state, identity, name) { |
| 55 | + return Some(Ok(vec![Response::Execution(Tag::new("DROP COLLECTION"))])); |
| 56 | + } |
| 57 | + None |
| 58 | + } |
| 59 | + |
| 60 | + NodedbStatement::DropIndex { |
| 61 | + if_exists: true, .. |
| 62 | + } => None, // legacy handler has its own check |
| 63 | + |
| 64 | + NodedbStatement::DropTrigger { |
| 65 | + name, |
| 66 | + if_exists: true, |
| 67 | + .. |
| 68 | + } => { |
| 69 | + if !trigger_exists(state, identity, name) { |
| 70 | + return Some(Ok(vec![Response::Execution(Tag::new("DROP TRIGGER"))])); |
| 71 | + } |
| 72 | + None |
| 73 | + } |
| 74 | + |
| 75 | + NodedbStatement::DropSchedule { |
| 76 | + name, |
| 77 | + if_exists: true, |
| 78 | + } => { |
| 79 | + if !schedule_exists(state, identity, name) { |
| 80 | + return Some(Ok(vec![Response::Execution(Tag::new("DROP SCHEDULE"))])); |
| 81 | + } |
| 82 | + None |
| 83 | + } |
| 84 | + |
| 85 | + NodedbStatement::DropSequence { |
| 86 | + name, |
| 87 | + if_exists: true, |
| 88 | + } => { |
| 89 | + if !sequence_exists(state, identity, name) { |
| 90 | + return Some(Ok(vec![Response::Execution(Tag::new("DROP SEQUENCE"))])); |
| 91 | + } |
| 92 | + None |
| 93 | + } |
| 94 | + |
| 95 | + NodedbStatement::DropAlert { |
| 96 | + name, |
| 97 | + if_exists: true, |
| 98 | + } => { |
| 99 | + if !alert_exists(state, identity, name) { |
| 100 | + return Some(Ok(vec![Response::Execution(Tag::new("DROP ALERT"))])); |
| 101 | + } |
| 102 | + None |
| 103 | + } |
| 104 | + |
| 105 | + NodedbStatement::DropRetentionPolicy { |
| 106 | + name, |
| 107 | + if_exists: true, |
| 108 | + } => { |
| 109 | + if !retention_policy_exists(state, identity, name) { |
| 110 | + return Some(Ok(vec![Response::Execution(Tag::new( |
| 111 | + "DROP RETENTION POLICY", |
| 112 | + ))])); |
| 113 | + } |
| 114 | + None |
| 115 | + } |
| 116 | + |
| 117 | + NodedbStatement::DropChangeStream { |
| 118 | + name, |
| 119 | + if_exists: true, |
| 120 | + } => { |
| 121 | + if !change_stream_exists(state, identity, name) { |
| 122 | + return Some(Ok(vec![Response::Execution(Tag::new( |
| 123 | + "DROP CHANGE STREAM", |
| 124 | + ))])); |
| 125 | + } |
| 126 | + None |
| 127 | + } |
| 128 | + |
| 129 | + NodedbStatement::DropMaterializedView { |
| 130 | + name, |
| 131 | + if_exists: true, |
| 132 | + } => { |
| 133 | + if !materialized_view_exists(state, identity, name) { |
| 134 | + return Some(Ok(vec![Response::Execution(Tag::new( |
| 135 | + "DROP MATERIALIZED VIEW", |
| 136 | + ))])); |
| 137 | + } |
| 138 | + None |
| 139 | + } |
| 140 | + |
| 141 | + NodedbStatement::DropContinuousAggregate { |
| 142 | + name, |
| 143 | + if_exists: true, |
| 144 | + } => { |
| 145 | + if !continuous_aggregate_exists(state, identity, name) { |
| 146 | + return Some(Ok(vec![Response::Execution(Tag::new( |
| 147 | + "DROP CONTINUOUS AGGREGATE", |
| 148 | + ))])); |
| 149 | + } |
| 150 | + None |
| 151 | + } |
| 152 | + |
| 153 | + NodedbStatement::DropRlsPolicy { |
| 154 | + if_exists: true, .. |
| 155 | + } => { |
| 156 | + // RLS policy existence check would need collection context; |
| 157 | + // fall through to legacy handler which already handles this. |
| 158 | + None |
| 159 | + } |
| 160 | + |
| 161 | + NodedbStatement::DropConsumerGroup { |
| 162 | + if_exists: true, .. |
| 163 | + } => None, // legacy handler |
| 164 | + |
| 165 | + // All other variants fall through to legacy dispatch. |
| 166 | + _ => None, |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +fn collection_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 171 | + let Some(catalog) = state.credentials.catalog() else { |
| 172 | + return false; |
| 173 | + }; |
| 174 | + let tid = identity.tenant_id.as_u32(); |
| 175 | + matches!(catalog.get_collection(tid, name), Ok(Some(_))) |
| 176 | +} |
| 177 | + |
| 178 | +fn trigger_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 179 | + let Some(catalog) = state.credentials.catalog() else { |
| 180 | + return false; |
| 181 | + }; |
| 182 | + let tid = identity.tenant_id.as_u32(); |
| 183 | + matches!(catalog.get_trigger(tid, name), Ok(Some(_))) |
| 184 | +} |
| 185 | + |
| 186 | +fn schedule_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 187 | + let tid = identity.tenant_id.as_u32(); |
| 188 | + state.schedule_registry.get(tid, name).is_some() |
| 189 | +} |
| 190 | + |
| 191 | +fn sequence_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 192 | + let tid = identity.tenant_id.as_u32(); |
| 193 | + state.sequence_registry.exists(tid, name) |
| 194 | +} |
| 195 | + |
| 196 | +fn alert_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 197 | + let tid = identity.tenant_id.as_u32(); |
| 198 | + state.alert_registry.get(tid, name).is_some() |
| 199 | +} |
| 200 | + |
| 201 | +fn retention_policy_exists( |
| 202 | + state: &SharedState, |
| 203 | + identity: &AuthenticatedIdentity, |
| 204 | + name: &str, |
| 205 | +) -> bool { |
| 206 | + let tid = identity.tenant_id.as_u32(); |
| 207 | + state.retention_policy_registry.get(tid, name).is_some() |
| 208 | +} |
| 209 | + |
| 210 | +fn change_stream_exists(state: &SharedState, identity: &AuthenticatedIdentity, name: &str) -> bool { |
| 211 | + let tid = identity.tenant_id.as_u32(); |
| 212 | + state.stream_registry.get(tid, name).is_some() |
| 213 | +} |
| 214 | + |
| 215 | +fn materialized_view_exists( |
| 216 | + state: &SharedState, |
| 217 | + identity: &AuthenticatedIdentity, |
| 218 | + name: &str, |
| 219 | +) -> bool { |
| 220 | + let tid = identity.tenant_id.as_u32(); |
| 221 | + state.mv_registry.get_def(tid, name).is_some() |
| 222 | +} |
| 223 | + |
| 224 | +fn continuous_aggregate_exists( |
| 225 | + state: &SharedState, |
| 226 | + identity: &AuthenticatedIdentity, |
| 227 | + name: &str, |
| 228 | +) -> bool { |
| 229 | + let tid = identity.tenant_id.as_u32(); |
| 230 | + state.mv_registry.get_def(tid, name).is_some() |
| 231 | +} |
0 commit comments