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>,
)
| 169 | /// SQL is the most stable part of Materialize, so SQL is used to describe the |
| 170 | /// items that are persisted in the catalog. |
| 171 | pub 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); |
no test coverage detected