1use std::collections::{BTreeMap, BTreeSet};
11use std::net::IpAddr;
12use std::pin::Pin;
13use std::sync::Arc;
14use std::time::Duration;
15
16use chrono::{DateTime, Utc};
17use derivative::Derivative;
18use enum_kinds::EnumKind;
19use futures::Stream;
20use mz_adapter_types::connection::{ConnectionId, ConnectionIdType};
21use mz_auth::password::Password;
22use mz_cluster_client::ReplicaId;
23use mz_compute_types::ComputeInstanceId;
24use mz_compute_types::dataflows::DataflowDescription;
25use mz_controller_types::ClusterId;
26use mz_expr::RowSetFinishing;
27use mz_ore::collections::CollectionExt;
28use mz_ore::soft_assert_no_log;
29use mz_ore::tracing::OpenTelemetryContext;
30use mz_persist_client::PersistClient;
31use mz_pgcopy::CopyFormatParams;
32use mz_repr::global_id::TransientIdGen;
33use mz_repr::role_id::RoleId;
34use mz_repr::{CatalogItemId, ColumnIndex, GlobalId, RowIterator, SqlRelationType};
35use mz_sql::ast::{FetchDirection, Raw, Statement};
36use mz_sql::catalog::ObjectType;
37use mz_sql::optimizer_metrics::OptimizerMetrics;
38use mz_sql::plan;
39use mz_sql::plan::{ExecuteTimeout, Plan, PlanKind, SideEffectingFunc};
40use mz_sql::session::user::User;
41use mz_sql::session::vars::{OwnedVarInput, SystemVars};
42use mz_sql_parser::ast::{AlterObjectRenameStatement, AlterOwnerStatement, DropObjectsStatement};
43use mz_storage_types::sources::Timeline;
44use mz_timestamp_oracle::TimestampOracle;
45use tokio::sync::{mpsc, oneshot};
46use uuid::Uuid;
47
48use crate::catalog::Catalog;
49use crate::config::{ScopedParameters, ScopedParametersScope, SystemParameterFrontend};
50use crate::coord::appends::BuiltinTableAppendNotify;
51use crate::coord::consistency::CoordinatorInconsistencies;
52use crate::coord::peek::{PeekDataflowPlan, PeekResponseUnary};
53use crate::coord::timestamp_selection::TimestampDetermination;
54use crate::coord::{ExecuteContextExtra, ExecuteContextGuard};
55use crate::error::AdapterError;
56use crate::session::{EndTransactionAction, RowBatchStream, Session};
57use crate::statement_logging::{
58 FrontendStatementLoggingEvent, StatementEndedExecutionReason, StatementExecutionStrategy,
59 StatementLoggingFrontend,
60};
61use crate::statement_logging::{StatementLoggingId, WatchSetCreation};
62use crate::util::Transmittable;
63use crate::webhook::AppendWebhookResponse;
64use crate::{
65 AdapterNotice, AppendWebhookError, CollectionIdBundle, ReadHolds, TimestampExplanation,
66};
67
68#[derive(Debug)]
71pub struct CopyFromStdinWriter {
72 pub batch_txs: Vec<mpsc::Sender<Vec<u8>>>,
75 pub completion_rx: oneshot::Receiver<
78 Result<(Vec<mz_persist_client::batch::ProtoBatch>, u64), crate::AdapterError>,
79 >,
80}
81
82#[derive(Debug)]
83pub struct CatalogSnapshot {
84 pub catalog: Arc<Catalog>,
85}
86
87#[derive(Debug)]
88pub enum Command {
89 CatalogSnapshot {
90 tx: oneshot::Sender<CatalogSnapshot>,
91 },
92
93 Startup {
94 tx: oneshot::Sender<Result<StartupResponse, AdapterError>>,
95 user: User,
96 conn_id: ConnectionId,
97 client_ip: Option<IpAddr>,
98 secret_key: u32,
99 uuid: Uuid,
100 application_name: String,
101 notice_tx: mpsc::UnboundedSender<AdapterNotice>,
102 },
103
104 AuthenticatePassword {
105 tx: oneshot::Sender<Result<(), AdapterError>>,
106 role_name: String,
107 password: Option<Password>,
108 },
109
110 AuthenticateGetSASLChallenge {
111 tx: oneshot::Sender<Result<SASLChallengeResponse, AdapterError>>,
112 role_name: String,
113 nonce: String,
114 },
115
116 AuthenticateVerifySASLProof {
117 tx: oneshot::Sender<Result<SASLVerifyProofResponse, AdapterError>>,
118 role_name: String,
119 proof: String,
120 auth_message: String,
121 mock_hash: String,
122 },
123
124 CheckRoleCanLogin {
125 tx: oneshot::Sender<Result<(), AdapterError>>,
126 role_name: String,
127 },
128
129 Execute {
130 portal_name: String,
131 session: Session,
132 tx: oneshot::Sender<Response<ExecuteResponse>>,
133 outer_ctx_extra: Option<ExecuteContextGuard>,
134 },
135
136 Commit {
141 action: EndTransactionAction,
142 session: Session,
143 tx: oneshot::Sender<Response<ExecuteResponse>>,
144 },
145
146 CancelRequest {
147 conn_id: ConnectionIdType,
148 secret_key: u32,
149 },
150
151 PrivilegedCancelRequest {
152 conn_id: ConnectionId,
153 },
154
155 GetWebhook {
156 database: String,
157 schema: String,
158 name: String,
159 tx: oneshot::Sender<Result<AppendWebhookResponse, AppendWebhookError>>,
160 },
161
162 GetSystemVars {
163 tx: oneshot::Sender<SystemVars>,
164 },
165
166 SetSystemVars {
167 vars: BTreeMap<String, String>,
168 conn_id: ConnectionId,
169 tx: oneshot::Sender<Result<(), AdapterError>>,
170 },
171
172 UpdateScopedSystemParameters {
179 overrides: ScopedParameters,
180 prune_scope: Option<ScopedParametersScope>,
183 tx: oneshot::Sender<()>,
184 },
185
186 InstallScopedSystemParameterFrontend {
193 frontend: Arc<SystemParameterFrontend>,
194 },
195
196 InjectAuditEvents {
197 events: Vec<crate::catalog::InjectedAuditEvent>,
198 conn_id: ConnectionId,
199 tx: oneshot::Sender<Result<(), AdapterError>>,
200 },
201
202 Terminate {
203 conn_id: ConnectionId,
204 tx: Option<oneshot::Sender<Result<(), AdapterError>>>,
205 },
206
207 StartCopyFromStdin {
211 target_id: CatalogItemId,
212 target_name: String,
213 columns: Vec<ColumnIndex>,
214 row_desc: mz_repr::RelationDesc,
216 params: mz_pgcopy::CopyFormatParams<'static>,
218 session: Session,
219 tx: oneshot::Sender<Response<CopyFromStdinWriter>>,
220 },
221
222 RetireExecute {
229 data: ExecuteContextExtra,
230 reason: StatementEndedExecutionReason,
231 },
232
233 CheckConsistency {
234 tx: oneshot::Sender<Result<(), CoordinatorInconsistencies>>,
235 },
236
237 Dump {
238 tx: oneshot::Sender<Result<serde_json::Value, anyhow::Error>>,
239 },
240
241 GetComputeInstanceClient {
242 instance_id: ComputeInstanceId,
243 tx: oneshot::Sender<
244 Result<
245 mz_compute_client::controller::instance_client::InstanceClient,
246 mz_compute_client::controller::error::InstanceMissing,
247 >,
248 >,
249 },
250
251 GetOracle {
252 timeline: Timeline,
253 tx: oneshot::Sender<
254 Result<Arc<dyn TimestampOracle<mz_repr::Timestamp> + Send + Sync>, AdapterError>,
255 >,
256 },
257
258 DetermineRealTimeRecentTimestamp {
259 source_ids: BTreeSet<GlobalId>,
260 real_time_recency_timeout: Duration,
261 tx: oneshot::Sender<Result<Option<mz_repr::Timestamp>, AdapterError>>,
262 },
263
264 GetTransactionReadHoldsBundle {
265 conn_id: ConnectionId,
266 tx: oneshot::Sender<Option<ReadHolds>>,
267 },
268
269 StoreTransactionReadHolds {
271 conn_id: ConnectionId,
272 read_holds: ReadHolds,
273 tx: oneshot::Sender<()>,
274 },
275
276 ExecuteSlowPathPeek {
277 dataflow_plan: Box<PeekDataflowPlan>,
278 determination: TimestampDetermination,
279 finishing: RowSetFinishing,
280 compute_instance: ComputeInstanceId,
281 target_replica: Option<ReplicaId>,
282 intermediate_result_type: SqlRelationType,
283 source_ids: BTreeSet<GlobalId>,
284 conn_id: ConnectionId,
285 max_result_size: u64,
286 max_query_result_size: Option<u64>,
287 watch_set: Option<WatchSetCreation>,
290 tx: oneshot::Sender<Result<ExecuteResponse, AdapterError>>,
291 },
292
293 ExecuteSubscribe {
294 df_desc: DataflowDescription<mz_compute_types::plan::LirRelationExpr>,
295 dependency_ids: BTreeSet<GlobalId>,
296 cluster_id: ComputeInstanceId,
297 replica_id: Option<ReplicaId>,
298 conn_id: ConnectionId,
299 session_uuid: Uuid,
300 read_holds: ReadHolds,
301 plan: plan::SubscribePlan,
302 statement_logging_id: Option<StatementLoggingId>,
303 tx: oneshot::Sender<Result<ExecuteResponse, AdapterError>>,
304 },
305
306 CopyToPreflight {
310 s3_sink_connection: mz_compute_types::sinks::CopyToS3OneshotSinkConnection,
312 sink_id: GlobalId,
314 tx: oneshot::Sender<Result<(), AdapterError>>,
316 },
317
318 ExecuteCopyTo {
319 df_desc: Box<DataflowDescription<mz_compute_types::plan::LirRelationExpr>>,
320 compute_instance: ComputeInstanceId,
321 target_replica: Option<ReplicaId>,
322 source_ids: BTreeSet<GlobalId>,
323 conn_id: ConnectionId,
324 watch_set: Option<WatchSetCreation>,
327 tx: oneshot::Sender<Result<ExecuteResponse, AdapterError>>,
328 },
329
330 ExecuteSideEffectingFunc {
332 plan: SideEffectingFunc,
333 conn_id: ConnectionId,
334 tx: oneshot::Sender<Result<ExecuteResponse, AdapterError>>,
335 },
336
337 LookupConnection {
347 connection_id: u32,
348 tx: oneshot::Sender<Option<(ConnectionId, RoleId)>>,
349 },
350
351 RegisterFrontendPeek {
355 uuid: Uuid,
356 conn_id: ConnectionId,
357 cluster_id: mz_controller_types::ClusterId,
358 depends_on: BTreeSet<GlobalId>,
359 is_fast_path: bool,
360 watch_set: Option<WatchSetCreation>,
363 tx: oneshot::Sender<Result<(), AdapterError>>,
364 },
365
366 UnregisterFrontendPeek {
375 uuid: Uuid,
376 reason: StatementEndedExecutionReason,
377 tx: oneshot::Sender<()>,
378 },
379
380 ExplainTimestamp {
383 conn_id: ConnectionId,
384 session_wall_time: DateTime<Utc>,
385 cluster_id: ClusterId,
386 id_bundle: CollectionIdBundle,
387 determination: TimestampDetermination,
388 tx: oneshot::Sender<TimestampExplanation>,
389 },
390
391 FrontendStatementLogging(FrontendStatementLoggingEvent),
394}
395
396impl Command {
397 pub fn session(&self) -> Option<&Session> {
398 match self {
399 Command::Execute { session, .. }
400 | Command::Commit { session, .. }
401 | Command::StartCopyFromStdin { session, .. } => Some(session),
402 Command::CancelRequest { .. }
403 | Command::Startup { .. }
404 | Command::AuthenticatePassword { .. }
405 | Command::AuthenticateGetSASLChallenge { .. }
406 | Command::AuthenticateVerifySASLProof { .. }
407 | Command::CheckRoleCanLogin { .. }
408 | Command::CatalogSnapshot { .. }
409 | Command::PrivilegedCancelRequest { .. }
410 | Command::GetWebhook { .. }
411 | Command::Terminate { .. }
412 | Command::GetSystemVars { .. }
413 | Command::SetSystemVars { .. }
414 | Command::UpdateScopedSystemParameters { .. }
415 | Command::InstallScopedSystemParameterFrontend { .. }
416 | Command::RetireExecute { .. }
417 | Command::CheckConsistency { .. }
418 | Command::Dump { .. }
419 | Command::GetComputeInstanceClient { .. }
420 | Command::GetOracle { .. }
421 | Command::DetermineRealTimeRecentTimestamp { .. }
422 | Command::GetTransactionReadHoldsBundle { .. }
423 | Command::StoreTransactionReadHolds { .. }
424 | Command::ExecuteSlowPathPeek { .. }
425 | Command::ExecuteSubscribe { .. }
426 | Command::CopyToPreflight { .. }
427 | Command::ExecuteCopyTo { .. }
428 | Command::ExecuteSideEffectingFunc { .. }
429 | Command::LookupConnection { .. }
430 | Command::RegisterFrontendPeek { .. }
431 | Command::UnregisterFrontendPeek { .. }
432 | Command::ExplainTimestamp { .. }
433 | Command::FrontendStatementLogging(..)
434 | Command::InjectAuditEvents { .. } => None,
435 }
436 }
437
438 pub fn session_mut(&mut self) -> Option<&mut Session> {
439 match self {
440 Command::Execute { session, .. }
441 | Command::Commit { session, .. }
442 | Command::StartCopyFromStdin { session, .. } => Some(session),
443 Command::CancelRequest { .. }
444 | Command::Startup { .. }
445 | Command::AuthenticatePassword { .. }
446 | Command::AuthenticateGetSASLChallenge { .. }
447 | Command::AuthenticateVerifySASLProof { .. }
448 | Command::CheckRoleCanLogin { .. }
449 | Command::CatalogSnapshot { .. }
450 | Command::PrivilegedCancelRequest { .. }
451 | Command::GetWebhook { .. }
452 | Command::Terminate { .. }
453 | Command::GetSystemVars { .. }
454 | Command::SetSystemVars { .. }
455 | Command::UpdateScopedSystemParameters { .. }
456 | Command::InstallScopedSystemParameterFrontend { .. }
457 | Command::RetireExecute { .. }
458 | Command::CheckConsistency { .. }
459 | Command::Dump { .. }
460 | Command::GetComputeInstanceClient { .. }
461 | Command::GetOracle { .. }
462 | Command::DetermineRealTimeRecentTimestamp { .. }
463 | Command::GetTransactionReadHoldsBundle { .. }
464 | Command::StoreTransactionReadHolds { .. }
465 | Command::ExecuteSlowPathPeek { .. }
466 | Command::ExecuteSubscribe { .. }
467 | Command::CopyToPreflight { .. }
468 | Command::ExecuteCopyTo { .. }
469 | Command::ExecuteSideEffectingFunc { .. }
470 | Command::LookupConnection { .. }
471 | Command::RegisterFrontendPeek { .. }
472 | Command::UnregisterFrontendPeek { .. }
473 | Command::ExplainTimestamp { .. }
474 | Command::FrontendStatementLogging(..)
475 | Command::InjectAuditEvents { .. } => None,
476 }
477 }
478}
479
480#[derive(Debug)]
481pub struct Response<T> {
482 pub result: Result<T, AdapterError>,
483 pub session: Session,
484 pub otel_ctx: OpenTelemetryContext,
485}
486
487#[derive(Debug, Clone, Copy)]
488pub struct SuperuserAttribute(pub Option<bool>);
489
490#[derive(Derivative)]
492#[derivative(Debug)]
493pub struct StartupResponse {
494 pub role_id: RoleId,
496 pub superuser_attribute: SuperuserAttribute,
501 #[derivative(Debug = "ignore")]
503 pub write_notify: BuiltinTableAppendNotify,
504 pub session_defaults: BTreeMap<String, OwnedVarInput>,
506 pub catalog: Arc<Catalog>,
507 pub storage_collections:
508 Arc<dyn mz_storage_client::storage_collections::StorageCollections + Send + Sync>,
509 pub transient_id_gen: Arc<TransientIdGen>,
510 pub optimizer_metrics: OptimizerMetrics,
511 pub persist_client: PersistClient,
512 pub statement_logging_frontend: StatementLoggingFrontend,
513}
514
515#[derive(Derivative)]
516#[derivative(Debug)]
517pub struct SASLChallengeResponse {
518 pub iteration_count: usize,
519 pub salt: String,
521 pub nonce: String,
522}
523
524#[derive(Derivative)]
525#[derivative(Debug)]
526pub struct SASLVerifyProofResponse {
527 pub verifier: String,
528}
529
530impl Transmittable for StartupResponse {
533 type Allowed = bool;
534 fn to_allowed(&self) -> Self::Allowed {
535 true
536 }
537}
538
539#[derive(Debug, Clone)]
541pub struct CatalogDump(String);
542
543impl CatalogDump {
544 pub fn new(raw: String) -> Self {
545 CatalogDump(raw)
546 }
547
548 pub fn into_string(self) -> String {
549 self.0
550 }
551}
552
553impl Transmittable for CatalogDump {
554 type Allowed = bool;
555 fn to_allowed(&self) -> Self::Allowed {
556 true
557 }
558}
559
560impl Transmittable for SystemVars {
561 type Allowed = bool;
562 fn to_allowed(&self) -> Self::Allowed {
563 true
564 }
565}
566
567#[derive(EnumKind, Derivative)]
569#[derivative(Debug)]
570#[enum_kind(ExecuteResponseKind, derive(PartialOrd, Ord))]
571pub enum ExecuteResponse {
572 AlteredDefaultPrivileges,
574 AlteredObject(ObjectType),
576 AlteredRole,
578 AlteredSystemConfiguration,
580 ClosedCursor,
582 Comment,
584 Copied(usize),
586 CopyTo {
588 format: mz_sql::plan::CopyFormat,
589 resp: Box<ExecuteResponse>,
590 },
591 CopyFrom {
592 target_id: CatalogItemId,
594 target_name: String,
596 columns: Vec<ColumnIndex>,
597 params: CopyFormatParams<'static>,
598 ctx_extra: ExecuteContextGuard,
599 },
600 CreatedConnection,
602 CreatedDatabase,
604 CreatedSchema,
606 CreatedRole,
608 CreatedCluster,
610 CreatedClusterReplica,
612 CreatedIndex,
614 CreatedIntrospectionSubscribe,
616 CreatedSecret,
618 CreatedSink,
620 CreatedSource,
622 CreatedTable,
624 CreatedView,
626 CreatedViews,
628 CreatedMaterializedView,
630 CreatedType,
632 CreatedNetworkPolicy,
634 Deallocate { all: bool },
636 DeclaredCursor,
638 Deleted(usize),
640 DiscardedTemp,
642 DiscardedAll,
644 DroppedObject(ObjectType),
646 DroppedOwned,
648 EmptyQuery,
650 Fetch {
652 name: String,
654 count: Option<FetchDirection>,
656 timeout: ExecuteTimeout,
658 ctx_extra: ExecuteContextGuard,
659 },
660 GrantedPrivilege,
662 GrantedRole,
664 Inserted(usize),
666 Prepare,
668 Raised,
670 ReassignOwned,
672 RevokedPrivilege,
674 RevokedRole,
676 SendingRowsStreaming {
678 #[derivative(Debug = "ignore")]
679 rows: Pin<Box<dyn Stream<Item = PeekResponseUnary> + Send + Sync>>,
680 instance_id: ComputeInstanceId,
681 strategy: StatementExecutionStrategy,
682 },
683 SendingRowsImmediate {
686 #[derivative(Debug = "ignore")]
687 rows: Box<dyn RowIterator + Send + Sync>,
688 },
689 SetVariable {
691 name: String,
692 reset: bool,
694 },
695 StartedTransaction,
697 Subscribing {
700 rx: RowBatchStream,
701 ctx_extra: ExecuteContextGuard,
702 instance_id: ComputeInstanceId,
703 },
704 TransactionCommitted {
706 params: BTreeMap<&'static str, String>,
708 },
709 TransactionRolledBack {
711 params: BTreeMap<&'static str, String>,
713 },
714 Updated(usize),
716 ValidatedConnection,
718}
719
720impl TryFrom<&Statement<Raw>> for ExecuteResponse {
721 type Error = ();
722
723 fn try_from(stmt: &Statement<Raw>) -> Result<Self, Self::Error> {
725 let resp_kinds = Plan::generated_from(&stmt.into())
726 .iter()
727 .map(ExecuteResponse::generated_from)
728 .flatten()
729 .cloned()
730 .collect::<BTreeSet<ExecuteResponseKind>>();
731 let resps = resp_kinds
732 .iter()
733 .map(|r| (*r).try_into())
734 .collect::<Result<Vec<ExecuteResponse>, _>>();
735 if let Ok(resps) = resps {
737 if resps.len() == 1 {
738 return Ok(resps.into_element());
739 }
740 }
741 let resp = match stmt {
742 Statement::DropObjects(DropObjectsStatement { object_type, .. }) => {
743 ExecuteResponse::DroppedObject((*object_type).into())
744 }
745 Statement::AlterObjectRename(AlterObjectRenameStatement { object_type, .. })
746 | Statement::AlterOwner(AlterOwnerStatement { object_type, .. }) => {
747 ExecuteResponse::AlteredObject((*object_type).into())
748 }
749 _ => return Err(()),
750 };
751 soft_assert_no_log!(
753 resp_kinds.len() == 1
754 && resp_kinds.first().expect("must exist") == &ExecuteResponseKind::from(&resp),
755 "ExecuteResponses out of sync with planner"
756 );
757 Ok(resp)
758 }
759}
760
761impl TryInto<ExecuteResponse> for ExecuteResponseKind {
762 type Error = ();
763
764 fn try_into(self) -> Result<ExecuteResponse, Self::Error> {
767 match self {
768 ExecuteResponseKind::AlteredDefaultPrivileges => {
769 Ok(ExecuteResponse::AlteredDefaultPrivileges)
770 }
771 ExecuteResponseKind::AlteredObject => Err(()),
772 ExecuteResponseKind::AlteredRole => Ok(ExecuteResponse::AlteredRole),
773 ExecuteResponseKind::AlteredSystemConfiguration => {
774 Ok(ExecuteResponse::AlteredSystemConfiguration)
775 }
776 ExecuteResponseKind::ClosedCursor => Ok(ExecuteResponse::ClosedCursor),
777 ExecuteResponseKind::Comment => Ok(ExecuteResponse::Comment),
778 ExecuteResponseKind::Copied => Err(()),
779 ExecuteResponseKind::CopyTo => Err(()),
780 ExecuteResponseKind::CopyFrom => Err(()),
781 ExecuteResponseKind::CreatedConnection => Ok(ExecuteResponse::CreatedConnection),
782 ExecuteResponseKind::CreatedDatabase => Ok(ExecuteResponse::CreatedDatabase),
783 ExecuteResponseKind::CreatedSchema => Ok(ExecuteResponse::CreatedSchema),
784 ExecuteResponseKind::CreatedRole => Ok(ExecuteResponse::CreatedRole),
785 ExecuteResponseKind::CreatedCluster => Ok(ExecuteResponse::CreatedCluster),
786 ExecuteResponseKind::CreatedClusterReplica => {
787 Ok(ExecuteResponse::CreatedClusterReplica)
788 }
789 ExecuteResponseKind::CreatedIndex => Ok(ExecuteResponse::CreatedIndex),
790 ExecuteResponseKind::CreatedSecret => Ok(ExecuteResponse::CreatedSecret),
791 ExecuteResponseKind::CreatedSink => Ok(ExecuteResponse::CreatedSink),
792 ExecuteResponseKind::CreatedSource => Ok(ExecuteResponse::CreatedSource),
793 ExecuteResponseKind::CreatedTable => Ok(ExecuteResponse::CreatedTable),
794 ExecuteResponseKind::CreatedView => Ok(ExecuteResponse::CreatedView),
795 ExecuteResponseKind::CreatedViews => Ok(ExecuteResponse::CreatedViews),
796 ExecuteResponseKind::CreatedMaterializedView => {
797 Ok(ExecuteResponse::CreatedMaterializedView)
798 }
799 ExecuteResponseKind::CreatedNetworkPolicy => Ok(ExecuteResponse::CreatedNetworkPolicy),
800 ExecuteResponseKind::CreatedType => Ok(ExecuteResponse::CreatedType),
801 ExecuteResponseKind::Deallocate => Err(()),
802 ExecuteResponseKind::DeclaredCursor => Ok(ExecuteResponse::DeclaredCursor),
803 ExecuteResponseKind::Deleted => Err(()),
804 ExecuteResponseKind::DiscardedTemp => Ok(ExecuteResponse::DiscardedTemp),
805 ExecuteResponseKind::DiscardedAll => Ok(ExecuteResponse::DiscardedAll),
806 ExecuteResponseKind::DroppedObject => Err(()),
807 ExecuteResponseKind::DroppedOwned => Ok(ExecuteResponse::DroppedOwned),
808 ExecuteResponseKind::EmptyQuery => Ok(ExecuteResponse::EmptyQuery),
809 ExecuteResponseKind::Fetch => Err(()),
810 ExecuteResponseKind::GrantedPrivilege => Ok(ExecuteResponse::GrantedPrivilege),
811 ExecuteResponseKind::GrantedRole => Ok(ExecuteResponse::GrantedRole),
812 ExecuteResponseKind::Inserted => Err(()),
813 ExecuteResponseKind::Prepare => Ok(ExecuteResponse::Prepare),
814 ExecuteResponseKind::Raised => Ok(ExecuteResponse::Raised),
815 ExecuteResponseKind::ReassignOwned => Ok(ExecuteResponse::ReassignOwned),
816 ExecuteResponseKind::RevokedPrivilege => Ok(ExecuteResponse::RevokedPrivilege),
817 ExecuteResponseKind::RevokedRole => Ok(ExecuteResponse::RevokedRole),
818 ExecuteResponseKind::SetVariable => Err(()),
819 ExecuteResponseKind::StartedTransaction => Ok(ExecuteResponse::StartedTransaction),
820 ExecuteResponseKind::Subscribing => Err(()),
821 ExecuteResponseKind::TransactionCommitted => Err(()),
822 ExecuteResponseKind::TransactionRolledBack => Err(()),
823 ExecuteResponseKind::Updated => Err(()),
824 ExecuteResponseKind::ValidatedConnection => Ok(ExecuteResponse::ValidatedConnection),
825 ExecuteResponseKind::SendingRowsStreaming => Err(()),
826 ExecuteResponseKind::SendingRowsImmediate => Err(()),
827 ExecuteResponseKind::CreatedIntrospectionSubscribe => {
828 Ok(ExecuteResponse::CreatedIntrospectionSubscribe)
829 }
830 }
831 }
832}
833
834impl ExecuteResponse {
835 pub fn tag(&self) -> Option<String> {
836 use ExecuteResponse::*;
837 match self {
838 AlteredDefaultPrivileges => Some("ALTER DEFAULT PRIVILEGES".into()),
839 AlteredObject(o) => Some(format!("ALTER {}", o)),
840 AlteredRole => Some("ALTER ROLE".into()),
841 AlteredSystemConfiguration => Some("ALTER SYSTEM".into()),
842 ClosedCursor => Some("CLOSE CURSOR".into()),
843 Comment => Some("COMMENT".into()),
844 Copied(n) => Some(format!("COPY {}", n)),
845 CopyTo { .. } => None,
846 CopyFrom { .. } => None,
847 CreatedConnection { .. } => Some("CREATE CONNECTION".into()),
848 CreatedDatabase { .. } => Some("CREATE DATABASE".into()),
849 CreatedSchema { .. } => Some("CREATE SCHEMA".into()),
850 CreatedRole => Some("CREATE ROLE".into()),
851 CreatedCluster { .. } => Some("CREATE CLUSTER".into()),
852 CreatedClusterReplica { .. } => Some("CREATE CLUSTER REPLICA".into()),
853 CreatedIndex { .. } => Some("CREATE INDEX".into()),
854 CreatedSecret { .. } => Some("CREATE SECRET".into()),
855 CreatedSink { .. } => Some("CREATE SINK".into()),
856 CreatedSource { .. } => Some("CREATE SOURCE".into()),
857 CreatedTable { .. } => Some("CREATE TABLE".into()),
858 CreatedView { .. } => Some("CREATE VIEW".into()),
859 CreatedViews { .. } => Some("CREATE VIEWS".into()),
860 CreatedMaterializedView { .. } => Some("CREATE MATERIALIZED VIEW".into()),
861 CreatedType => Some("CREATE TYPE".into()),
862 CreatedNetworkPolicy => Some("CREATE NETWORKPOLICY".into()),
863 Deallocate { all } => Some(format!("DEALLOCATE{}", if *all { " ALL" } else { "" })),
864 DeclaredCursor => Some("DECLARE CURSOR".into()),
865 Deleted(n) => Some(format!("DELETE {}", n)),
866 DiscardedTemp => Some("DISCARD TEMP".into()),
867 DiscardedAll => Some("DISCARD ALL".into()),
868 DroppedObject(o) => Some(format!("DROP {o}")),
869 DroppedOwned => Some("DROP OWNED".into()),
870 EmptyQuery => None,
871 Fetch { .. } => None,
872 GrantedPrivilege => Some("GRANT".into()),
873 GrantedRole => Some("GRANT ROLE".into()),
874 Inserted(n) => {
875 Some(format!("INSERT 0 {}", n))
883 }
884 Prepare => Some("PREPARE".into()),
885 Raised => Some("RAISE".into()),
886 ReassignOwned => Some("REASSIGN OWNED".into()),
887 RevokedPrivilege => Some("REVOKE".into()),
888 RevokedRole => Some("REVOKE ROLE".into()),
889 SendingRowsStreaming { .. } | SendingRowsImmediate { .. } => None,
890 SetVariable { reset: true, .. } => Some("RESET".into()),
891 SetVariable { reset: false, .. } => Some("SET".into()),
892 StartedTransaction { .. } => Some("BEGIN".into()),
893 Subscribing { .. } => None,
894 TransactionCommitted { .. } => Some("COMMIT".into()),
895 TransactionRolledBack { .. } => Some("ROLLBACK".into()),
896 Updated(n) => Some(format!("UPDATE {}", n)),
897 ValidatedConnection => Some("VALIDATE CONNECTION".into()),
898 CreatedIntrospectionSubscribe => Some("CREATE INTROSPECTION SUBSCRIBE".into()),
899 }
900 }
901
902 pub fn generated_from(plan: &PlanKind) -> &'static [ExecuteResponseKind] {
906 use ExecuteResponseKind::*;
907 use PlanKind::*;
908
909 match plan {
910 AbortTransaction => &[TransactionRolledBack],
911 AlterClusterRename
912 | AlterClusterSwap
913 | AlterCluster
914 | AlterClusterReplicaRename
915 | AlterOwner
916 | AlterItemRename
917 | AlterRetainHistory
918 | AlterSourceTimestampInterval
919 | AlterNoop
920 | AlterSchemaRename
921 | AlterSchemaSwap
922 | AlterSecret
923 | AlterConnection
924 | AlterSource
925 | AlterSink
926 | AlterTableAddColumn
927 | AlterMaterializedViewApplyReplacement
928 | AlterNetworkPolicy => &[AlteredObject],
929 AlterDefaultPrivileges => &[AlteredDefaultPrivileges],
930 AlterSetCluster => &[AlteredObject],
931 AlterRole => &[AlteredRole],
932 AlterSystemSet | AlterSystemReset | AlterSystemResetAll => {
933 &[AlteredSystemConfiguration]
934 }
935 Close => &[ClosedCursor],
936 PlanKind::CopyFrom => &[ExecuteResponseKind::CopyFrom, ExecuteResponseKind::Copied],
937 PlanKind::CopyTo => &[ExecuteResponseKind::Copied],
938 PlanKind::Comment => &[ExecuteResponseKind::Comment],
939 CommitTransaction => &[TransactionCommitted, TransactionRolledBack],
940 CreateConnection => &[CreatedConnection],
941 CreateDatabase => &[CreatedDatabase],
942 CreateSchema => &[CreatedSchema],
943 CreateRole => &[CreatedRole],
944 CreateCluster => &[CreatedCluster],
945 CreateClusterReplica => &[CreatedClusterReplica],
946 CreateSource | CreateSources => &[CreatedSource],
947 CreateSecret => &[CreatedSecret],
948 CreateSink => &[CreatedSink],
949 CreateTable => &[CreatedTable],
950 CreateView => &[CreatedView],
951 CreateMaterializedView => &[CreatedMaterializedView],
952 CreateIndex => &[CreatedIndex],
953 CreateType => &[CreatedType],
954 PlanKind::Deallocate => &[ExecuteResponseKind::Deallocate],
955 CreateNetworkPolicy => &[CreatedNetworkPolicy],
956 Declare => &[DeclaredCursor],
957 DiscardTemp => &[DiscardedTemp],
958 DiscardAll => &[DiscardedAll],
959 DropObjects => &[DroppedObject],
960 DropOwned => &[DroppedOwned],
961 PlanKind::EmptyQuery => &[ExecuteResponseKind::EmptyQuery],
962 ExplainPlan | ExplainPushdown | ExplainTimestamp | Select | ShowAllVariables
963 | ShowCreate | ShowColumns | ShowVariable | InspectShard | ExplainSinkSchema => &[
964 ExecuteResponseKind::CopyTo,
965 SendingRowsStreaming,
966 SendingRowsImmediate,
967 ],
968 Execute | ReadThenWrite => &[
969 Deleted,
970 Inserted,
971 SendingRowsStreaming,
972 SendingRowsImmediate,
973 Updated,
974 ],
975 PlanKind::Fetch => &[ExecuteResponseKind::Fetch],
976 GrantPrivileges => &[GrantedPrivilege],
977 GrantRole => &[GrantedRole],
978 Insert => &[Inserted, SendingRowsImmediate],
979 PlanKind::Prepare => &[ExecuteResponseKind::Prepare],
980 PlanKind::Raise => &[ExecuteResponseKind::Raised],
981 PlanKind::ReassignOwned => &[ExecuteResponseKind::ReassignOwned],
982 RevokePrivileges => &[RevokedPrivilege],
983 RevokeRole => &[RevokedRole],
984 PlanKind::SetVariable | ResetVariable | PlanKind::SetTransaction => {
985 &[ExecuteResponseKind::SetVariable]
986 }
987 PlanKind::Subscribe => &[Subscribing, ExecuteResponseKind::CopyTo],
988 StartTransaction => &[StartedTransaction],
989 SideEffectingFunc => &[SendingRowsStreaming, SendingRowsImmediate],
990 ValidateConnection => &[ExecuteResponseKind::ValidatedConnection],
991 }
992 }
993}
994
995impl Transmittable for ExecuteResponse {
999 type Allowed = ExecuteResponseKind;
1000 fn to_allowed(&self) -> Self::Allowed {
1001 ExecuteResponseKind::from(self)
1002 }
1003}