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/// Move external indexes onto the staging cluster for staging environments.
52///
53/// Used for indexes belonging to objects that are not being redeployed but whose
54/// cluster is staged. Each index's `IN CLUSTER` is suffixed onto the staging
55/// cluster, and the index's own name is suffixed too.
56///
57/// The name suffix matters: the index still targets the production relation
58/// (its `on_name` is unchanged), and Materialize derives a named index's schema
59/// from that target. Without renaming, the recreated index would collide with
60/// the production index of the same name in the same schema. The optimizer
61/// selects indexes by cluster and structure, not by name, so the renamed index
62/// still serves the staged objects.
63///
64/// # Arguments
65/// * `indexes` - Slice of index statements to transform in place
66/// * `staging_suffix` - The suffix to append (e.g., "_staging")
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.as_str(), staging_suffix);
75 *cluster_name =
76 RawClusterName::Unresolved(Ident::new(&new_name).expect("valid cluster name"));
77 }
78 }
79 if let Some(ref mut name) = index.name {
80 let new_name = format!("{}{}", name.as_str(), staging_suffix);
81 *name = Ident::new(&new_name).expect("valid index name");
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests;