MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / create_statement

Function create_statement

src/sql/src/normalize.rs:171–481  ·  view source on GitHub ↗

Normalizes a `CREATE` statement. The resulting statement will not depend upon any session parameters, nor specify any non-default options (like `TEMPORARY`, `IF NOT EXISTS`, etc). The goal is to construct a backwards-compatible description of the item. SQL is the most stable part of Materialize, so SQL is used to describe the items that are persisted in the catalog.

(
    scx: &StatementContext,
    mut stmt: Statement<Aug>,
)

Source from the content-addressed store, hash-verified

169/// SQL is the most stable part of Materialize, so SQL is used to describe the
170/// items that are persisted in the catalog.
171pub fn create_statement(
172 scx: &StatementContext,
173 mut stmt: Statement<Aug>,
174) -> Result<String, PlanError> {
175 let allocate_name = |name: &UnresolvedItemName| -> Result<_, PlanError> {
176 Ok(unresolve(
177 scx.allocate_full_name(unresolved_item_name(name.clone())?)?,
178 ))
179 };
180
181 let allocate_temporary_name = |name: &UnresolvedItemName| -> Result<_, PlanError> {
182 Ok(unresolve(scx.allocate_temporary_full_name(
183 unresolved_item_name(name.clone())?,
184 )))
185 };
186
187 struct QueryNormalizer {
188 ctes: Vec<Ident>,
189 err: Option<PlanError>,
190 }
191
192 impl QueryNormalizer {
193 fn new() -> QueryNormalizer {
194 QueryNormalizer {
195 ctes: vec![],
196 err: None,
197 }
198 }
199 }
200
201 impl<'ast> VisitMut<'ast, Aug> for QueryNormalizer {
202 fn visit_query_mut(&mut self, query: &'ast mut Query<Aug>) {
203 let n = self.ctes.len();
204 match &query.ctes {
205 CteBlock::Simple(ctes) => {
206 for cte in ctes.iter() {
207 self.ctes.push(cte.alias.name.clone());
208 }
209 }
210 CteBlock::MutuallyRecursive(MutRecBlock { options: _, ctes }) => {
211 for cte in ctes.iter() {
212 self.ctes.push(cte.name.clone());
213 }
214 }
215 }
216 visit_mut::visit_query_mut(self, query);
217 self.ctes.truncate(n);
218 }
219
220 fn visit_function_mut(&mut self, func: &'ast mut Function<Aug>) {
221 match &mut func.args {
222 FunctionArgs::Star => (),
223 FunctionArgs::Args { args, order_by } => {
224 for arg in args {
225 self.visit_expr_mut(arg);
226 }
227 for expr in order_by {
228 self.visit_order_by_expr_mut(expr);

Callers 12

plan_create_tableFunction · 0.85
plan_create_sourceFunction · 0.85
plan_create_subsourceFunction · 0.85
plan_viewFunction · 0.85
plan_sinkFunction · 0.85
plan_create_indexFunction · 0.85
plan_create_typeFunction · 0.85
plan_create_secretFunction · 0.85
plan_create_connectionFunction · 0.85

Calls 9

unresolveFunction · 0.85
unresolved_item_nameFunction · 0.85
allocate_full_nameMethod · 0.80
sortMethod · 0.80
to_ast_string_stableMethod · 0.80
cloneMethod · 0.45
visit_query_mutMethod · 0.45
visit_expr_mutMethod · 0.45

Tested by

no test coverage detected