Skip to main content

mz_environmentd/http/
sql.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::collections::BTreeMap;
11use std::net::{IpAddr, SocketAddr};
12use std::pin::pin;
13use std::sync::Arc;
14use std::time::Duration;
15
16use anyhow::anyhow;
17use async_trait::async_trait;
18use axum::extract::connect_info::ConnectInfo;
19use axum::extract::ws::{CloseFrame, Message, Utf8Bytes, WebSocket};
20use axum::extract::{State, WebSocketUpgrade};
21use axum::response::IntoResponse;
22use axum::{Extension, Json};
23use futures::Future;
24use futures::future::BoxFuture;
25
26use http::StatusCode;
27use itertools::Itertools;
28use mz_adapter::client::RecordFirstRowStream;
29use mz_adapter::session::{EndTransactionAction, TransactionStatus};
30use mz_adapter::statement_logging::{StatementEndedExecutionReason, StatementExecutionStrategy};
31use mz_adapter::{
32    AdapterError, AdapterNotice, ExecuteContextGuard, ExecuteResponse, ExecuteResponseKind,
33    PeekResponseUnary, SessionClient, verify_datum_desc,
34};
35use mz_auth::password::Password;
36use mz_catalog::memory::objects::{Cluster, ClusterReplica};
37use mz_interchange::encode::TypedDatum;
38use mz_interchange::json::{JsonNumberPolicy, ToJson};
39use mz_ore::cast::CastFrom;
40use mz_ore::metrics::{MakeCollectorOpts, MetricsRegistry};
41use mz_ore::result::ResultExt;
42use mz_repr::{Datum, RelationDesc, RowArena, RowIterator};
43use mz_sql::ast::display::AstDisplay;
44use mz_sql::ast::{CopyDirection, CopyStatement, CopyTarget, Raw, Statement, StatementKind};
45use mz_sql::parse::StatementParseResult;
46use mz_sql::plan::Plan;
47use mz_sql::session::metadata::SessionMetadata;
48use prometheus::Opts;
49use prometheus::core::{AtomicF64, GenericGaugeVec};
50use serde::{Deserialize, Serialize};
51use tokio::{select, time};
52use tokio_postgres::error::SqlState;
53use tokio_stream::wrappers::UnboundedReceiverStream;
54use tower_sessions::Session as TowerSession;
55use tracing::{debug, info};
56use tungstenite::protocol::frame::coding::CloseCode;
57
58use crate::http::prometheus::PrometheusSqlQuery;
59use crate::http::{
60    AuthError, AuthedClient, AuthedUser, MAX_REQUEST_SIZE, WsState, ensure_session_unexpired,
61    init_ws, maybe_get_authenticated_session,
62};
63
64#[derive(Debug, thiserror::Error)]
65pub enum Error {
66    #[error(transparent)]
67    Adapter(#[from] AdapterError),
68    #[error(transparent)]
69    Json(#[from] serde_json::Error),
70    #[error(transparent)]
71    Axum(#[from] axum::Error),
72    #[error("SUBSCRIBE only supported over websocket")]
73    SubscribeOnlyOverWs,
74    #[error("current transaction is aborted, commands ignored until end of transaction block")]
75    AbortedTransaction,
76    #[error("unsupported via this API: {0}")]
77    Unsupported(String),
78    #[error("{0}")]
79    Unstructured(anyhow::Error),
80}
81
82impl Error {
83    pub fn detail(&self) -> Option<String> {
84        match self {
85            Error::Adapter(err) => err.detail(),
86            _ => None,
87        }
88    }
89
90    pub fn hint(&self) -> Option<String> {
91        match self {
92            Error::Adapter(err) => err.hint(),
93            _ => None,
94        }
95    }
96
97    pub fn position(&self) -> Option<usize> {
98        match self {
99            Error::Adapter(err) => err.position(),
100            _ => None,
101        }
102    }
103
104    pub fn code(&self) -> SqlState {
105        match self {
106            Error::Adapter(err) => err.code(),
107            Error::AbortedTransaction => SqlState::IN_FAILED_SQL_TRANSACTION,
108            _ => SqlState::INTERNAL_ERROR,
109        }
110    }
111}
112
113static PER_REPLICA_LABELS: &[&str] = &["replica_full_name", "instance_id", "replica_id"];
114
115async fn execute_promsql_query(
116    client: &mut AuthedClient,
117    query: &PrometheusSqlQuery<'_>,
118    metrics_registry: &MetricsRegistry,
119    metrics_by_name: &mut BTreeMap<String, GenericGaugeVec<AtomicF64>>,
120    cluster: Option<(&Cluster, &ClusterReplica)>,
121) {
122    assert_eq!(query.per_replica, cluster.is_some());
123
124    let mut res = SqlResponse {
125        results: Vec::new(),
126    };
127
128    execute_request(client, query.to_sql_request(cluster), &mut res)
129        .await
130        .expect("valid SQL query");
131
132    let result = match res.results.as_slice() {
133        // Each query issued is preceded by several SET commands
134        // to make sure it is routed to the right cluster replica.
135        [
136            SqlResult::Ok { .. },
137            SqlResult::Ok { .. },
138            SqlResult::Ok { .. },
139            result,
140        ] => result,
141        // Transient errors are fine, like if the cluster or replica
142        // was dropped before the promsql query was executed. We
143        // should not see errors in the steady state.
144        _ => {
145            info!(
146                "error executing prometheus query {}: {:?}",
147                query.metric_name, res
148            );
149            return;
150        }
151    };
152
153    let SqlResult::Rows { desc, rows, .. } = result else {
154        info!(
155            "did not receive rows for SQL query for prometheus metric {}: {:?}, {:?}",
156            query.metric_name, result, cluster
157        );
158        return;
159    };
160
161    let gauge_vec = metrics_by_name
162        .entry(query.metric_name.to_string())
163        .or_insert_with(|| {
164            let mut label_names: Vec<String> = desc
165                .columns
166                .iter()
167                .filter(|col| col.name != query.value_column_name)
168                .map(|col| col.name.clone())
169                .collect();
170
171            if query.per_replica {
172                label_names.extend(PER_REPLICA_LABELS.iter().map(|label| label.to_string()));
173            }
174
175            metrics_registry.register::<GenericGaugeVec<AtomicF64>>(MakeCollectorOpts {
176                opts: Opts::new(query.metric_name, query.help).variable_labels(label_names),
177                buckets: None,
178            })
179        });
180
181    for row in rows {
182        let mut label_values = desc
183            .columns
184            .iter()
185            .zip_eq(row)
186            .filter(|(col, _)| col.name != query.value_column_name)
187            .map(|(_, val)| val.as_str().expect("must be string"))
188            .collect::<Vec<_>>();
189
190        let value = desc
191            .columns
192            .iter()
193            .zip_eq(row)
194            .find(|(col, _)| col.name == query.value_column_name)
195            .map(|(_, val)| val.as_str().unwrap_or("0").parse::<f64>().unwrap_or(0.0))
196            .unwrap_or(0.0);
197
198        match cluster {
199            Some((cluster, replica)) => {
200                let replica_full_name = format!("{}.{}", cluster.name, replica.name);
201                let cluster_id = cluster.id.to_string();
202                let replica_id = replica.replica_id.to_string();
203
204                label_values.push(&replica_full_name);
205                label_values.push(&cluster_id);
206                label_values.push(&replica_id);
207
208                gauge_vec
209                    .get_metric_with_label_values(&label_values)
210                    .expect("valid labels")
211                    .set(value);
212            }
213            None => {
214                gauge_vec
215                    .get_metric_with_label_values(&label_values)
216                    .expect("valid labels")
217                    .set(value);
218            }
219        }
220    }
221}
222
223async fn handle_promsql_query(
224    client: &mut AuthedClient,
225    query: &PrometheusSqlQuery<'_>,
226    metrics_registry: &MetricsRegistry,
227    metrics_by_name: &mut BTreeMap<String, GenericGaugeVec<AtomicF64>>,
228) {
229    if !query.per_replica {
230        execute_promsql_query(client, query, metrics_registry, metrics_by_name, None).await;
231        return;
232    }
233
234    let catalog = client.client.catalog_snapshot("handle_promsql_query").await;
235    let clusters: Vec<&Cluster> = catalog.clusters().collect();
236
237    for cluster in clusters {
238        for replica in cluster.replicas() {
239            execute_promsql_query(
240                client,
241                query,
242                metrics_registry,
243                metrics_by_name,
244                Some((cluster, replica)),
245            )
246            .await;
247        }
248    }
249}
250
251pub async fn handle_promsql(
252    mut client: AuthedClient,
253    queries: &[PrometheusSqlQuery<'_>],
254) -> MetricsRegistry {
255    let metrics_registry = MetricsRegistry::new();
256    let mut metrics_by_name = BTreeMap::new();
257
258    for query in queries {
259        handle_promsql_query(&mut client, query, &metrics_registry, &mut metrics_by_name).await;
260    }
261
262    metrics_registry
263}
264
265pub async fn handle_sql(
266    mut client: AuthedClient,
267    Json(request): Json<SqlRequest>,
268) -> impl IntoResponse {
269    let mut res = SqlResponse {
270        results: Vec::new(),
271    };
272    // Don't need to worry about timeouts or resetting cancel here because there is always exactly 1
273    // request.
274    match execute_request(&mut client, request, &mut res).await {
275        Ok(()) => Ok(Json(res)),
276        Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
277    }
278}
279
280#[derive(Debug)]
281pub enum ExistingUser {
282    /// An AuthedUser provided by the
283    /// `x_materialize_user_header_auth` middleware
284    XMaterializeUserHeader(AuthedUser),
285    /// An AuthedUser provided by an authenticated session
286    /// established via [`crate::http::handle_login`].
287    Session(AuthedUser),
288}
289
290pub(crate) async fn handle_sql_ws(
291    State(state): State<WsState>,
292    existing_user: Option<Extension<AuthedUser>>,
293    ws: WebSocketUpgrade,
294    ConnectInfo(addr): ConnectInfo<SocketAddr>,
295    tower_session: Option<Extension<TowerSession>>,
296) -> Result<impl IntoResponse, AuthError> {
297    let session = tower_session.map(|Extension(session)| session);
298    // The `x_materialize_user_header_auth` middleware may have already provided the user for us
299    let user = match existing_user {
300        Some(Extension(user)) => Some(ExistingUser::XMaterializeUserHeader(user)),
301        None => {
302            let session = maybe_get_authenticated_session(session.as_ref()).await;
303            if let Some((session, session_data)) = session {
304                let user = ensure_session_unexpired(session, session_data).await?;
305                Some(ExistingUser::Session(user))
306            } else {
307                None
308            }
309        }
310    };
311
312    let addr = Box::new(addr.ip());
313    Ok(ws
314        .max_message_size(MAX_REQUEST_SIZE)
315        .on_upgrade(|ws| async move { run_ws(state, user, *addr, ws).await }))
316}
317
318#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
319#[serde(untagged)]
320pub enum WebSocketAuth {
321    Basic {
322        user: String,
323        password: Password,
324        #[serde(default)]
325        options: BTreeMap<String, String>,
326    },
327    Bearer {
328        token: String,
329        #[serde(default)]
330        options: BTreeMap<String, String>,
331    },
332    OptionsOnly {
333        #[serde(default)]
334        options: BTreeMap<String, String>,
335    },
336}
337
338async fn run_ws(state: WsState, user: Option<ExistingUser>, peer_addr: IpAddr, mut ws: WebSocket) {
339    let mut client = match init_ws(state, user, peer_addr, &mut ws).await {
340        Ok(client) => client,
341        Err(e) => {
342            // We omit most detail from the error message we send to the client, to
343            // avoid giving attackers unnecessary information during auth. AdapterErrors
344            // are safe to return because they're generated after authentication.
345            debug!("WS request failed init: {}", e);
346            let reason: Utf8Bytes = match e.downcast_ref::<AdapterError>() {
347                Some(error) => error.to_string().into(),
348                None => "unauthorized".to_string().into(),
349            };
350            let _ = ws
351                .send(Message::Close(Some(CloseFrame {
352                    code: CloseCode::Protocol.into(),
353                    reason,
354                })))
355                .await;
356            return;
357        }
358    };
359
360    // Successful auth, send startup messages.
361    let mut msgs = Vec::new();
362    let session = client.client.session();
363    for var in session.vars().notify_set() {
364        msgs.push(WebSocketResponse::ParameterStatus(ParameterStatus {
365            name: var.name().to_string(),
366            value: var.value(),
367        }));
368    }
369    msgs.push(WebSocketResponse::BackendKeyData(BackendKeyData {
370        conn_id: session.conn_id().unhandled(),
371        secret_key: session.secret_key(),
372    }));
373    msgs.push(WebSocketResponse::ReadyForQuery(
374        session.transaction_code().into(),
375    ));
376    for msg in msgs {
377        let _ = ws
378            .send(Message::Text(
379                serde_json::to_string(&msg).expect("must serialize").into(),
380            ))
381            .await;
382    }
383
384    // Send any notices that might have been generated on startup.
385    let notices = session.drain_notices();
386    if let Err(err) = forward_notices(&mut ws, notices).await {
387        debug!("failed to forward notices to WebSocket, {err:?}");
388        return;
389    }
390
391    loop {
392        // Handle timeouts first so we don't execute any statements when there's a pending timeout.
393        let msg = select! {
394            biased;
395
396            // `recv_timeout()` is cancel-safe as per it's docs.
397            Some(timeout) = client.client.recv_timeout() => {
398                client.client.terminate().await;
399                // We must wait for the client to send a request before we can send the error
400                // response. Although this isn't the PG wire protocol, we choose to mirror it by
401                // only sending errors as responses to requests.
402                let _ = ws.recv().await;
403                let err = Error::from(AdapterError::from(timeout));
404                let _ = send_ws_response(&mut ws, WebSocketResponse::Error(err.into())).await;
405                return;
406            },
407            message = ws.recv() => message,
408        };
409
410        client.client.remove_idle_in_transaction_session_timeout();
411
412        let msg = match msg {
413            Some(Ok(msg)) => msg,
414            _ => {
415                // client disconnected
416                return;
417            }
418        };
419
420        let req: Result<SqlRequest, Error> = match msg {
421            Message::Text(data) => serde_json::from_str(&data).err_into(),
422            Message::Binary(data) => serde_json::from_slice(&data).err_into(),
423            // Handled automatically by the server.
424            Message::Ping(_) => {
425                continue;
426            }
427            Message::Pong(_) => {
428                continue;
429            }
430            Message::Close(_) => {
431                return;
432            }
433        };
434
435        // Figure out if we need to send an error, any notices, but always the ready message.
436        let err = match run_ws_request(req, &mut client, &mut ws).await {
437            Ok(()) => None,
438            Err(err) => Some(WebSocketResponse::Error(err.into())),
439        };
440
441        // After running our request, there are several messages we need to send in a
442        // specific order.
443        //
444        // Note: we nest these into a closure so we can centralize our error handling
445        // for when sending over the WebSocket fails. We could also use a try {} block
446        // here, but those aren't stabilized yet.
447        let ws_response = || async {
448            // First respond with any error that might have occurred.
449            if let Some(e_resp) = err {
450                send_ws_response(&mut ws, e_resp).await?;
451            }
452
453            // Then forward along any notices we generated.
454            let notices = client.client.session().drain_notices();
455            forward_notices(&mut ws, notices).await?;
456
457            // Finally, respond that we're ready for the next query.
458            let ready =
459                WebSocketResponse::ReadyForQuery(client.client.session().transaction_code().into());
460            send_ws_response(&mut ws, ready).await?;
461
462            Ok::<_, Error>(())
463        };
464
465        if let Err(err) = ws_response().await {
466            debug!("failed to send response over WebSocket, {err:?}");
467            return;
468        }
469    }
470}
471
472async fn run_ws_request(
473    req: Result<SqlRequest, Error>,
474    client: &mut AuthedClient,
475    ws: &mut WebSocket,
476) -> Result<(), Error> {
477    let req = req?;
478    execute_request(client, req, ws).await
479}
480
481/// Sends a single [`WebSocketResponse`] over the provided [`WebSocket`].
482async fn send_ws_response(ws: &mut WebSocket, resp: WebSocketResponse) -> Result<(), Error> {
483    let msg = serde_json::to_string(&resp).unwrap();
484    let msg = Message::Text(msg.into());
485    ws.send(msg).await?;
486
487    Ok(())
488}
489
490/// Forwards a collection of Notices to the provided [`WebSocket`].
491async fn forward_notices(
492    ws: &mut WebSocket,
493    notices: impl IntoIterator<Item = AdapterNotice>,
494) -> Result<(), Error> {
495    let ws_notices = notices.into_iter().map(|notice| {
496        WebSocketResponse::Notice(Notice {
497            message: notice.to_string(),
498            code: notice.code().code().to_string(),
499            severity: notice.severity().as_str().to_lowercase(),
500            detail: notice.detail(),
501            hint: notice.hint(),
502        })
503    });
504
505    for notice in ws_notices {
506        send_ws_response(ws, notice).await?;
507    }
508
509    Ok(())
510}
511
512/// A request to execute SQL over HTTP.
513#[derive(Serialize, Deserialize, Debug)]
514#[serde(untagged)]
515pub enum SqlRequest {
516    /// A simple query request.
517    Simple {
518        /// A query string containing zero or more queries delimited by
519        /// semicolons.
520        query: String,
521    },
522    /// An extended query request.
523    Extended {
524        /// Queries to execute using the extended protocol.
525        queries: Vec<ExtendedRequest>,
526    },
527}
528
529/// An request to execute a SQL query using the extended protocol.
530#[derive(Serialize, Deserialize, Debug)]
531pub struct ExtendedRequest {
532    /// A query string containing zero or one queries.
533    query: String,
534    /// Optional parameters for the query.
535    #[serde(default)]
536    params: Vec<Option<String>>,
537}
538
539/// The response to a `SqlRequest`.
540#[derive(Debug, Serialize, Deserialize)]
541pub struct SqlResponse {
542    /// The results for each query in the request.
543    pub(in crate::http) results: Vec<SqlResult>,
544}
545
546impl SqlResponse {
547    /// Creates a new empty SqlResponse for collecting results.
548    pub(in crate::http) fn new() -> Self {
549        Self {
550            results: Vec::new(),
551        }
552    }
553}
554
555pub(in crate::http) enum StatementResult {
556    SqlResult(SqlResult),
557    Subscribe {
558        desc: RelationDesc,
559        tag: String,
560        rx: RecordFirstRowStream,
561        ctx_extra: ExecuteContextGuard,
562    },
563}
564
565impl From<SqlResult> for StatementResult {
566    fn from(inner: SqlResult) -> Self {
567        Self::SqlResult(inner)
568    }
569}
570
571/// The result of a single query in a [`SqlResponse`].
572#[derive(Debug, Serialize, Deserialize)]
573#[serde(untagged)]
574pub enum SqlResult {
575    /// The query returned rows.
576    Rows {
577        /// The command complete tag.
578        tag: String,
579        /// The result rows.
580        rows: Vec<Vec<serde_json::Value>>,
581        /// Information about each column.
582        desc: Description,
583        // Any notices generated during execution of the query.
584        notices: Vec<Notice>,
585    },
586    /// The query executed successfully but did not return rows.
587    Ok {
588        /// The command complete tag.
589        ok: String,
590        /// Any notices generated during execution of the query.
591        notices: Vec<Notice>,
592        /// Any parameters that may have changed.
593        ///
594        /// Note: skip serializing this field in a response if the list of parameters is empty.
595        #[serde(skip_serializing_if = "Vec::is_empty")]
596        parameters: Vec<ParameterStatus>,
597    },
598    /// The query returned an error.
599    Err {
600        error: SqlError,
601        // Any notices generated during execution of the query.
602        notices: Vec<Notice>,
603    },
604}
605
606impl SqlResult {
607    /// Convert adapter Row results into the web row result format. Error if the row format does not
608    /// match the expected descriptor.
609    // TODO(aljoscha): Bail when max_result_size is exceeded.
610    async fn rows<S>(
611        sender: &mut S,
612        client: &mut SessionClient,
613        mut rows_stream: RecordFirstRowStream,
614        max_query_result_size: usize,
615        desc: &RelationDesc,
616    ) -> Result<SqlResult, Error>
617    where
618        S: ResultSender,
619    {
620        let mut rows: Vec<Vec<serde_json::Value>> = vec![];
621        let mut datum_vec = mz_repr::DatumVec::new();
622        let types = &desc.typ().column_types;
623
624        let mut query_result_size = 0;
625
626        loop {
627            let peek_response = tokio::select! {
628                notice = client.session().recv_notice(), if S::SUPPORTS_STREAMING_NOTICES => {
629                    sender.emit_streaming_notices(vec![notice]).await?;
630                    continue;
631                }
632                e = sender.connection_error() => return Err(e),
633                r = rows_stream.recv() => {
634                    match r {
635                        Some(r) => r,
636                        None => break,
637                    }
638                },
639            };
640
641            let mut sql_rows = match peek_response {
642                PeekResponseUnary::Rows(rows) => rows,
643                PeekResponseUnary::Error(e) => {
644                    return Ok(SqlResult::err(client, Error::Unstructured(anyhow!(e))));
645                }
646                PeekResponseUnary::Canceled => {
647                    return Ok(SqlResult::err(client, AdapterError::Canceled));
648                }
649            };
650
651            if let Err(err) = verify_datum_desc(desc, &mut sql_rows) {
652                return Ok(SqlResult::Err {
653                    error: err.into(),
654                    notices: make_notices(client),
655                });
656            }
657
658            while let Some(row) = sql_rows.next() {
659                query_result_size += row.byte_len();
660                if query_result_size > max_query_result_size {
661                    use bytesize::ByteSize;
662                    return Ok(SqlResult::err(
663                        client,
664                        AdapterError::ResultSize(format!(
665                            "result exceeds max size of {}",
666                            ByteSize::b(u64::cast_from(max_query_result_size))
667                        )),
668                    ));
669                }
670
671                let datums = datum_vec.borrow_with(row);
672                rows.push(
673                    datums
674                        .iter()
675                        .enumerate()
676                        .map(|(i, d)| {
677                            TypedDatum::new(*d, &types[i])
678                                .json(&JsonNumberPolicy::ConvertNumberToString)
679                        })
680                        .collect(),
681                );
682            }
683        }
684
685        let tag = format!("SELECT {}", rows.len());
686        Ok(SqlResult::Rows {
687            tag,
688            rows,
689            desc: Description::from(desc),
690            notices: make_notices(client),
691        })
692    }
693
694    fn err(client: &mut SessionClient, error: impl Into<SqlError>) -> SqlResult {
695        SqlResult::Err {
696            error: error.into(),
697            notices: make_notices(client),
698        }
699    }
700
701    fn ok(client: &mut SessionClient, tag: String, params: Vec<ParameterStatus>) -> SqlResult {
702        SqlResult::Ok {
703            ok: tag,
704            parameters: params,
705            notices: make_notices(client),
706        }
707    }
708}
709
710#[derive(Debug, Deserialize, Serialize)]
711pub struct SqlError {
712    pub message: String,
713    pub code: String,
714    #[serde(skip_serializing_if = "Option::is_none")]
715    pub detail: Option<String>,
716    #[serde(skip_serializing_if = "Option::is_none")]
717    pub hint: Option<String>,
718    #[serde(skip_serializing_if = "Option::is_none")]
719    pub position: Option<usize>,
720}
721
722impl From<Error> for SqlError {
723    fn from(err: Error) -> Self {
724        SqlError {
725            message: err.to_string(),
726            code: err.code().code().to_string(),
727            detail: err.detail(),
728            hint: err.hint(),
729            position: err.position(),
730        }
731    }
732}
733
734impl From<AdapterError> for SqlError {
735    fn from(value: AdapterError) -> Self {
736        Error::from(value).into()
737    }
738}
739
740#[derive(Debug, Deserialize, Serialize)]
741#[serde(tag = "type", content = "payload")]
742pub enum WebSocketResponse {
743    ReadyForQuery(String),
744    Notice(Notice),
745    Rows(Description),
746    Row(Vec<serde_json::Value>),
747    CommandStarting(CommandStarting),
748    CommandComplete(String),
749    Error(SqlError),
750    ParameterStatus(ParameterStatus),
751    BackendKeyData(BackendKeyData),
752}
753
754#[derive(Debug, Serialize, Deserialize)]
755pub struct Notice {
756    message: String,
757    code: String,
758    severity: String,
759    #[serde(skip_serializing_if = "Option::is_none")]
760    pub detail: Option<String>,
761    #[serde(skip_serializing_if = "Option::is_none")]
762    pub hint: Option<String>,
763}
764
765impl Notice {
766    pub fn message(&self) -> &str {
767        &self.message
768    }
769}
770
771#[derive(Debug, Serialize, Deserialize)]
772pub struct Description {
773    pub columns: Vec<Column>,
774}
775
776impl From<&RelationDesc> for Description {
777    fn from(desc: &RelationDesc) -> Self {
778        let columns = desc
779            .iter()
780            .map(|(name, typ)| {
781                let pg_type = mz_pgrepr::Type::from(&typ.scalar_type);
782                Column {
783                    name: name.to_string(),
784                    type_oid: pg_type.oid(),
785                    type_len: pg_type.typlen(),
786                    type_mod: pg_type.typmod(),
787                }
788            })
789            .collect();
790        Description { columns }
791    }
792}
793
794#[derive(Debug, Serialize, Deserialize)]
795pub struct Column {
796    pub name: String,
797    pub type_oid: u32,
798    pub type_len: i16,
799    pub type_mod: i32,
800}
801
802#[derive(Debug, Serialize, Deserialize)]
803pub struct ParameterStatus {
804    name: String,
805    value: String,
806}
807
808#[derive(Debug, Serialize, Deserialize)]
809pub struct BackendKeyData {
810    conn_id: u32,
811    secret_key: u32,
812}
813
814#[derive(Debug, Serialize, Deserialize)]
815pub struct CommandStarting {
816    has_rows: bool,
817    is_streaming: bool,
818}
819
820/// Trait describing how to transmit a response to a client. HTTP clients
821/// accumulate into a Vec and send all at once. WebSocket clients send each
822/// message as they occur.
823#[async_trait]
824pub(in crate::http) trait ResultSender: Send {
825    const SUPPORTS_STREAMING_NOTICES: bool = false;
826
827    /// Adds a result to the client. The first component of the return value is
828    /// Err if sending to the client
829    /// produced an error and the server should disconnect. It is Ok(Err) if the statement
830    /// produced an error and should error the transaction, but remain connected. It is Ok(Ok(()))
831    /// if the statement succeeded.
832    /// The second component of the return value is `Some` if execution still
833    /// needs to be retired for statement logging purposes.
834    async fn add_result(
835        &mut self,
836        client: &mut SessionClient,
837        res: StatementResult,
838    ) -> (
839        Result<Result<(), ()>, Error>,
840        Option<(StatementEndedExecutionReason, ExecuteContextGuard)>,
841    );
842
843    /// Returns a future that resolves only when the client connection has gone away.
844    fn connection_error(&mut self) -> BoxFuture<'_, Error>;
845    /// Reports whether the client supports streaming SUBSCRIBE results.
846    fn allow_subscribe(&self) -> bool;
847
848    /// Emits a streaming notice if the sender supports it.
849    ///
850    /// Does nothing if `SUPPORTS_STREAMING_NOTICES` is false.
851    async fn emit_streaming_notices(&mut self, _: Vec<AdapterNotice>) -> Result<(), Error> {
852        unreachable!("streaming notices marked as unsupported")
853    }
854}
855
856#[async_trait]
857impl ResultSender for SqlResponse {
858    // The first component of the return value is
859    // Err if sending to the client
860    // produced an error and the server should disconnect. It is Ok(Err) if the statement
861    // produced an error and should error the transaction, but remain connected. It is Ok(Ok(()))
862    // if the statement succeeded.
863    // The second component of the return value is `Some` if execution still
864    // needs to be retired for statement logging purposes.
865    async fn add_result(
866        &mut self,
867        _client: &mut SessionClient,
868        res: StatementResult,
869    ) -> (
870        Result<Result<(), ()>, Error>,
871        Option<(StatementEndedExecutionReason, ExecuteContextGuard)>,
872    ) {
873        let (res, stmt_logging) = match res {
874            StatementResult::SqlResult(res) => {
875                let is_err = matches!(res, SqlResult::Err { .. });
876                self.results.push(res);
877                let res = if is_err { Err(()) } else { Ok(()) };
878                (res, None)
879            }
880            StatementResult::Subscribe { ctx_extra, .. } => {
881                let message = "SUBSCRIBE only supported over websocket";
882                self.results.push(SqlResult::Err {
883                    error: Error::SubscribeOnlyOverWs.into(),
884                    notices: Vec::new(),
885                });
886                (
887                    Err(()),
888                    Some((
889                        StatementEndedExecutionReason::Errored {
890                            error: message.into(),
891                        },
892                        ctx_extra,
893                    )),
894                )
895            }
896        };
897        (Ok(res), stmt_logging)
898    }
899
900    fn connection_error(&mut self) -> BoxFuture<'_, Error> {
901        Box::pin(futures::future::pending())
902    }
903
904    fn allow_subscribe(&self) -> bool {
905        false
906    }
907}
908
909#[async_trait]
910impl ResultSender for WebSocket {
911    const SUPPORTS_STREAMING_NOTICES: bool = true;
912
913    // The first component of the return value is Err if sending to the client produced an error and
914    // the server should disconnect. It is Ok(Err) if the statement produced an error and should
915    // error the transaction, but remain connected. It is Ok(Ok(())) if the statement succeeded. The
916    // second component of the return value is `Some` if execution still needs to be retired for
917    // statement logging purposes.
918    async fn add_result(
919        &mut self,
920        client: &mut SessionClient,
921        res: StatementResult,
922    ) -> (
923        Result<Result<(), ()>, Error>,
924        Option<(StatementEndedExecutionReason, ExecuteContextGuard)>,
925    ) {
926        let (has_rows, is_streaming) = match res {
927            StatementResult::SqlResult(SqlResult::Err { .. }) => (false, false),
928            StatementResult::SqlResult(SqlResult::Ok { .. }) => (false, false),
929            StatementResult::SqlResult(SqlResult::Rows { .. }) => (true, false),
930            StatementResult::Subscribe { .. } => (true, true),
931        };
932        if let Err(e) = send_ws_response(
933            self,
934            WebSocketResponse::CommandStarting(CommandStarting {
935                has_rows,
936                is_streaming,
937            }),
938        )
939        .await
940        {
941            return (Err(e), None);
942        }
943
944        let (is_err, msgs, stmt_logging) = match res {
945            StatementResult::SqlResult(SqlResult::Rows {
946                tag,
947                rows,
948                desc,
949                notices,
950            }) => {
951                // Stream rows directly to avoid buffering all rows as
952                // WebSocketResponse, which inflates memory due to enum sizing.
953                if let Err(e) = send_ws_response(self, WebSocketResponse::Rows(desc)).await {
954                    return (Err(e), None);
955                }
956                for row in rows {
957                    if let Err(e) = send_ws_response(self, WebSocketResponse::Row(row)).await {
958                        return (Err(e), None);
959                    }
960                }
961                let mut msgs = vec![WebSocketResponse::CommandComplete(tag)];
962                msgs.extend(notices.into_iter().map(WebSocketResponse::Notice));
963                (false, msgs, None)
964            }
965            StatementResult::SqlResult(SqlResult::Ok {
966                ok,
967                parameters,
968                notices,
969            }) => {
970                let mut msgs = vec![WebSocketResponse::CommandComplete(ok)];
971                msgs.extend(notices.into_iter().map(WebSocketResponse::Notice));
972                msgs.extend(
973                    parameters
974                        .into_iter()
975                        .map(WebSocketResponse::ParameterStatus),
976                );
977                (false, msgs, None)
978            }
979            StatementResult::SqlResult(SqlResult::Err { error, notices }) => {
980                let mut msgs = vec![WebSocketResponse::Error(error)];
981                msgs.extend(notices.into_iter().map(WebSocketResponse::Notice));
982                (true, msgs, None)
983            }
984            StatementResult::Subscribe {
985                ref desc,
986                tag,
987                mut rx,
988                ctx_extra,
989            } => {
990                if let Err(e) = send_ws_response(self, WebSocketResponse::Rows(desc.into())).await {
991                    // We consider the remote breaking the connection to be a cancellation,
992                    // matching the behavior for pgwire
993                    return (
994                        Err(e),
995                        Some((StatementEndedExecutionReason::Canceled, ctx_extra)),
996                    );
997                }
998
999                let mut datum_vec = mz_repr::DatumVec::new();
1000                let mut result_size: usize = 0;
1001                let mut rows_returned = 0;
1002                loop {
1003                    let res = match await_rows(self, client, rx.recv()).await {
1004                        Ok(res) => res,
1005                        Err(e) => {
1006                            // We consider the remote breaking the connection to be a cancellation,
1007                            // matching the behavior for pgwire
1008                            return (
1009                                Err(e),
1010                                Some((StatementEndedExecutionReason::Canceled, ctx_extra)),
1011                            );
1012                        }
1013                    };
1014                    match res {
1015                        Some(PeekResponseUnary::Rows(mut rows)) => {
1016                            if let Err(err) = verify_datum_desc(desc, &mut rows) {
1017                                let error = err.to_string();
1018                                break (
1019                                    true,
1020                                    vec![WebSocketResponse::Error(err.into())],
1021                                    Some((
1022                                        StatementEndedExecutionReason::Errored { error },
1023                                        ctx_extra,
1024                                    )),
1025                                );
1026                            }
1027
1028                            rows_returned += rows.count();
1029                            while let Some(row) = rows.next() {
1030                                result_size += row.byte_len();
1031                                let datums = datum_vec.borrow_with(row);
1032                                let types = &desc.typ().column_types;
1033                                if let Err(e) = send_ws_response(
1034                                    self,
1035                                    WebSocketResponse::Row(
1036                                        datums
1037                                            .iter()
1038                                            .enumerate()
1039                                            .map(|(i, d)| {
1040                                                TypedDatum::new(*d, &types[i])
1041                                                    .json(&JsonNumberPolicy::ConvertNumberToString)
1042                                            })
1043                                            .collect(),
1044                                    ),
1045                                )
1046                                .await
1047                                {
1048                                    // We consider the remote breaking the connection to be a cancellation,
1049                                    // matching the behavior for pgwire
1050                                    return (
1051                                        Err(e),
1052                                        Some((StatementEndedExecutionReason::Canceled, ctx_extra)),
1053                                    );
1054                                }
1055                            }
1056                        }
1057                        Some(PeekResponseUnary::Error(error)) => {
1058                            break (
1059                                true,
1060                                vec![WebSocketResponse::Error(
1061                                    Error::Unstructured(anyhow!(error.clone())).into(),
1062                                )],
1063                                Some((StatementEndedExecutionReason::Errored { error }, ctx_extra)),
1064                            );
1065                        }
1066                        Some(PeekResponseUnary::Canceled) => {
1067                            break (
1068                                true,
1069                                vec![WebSocketResponse::Error(AdapterError::Canceled.into())],
1070                                Some((StatementEndedExecutionReason::Canceled, ctx_extra)),
1071                            );
1072                        }
1073                        None => {
1074                            break (
1075                                false,
1076                                vec![WebSocketResponse::CommandComplete(tag)],
1077                                Some((
1078                                    StatementEndedExecutionReason::Success {
1079                                        result_size: Some(u64::cast_from(result_size)),
1080                                        rows_returned: Some(u64::cast_from(rows_returned)),
1081                                        execution_strategy: Some(
1082                                            StatementExecutionStrategy::Standard,
1083                                        ),
1084                                    },
1085                                    ctx_extra,
1086                                )),
1087                            );
1088                        }
1089                    }
1090                }
1091            }
1092        };
1093        for msg in msgs {
1094            if let Err(e) = send_ws_response(self, msg).await {
1095                return (
1096                    Err(e),
1097                    stmt_logging.map(|(_old_reason, ctx_extra)| {
1098                        (StatementEndedExecutionReason::Canceled, ctx_extra)
1099                    }),
1100                );
1101            }
1102        }
1103        (Ok(if is_err { Err(()) } else { Ok(()) }), stmt_logging)
1104    }
1105
1106    // Send a websocket Ping every second to verify the client is still
1107    // connected.
1108    fn connection_error(&mut self) -> BoxFuture<'_, Error> {
1109        Box::pin(async {
1110            let mut tick = time::interval(Duration::from_secs(1));
1111            tick.tick().await;
1112            loop {
1113                tick.tick().await;
1114                if let Err(err) = self.send(Message::Ping(Vec::new().into())).await {
1115                    return err.into();
1116                }
1117            }
1118        })
1119    }
1120
1121    fn allow_subscribe(&self) -> bool {
1122        true
1123    }
1124
1125    async fn emit_streaming_notices(&mut self, notices: Vec<AdapterNotice>) -> Result<(), Error> {
1126        forward_notices(self, notices).await
1127    }
1128}
1129
1130async fn await_rows<S, F, R>(sender: &mut S, client: &mut SessionClient, f: F) -> Result<R, Error>
1131where
1132    S: ResultSender,
1133    F: Future<Output = R> + Send,
1134{
1135    let mut f = pin!(f);
1136    loop {
1137        tokio::select! {
1138            notice = client.session().recv_notice(), if S::SUPPORTS_STREAMING_NOTICES => {
1139                sender.emit_streaming_notices(vec![notice]).await?;
1140            }
1141            e = sender.connection_error() => return Err(e),
1142            r = &mut f => return Ok(r),
1143        }
1144    }
1145}
1146
1147async fn send_and_retire<S: ResultSender>(
1148    res: StatementResult,
1149    client: &mut SessionClient,
1150    sender: &mut S,
1151) -> Result<Result<(), ()>, Error> {
1152    let (res, stmt_logging) = sender.add_result(client, res).await;
1153    if let Some((reason, ctx_extra)) = stmt_logging {
1154        client.retire_execute(ctx_extra, reason);
1155    }
1156    res
1157}
1158
1159/// Returns Ok(Err) if any statement error'd during execution.
1160async fn execute_stmt_group<S: ResultSender>(
1161    client: &mut SessionClient,
1162    sender: &mut S,
1163    stmt_group: Vec<(Statement<Raw>, String, Vec<Option<String>>)>,
1164) -> Result<Result<(), ()>, Error> {
1165    let num_stmts = stmt_group.len();
1166    for (stmt, sql, params) in stmt_group {
1167        assert!(
1168            num_stmts <= 1 || params.is_empty(),
1169            "statement groups contain more than 1 statement iff Simple request, which does not support parameters"
1170        );
1171
1172        let is_aborted_txn = matches!(client.session().transaction(), TransactionStatus::Failed(_));
1173        if is_aborted_txn && !is_txn_exit_stmt(&stmt) {
1174            let err = SqlResult::err(client, Error::AbortedTransaction);
1175            let _ = send_and_retire(err.into(), client, sender).await?;
1176            return Ok(Err(()));
1177        }
1178
1179        // Mirror the behavior of the PostgreSQL simple query protocol.
1180        // See the pgwire::protocol::StateMachine::query method for details.
1181        if let Err(e) = client.start_transaction(Some(num_stmts)) {
1182            let err = SqlResult::err(client, e);
1183            let _ = send_and_retire(err.into(), client, sender).await?;
1184            return Ok(Err(()));
1185        }
1186        let res = execute_stmt(client, sender, stmt, sql, params).await?;
1187        let is_err = send_and_retire(res, client, sender).await?;
1188
1189        if is_err.is_err() {
1190            // Mirror StateMachine::error, which sometimes will clean up the
1191            // transaction state instead of always leaving it in Failed.
1192            let txn = client.session().transaction();
1193            match txn {
1194                // Error can be called from describe and parse and so might not be in an active
1195                // transaction.
1196                TransactionStatus::Default | TransactionStatus::Failed(_) => {}
1197                // In Started (i.e., a single statement) and implicit transactions cleanup themselves.
1198                TransactionStatus::Started(_) | TransactionStatus::InTransactionImplicit(_) => {
1199                    if let Err(err) = client.end_transaction(EndTransactionAction::Rollback).await {
1200                        let err = SqlResult::err(client, err);
1201                        let _ = send_and_retire(err.into(), client, sender).await?;
1202                    }
1203                }
1204                // Explicit transactions move to failed.
1205                TransactionStatus::InTransaction(_) => {
1206                    client.fail_transaction();
1207                }
1208            }
1209            return Ok(Err(()));
1210        }
1211    }
1212    Ok(Ok(()))
1213}
1214
1215/// Executes an entire [`SqlRequest`].
1216///
1217/// See the user-facing documentation about the HTTP API for a description of
1218/// the semantics of this function.
1219/// Executes a SQL request and sends results to the provided sender.
1220///
1221/// Made visible to http submodules (like mcp) via `pub(in crate::http)` to allow
1222/// reuse of SQL execution logic.
1223pub(in crate::http) async fn execute_request<S: ResultSender>(
1224    client: &mut AuthedClient,
1225    request: SqlRequest,
1226    sender: &mut S,
1227) -> Result<(), Error> {
1228    let client = &mut client.client;
1229
1230    // This API prohibits executing statements with responses whose
1231    // semantics are at odds with an HTTP response.
1232    fn check_prohibited_stmts<S: ResultSender>(
1233        sender: &S,
1234        stmt: &Statement<Raw>,
1235    ) -> Result<(), Error> {
1236        let kind: StatementKind = stmt.into();
1237        let execute_responses = Plan::generated_from(&kind)
1238            .into_iter()
1239            .map(ExecuteResponse::generated_from)
1240            .flatten()
1241            .collect::<Vec<_>>();
1242
1243        // Special-case `COPY TO` statements that are not `COPY ... TO STDOUT`, since
1244        // StatementKind::Copy links to several `ExecuteResponseKind`s that are not supported,
1245        // but this specific statement should be allowed.
1246        let is_valid_copy = matches!(
1247            stmt,
1248            Statement::Copy(CopyStatement {
1249                direction: CopyDirection::To,
1250                target: CopyTarget::Expr(_),
1251                ..
1252            }) | Statement::Copy(CopyStatement {
1253                direction: CopyDirection::From,
1254                target: CopyTarget::Expr(_),
1255                ..
1256            })
1257        );
1258
1259        if !is_valid_copy
1260            && execute_responses.iter().any(|execute_response| {
1261                // Returns true if a statement or execute response are unsupported.
1262                match execute_response {
1263                    ExecuteResponseKind::Subscribing if sender.allow_subscribe() => false,
1264                    ExecuteResponseKind::Fetch
1265                    | ExecuteResponseKind::Subscribing
1266                    | ExecuteResponseKind::CopyFrom
1267                    | ExecuteResponseKind::DeclaredCursor
1268                    | ExecuteResponseKind::ClosedCursor => true,
1269                    // Various statements generate `PeekPlan` (`SELECT`, `COPY`,
1270                    // `EXPLAIN`, `SHOW`) which has both `SendRows` and `CopyTo` as its
1271                    // possible response types. but `COPY` needs be picked out because
1272                    // http don't support its response type
1273                    ExecuteResponseKind::CopyTo if matches!(kind, StatementKind::Copy) => true,
1274                    _ => false,
1275                }
1276            })
1277        {
1278            return Err(Error::Unsupported(stmt.to_ast_string_simple()));
1279        }
1280        Ok(())
1281    }
1282
1283    fn parse<'a>(
1284        client: &SessionClient,
1285        query: &'a str,
1286    ) -> Result<Vec<StatementParseResult<'a>>, Error> {
1287        let result = client
1288            .parse(query)
1289            .map_err(|e| Error::Unstructured(anyhow!(e)))?;
1290        result.map_err(|e| AdapterError::from(e).into())
1291    }
1292
1293    let mut stmt_groups = vec![];
1294
1295    match request {
1296        SqlRequest::Simple { query } => match parse(client, &query) {
1297            Ok(stmts) => {
1298                let mut stmt_group = Vec::with_capacity(stmts.len());
1299                let mut stmt_err = None;
1300                for StatementParseResult { ast: stmt, sql } in stmts {
1301                    if let Err(err) = check_prohibited_stmts(sender, &stmt) {
1302                        stmt_err = Some(err);
1303                        break;
1304                    }
1305                    stmt_group.push((stmt, sql.to_string(), vec![]));
1306                }
1307                stmt_groups.push(stmt_err.map(Err).unwrap_or_else(|| Ok(stmt_group)));
1308            }
1309            Err(e) => stmt_groups.push(Err(e)),
1310        },
1311        SqlRequest::Extended { queries } => {
1312            for ExtendedRequest { query, params } in queries {
1313                match parse(client, &query) {
1314                    Ok(mut stmts) => {
1315                        if stmts.len() != 1 {
1316                            return Err(Error::Unstructured(anyhow!(
1317                                "each query must contain exactly 1 statement, but \"{}\" contains {}",
1318                                query,
1319                                stmts.len()
1320                            )));
1321                        }
1322
1323                        let StatementParseResult { ast: stmt, sql } = stmts.pop().unwrap();
1324                        stmt_groups.push(
1325                            check_prohibited_stmts(sender, &stmt)
1326                                .map(|_| vec![(stmt, sql.to_string(), params)]),
1327                        );
1328                    }
1329                    Err(e) => stmt_groups.push(Err(e)),
1330                };
1331            }
1332        }
1333    }
1334
1335    for stmt_group_res in stmt_groups {
1336        let executed = match stmt_group_res {
1337            Ok(stmt_group) => execute_stmt_group(client, sender, stmt_group).await,
1338            Err(e) => {
1339                let err = SqlResult::err(client, e);
1340                let _ = send_and_retire(err.into(), client, sender).await?;
1341                Ok(Err(()))
1342            }
1343        };
1344        // At the end of each group, commit implicit transactions. Do that here so that any `?`
1345        // early return can still be handled here.
1346        if client.session().transaction().is_implicit() {
1347            let ended = client.end_transaction(EndTransactionAction::Commit).await;
1348            if let Err(err) = ended {
1349                let err = SqlResult::err(client, err);
1350                let _ = send_and_retire(StatementResult::SqlResult(err), client, sender).await?;
1351            }
1352        }
1353        if executed?.is_err() {
1354            break;
1355        }
1356    }
1357
1358    Ok(())
1359}
1360
1361/// Executes a single statement in a [`SqlRequest`].
1362async fn execute_stmt<S: ResultSender>(
1363    client: &mut SessionClient,
1364    sender: &mut S,
1365    stmt: Statement<Raw>,
1366    sql: String,
1367    raw_params: Vec<Option<String>>,
1368) -> Result<StatementResult, Error> {
1369    const EMPTY_PORTAL: &str = "";
1370    if let Err(e) = client
1371        .prepare(EMPTY_PORTAL.into(), Some(stmt.clone()), sql, vec![])
1372        .await
1373    {
1374        return Ok(SqlResult::err(client, e).into());
1375    }
1376
1377    let prep_stmt = match client.get_prepared_statement(EMPTY_PORTAL).await {
1378        Ok(stmt) => stmt,
1379        Err(err) => {
1380            return Ok(SqlResult::err(client, err).into());
1381        }
1382    };
1383
1384    let param_types = &prep_stmt.desc().param_types;
1385    if param_types.len() != raw_params.len() {
1386        let message = anyhow!(
1387            "request supplied {actual} parameters, \
1388                        but {statement} requires {expected}",
1389            statement = stmt.to_ast_string_simple(),
1390            actual = raw_params.len(),
1391            expected = param_types.len()
1392        );
1393        return Ok(SqlResult::err(client, Error::Unstructured(message)).into());
1394    }
1395
1396    let buf = RowArena::new();
1397    let mut params = vec![];
1398    for (raw_param, mz_typ) in raw_params.into_iter().zip_eq(param_types) {
1399        let pg_typ = mz_pgrepr::Type::from(mz_typ);
1400        let datum = match raw_param {
1401            None => Datum::Null,
1402            Some(raw_param) => {
1403                match mz_pgrepr::Value::decode(
1404                    mz_pgwire_common::Format::Text,
1405                    &pg_typ,
1406                    raw_param.as_bytes(),
1407                ) {
1408                    Ok(param) => match param.into_datum_decode_error(&buf, &pg_typ, "parameter") {
1409                        Ok(datum) => datum,
1410                        Err(msg) => {
1411                            return Ok(
1412                                SqlResult::err(client, Error::Unstructured(anyhow!(msg))).into()
1413                            );
1414                        }
1415                    },
1416                    Err(err) => {
1417                        let msg = anyhow!("unable to decode parameter: {}", err);
1418                        return Ok(SqlResult::err(client, Error::Unstructured(msg)).into());
1419                    }
1420                }
1421            }
1422        };
1423        params.push((datum, mz_typ.clone()))
1424    }
1425
1426    let result_formats = vec![
1427        mz_pgwire_common::Format::Text;
1428        prep_stmt
1429            .desc()
1430            .relation_desc
1431            .clone()
1432            .map(|desc| desc.typ().column_types.len())
1433            .unwrap_or(0)
1434    ];
1435
1436    let desc = prep_stmt.desc().clone();
1437    let logging = Arc::clone(prep_stmt.logging());
1438    let stmt_ast = prep_stmt.stmt().cloned();
1439    let state_revision = prep_stmt.state_revision;
1440    if let Err(err) = client.session().set_portal(
1441        EMPTY_PORTAL.into(),
1442        desc,
1443        stmt_ast,
1444        logging,
1445        params,
1446        result_formats,
1447        state_revision,
1448    ) {
1449        return Ok(SqlResult::err(client, err).into());
1450    }
1451
1452    let desc = client
1453        .session()
1454        // We do not need to verify here because `client.execute` verifies below.
1455        .get_portal_unverified(EMPTY_PORTAL)
1456        .map(|portal| portal.desc.clone())
1457        .expect("unnamed portal should be present");
1458
1459    let res = client
1460        .execute(EMPTY_PORTAL.into(), futures::future::pending(), None)
1461        .await;
1462
1463    if S::SUPPORTS_STREAMING_NOTICES {
1464        sender
1465            .emit_streaming_notices(client.session().drain_notices())
1466            .await?;
1467    }
1468
1469    let (res, execute_started) = match res {
1470        Ok(res) => res,
1471        Err(e) => {
1472            return Ok(SqlResult::err(client, e).into());
1473        }
1474    };
1475    let tag = res.tag();
1476
1477    Ok(match res {
1478        ExecuteResponse::CreatedConnection { .. }
1479        | ExecuteResponse::CreatedDatabase { .. }
1480        | ExecuteResponse::CreatedSchema { .. }
1481        | ExecuteResponse::CreatedRole
1482        | ExecuteResponse::CreatedCluster { .. }
1483        | ExecuteResponse::CreatedClusterReplica { .. }
1484        | ExecuteResponse::CreatedTable { .. }
1485        | ExecuteResponse::CreatedIndex { .. }
1486        | ExecuteResponse::CreatedIntrospectionSubscribe
1487        | ExecuteResponse::CreatedSecret { .. }
1488        | ExecuteResponse::CreatedSource { .. }
1489        | ExecuteResponse::CreatedSink { .. }
1490        | ExecuteResponse::CreatedView { .. }
1491        | ExecuteResponse::CreatedViews { .. }
1492        | ExecuteResponse::CreatedMaterializedView { .. }
1493        | ExecuteResponse::CreatedContinualTask { .. }
1494        | ExecuteResponse::CreatedType
1495        | ExecuteResponse::CreatedNetworkPolicy
1496        | ExecuteResponse::Comment
1497        | ExecuteResponse::Deleted(_)
1498        | ExecuteResponse::DiscardedTemp
1499        | ExecuteResponse::DiscardedAll
1500        | ExecuteResponse::DroppedObject(_)
1501        | ExecuteResponse::DroppedOwned
1502        | ExecuteResponse::EmptyQuery
1503        | ExecuteResponse::GrantedPrivilege
1504        | ExecuteResponse::GrantedRole
1505        | ExecuteResponse::Inserted(_)
1506        | ExecuteResponse::Copied(_)
1507        | ExecuteResponse::Raised
1508        | ExecuteResponse::ReassignOwned
1509        | ExecuteResponse::RevokedPrivilege
1510        | ExecuteResponse::AlteredDefaultPrivileges
1511        | ExecuteResponse::RevokedRole
1512        | ExecuteResponse::StartedTransaction { .. }
1513        | ExecuteResponse::Updated(_)
1514        | ExecuteResponse::AlteredObject(_)
1515        | ExecuteResponse::AlteredRole
1516        | ExecuteResponse::AlteredSystemConfiguration
1517        | ExecuteResponse::Deallocate { .. }
1518        | ExecuteResponse::ValidatedConnection
1519        | ExecuteResponse::Prepare => SqlResult::ok(
1520            client,
1521            tag.expect("ok only called on tag-generating results"),
1522            Vec::default(),
1523        )
1524        .into(),
1525        ExecuteResponse::TransactionCommitted { params }
1526        | ExecuteResponse::TransactionRolledBack { params } => {
1527            let notify_set: mz_ore::collections::HashSet<_> = client
1528                .session()
1529                .vars()
1530                .notify_set()
1531                .map(|v| v.name().to_string())
1532                .collect();
1533            let params = params
1534                .into_iter()
1535                .filter(|(name, _value)| notify_set.contains(*name))
1536                .map(|(name, value)| ParameterStatus {
1537                    name: name.to_string(),
1538                    value,
1539                })
1540                .collect();
1541            SqlResult::ok(
1542                client,
1543                tag.expect("ok only called on tag-generating results"),
1544                params,
1545            )
1546            .into()
1547        }
1548        ExecuteResponse::SetVariable { name, .. } => {
1549            let mut params = Vec::with_capacity(1);
1550            if let Some(var) = client
1551                .session()
1552                .vars()
1553                .notify_set()
1554                .find(|v| v.name() == &name)
1555            {
1556                params.push(ParameterStatus {
1557                    name,
1558                    value: var.value(),
1559                });
1560            };
1561            SqlResult::ok(
1562                client,
1563                tag.expect("ok only called on tag-generating results"),
1564                params,
1565            )
1566            .into()
1567        }
1568        ExecuteResponse::SendingRowsStreaming {
1569            rows,
1570            instance_id,
1571            strategy,
1572        } => {
1573            let max_query_result_size =
1574                usize::cast_from(client.get_system_vars().await.max_result_size());
1575
1576            let rows_stream = RecordFirstRowStream::new(
1577                Box::new(rows),
1578                execute_started,
1579                client,
1580                Some(instance_id),
1581                Some(strategy),
1582            );
1583
1584            SqlResult::rows(
1585                sender,
1586                client,
1587                rows_stream,
1588                max_query_result_size,
1589                &desc.relation_desc.expect("RelationDesc must exist"),
1590            )
1591            .await?
1592            .into()
1593        }
1594        ExecuteResponse::SendingRowsImmediate { rows } => {
1595            let max_query_result_size =
1596                usize::cast_from(client.get_system_vars().await.max_result_size());
1597
1598            let rows = futures::stream::once(futures::future::ready(PeekResponseUnary::Rows(rows)));
1599            let rows_stream =
1600                RecordFirstRowStream::new(Box::new(rows), execute_started, client, None, None);
1601
1602            SqlResult::rows(
1603                sender,
1604                client,
1605                rows_stream,
1606                max_query_result_size,
1607                &desc.relation_desc.expect("RelationDesc must exist"),
1608            )
1609            .await?
1610            .into()
1611        }
1612        ExecuteResponse::Subscribing {
1613            rx,
1614            ctx_extra,
1615            instance_id,
1616        } => StatementResult::Subscribe {
1617            tag: "SUBSCRIBE".into(),
1618            desc: desc.relation_desc.unwrap(),
1619            rx: RecordFirstRowStream::new(
1620                Box::new(UnboundedReceiverStream::new(rx)),
1621                execute_started,
1622                client,
1623                Some(instance_id),
1624                None,
1625            ),
1626            ctx_extra,
1627        },
1628        res @ (ExecuteResponse::Fetch { .. }
1629        | ExecuteResponse::CopyTo { .. }
1630        | ExecuteResponse::CopyFrom { .. }
1631        | ExecuteResponse::DeclaredCursor
1632        | ExecuteResponse::ClosedCursor) => SqlResult::err(
1633            client,
1634            Error::Unstructured(anyhow!(
1635                "internal error: encountered prohibited ExecuteResponse {:?}.\n\n
1636            This is a bug. Can you please file an bug report letting us know?\n
1637            https://github.com/MaterializeInc/materialize/discussions/new?category=bug-reports",
1638                ExecuteResponseKind::from(res)
1639            )),
1640        )
1641        .into(),
1642    })
1643}
1644
1645fn make_notices(client: &mut SessionClient) -> Vec<Notice> {
1646    client
1647        .session()
1648        .drain_notices()
1649        .into_iter()
1650        .map(|notice| Notice {
1651            message: notice.to_string(),
1652            code: notice.code().code().to_string(),
1653            severity: notice.severity().as_str().to_lowercase(),
1654            detail: notice.detail(),
1655            hint: notice.hint(),
1656        })
1657        .collect()
1658}
1659
1660// Duplicated from protocol.rs.
1661// See postgres' backend/tcop/postgres.c IsTransactionExitStmt.
1662fn is_txn_exit_stmt(stmt: &Statement<Raw>) -> bool {
1663    matches!(
1664        stmt,
1665        Statement::Commit(_) | Statement::Rollback(_) | Statement::Prepare(_)
1666    )
1667}
1668
1669#[cfg(test)]
1670mod tests {
1671    use std::collections::BTreeMap;
1672
1673    use super::{Password, WebSocketAuth};
1674
1675    #[mz_ore::test]
1676    fn smoke_test_websocket_auth_parse() {
1677        struct TestCase {
1678            json: &'static str,
1679            expected: WebSocketAuth,
1680        }
1681
1682        let test_cases = vec![
1683            TestCase {
1684                json: r#"{ "user": "mz", "password": "1234" }"#,
1685                expected: WebSocketAuth::Basic {
1686                    user: "mz".to_string(),
1687                    password: Password("1234".to_string()),
1688                    options: BTreeMap::default(),
1689                },
1690            },
1691            TestCase {
1692                json: r#"{ "user": "mz", "password": "1234", "options": {} }"#,
1693                expected: WebSocketAuth::Basic {
1694                    user: "mz".to_string(),
1695                    password: Password("1234".to_string()),
1696                    options: BTreeMap::default(),
1697                },
1698            },
1699            TestCase {
1700                json: r#"{ "token": "i_am_a_token" }"#,
1701                expected: WebSocketAuth::Bearer {
1702                    token: "i_am_a_token".to_string(),
1703                    options: BTreeMap::default(),
1704                },
1705            },
1706            TestCase {
1707                json: r#"{ "token": "i_am_a_token", "options": { "foo": "bar" } }"#,
1708                expected: WebSocketAuth::Bearer {
1709                    token: "i_am_a_token".to_string(),
1710                    options: BTreeMap::from([("foo".to_string(), "bar".to_string())]),
1711                },
1712            },
1713        ];
1714
1715        fn assert_parse(json: &'static str, expected: WebSocketAuth) {
1716            let parsed: WebSocketAuth = serde_json::from_str(json).unwrap();
1717            assert_eq!(parsed, expected);
1718        }
1719
1720        for TestCase { json, expected } in test_cases {
1721            assert_parse(json, expected)
1722        }
1723    }
1724}