Plan an INSERT statement.
(ins: &ast::Insert, catalog: &dyn SqlCatalog)
| 60 | |
| 61 | /// Plan an INSERT statement. |
| 62 | pub fn plan_insert(ins: &ast::Insert, catalog: &dyn SqlCatalog) -> Result<Vec<SqlPlan>> { |
| 63 | // `INSERT ... ON CONFLICT DO UPDATE SET` reroutes to the upsert path |
| 64 | // with the assignments carried through. `DO NOTHING` stays on the |
| 65 | // INSERT path with `if_absent=true`. |
| 66 | let if_absent = match classify_on_conflict(ins)? { |
| 67 | OnConflict::None => false, |
| 68 | OnConflict::DoNothing => true, |
| 69 | OnConflict::DoUpdate(updates) => { |
| 70 | return plan_upsert_with_on_conflict(ins, catalog, updates); |
| 71 | } |
| 72 | }; |
| 73 | let table_name = match &ins.table { |
| 74 | ast::TableObject::TableName(name) => normalize_object_name_checked(name)?, |
| 75 | ast::TableObject::TableFunction(_) => { |
| 76 | return Err(SqlError::Unsupported { |
| 77 | detail: "INSERT INTO table function not supported".into(), |
| 78 | }); |
| 79 | } |
| 80 | }; |
| 81 | let info = catalog |
| 82 | .get_collection(DatabaseId::DEFAULT, &table_name)? |
| 83 | .ok_or_else(|| SqlError::UnknownTable { |
| 84 | name: table_name.clone(), |
| 85 | })?; |
| 86 | |
| 87 | let columns: Vec<String> = ins.columns.iter().map(normalize_ident).collect(); |
| 88 | |
| 89 | // Check for INSERT...SELECT. |
| 90 | if let Some(source) = &ins.source |
| 91 | && let ast::SetExpr::Select(_select) = &*source.body |
| 92 | { |
| 93 | let source_plan = super::select::plan_query( |
| 94 | source, |
| 95 | catalog, |
| 96 | &crate::functions::registry::FunctionRegistry::new(), |
| 97 | crate::TemporalScope::default(), |
| 98 | )?; |
| 99 | return Ok(vec![SqlPlan::InsertSelect { |
| 100 | target: table_name, |
| 101 | source: Box::new(source_plan), |
| 102 | limit: 0, |
| 103 | }]); |
| 104 | } |
| 105 | |
| 106 | // VALUES clause. |
| 107 | let source = ins.source.as_ref().ok_or_else(|| SqlError::Parse { |
| 108 | detail: "INSERT requires VALUES or SELECT".into(), |
| 109 | })?; |
| 110 | |
| 111 | let rows_ast = match &*source.body { |
| 112 | ast::SetExpr::Values(values) => &values.rows, |
| 113 | _ => { |
| 114 | return Err(SqlError::Unsupported { |
| 115 | detail: "INSERT source must be VALUES or SELECT".into(), |
| 116 | }); |
| 117 | } |
| 118 | }; |
| 119 |
no test coverage detected