Skip to main content

mz_deploy/project/resolve/
normalize.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//! Name normalization for SQL statements using the visitor pattern.
11//!
12//! This module provides a flexible framework for transforming object names in SQL
13//! statements. It uses a trait-based visitor pattern to support different normalization
14//! strategies while sharing the same traversal logic.
15//!
16//! # Normalization Strategies
17//!
18//! - **Fully Qualifying**: Transforms names to `database.schema.object` format
19//! - **Flattening**: Transforms names to `database_schema_object` format (single identifier)
20//!
21//! # Usage
22//!
23//! ```rust,ignore
24//! use mz_deploy::project::resolve::normalize::NormalizingVisitor;
25//!
26//! // Create a fully qualifying visitor
27//! let visitor = NormalizingVisitor::fully_qualifying(&fqn);
28//!
29//! // Or create a flattening visitor
30//! let visitor = NormalizingVisitor::flattening(&fqn);
31//! ```
32//!
33//! # Module Structure
34//!
35//! - `transformers`: Name transformation strategies (FullyQualifying, Flattening, Staging)
36//! - `visitor`: The NormalizingVisitor that traverses SQL AST and applies transformations
37//! - `mod_rewriter`: AST-based rewriting of database/schema names in mod statements
38
39mod mod_rewriter;
40pub(crate) mod overlay_transformer;
41mod transformers;
42mod visitor;
43
44// Re-export all public types and functions
45pub(crate) use mod_rewriter::{rewrite_database_names, rewrite_schema_names};
46pub(crate) use transformers::{ClusterTransformer, NameTransformer};
47pub(crate) use visitor::NormalizingVisitor;
48
49use mz_sql_parser::ast::{CreateIndexStatement, Ident, Raw, RawClusterName};
50
51/// Transform cluster names in index statements for staging environments.
52///
53/// This is a standalone function that transforms cluster references without
54/// needing a full `NormalizingVisitor`. Use this when you only need to rename
55/// clusters (e.g., `quickstart` -> `quickstart_staging`) without transforming
56/// object names.
57///
58/// # Arguments
59/// * `indexes` - Slice of index statements to transform in place
60/// * `staging_suffix` - The suffix to append to cluster names (e.g., "_staging")
61///
62/// # Example
63/// ```rust,ignore
64/// transform_cluster_names_for_staging(&mut indexes, "_staging");
65/// // Transforms: IN CLUSTER quickstart -> IN CLUSTER quickstart_staging
66/// ```
67pub(crate) fn transform_cluster_names_for_staging(
68    indexes: &mut [CreateIndexStatement<Raw>],
69    staging_suffix: &str,
70) {
71    for index in indexes {
72        if let Some(ref mut cluster_name) = index.in_cluster {
73            if let RawClusterName::Unresolved(ident) = cluster_name {
74                let new_name = format!("{}{}", ident, staging_suffix);
75                *cluster_name =
76                    RawClusterName::Unresolved(Ident::new(&new_name).expect("valid cluster name"));
77            }
78        }
79    }
80}
81
82#[cfg(test)]
83mod tests;