Skip to main content

mz_sql/plan/statement/
show.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
10//! Queries that show the state of the database system.
11//!
12//! This module houses the handlers for the `SHOW` suite of statements, like
13//! `SHOW CREATE TABLE` and `SHOW VIEWS`. Note that `SHOW <var>` is considered
14//! an SCL statement.
15
16use std::collections::{BTreeMap, BTreeSet};
17use std::fmt::Write;
18
19use mz_ore::collections::CollectionExt;
20use mz_repr::{CatalogItemId, Datum, RelationDesc, Row, SqlScalarType};
21use mz_sql_parser::ast::display::{AstDisplay, FormatMode};
22use mz_sql_parser::ast::{
23    CreateSinkOptionName, CreateSubsourceOptionName, ExternalReferenceExport, ExternalReferences,
24    ObjectType, ShowCreateClusterStatement, ShowCreateConnectionStatement,
25    ShowCreateMaterializedViewStatement, ShowCreateTypeStatement, ShowObjectType,
26    SqlServerConfigOptionName, SystemObjectType, UnresolvedItemName, WithOptionValue,
27};
28use mz_sql_pretty::PrettyConfig;
29use query::QueryContext;
30
31use crate::ast::display::escaped_string_literal;
32use crate::ast::visit_mut::VisitMut;
33use crate::ast::{
34    SelectStatement, ShowColumnsStatement, ShowCreateIndexStatement, ShowCreateSinkStatement,
35    ShowCreateSourceStatement, ShowCreateTableStatement, ShowCreateViewStatement,
36    ShowObjectsStatement, ShowStatementFilter, Statement, Value,
37};
38use crate::catalog::{CatalogItemType, SessionCatalog};
39use crate::names::{
40    self, Aug, NameSimplifier, ObjectId, ResolvedClusterName, ResolvedDataType,
41    ResolvedDatabaseName, ResolvedIds, ResolvedItemName, ResolvedRoleName, ResolvedSchemaName,
42};
43use crate::parse;
44use crate::plan::scope::Scope;
45use crate::plan::statement::ddl::unplan_create_cluster;
46use crate::plan::statement::{StatementContext, StatementDesc, dml};
47use crate::plan::{
48    HirRelationExpr, Params, Plan, PlanError, ShowColumnsPlan, ShowCreatePlan, query, transform_ast,
49};
50
51pub fn describe_show_create_view(
52    _: &StatementContext,
53    _: ShowCreateViewStatement<Aug>,
54) -> Result<StatementDesc, PlanError> {
55    Ok(StatementDesc::new(Some(
56        RelationDesc::builder()
57            .with_column("name", SqlScalarType::String.nullable(false))
58            .with_column("create_sql", SqlScalarType::String.nullable(false))
59            .finish(),
60    )))
61}
62
63pub fn plan_show_create_view(
64    scx: &StatementContext,
65    ShowCreateViewStatement {
66        view_name,
67        redacted,
68    }: ShowCreateViewStatement<Aug>,
69) -> Result<ShowCreatePlan, PlanError> {
70    plan_show_create_item(scx, &view_name, CatalogItemType::View, redacted)
71}
72
73pub fn describe_show_create_materialized_view(
74    _: &StatementContext,
75    _: ShowCreateMaterializedViewStatement<Aug>,
76) -> Result<StatementDesc, PlanError> {
77    Ok(StatementDesc::new(Some(
78        RelationDesc::builder()
79            .with_column("name", SqlScalarType::String.nullable(false))
80            .with_column("create_sql", SqlScalarType::String.nullable(false))
81            .finish(),
82    )))
83}
84
85pub fn plan_show_create_materialized_view(
86    scx: &StatementContext,
87    ShowCreateMaterializedViewStatement {
88        materialized_view_name,
89        redacted,
90    }: ShowCreateMaterializedViewStatement<Aug>,
91) -> Result<ShowCreatePlan, PlanError> {
92    plan_show_create_item(
93        scx,
94        &materialized_view_name,
95        CatalogItemType::MaterializedView,
96        redacted,
97    )
98}
99
100pub fn describe_show_create_table(
101    _: &StatementContext,
102    _: ShowCreateTableStatement<Aug>,
103) -> Result<StatementDesc, PlanError> {
104    Ok(StatementDesc::new(Some(
105        RelationDesc::builder()
106            .with_column("name", SqlScalarType::String.nullable(false))
107            .with_column("create_sql", SqlScalarType::String.nullable(false))
108            .finish(),
109    )))
110}
111
112fn plan_show_create_item(
113    scx: &StatementContext,
114    name: &ResolvedItemName,
115    expect_type: CatalogItemType,
116    redacted: bool,
117) -> Result<ShowCreatePlan, PlanError> {
118    let item = scx.get_item_by_resolved_name(name)?;
119    let name = name.full_name_str();
120    if item.id().is_system()
121        && matches!(
122            expect_type,
123            CatalogItemType::Table | CatalogItemType::Source
124        )
125    {
126        sql_bail!("cannot show create for system object {name}");
127    }
128    if item.item_type() == CatalogItemType::MaterializedView && expect_type == CatalogItemType::View
129    {
130        return Err(PlanError::ShowCreateViewOnMaterializedView(name));
131    }
132    if item.item_type() != expect_type {
133        sql_bail!("{name} is not a {expect_type}");
134    }
135    let create_sql =
136        humanize_sql_for_show_create(scx.catalog, item.id(), item.create_sql(), redacted)?;
137    Ok(ShowCreatePlan {
138        id: ObjectId::Item(item.id()),
139        row: Row::pack_slice(&[Datum::String(&name), Datum::String(&create_sql)]),
140    })
141}
142
143pub fn plan_show_create_table(
144    scx: &StatementContext,
145    ShowCreateTableStatement {
146        table_name,
147        redacted,
148    }: ShowCreateTableStatement<Aug>,
149) -> Result<ShowCreatePlan, PlanError> {
150    plan_show_create_item(scx, &table_name, CatalogItemType::Table, redacted)
151}
152
153pub fn describe_show_create_source(
154    _: &StatementContext,
155    _: ShowCreateSourceStatement<Aug>,
156) -> Result<StatementDesc, PlanError> {
157    Ok(StatementDesc::new(Some(
158        RelationDesc::builder()
159            .with_column("name", SqlScalarType::String.nullable(false))
160            .with_column("create_sql", SqlScalarType::String.nullable(false))
161            .finish(),
162    )))
163}
164
165pub fn plan_show_create_source(
166    scx: &StatementContext,
167    ShowCreateSourceStatement {
168        source_name,
169        redacted,
170    }: ShowCreateSourceStatement<Aug>,
171) -> Result<ShowCreatePlan, PlanError> {
172    plan_show_create_item(scx, &source_name, CatalogItemType::Source, redacted)
173}
174
175pub fn describe_show_create_sink(
176    _: &StatementContext,
177    _: ShowCreateSinkStatement<Aug>,
178) -> Result<StatementDesc, PlanError> {
179    Ok(StatementDesc::new(Some(
180        RelationDesc::builder()
181            .with_column("name", SqlScalarType::String.nullable(false))
182            .with_column("create_sql", SqlScalarType::String.nullable(false))
183            .finish(),
184    )))
185}
186
187pub fn plan_show_create_sink(
188    scx: &StatementContext,
189    ShowCreateSinkStatement {
190        sink_name,
191        redacted,
192    }: ShowCreateSinkStatement<Aug>,
193) -> Result<ShowCreatePlan, PlanError> {
194    plan_show_create_item(scx, &sink_name, CatalogItemType::Sink, redacted)
195}
196
197pub fn describe_show_create_index(
198    _: &StatementContext,
199    _: ShowCreateIndexStatement<Aug>,
200) -> Result<StatementDesc, PlanError> {
201    Ok(StatementDesc::new(Some(
202        RelationDesc::builder()
203            .with_column("name", SqlScalarType::String.nullable(false))
204            .with_column("create_sql", SqlScalarType::String.nullable(false))
205            .finish(),
206    )))
207}
208
209pub fn plan_show_create_index(
210    scx: &StatementContext,
211    ShowCreateIndexStatement {
212        index_name,
213        redacted,
214    }: ShowCreateIndexStatement<Aug>,
215) -> Result<ShowCreatePlan, PlanError> {
216    plan_show_create_item(scx, &index_name, CatalogItemType::Index, redacted)
217}
218
219pub fn describe_show_create_connection(
220    _: &StatementContext,
221    _: ShowCreateConnectionStatement<Aug>,
222) -> Result<StatementDesc, PlanError> {
223    Ok(StatementDesc::new(Some(
224        RelationDesc::builder()
225            .with_column("name", SqlScalarType::String.nullable(false))
226            .with_column("create_sql", SqlScalarType::String.nullable(false))
227            .finish(),
228    )))
229}
230
231pub fn plan_show_create_cluster(
232    scx: &StatementContext,
233    ShowCreateClusterStatement { cluster_name }: ShowCreateClusterStatement<Aug>,
234) -> Result<ShowCreatePlan, PlanError> {
235    let cluster = scx.get_cluster(&cluster_name.id);
236    let name = cluster.name().to_string();
237    let plan = cluster.try_to_plan()?;
238    let stmt = unplan_create_cluster(scx, plan)?;
239    let create_sql = stmt.to_ast_string_stable();
240    Ok(ShowCreatePlan {
241        id: ObjectId::Cluster(cluster_name.id),
242        row: Row::pack_slice(&[Datum::String(&name), Datum::String(&create_sql)]),
243    })
244}
245
246pub fn describe_show_create_cluster(
247    _: &StatementContext,
248    _: ShowCreateClusterStatement<Aug>,
249) -> Result<StatementDesc, PlanError> {
250    Ok(StatementDesc::new(Some(
251        RelationDesc::builder()
252            .with_column("name", SqlScalarType::String.nullable(false))
253            .with_column("create_sql", SqlScalarType::String.nullable(false))
254            .finish(),
255    )))
256}
257
258pub fn plan_show_create_type(
259    scx: &StatementContext,
260    ShowCreateTypeStatement {
261        type_name,
262        redacted,
263    }: ShowCreateTypeStatement<Aug>,
264) -> Result<ShowCreatePlan, PlanError> {
265    let ResolvedDataType::Named { id, full_name, .. } = type_name else {
266        sql_bail!("{type_name} is not a named type");
267    };
268
269    let type_item = scx.get_item(&id);
270
271    if id.is_system() {
272        sql_bail!("cannot show create for system type {full_name}");
273    }
274
275    let name = full_name.to_string();
276
277    let create_sql = humanize_sql_for_show_create(
278        scx.catalog,
279        type_item.id(),
280        type_item.create_sql(),
281        redacted,
282    )?;
283
284    Ok(ShowCreatePlan {
285        id: ObjectId::Item(id),
286        row: Row::pack_slice(&[Datum::String(&name), Datum::String(&create_sql)]),
287    })
288}
289
290pub fn describe_show_create_type(
291    _: &StatementContext,
292    _: ShowCreateTypeStatement<Aug>,
293) -> Result<StatementDesc, PlanError> {
294    Ok(StatementDesc::new(Some(
295        RelationDesc::builder()
296            .with_column("name", SqlScalarType::String.nullable(false))
297            .with_column("create_sql", SqlScalarType::String.nullable(false))
298            .finish(),
299    )))
300}
301
302pub fn plan_show_create_connection(
303    scx: &StatementContext,
304    ShowCreateConnectionStatement {
305        connection_name,
306        redacted,
307    }: ShowCreateConnectionStatement<Aug>,
308) -> Result<ShowCreatePlan, PlanError> {
309    plan_show_create_item(scx, &connection_name, CatalogItemType::Connection, redacted)
310}
311
312pub fn show_databases<'a>(
313    scx: &'a StatementContext<'a>,
314    filter: Option<ShowStatementFilter<Aug>>,
315) -> Result<ShowSelect<'a>, PlanError> {
316    let query = "SELECT name, comment FROM mz_internal.mz_show_databases".to_string();
317    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
318}
319
320pub fn show_schemas<'a>(
321    scx: &'a StatementContext<'a>,
322    from: Option<ResolvedDatabaseName>,
323    filter: Option<ShowStatementFilter<Aug>>,
324) -> Result<ShowSelect<'a>, PlanError> {
325    let database_id = match from {
326        Some(ResolvedDatabaseName::Database { id, .. }) => id.to_string(),
327        None => match scx.active_database() {
328            Some(id) => id.to_string(),
329            None => sql_bail!("no database specified and no active database"),
330        },
331        Some(ResolvedDatabaseName::Error) => {
332            bail_internal!("unresolved database name")
333        }
334    };
335    let query = format!(
336        "SELECT name, comment
337        FROM mz_internal.mz_show_schemas
338        WHERE database_id IS NULL OR database_id = '{database_id}'",
339    );
340    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
341}
342
343pub fn show_roles<'a>(
344    scx: &'a StatementContext<'a>,
345    filter: Option<ShowStatementFilter<Aug>>,
346) -> Result<ShowSelect<'a>, PlanError> {
347    let query = "SELECT name, comment FROM mz_internal.mz_show_roles".to_string();
348    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
349}
350
351pub fn show_network_policies<'a>(
352    scx: &'a StatementContext<'a>,
353    filter: Option<ShowStatementFilter<Aug>>,
354) -> Result<ShowSelect<'a>, PlanError> {
355    let query = "SELECT name, rules, comment FROM mz_internal.mz_show_network_policies".to_string();
356    ShowSelect::new(
357        scx,
358        query,
359        filter,
360        None,
361        Some(&["name", "rules", "comment"]),
362    )
363}
364
365/// Ensures that the `FROM` clause was not provided for `SHOW` commands that
366/// don't accept it. The parser is supposed to reject such cases, so this is an
367/// internal-only invariant.
368fn ensure_no_from<T>(from: Option<T>) -> Result<(), PlanError> {
369    if from.is_some() {
370        bail_internal!("FROM not supported for this SHOW command");
371    }
372    Ok(())
373}
374
375pub fn show_objects<'a>(
376    scx: &'a StatementContext<'a>,
377    ShowObjectsStatement {
378        object_type,
379        from,
380        filter,
381    }: ShowObjectsStatement<Aug>,
382) -> Result<ShowSelect<'a>, PlanError> {
383    match object_type {
384        ShowObjectType::Table { on_source } => show_tables(scx, from, on_source, filter),
385        ShowObjectType::Source { in_cluster } => show_sources(scx, from, in_cluster, filter),
386        ShowObjectType::Subsource { on_source } => show_subsources(scx, from, on_source, filter),
387        ShowObjectType::View => show_views(scx, from, filter),
388        ShowObjectType::Sink { in_cluster } => show_sinks(scx, from, in_cluster, filter),
389        ShowObjectType::Type => show_types(scx, from, filter),
390        ShowObjectType::Object => show_all_objects(scx, from, filter),
391        ShowObjectType::Role => {
392            ensure_no_from(from)?;
393            show_roles(scx, filter)
394        }
395        ShowObjectType::Cluster => {
396            ensure_no_from(from)?;
397            show_clusters(scx, filter)
398        }
399        ShowObjectType::ClusterReplica => {
400            ensure_no_from(from)?;
401            show_cluster_replicas(scx, filter)
402        }
403        ShowObjectType::Secret => show_secrets(scx, from, filter),
404        ShowObjectType::Connection => show_connections(scx, from, filter),
405        ShowObjectType::MaterializedView { in_cluster } => {
406            show_materialized_views(scx, from, in_cluster, filter)
407        }
408        ShowObjectType::Index {
409            in_cluster,
410            on_object,
411        } => show_indexes(scx, from, on_object, in_cluster, filter),
412        ShowObjectType::Database => {
413            ensure_no_from(from)?;
414            show_databases(scx, filter)
415        }
416        ShowObjectType::Schema { from: db_from } => {
417            ensure_no_from(from)?;
418            show_schemas(scx, db_from, filter)
419        }
420        ShowObjectType::Privileges { object_type, role } => {
421            ensure_no_from(from)?;
422            show_privileges(scx, object_type, role, filter)
423        }
424        ShowObjectType::DefaultPrivileges { object_type, role } => {
425            ensure_no_from(from)?;
426            show_default_privileges(scx, object_type, role, filter)
427        }
428        ShowObjectType::RoleMembership { role } => {
429            ensure_no_from(from)?;
430            show_role_membership(scx, role, filter)
431        }
432        ShowObjectType::NetworkPolicy => {
433            ensure_no_from(from)?;
434            show_network_policies(scx, filter)
435        }
436    }
437}
438
439fn show_connections<'a>(
440    scx: &'a StatementContext<'a>,
441    from: Option<ResolvedSchemaName>,
442    filter: Option<ShowStatementFilter<Aug>>,
443) -> Result<ShowSelect<'a>, PlanError> {
444    let schema_spec = scx.resolve_optional_schema(&from)?;
445    let query = format!(
446        "SELECT name, type, comment
447        FROM mz_internal.mz_show_connections connections
448        WHERE schema_id = '{schema_spec}'",
449    );
450    ShowSelect::new(scx, query, filter, None, Some(&["name", "type", "comment"]))
451}
452
453fn show_tables<'a>(
454    scx: &'a StatementContext<'a>,
455    from: Option<ResolvedSchemaName>,
456    on_source: Option<ResolvedItemName>,
457    filter: Option<ShowStatementFilter<Aug>>,
458) -> Result<ShowSelect<'a>, PlanError> {
459    let schema_spec = scx.resolve_optional_schema(&from)?;
460    let mut query = format!(
461        "SELECT name, comment
462        FROM mz_internal.mz_show_tables tables
463        WHERE tables.schema_id = '{schema_spec}'",
464    );
465    if let Some(on_source) = &on_source {
466        let on_item = scx.get_item_by_resolved_name(on_source)?;
467        if on_item.item_type() != CatalogItemType::Source {
468            sql_bail!(
469                "cannot show tables on {} because it is a {}",
470                on_source.full_name_str(),
471                on_item.item_type(),
472            );
473        }
474        query += &format!(" AND tables.source_id = '{}'", on_item.id());
475    }
476    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
477}
478
479fn show_sources<'a>(
480    scx: &'a StatementContext<'a>,
481    from: Option<ResolvedSchemaName>,
482    in_cluster: Option<ResolvedClusterName>,
483    filter: Option<ShowStatementFilter<Aug>>,
484) -> Result<ShowSelect<'a>, PlanError> {
485    let schema_spec = scx.resolve_optional_schema(&from)?;
486    let mut where_clause = format!("schema_id = '{schema_spec}'");
487
488    if let Some(cluster) = in_cluster {
489        write!(where_clause, " AND cluster_id = '{}'", cluster.id)
490            .expect("write on string cannot fail");
491    }
492
493    let query = format!(
494        "SELECT name, type, cluster, comment
495        FROM mz_internal.mz_show_sources
496        WHERE {where_clause}"
497    );
498    ShowSelect::new(
499        scx,
500        query,
501        filter,
502        None,
503        Some(&["name", "type", "cluster", "comment"]),
504    )
505}
506
507fn show_subsources<'a>(
508    scx: &'a StatementContext<'a>,
509    from_schema: Option<ResolvedSchemaName>,
510    on_source: Option<ResolvedItemName>,
511    filter: Option<ShowStatementFilter<Aug>>,
512) -> Result<ShowSelect<'a>, PlanError> {
513    let mut query_filter = Vec::new();
514
515    if on_source.is_none() && from_schema.is_none() {
516        query_filter.push("subsources.id NOT LIKE 's%'".into());
517        let schema_spec = scx.resolve_active_schema().map(|spec| spec.clone())?;
518        query_filter.push(format!("subsources.schema_id = '{schema_spec}'"));
519    }
520
521    if let Some(on_source) = &on_source {
522        let on_item = scx.get_item_by_resolved_name(on_source)?;
523        if on_item.item_type() != CatalogItemType::Source {
524            sql_bail!(
525                "cannot show subsources on {} because it is a {}",
526                on_source.full_name_str(),
527                on_item.item_type(),
528            );
529        }
530        query_filter.push(format!("sources.id = '{}'", on_item.id()));
531    }
532
533    if let Some(schema) = from_schema {
534        let schema_spec = schema.schema_spec();
535        query_filter.push(format!("subsources.schema_id = '{schema_spec}'"));
536    }
537
538    // TODO(database-issues#8322): this looks in both directions for subsources as long as
539    // progress collections still exist
540    let query = format!(
541        "SELECT DISTINCT
542            subsources.name AS name,
543            subsources.type AS type
544        FROM
545            mz_sources AS subsources
546            JOIN mz_internal.mz_object_dependencies deps ON (subsources.id = deps.object_id OR subsources.id = deps.referenced_object_id)
547            JOIN mz_sources AS sources ON (sources.id = deps.object_id OR sources.id = deps.referenced_object_id)
548        WHERE (subsources.type = 'subsource' OR subsources.type = 'progress') AND {}",
549        itertools::join(query_filter, " AND "),
550    );
551    ShowSelect::new(scx, query, filter, None, None)
552}
553
554fn show_views<'a>(
555    scx: &'a StatementContext<'a>,
556    from: Option<ResolvedSchemaName>,
557    filter: Option<ShowStatementFilter<Aug>>,
558) -> Result<ShowSelect<'a>, PlanError> {
559    let schema_spec = scx.resolve_optional_schema(&from)?;
560    let query = format!(
561        "SELECT name, comment
562        FROM mz_internal.mz_show_views
563        WHERE schema_id = '{schema_spec}'"
564    );
565    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
566}
567
568fn show_materialized_views<'a>(
569    scx: &'a StatementContext<'a>,
570    from: Option<ResolvedSchemaName>,
571    in_cluster: Option<ResolvedClusterName>,
572    filter: Option<ShowStatementFilter<Aug>>,
573) -> Result<ShowSelect<'a>, PlanError> {
574    let schema_spec = scx.resolve_optional_schema(&from)?;
575    let mut where_clause = format!("schema_id = '{schema_spec}'");
576
577    if let Some(cluster) = in_cluster {
578        write!(where_clause, " AND cluster_id = '{}'", cluster.id)
579            .expect("write on string cannot fail");
580    }
581
582    let query = format!(
583        "SELECT name, cluster, comment
584            FROM mz_internal.mz_show_materialized_views
585            WHERE {where_clause}"
586    );
587
588    let projection = vec!["name", "cluster", "comment"];
589
590    ShowSelect::new(scx, query, filter, None, Some(&projection))
591}
592
593fn show_sinks<'a>(
594    scx: &'a StatementContext<'a>,
595    from: Option<ResolvedSchemaName>,
596    in_cluster: Option<ResolvedClusterName>,
597    filter: Option<ShowStatementFilter<Aug>>,
598) -> Result<ShowSelect<'a>, PlanError> {
599    let schema_spec = if let Some(ResolvedSchemaName::Schema { schema_spec, .. }) = from {
600        schema_spec.to_string()
601    } else {
602        scx.resolve_active_schema()?.to_string()
603    };
604
605    let mut where_clause = format!("schema_id = '{schema_spec}'");
606
607    if let Some(cluster) = in_cluster {
608        write!(where_clause, " AND cluster_id = '{}'", cluster.id)
609            .expect("write on string cannot fail");
610    }
611
612    let query = format!(
613        "SELECT name, type, cluster, comment
614        FROM mz_internal.mz_show_sinks sinks
615        WHERE {where_clause}"
616    );
617    ShowSelect::new(
618        scx,
619        query,
620        filter,
621        None,
622        Some(&["name", "type", "cluster", "comment"]),
623    )
624}
625
626fn show_types<'a>(
627    scx: &'a StatementContext<'a>,
628    from: Option<ResolvedSchemaName>,
629    filter: Option<ShowStatementFilter<Aug>>,
630) -> Result<ShowSelect<'a>, PlanError> {
631    let schema_spec = scx.resolve_optional_schema(&from)?;
632    let query = format!(
633        "SELECT name, comment
634        FROM mz_internal.mz_show_types
635        WHERE schema_id = '{schema_spec}'"
636    );
637    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
638}
639
640fn show_all_objects<'a>(
641    scx: &'a StatementContext<'a>,
642    from: Option<ResolvedSchemaName>,
643    filter: Option<ShowStatementFilter<Aug>>,
644) -> Result<ShowSelect<'a>, PlanError> {
645    let schema_spec = scx.resolve_optional_schema(&from)?;
646    let query = format!(
647        "SELECT name, type, comment
648         FROM mz_internal.mz_show_all_objects
649         WHERE schema_id = '{schema_spec}'",
650    );
651    ShowSelect::new(scx, query, filter, None, Some(&["name", "type", "comment"]))
652}
653
654pub fn show_indexes<'a>(
655    scx: &'a StatementContext<'a>,
656    from_schema: Option<ResolvedSchemaName>,
657    on_object: Option<ResolvedItemName>,
658    in_cluster: Option<ResolvedClusterName>,
659    filter: Option<ShowStatementFilter<Aug>>,
660) -> Result<ShowSelect<'a>, PlanError> {
661    let mut query_filter = Vec::new();
662
663    if on_object.is_none() && from_schema.is_none() && in_cluster.is_none() {
664        query_filter.push("on_id NOT LIKE 's%'".into());
665        let schema_spec = scx.resolve_active_schema().map(|spec| spec.clone())?;
666        query_filter.push(format!("schema_id = '{schema_spec}'"));
667    }
668
669    if let Some(on_object) = &on_object {
670        let on_item = scx.get_item_by_resolved_name(on_object)?;
671        if on_item.item_type() != CatalogItemType::View
672            && on_item.item_type() != CatalogItemType::MaterializedView
673            && on_item.item_type() != CatalogItemType::Source
674            && on_item.item_type() != CatalogItemType::Table
675        {
676            sql_bail!(
677                "cannot show indexes on {} because it is a {}",
678                on_object.full_name_str(),
679                on_item.item_type(),
680            );
681        }
682        query_filter.push(format!("on_id = '{}'", on_item.id()));
683    }
684
685    if let Some(schema) = from_schema {
686        let schema_spec = schema.schema_spec();
687        query_filter.push(format!("schema_id = '{schema_spec}'"));
688    }
689
690    if let Some(cluster) = in_cluster {
691        query_filter.push(format!("cluster_id = '{}'", cluster.id))
692    };
693
694    let query = format!(
695        "SELECT name, on, cluster, key, comment
696        FROM mz_internal.mz_show_indexes
697        WHERE {}",
698        itertools::join(query_filter.iter(), " AND ")
699    );
700
701    ShowSelect::new(
702        scx,
703        query,
704        filter,
705        None,
706        Some(&["name", "on", "cluster", "key", "comment"]),
707    )
708}
709
710pub fn show_columns<'a>(
711    scx: &'a StatementContext<'a>,
712    ShowColumnsStatement { table_name, filter }: ShowColumnsStatement<Aug>,
713) -> Result<ShowColumnsSelect<'a>, PlanError> {
714    let entry = scx.get_item_by_resolved_name(&table_name)?;
715    let full_name = scx.catalog.resolve_full_name(entry.name());
716
717    match entry.item_type() {
718        CatalogItemType::Source
719        | CatalogItemType::Table
720        | CatalogItemType::View
721        | CatalogItemType::MaterializedView => (),
722        ty @ CatalogItemType::Connection
723        | ty @ CatalogItemType::Index
724        | ty @ CatalogItemType::Func
725        | ty @ CatalogItemType::Secret
726        | ty @ CatalogItemType::Type
727        | ty @ CatalogItemType::Sink => {
728            sql_bail!("{full_name} is a {ty} and so does not have columns");
729        }
730    }
731
732    let query = format!(
733        "SELECT name, nullable, type, position, comment
734         FROM mz_internal.mz_show_columns columns
735         WHERE columns.id = '{}'",
736        entry.id(),
737    );
738    let (show_select, new_resolved_ids) = ShowSelect::new_with_resolved_ids(
739        scx,
740        query,
741        filter,
742        Some("position"),
743        Some(&["name", "nullable", "type", "comment"]),
744    )?;
745    scx.record_sql_impl_ids(&new_resolved_ids);
746    Ok(ShowColumnsSelect {
747        id: entry.id(),
748        show_select,
749        new_resolved_ids,
750    })
751}
752
753// The rationale for which fields to include in the tuples are those
754// that are mandatory when creating a replica as part of the CREATE
755// CLUSTER command, i.e., name and size.
756pub fn show_clusters<'a>(
757    scx: &'a StatementContext<'a>,
758    filter: Option<ShowStatementFilter<Aug>>,
759) -> Result<ShowSelect<'a>, PlanError> {
760    let query =
761        "SELECT name, replicas, activity, comment FROM mz_internal.mz_show_clusters".to_string();
762    ShowSelect::new(
763        scx,
764        query,
765        filter,
766        None,
767        Some(&["name", "replicas", "activity", "comment"]),
768    )
769}
770
771pub fn show_cluster_replicas<'a>(
772    scx: &'a StatementContext<'a>,
773    filter: Option<ShowStatementFilter<Aug>>,
774) -> Result<ShowSelect<'a>, PlanError> {
775    let query = "
776    SELECT cluster, replica, size, ready, comment
777    FROM mz_internal.mz_show_cluster_replicas
778    "
779    .to_string();
780
781    ShowSelect::new(
782        scx,
783        query,
784        filter,
785        None,
786        Some(&["cluster", "replica", "size", "ready", "comment"]),
787    )
788}
789
790pub fn show_secrets<'a>(
791    scx: &'a StatementContext<'a>,
792    from: Option<ResolvedSchemaName>,
793    filter: Option<ShowStatementFilter<Aug>>,
794) -> Result<ShowSelect<'a>, PlanError> {
795    let schema_spec = scx.resolve_optional_schema(&from)?;
796
797    let query = format!(
798        "SELECT name, comment
799        FROM mz_internal.mz_show_secrets
800        WHERE schema_id = '{schema_spec}'",
801    );
802
803    ShowSelect::new(scx, query, filter, None, Some(&["name", "comment"]))
804}
805
806pub fn show_privileges<'a>(
807    scx: &'a StatementContext<'a>,
808    object_type: Option<SystemObjectType>,
809    role: Option<ResolvedRoleName>,
810    filter: Option<ShowStatementFilter<Aug>>,
811) -> Result<ShowSelect<'a>, PlanError> {
812    let mut query_filter = Vec::new();
813    if let Some(object_type) = object_type {
814        query_filter.push(format!(
815            "object_type = '{}'",
816            object_type.to_string().to_lowercase()
817        ));
818    }
819    if let Some(role) = role {
820        let name = escaped_string_literal(&role.name);
821        query_filter.push(format!(
822            "CASE WHEN grantee = 'PUBLIC' THEN true ELSE pg_has_role({name}, grantee, 'USAGE') END"
823        ));
824    }
825    let query_filter = if query_filter.len() > 0 {
826        format!("WHERE {}", itertools::join(query_filter, " AND "))
827    } else {
828        "".to_string()
829    };
830
831    let query = format!(
832        "SELECT grantor, grantee, database, schema, name, object_type, privilege_type
833        FROM mz_internal.mz_show_all_privileges
834        {query_filter}",
835    );
836
837    ShowSelect::new(
838        scx,
839        query,
840        filter,
841        None,
842        Some(&[
843            "grantor",
844            "grantee",
845            "database",
846            "schema",
847            "name",
848            "object_type",
849            "privilege_type",
850        ]),
851    )
852}
853
854pub fn show_default_privileges<'a>(
855    scx: &'a StatementContext<'a>,
856    object_type: Option<ObjectType>,
857    role: Option<ResolvedRoleName>,
858    filter: Option<ShowStatementFilter<Aug>>,
859) -> Result<ShowSelect<'a>, PlanError> {
860    let mut query_filter = Vec::new();
861    if let Some(object_type) = object_type {
862        query_filter.push(format!(
863            "object_type = '{}'",
864            object_type.to_string().to_lowercase()
865        ));
866    }
867    if let Some(role) = role {
868        let name = escaped_string_literal(&role.name);
869        query_filter.push(format!(
870            "CASE WHEN grantee = 'PUBLIC' THEN true ELSE pg_has_role({name}, grantee, 'USAGE') END"
871        ));
872    }
873    let query_filter = if query_filter.len() > 0 {
874        format!("WHERE {}", itertools::join(query_filter, " AND "))
875    } else {
876        "".to_string()
877    };
878
879    let query = format!(
880        "SELECT object_owner, database, schema, object_type, grantee, privilege_type
881        FROM mz_internal.mz_show_default_privileges
882        {query_filter}",
883    );
884
885    ShowSelect::new(
886        scx,
887        query,
888        filter,
889        None,
890        Some(&[
891            "object_owner",
892            "database",
893            "schema",
894            "object_type",
895            "grantee",
896            "privilege_type",
897        ]),
898    )
899}
900
901pub fn show_role_membership<'a>(
902    scx: &'a StatementContext<'a>,
903    role: Option<ResolvedRoleName>,
904    filter: Option<ShowStatementFilter<Aug>>,
905) -> Result<ShowSelect<'a>, PlanError> {
906    let mut query_filter = Vec::new();
907    if let Some(role) = role {
908        let name = escaped_string_literal(&role.name);
909        query_filter.push(format!("pg_has_role({name}, member, 'USAGE')"));
910    }
911    let query_filter = if query_filter.len() > 0 {
912        format!("WHERE {}", itertools::join(query_filter, " AND "))
913    } else {
914        "".to_string()
915    };
916
917    let query = format!(
918        "SELECT role, member, grantor
919        FROM mz_internal.mz_show_role_members
920        {query_filter}",
921    );
922
923    ShowSelect::new(
924        scx,
925        query,
926        filter,
927        None,
928        Some(&["role", "member", "grantor"]),
929    )
930}
931
932/// An intermediate result when planning a `SHOW` query.
933///
934/// Can be interrogated for its columns, or converted into a proper [`Plan`].
935pub struct ShowSelect<'a> {
936    scx: &'a StatementContext<'a>,
937    pub(crate) stmt: SelectStatement<Aug>,
938}
939
940impl<'a> ShowSelect<'a> {
941    /// Constructs a new [`ShowSelect`] from a query that provides the base
942    /// data and an optional user-supplied filter, order column, and
943    /// projection on that data.
944    ///
945    /// Note that the query must return a column named `name`, as the filter
946    /// may implicitly reference this column. Any `ORDER BY` in the query is
947    /// ignored. `ShowSelects`s are always ordered in ascending order by all
948    /// columns from left to right unless an order field is supplied.
949    fn new(
950        scx: &'a StatementContext,
951        query: String,
952        filter: Option<ShowStatementFilter<Aug>>,
953        order: Option<&str>,
954        projection: Option<&[&str]>,
955    ) -> Result<ShowSelect<'a>, PlanError> {
956        let (show_select, new_resolved_ids) =
957            Self::new_with_resolved_ids(scx, query, filter, order, projection)?;
958        scx.sql_impl_resolved_ids
959            .lock()
960            .expect("planning is single-threaded")
961            .extend_from(&new_resolved_ids);
962        Ok(show_select)
963    }
964
965    fn new_with_resolved_ids(
966        scx: &'a StatementContext,
967        query: String,
968        filter: Option<ShowStatementFilter<Aug>>,
969        order: Option<&str>,
970        projection: Option<&[&str]>,
971    ) -> Result<(ShowSelect<'a>, ResolvedIds), PlanError> {
972        let filter = match filter {
973            Some(ShowStatementFilter::Like(like)) => format!("name LIKE {}", Value::String(like)),
974            Some(ShowStatementFilter::Where(expr)) => expr.to_string(),
975            None => "true".to_string(),
976        };
977        let query = format!(
978            "SELECT {} FROM ({}) q WHERE {} ORDER BY {}",
979            projection
980                .map(|ps| ps.join(", "))
981                .unwrap_or_else(|| "*".into()),
982            query,
983            filter,
984            order.unwrap_or("q.*")
985        );
986
987        Self::new_from_bare_query(scx, query)
988    }
989
990    pub fn new_from_bare_query(
991        scx: &'a StatementContext,
992        query: String,
993    ) -> Result<(ShowSelect<'a>, ResolvedIds), PlanError> {
994        let stmts = parse::parse(&query)
995            .map_err(|e| internal_err!("failed to parse generated SHOW query: {}", e))?;
996        let stmt = match stmts.into_element().ast {
997            Statement::Select(select) => select,
998            _ => bail_internal!("generated SHOW query was not a SELECT statement"),
999        };
1000        let (mut stmt, new_resolved_ids) = names::resolve(scx.catalog, stmt)?;
1001        transform_ast::transform(scx, &mut stmt)?;
1002        Ok((ShowSelect { scx, stmt }, new_resolved_ids))
1003    }
1004
1005    /// Computes the shape of this `ShowSelect`.
1006    pub fn describe(self) -> Result<StatementDesc, PlanError> {
1007        dml::describe_select(self.scx, self.stmt)
1008    }
1009
1010    /// Converts this `ShowSelect` into a [`Plan`].
1011    pub fn plan(self) -> Result<Plan, PlanError> {
1012        dml::plan_select(self.scx, self.stmt, &Params::empty(), None)
1013    }
1014
1015    /// Converts this `ShowSelect` into a [`(HirRelationExpr, Scope)`].
1016    pub fn plan_hir(self, qcx: &QueryContext) -> Result<(HirRelationExpr, Scope), PlanError> {
1017        query::plan_nested_query(&mut qcx.clone(), &self.stmt.query)
1018    }
1019}
1020
1021pub struct ShowColumnsSelect<'a> {
1022    id: CatalogItemId,
1023    new_resolved_ids: ResolvedIds,
1024    show_select: ShowSelect<'a>,
1025}
1026
1027impl<'a> ShowColumnsSelect<'a> {
1028    pub fn describe(self) -> Result<StatementDesc, PlanError> {
1029        self.show_select.describe()
1030    }
1031
1032    pub fn plan(self) -> Result<Plan, PlanError> {
1033        let select_plan = self.show_select.plan()?;
1034        match select_plan {
1035            Plan::Select(select_plan) => Ok(Plan::ShowColumns(ShowColumnsPlan {
1036                id: self.id,
1037                select_plan,
1038                new_resolved_ids: self.new_resolved_ids,
1039            })),
1040            _ => {
1041                tracing::error!(
1042                    "SHOW COLUMNS produced a non select plan. plan: {:?}",
1043                    select_plan
1044                );
1045                Err(PlanError::Unstructured(
1046                    "SHOW COLUMNS produced an unexpected plan. Please file a bug.".to_string(),
1047                ))
1048            }
1049        }
1050    }
1051
1052    pub fn plan_hir(self, qcx: &QueryContext) -> Result<(HirRelationExpr, Scope), PlanError> {
1053        self.show_select.plan_hir(qcx)
1054    }
1055}
1056
1057/// Convert a SQL statement into a form that could be used as input, as well as
1058/// is more amenable to human consumption.
1059///
1060/// Note that the bits we omit here (e.g. the internal `AS OF` of a materialized
1061/// view, or the `DETAILS` of a `CREATE TABLE ... FROM SOURCE`) are not
1062/// user-typeable, but remain accessible for debugging in the raw `create_sql`
1063/// (and `redacted_create_sql`) column of catalog builtins like
1064/// `mz_catalog.mz_materialized_views` and `mz_catalog.mz_tables`. They are not
1065/// in the `definition` column, which is parsed back out of `create_sql` and only
1066/// retains the inner query.
1067fn humanize_sql_for_show_create(
1068    catalog: &dyn SessionCatalog,
1069    id: CatalogItemId,
1070    sql: &str,
1071    redacted: bool,
1072) -> Result<String, PlanError> {
1073    use mz_sql_parser::ast::{
1074        CreateSourceConnection, MySqlConfigOptionName, PgConfigOptionName, TableFromSourceColumns,
1075        TableFromSourceOptionName,
1076    };
1077
1078    let parsed = parse::parse(sql)?.into_element().ast;
1079    let (mut resolved, _) = names::resolve(catalog, parsed)?;
1080
1081    // Simplify names.
1082    let mut simplifier = NameSimplifier { catalog };
1083    simplifier.visit_statement_mut(&mut resolved);
1084
1085    match &mut resolved {
1086        // Strip internal `AS OF` syntax.
1087        Statement::CreateMaterializedView(stmt) => stmt.as_of = None,
1088        // Strip the internal `DETAILS` option, which is not user-typeable and
1089        // does not roundtrip.
1090        Statement::CreateTableFromSource(stmt) => {
1091            stmt.with_options.retain_mut(|o| match o.name {
1092                TableFromSourceOptionName::TextColumns => true,
1093                TableFromSourceOptionName::ExcludeColumns => true,
1094                // Drop details, which does not roundtrip.
1095                TableFromSourceOptionName::Details => false,
1096                TableFromSourceOptionName::PartitionBy => true,
1097                TableFromSourceOptionName::RetainHistory => true,
1098            });
1099            // The `Defined` column list and constraints are populated during
1100            // purification (from the upstream schema), and `CREATE TABLE ... FROM
1101            // SOURCE` rejects them as input. Omit them so the statement
1102            // roundtrips; purification re-derives them from the source on replay,
1103            // and the schema-affecting `TEXT COLUMNS` / `EXCLUDE COLUMNS` options
1104            // are retained above so the re-derived schema matches. A user-typed
1105            // `Named` column list is left intact, since it does roundtrip.
1106            if matches!(stmt.columns, TableFromSourceColumns::Defined(_)) {
1107                stmt.columns = TableFromSourceColumns::NotSpecified;
1108            }
1109            // Constraints are never valid input here (purification populates them
1110            // alongside `Defined` columns), so always drop them.
1111            stmt.constraints = Vec::new();
1112        }
1113        // `CREATE SOURCE` statements should roundtrip. However, sources and
1114        // their subsources have a complex relationship, so we need to do a lot
1115        // of work to reconstruct the statement for multi-output sources.
1116        //
1117        // For instance, `DROP SOURCE` statements can leave dangling references
1118        // to subsources that must be filtered out here, that, due to catalog
1119        // transaction limitations, can only be cleaned up when a top-level
1120        // source is altered.
1121        Statement::CreateSource(stmt) => {
1122            // Collect all current subsource references.
1123            let mut curr_references: BTreeMap<UnresolvedItemName, Vec<UnresolvedItemName>> =
1124                catalog
1125                    .get_item(&id)
1126                    .used_by()
1127                    .into_iter()
1128                    .filter_map(|subsource| {
1129                        let item = catalog.get_item(subsource);
1130                        item.subsource_details().map(|(_id, reference, _details)| {
1131                            let name = item.name();
1132                            let subsource_name = catalog.resolve_full_name(name);
1133                            let subsource_name = UnresolvedItemName::from(subsource_name);
1134                            (reference.clone(), subsource_name)
1135                        })
1136                    })
1137                    .fold(BTreeMap::new(), |mut map, (reference, subsource_name)| {
1138                        map.entry(reference)
1139                            .or_insert_with(Vec::new)
1140                            .push(subsource_name);
1141                        map
1142                    });
1143
1144            match &mut stmt.connection {
1145                CreateSourceConnection::Postgres { options, .. } => {
1146                    options.retain_mut(|o| {
1147                        match o.name {
1148                            // Dropping a subsource does not remove any `TEXT
1149                            // COLUMNS` values that refer to the table it
1150                            // ingests, which we'll handle below.
1151                            PgConfigOptionName::TextColumns => {}
1152                            // Drop details, which does not roundtrip.
1153                            PgConfigOptionName::Details => return false,
1154                            _ => return true,
1155                        };
1156                        match &mut o.value {
1157                            Some(WithOptionValue::Sequence(text_cols)) => {
1158                                text_cols.retain(|v| match v {
1159                                    WithOptionValue::UnresolvedItemName(n) => {
1160                                        let mut name = n.clone();
1161                                        // Remove the column reference.
1162                                        name.0.truncate(3);
1163                                        curr_references.contains_key(&name)
1164                                    }
1165                                    _ => unreachable!(
1166                                        "TEXT COLUMNS must be sequence of unresolved item names"
1167                                    ),
1168                                });
1169                                !text_cols.is_empty()
1170                            }
1171                            _ => unreachable!(
1172                                "TEXT COLUMNS must be sequence of unresolved item names"
1173                            ),
1174                        }
1175                    });
1176                }
1177                CreateSourceConnection::SqlServer { options, .. } => {
1178                    // TODO(sql_server2): TEXT and EXCLUDE columns are represented by
1179                    // `schema.table.column` whereas our external table references are
1180                    // `database.schema.table`. We handle the mismatch here but should
1181                    // probably fully qualify our TEXT and EXCLUDE column references.
1182                    let adjusted_references: BTreeSet<_> = curr_references
1183                        .keys()
1184                        .map(|name| {
1185                            if name.0.len() == 3 {
1186                                // Strip the database component of the name.
1187                                let adjusted_name = name.0[1..].to_vec();
1188                                UnresolvedItemName(adjusted_name)
1189                            } else {
1190                                name.clone()
1191                            }
1192                        })
1193                        .collect();
1194
1195                    options.retain_mut(|o| {
1196                        match o.name {
1197                            // Dropping a subsource does not remove any `TEXT COLUMNS`
1198                            // values that refer to the table it ingests, which we'll
1199                            // handle below.
1200                            SqlServerConfigOptionName::TextColumns
1201                            | SqlServerConfigOptionName::ExcludeColumns => {}
1202                            // Drop details, which does not roundtrip.
1203                            SqlServerConfigOptionName::Details => return false,
1204                        };
1205
1206                        match &mut o.value {
1207                            Some(WithOptionValue::Sequence(seq_unresolved_item_names)) => {
1208                                seq_unresolved_item_names.retain(|v| match v {
1209                                    WithOptionValue::UnresolvedItemName(n) => {
1210                                        let mut name = n.clone();
1211                                        // Remove column reference.
1212                                        name.0.truncate(2);
1213                                        adjusted_references.contains(&name)
1214                                    }
1215                                    _ => unreachable!(
1216                                        "TEXT COLUMNS + EXCLUDE COLUMNS must be sequence of unresolved item names"
1217                                    ),
1218                                });
1219                                !seq_unresolved_item_names.is_empty()
1220                            }
1221                            _ => unreachable!(
1222                                "TEXT COLUMNS + EXCLUDE COLUMNS must be sequence of unresolved item names"
1223                            ),
1224                        }
1225                    });
1226                }
1227                CreateSourceConnection::MySql { options, .. } => {
1228                    options.retain_mut(|o| {
1229                        match o.name {
1230                            // Dropping a subsource does not remove any `TEXT
1231                            // COLUMNS` values that refer to the table it
1232                            // ingests, which we'll handle below.
1233                            MySqlConfigOptionName::TextColumns
1234                            | MySqlConfigOptionName::ExcludeColumns => {}
1235                            // Drop details, which does not roundtrip.
1236                            MySqlConfigOptionName::Details => return false,
1237                        };
1238
1239                        match &mut o.value {
1240                            Some(WithOptionValue::Sequence(seq_unresolved_item_names)) => {
1241                                seq_unresolved_item_names.retain(|v| match v {
1242                                    WithOptionValue::UnresolvedItemName(n) => {
1243                                        let mut name = n.clone();
1244                                        // Remove column reference.
1245                                        name.0.truncate(2);
1246                                        curr_references.contains_key(&name)
1247                                    }
1248                                    _ => unreachable!(
1249                                        "TEXT COLUMNS + EXCLUDE COLUMNS must be sequence of unresolved item names"
1250                                    ),
1251                                });
1252                                !seq_unresolved_item_names.is_empty()
1253                            }
1254                            _ => unreachable!(
1255                                "TEXT COLUMNS + EXCLUDE COLUMNS must be sequence of unresolved item names"
1256                            ),
1257                        }
1258                    });
1259                }
1260                CreateSourceConnection::LoadGenerator { .. } if !curr_references.is_empty() => {
1261                    // Load generator sources with any references only support
1262                    // `FOR ALL TABLES`. However, this would change if database-issues#7911
1263                    // landed.
1264                    curr_references.clear();
1265                    stmt.external_references = Some(ExternalReferences::All);
1266                }
1267                CreateSourceConnection::Kafka { .. }
1268                | CreateSourceConnection::LoadGenerator { .. } => {}
1269            }
1270
1271            // If this source has any references, reconstruct them.
1272            if !curr_references.is_empty() {
1273                let mut subsources: Vec<_> = curr_references
1274                    .into_iter()
1275                    .flat_map(|(reference, names)| {
1276                        names.into_iter().map(move |name| ExternalReferenceExport {
1277                            reference: reference.clone(),
1278                            alias: Some(name),
1279                        })
1280                    })
1281                    .collect();
1282                subsources.sort();
1283                stmt.external_references = Some(ExternalReferences::SubsetTables(subsources));
1284            }
1285        }
1286        Statement::CreateSubsource(stmt) => {
1287            stmt.with_options.retain_mut(|o| {
1288                match o.name {
1289                    CreateSubsourceOptionName::TextColumns => true,
1290                    CreateSubsourceOptionName::RetainHistory => true,
1291                    CreateSubsourceOptionName::ExcludeColumns => true,
1292                    // Drop details, which does not roundtrip.
1293                    CreateSubsourceOptionName::Details => false,
1294                    CreateSubsourceOptionName::ExternalReference => true,
1295                    CreateSubsourceOptionName::Progress => true,
1296                }
1297            });
1298        }
1299        Statement::CreateSink(stmt) => {
1300            stmt.with_options.retain_mut(|o| {
1301                match o.name {
1302                    CreateSinkOptionName::CommitInterval => true,
1303                    CreateSinkOptionName::PartitionStrategy => true,
1304                    CreateSinkOptionName::Snapshot => true,
1305                    // Drop version, which does not roundtrip.
1306                    CreateSinkOptionName::Version => false,
1307                }
1308            });
1309        }
1310        _ => (),
1311    }
1312
1313    Ok(mz_sql_pretty::to_pretty(
1314        &resolved,
1315        PrettyConfig {
1316            width: mz_sql_pretty::DEFAULT_WIDTH,
1317            format_mode: if redacted {
1318                FormatMode::SimpleRedacted
1319            } else {
1320                FormatMode::Simple
1321            },
1322        },
1323    ))
1324}