(
scx: &StatementContext,
stmt: CreateTableStatement<Aug>,
)
| 282 | } |
| 283 | |
| 284 | pub fn plan_create_table( |
| 285 | scx: &StatementContext, |
| 286 | stmt: CreateTableStatement<Aug>, |
| 287 | ) -> Result<Plan, PlanError> { |
| 288 | let CreateTableStatement { |
| 289 | name, |
| 290 | columns, |
| 291 | constraints, |
| 292 | if_not_exists, |
| 293 | temporary, |
| 294 | with_options, |
| 295 | } = &stmt; |
| 296 | |
| 297 | let names: Vec<_> = columns |
| 298 | .iter() |
| 299 | .filter(|c| { |
| 300 | // This set of `names` is used to create the initial RelationDesc. |
| 301 | // Columns that have been added at later versions of the table will |
| 302 | // get added further below. |
| 303 | let is_versioned = c |
| 304 | .options |
| 305 | .iter() |
| 306 | .any(|o| matches!(o.option, ColumnOption::Versioned { .. })); |
| 307 | !is_versioned |
| 308 | }) |
| 309 | .map(|c| normalize::column_name(c.name.clone())) |
| 310 | .collect(); |
| 311 | |
| 312 | if let Some(dup) = names.iter().duplicates().next() { |
| 313 | sql_bail!("column {} specified more than once", dup.quoted()); |
| 314 | } |
| 315 | |
| 316 | // Build initial relation type that handles declared data types |
| 317 | // and NOT NULL constraints. |
| 318 | let mut column_types = Vec::with_capacity(columns.len()); |
| 319 | let mut defaults = Vec::with_capacity(columns.len()); |
| 320 | let mut changes = BTreeMap::new(); |
| 321 | let mut keys = Vec::new(); |
| 322 | |
| 323 | for (i, c) in columns.into_iter().enumerate() { |
| 324 | let aug_data_type = &c.data_type; |
| 325 | let ty = query::scalar_type_from_sql(scx, aug_data_type)?; |
| 326 | let mut nullable = true; |
| 327 | let mut default = Expr::null(); |
| 328 | let mut versioned = false; |
| 329 | for option in &c.options { |
| 330 | match &option.option { |
| 331 | ColumnOption::NotNull => nullable = false, |
| 332 | ColumnOption::Default(expr) => { |
| 333 | // Ensure expression can be planned and yields the correct |
| 334 | // type. |
| 335 | let mut expr = expr.clone(); |
| 336 | transform_ast::transform(scx, &mut expr)?; |
| 337 | let _ = query::plan_default_expr(scx, &expr, &ty)?; |
| 338 | default = expr.clone(); |
| 339 | } |
| 340 | ColumnOption::Unique { is_primary } => { |
| 341 | keys.push(vec![i]); |
no test coverage detected