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

Function plan_insert_query

src/sql/src/plan/query.rs:218–435  ·  view source on GitHub ↗
(
    scx: &StatementContext,
    table_name: ResolvedItemName,
    columns: Vec<Ident>,
    source: InsertSource<Aug>,
    returning: Vec<SelectItem<Aug>>,
)

Source from the content-addressed store, hash-verified

216}
217
218pub fn plan_insert_query(
219 scx: &StatementContext,
220 table_name: ResolvedItemName,
221 columns: Vec<Ident>,
222 source: InsertSource<Aug>,
223 returning: Vec<SelectItem<Aug>>,
224) -> Result<
225 (
226 CatalogItemId,
227 HirRelationExpr,
228 PlannedRootQuery<Vec<HirScalarExpr>>,
229 ),
230 PlanError,
231> {
232 let mut qcx = QueryContext::root(scx, QueryLifetime::OneShot);
233 let table = scx.get_item_by_resolved_name(&table_name)?;
234
235 // Validate the target of the insert.
236 if table.item_type() != CatalogItemType::Table {
237 sql_bail!(
238 "cannot insert into {} '{}'",
239 table.item_type(),
240 table_name.full_name_str()
241 );
242 }
243 let desc = table
244 .relation_desc()
245 .ok_or_else(|| sql_err!("item does not have a relation description"))?;
246 let mut defaults = table
247 .writable_table_details()
248 .ok_or_else(|| {
249 sql_err!(
250 "cannot insert into non-writeable table '{}'",
251 table_name.full_name_str()
252 )
253 })?
254 .to_vec();
255
256 for default in &mut defaults {
257 transform_ast::transform(scx, default)?;
258 }
259
260 if table.id().is_system() {
261 sql_bail!(
262 "cannot insert into system table '{}'",
263 table_name.full_name_str()
264 );
265 }
266
267 let columns: Vec<_> = columns.into_iter().map(normalize::column_name).collect();
268
269 // Validate target column order.
270 let mut source_types = Vec::with_capacity(columns.len());
271 let mut ordering = Vec::with_capacity(columns.len());
272
273 if columns.is_empty() {
274 // Columns in source query must be in order. Let's guess the full shape and truncate to the
275 // right size later after planning the source query

Callers 2

describe_insertFunction · 0.85
plan_insertFunction · 0.85

Calls 15

transformFunction · 0.85
plan_values_insertFunction · 0.85
plan_nested_queryFunction · 0.85
cast_relationFunction · 0.85
plan_default_exprFunction · 0.85
expand_select_itemFunction · 0.85
plan_exprFunction · 0.85
to_vecMethod · 0.80
iter_typesMethod · 0.80
enumerateMethod · 0.80
iter_namesMethod · 0.80

Tested by

no test coverage detected