uqa-execution 0.6.2

Volcano physical operators with row-batch pipelines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Table INSERT scheduling, snapshot reads, row staging, and publication.

mod query;
use super::{
    codec::{
        decode_prepared_insert_spill_row, prepared_insert_spill_schema,
        push_prepared_insert_spill_row, PreparedInsertSpillRow,
    },
    known_new::KnownNewInserts,
    rows::prepare_values_insert_row,
    source::{InsertSelectConsumer, InsertSelectIdentity, PreparedInsertSelect},
    supplied_identities::SuppliedIdentities,
    triggers::insert_statement_events,
};
use crate::mutation::output::MutationOutput;
use crate::mutation::statement::context::{with_mutation_snapshot, MutationStatementContext};
use crate::{
    mutation::{
        assignment::{
            apply_missing_column_defaults, eval_mutation_assignment, MutationAssignmentTarget,
        },
        command_scope::MutationOverlayScope,
        conflict::update::InsertConflictLocks,
        constraints::{partition_insert_target, ConstraintStatement},
        errors::dml_storage_error,
        identity::{
            insert_identity_columns, persist_auto_increment_identity,
            prepare_auto_increment_identity, prepare_insert_identity,
        },
        publication::{apply_validated_prepared_insert, finish_mutation_publication},
        returning::{dml_returning_result, DmlReturningShape},
        statement_end,
        triggers::queue::StatementEvent,
    },
    query::CteScope,
};
use std::{rc::Rc, sync::Arc};
use uqa_sql::assignment::columns::validate_mutation_targets;
use uqa_sql::{
    plan::{ConflictActionPlan, ConflictPlan, InsertPlan, QueryPlan},
    semantics::{
        conflict::InferenceContext,
        returning::{validate_returning_alias_relations, ReturningAnalysisContext},
    },
    SQLError, SQLParam,
};
use uqa_storage::document_store::Document;

/// SQL analysis inputs and the planner's positional source-output rewrite for one INSERT.
pub struct InsertPlanning<'a> {
    pub inference: InferenceContext<'a>,
    pub returning: ReturningAnalysisContext<'a>,
    pub prune_source_outputs: crate::mutation::views::commands::SourceOutputPruning,
}

#[expect(clippy::too_many_lines, reason = "preserves DML lock and event order")]
pub fn run_table_insert<S: Clone + Send + Sync + 'static>(
    context: &MutationStatementContext<'_, S>,
    planning: InsertPlanning<'_>,
    stmt: &InsertPlan,
    params: &[SQLParam],
    inherited_ctes: Option<&CteScope<S>>,
) -> Result<MutationOutput, SQLError> {
    let mutation = &context.mutation;
    let preparation = mutation.preparation;
    let assignment = preparation.referential.assignment;
    let triggers = preparation.referential.triggers;
    let (statement_commands, _running_statement) =
        statement_end::statement_commands(inherited_ctes);
    let _trigger_scope = crate::mutation::triggers::TriggerStatementScope::enter();
    preparation.referential.locking.session.lock_relation(
        &stmt.table,
        crate::row_locks::RelationLockMode::RowExclusive,
    )?;
    let insert_rules = mutation
        .rules
        .rules
        .analysis
        .rules
        .rules_for(&stmt.table, uqa_sql::ast::RuleEvent::Insert)?;
    let has_insert_rules = !insert_rules.is_empty();
    let has_view_insert_rules = !stmt.view_rule_relations.is_empty();
    let has_any_insert_rules = has_insert_rules || has_view_insert_rules;
    if stmt.on_conflict.is_some()
        && (has_insert_rules
            || !mutation
                .rules
                .rules
                .analysis
                .rules
                .rules_for(&stmt.table, uqa_sql::ast::RuleEvent::Update)?
                .is_empty())
    {
        return Err(SQLError::Routine {
            sqlstate: "0A000".into(),
            message: "INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules".into(),
        });
    }
    validate_returning_alias_relations(&stmt.target_qualifier, &stmt.returning_aliases, None)?;
    uqa_sql::semantics::rules::validate_rule_returning_contract(
        mutation.rules.rules.analysis.rules,
        &stmt.table,
        uqa_sql::ast::RuleEvent::Insert,
        !stmt.returning.is_empty(),
    )?;
    if let Some(view_returning) = &stmt.view_rule_returning {
        uqa_sql::semantics::rules::validate_rule_returning_contract(
            mutation.rules.rules.analysis.rules,
            &view_returning.relation,
            uqa_sql::ast::RuleEvent::Insert,
            !view_returning.returning.is_empty(),
        )?;
    }
    let prepared_statement = uqa_sql::semantics::conflict::prepare_inference_predicate(
        planning.inference,
        stmt,
        params,
    )?;
    let stmt = prepared_statement.as_ref();
    if let Some(conflict) = stmt.on_conflict.as_ref() {
        uqa_sql::semantics::conflict::validate_conflict_target(
            planning.inference.catalog,
            &stmt.table,
            conflict,
        )?;
    }
    let conflict_update_columns = if let Some(ConflictPlan {
        action: ConflictActionPlan::Update { assignments, .. },
        ..
    }) = stmt.on_conflict.as_ref()
    {
        validate_mutation_targets(
            assignment.columns,
            &stmt.table,
            assignments
                .iter()
                .flat_map(|assignment| assignment.target.targets()),
            "INSERT ON CONFLICT DO UPDATE",
            false,
        )?;
        Some(
            assignments
                .iter()
                .flat_map(|assignment| assignment.target.column_names())
                .map(str::to_owned)
                .collect::<Vec<_>>(),
        )
    } else {
        None
    };
    if stmt.view_rule_relations.is_empty() && !stmt.columns.is_empty() {
        validate_mutation_targets(
            assignment.columns,
            &stmt.table,
            stmt.columns.iter(),
            "INSERT",
            true,
        )?;
    }
    if stmt.view_rule_relations.is_empty() {
        super::identity_targets::validate_insert_identity_targets(mutation.identities, stmt)?;
    }
    uqa_sql::semantics::returning::validate_insert_returning(
        planning.returning,
        stmt,
        params,
        inherited_ctes.map(|scope| scope as &dyn uqa_sql::semantics::returning::ReturningScope),
    )?;
    uqa_sql::semantics::mutation_privileges::ensure_insert_target_privileges(
        mutation.privileges,
        stmt,
        conflict_update_columns.as_deref(),
    )?;
    // The columns the statement supplies, which its constraint violations show to a role that may not read the table.
    let supplied_columns =
        uqa_sql::semantics::mutation_privileges::insert_target_columns(mutation.privileges, stmt)?
            .into_iter()
            .chain(conflict_update_columns.iter().flatten().cloned())
            .collect::<Vec<_>>();
    let statement_relation = crate::mutation::constraints::statement_relation(
        preparation.referential.constraints,
        &stmt.table,
    )?;
    let statement = ConstraintStatement::new(&statement_relation, &supplied_columns);
    let view_original_query = !stmt.view_rule_relations.iter().try_fold(
        false,
        |suppressed, relation| -> Result<bool, SQLError> {
            Ok(suppressed
                || mutation
                    .rules
                    .rules
                    .analysis
                    .rules
                    .rules_for(relation, uqa_sql::ast::RuleEvent::Insert)?
                    .iter()
                    .any(|rule| rule.definition.instead && rule.definition.condition.is_none()))
        },
    )?;
    let insert_original_query = view_original_query
        && !insert_rules
            .iter()
            .any(|rule| rule.definition.instead && rule.definition.condition.is_none());
    if insert_original_query {
        if !stmt.view_rule_relations.is_empty() {
            super::identity_targets::validate_insert_identity_targets(mutation.identities, stmt)?;
        }
        crate::mutation::routine_calls::insert(context, stmt, params, inherited_ctes)?;
    }
    let has_before_insert_statement_trigger = insert_original_query
        && !triggers
            .catalog
            .triggers_for(
                &stmt.table,
                uqa_sql::ast::TriggerTiming::Before,
                uqa_sql::ast::TriggerEvent::Insert,
                false,
                &[],
            )?
            .is_empty();
    let has_before_update_statement_trigger =
        if let Some(columns) = conflict_update_columns.as_deref() {
            !triggers
                .catalog
                .triggers_for(
                    &stmt.table,
                    uqa_sql::ast::TriggerTiming::Before,
                    uqa_sql::ast::TriggerEvent::Update,
                    false,
                    columns,
                )?
                .is_empty()
        } else {
            false
        };
    let statement_snapshot = match inherited_ctes.and_then(CteScope::command_cte_snapshot) {
        Some(snapshot) => Some(snapshot),
        None if has_before_insert_statement_trigger
            || has_before_update_statement_trigger
            || stmt.ctes.iter().any(|cte| cte.body.modifies_data()) =>
        {
            Some(std::sync::Arc::new(context.snapshots.capture()?))
        }
        None => None,
    };
    if insert_original_query {
        statement_commands.after_triggers().fire_before_statement(
            &triggers,
            &StatementEvent::new(&stmt.table, uqa_sql::ast::TriggerEvent::Insert, &[]),
        )?;
    }
    if let Some(columns) = conflict_update_columns.as_deref() {
        statement_commands.after_triggers().fire_before_statement(
            &triggers,
            &StatementEvent::new(&stmt.table, uqa_sql::ast::TriggerEvent::Update, columns),
        )?;
    }
    let mut statement_scope = None;
    let mut execute_read =
        |read_context: &MutationStatementContext<'_, S>| -> Result<MutationOutput, SQLError> {
            let read_assignment = read_context.mutation.preparation.referential.assignment;
            let scope = statement_scope.insert(read_context.mutation.scopes.command_scope(
                stmt.statement_privilege_subject.as_ref(),
                stmt.relations_bound,
            )?);
            if let Some(parent) = inherited_ctes {
                scope.inherit_cte_bindings(parent);
            }
            scope.set_statement_commands(Arc::clone(&statement_commands));
            scope.set_command_cte_snapshot(statement_snapshot.clone());
            crate::query::cte::materialize_command_ctes(
                context.query.source.ctes,
                &stmt.ctes,
                |control| {
                    uqa_sql::semantics::primary_command_cte_references_with_control(
                        &stmt.ctes,
                        stmt.query_inputs(),
                        None,
                        control,
                    )
                },
                params,
                scope,
            )?;
            scope.scalar_subqueries = stmt.subqueries.as_slice().into();
            let query_rules = (has_any_insert_rules || stmt.rule_values_input.is_some())
                .then(|| {
                    uqa_sql::semantics::rules::binding::InsertRuleQueries::bind(
                        mutation.rules.rules.analysis,
                        stmt,
                        &|table| mutation.rules.rules.security.privilege_subject(table),
                        scope.privilege_subject()?,
                    )
                })
                .transpose()?;
            if let Some(queries) = &query_rules {
                if queries.original.is_none() {
                    return crate::mutation::rules::run_insert_query_rules(
                        &mutation.rules.rules,
                        preparation.returning,
                        queries,
                        params,
                        scope,
                        planning.prune_source_outputs,
                    );
                }
            }
            // Resolve the column that names an inserted row: the table's single PRIMARY KEY column, whether a sequence generates its values or not, and otherwise the conventional legacy `id` slot of a table without declared columns. Both VALUES and SELECT sources must derive the internal doc id from this same column or later primary-key rewrites can address a different row than the one that was inserted.
            let (auto_id_col, id_column, identity_source) =
                insert_identity_columns(mutation.identities, &stmt.table, "INSERT")?;
            // INSERT ... SELECT: the query executor feeds each positional physical row directly into the INSERT sink. Ordinary source scans and scalar subqueries retain the statement snapshot, while a VOLATILE callback observes the logical mutations staged by preceding rows of this command.
            if let Some(source) = stmt.source.as_deref().or_else(|| {
                query_rules
                    .as_ref()
                    .and_then(|queries| queries.original.as_deref())
            }) {
                let output = query::run(query::InsertQueryExecution {
                    context,
                    read_context,
                    stmt,
                    source: query_rules
                        .as_ref()
                        .and_then(|queries| queries.original.as_deref())
                        .unwrap_or(source),
                    params,
                    scope,
                    identity: InsertSelectIdentity {
                        auto_id_column: auto_id_col,
                        id_column,
                        identity_source,
                    },
                    conflict_update_columns: conflict_update_columns.as_deref(),
                    statement_commands: &statement_commands,
                })?;
                if let Some(queries) = &query_rules {
                    crate::mutation::rules::run_insert_query_rules(
                        &mutation.rules.rules,
                        preparation.returning,
                        queries,
                        params,
                        scope,
                        planning.prune_source_outputs,
                    )?;
                }
                return Ok(output);
            }

            let implicit_columns = stmt.columns.is_empty();
            let columns: Vec<uqa_sql::ast::AssignmentTarget<uqa_sql::ScalarExpr>> =
                if implicit_columns {
                    // INSERT without explicit column list: project the table schema.
                    preparation
                        .returning
                        .catalog
                        .try_table_columns(&stmt.table)
                        .map_err(|error| dml_storage_error("INSERT", error))?
                        .into_iter()
                        .map(Into::into)
                        .collect()
                } else {
                    stmt.columns.clone()
                };
            if view_original_query {
                validate_mutation_targets(
                    assignment.columns,
                    &stmt.table,
                    columns.iter(),
                    "INSERT",
                    true,
                )?;
            }

            // No explicit id and no auto-increment column: allocate a synthetic u64 doc_id at insert time. Every table has an implicit doc_id even when the schema declares no primary key.

            let mut affected = 0u64;
            let mut returning_rows = crate::mutation::returning::ReturningRows::new(
                context.query.source.relational.runtime,
            )?;
            let cancel = context.query.source.relational.runtime.cancellation_token();
            // Evaluate, validate, and stage every VALUES row before writer promotion. A scalar subquery inside VALUES may carry FOR UPDATE, so holding the backend writer during that wait would fabricate a deadlock. Ordinary subqueries retain the statement snapshot while VOLATILE functions read the logical overlay left by preceding rows, matching PostgreSQL 18 command visibility.
            let snapshot_scope = scope.returning_statement_snapshot_scope();
            let overlay = MutationOverlayScope::new(mutation.state);
            let mut conflict_locks = InsertConflictLocks::new(&preparation.referential);
            let prepared_schema = prepared_insert_spill_schema();
            let mut prepared_rows = crate::SpillBuffer::new(
                crate::query::projection::physical_work_mem_bytes(
                    context.query.source.relational.runtime,
                )?
                .max(1),
            );
            let mut supplied_identities = SuppliedIdentities::default();
            let mut events = crate::mutation::events::MutationEventQueue::default();
            let mut has_prepared_effect = false;
            let mut has_prepared_auto_identity = false;
            let discarded_identities = super::identity_targets::user_value_identity_columns(
                mutation.identities,
                stmt,
                columns.iter().map(|column| column.column.as_str()),
            )?;
            for row in &stmt.rows {
                cancel.check()?;
                if row.len() > columns.len() || (!implicit_columns && row.len() != columns.len()) {
                    return Err(SQLError::Routine {
                        sqlstate: "42601".into(),
                        message: if row.len() > columns.len() {
                            "INSERT has more expressions than target columns"
                        } else {
                            "INSERT has more target columns than expressions"
                        }
                        .into(),
                    });
                }
                let mut document = Document::new();
                for (i, col) in columns.iter().take(row.len()).enumerate() {
                    if discarded_identities.contains(&col.column) {
                        continue;
                    }
                    let value = eval_mutation_assignment(
                        read_assignment,
                        &snapshot_scope,
                        MutationAssignmentTarget {
                            table: &stmt.table,
                            target: col,
                            current: document.get(&col.column),
                            final_column_write: !columns[i + 1..]
                                .iter()
                                .any(|next| next.column == col.column),
                            action: "INSERT",
                            new_row: true,
                        },
                        &row[i],
                        None,
                        params,
                    )?;
                    if let Some(value) = value {
                        document.insert(col.column.clone(), value);
                    }
                }
                apply_missing_column_defaults(assignment, &stmt.table, &mut document, params)?;
                let prepared_auto_identity = prepare_auto_increment_identity(
                    mutation.identities,
                    &stmt.table,
                    &id_column,
                    identity_source,
                    auto_id_col.as_deref(),
                    &mut document,
                    "prepare INSERT identity",
                )?;
                has_prepared_auto_identity |= prepared_auto_identity.is_some();
                let target_table = partition_insert_target(
                    preparation.referential.constraints,
                    statement,
                    &stmt.table,
                    &document,
                    params,
                    stmt.include_descendants,
                )?;
                preparation.referential.locking.session.lock_relation(
                    &target_table,
                    crate::row_locks::RelationLockMode::RowExclusive,
                )?;
                let insert_identity = match prepared_auto_identity {
                    Some(identity) => identity,
                    None => prepare_insert_identity(
                        mutation.identities,
                        &target_table,
                        &id_column,
                        identity_source,
                        None,
                        &mut document,
                        "prepare INSERT identity",
                    )?,
                };
                if let Some(staged) = prepare_values_insert_row(
                    preparation,
                    stmt,
                    statement,
                    params,
                    &snapshot_scope,
                    conflict_update_columns.as_deref().unwrap_or(&[]),
                    &id_column,
                    identity_source,
                    target_table,
                    document,
                    insert_identity,
                    &mut conflict_locks,
                )? {
                    if let Some(returning) = staged.returning {
                        returning_rows.push_to(
                            returning,
                            context.mutation.preparation.returning.destination,
                        )?;
                    }
                    events.append_after_rows(staged.after_row_events, &triggers)?;
                    if staged.prepared_effect {
                        affected += 1;
                        has_prepared_effect = true;
                    }
                    supplied_identities.note(&staged.target_table, &staged.prepared);
                    push_prepared_insert_spill_row(
                        &mut prepared_rows,
                        &prepared_schema,
                        PreparedInsertSpillRow {
                            target_table: staged.target_table,
                            document: Arc::unwrap_or_clone(staged.document),
                            conflict: staged.prepared,
                        },
                    )?;
                }
            }
            let published = overlay.finish();
            if has_prepared_effect || has_prepared_auto_identity {
                mutation.state.prepare_writer()?;
                persist_auto_increment_identity(
                    mutation.identities,
                    &stmt.table,
                    auto_id_col.as_deref(),
                    "persist INSERT identity",
                )?;
            }
            drop(conflict_locks);
            let observed = supplied_identities.observe(mutation.publication.storage)?;
            let mut publication =
                statement_end::publication_batch(&statement_commands).with_published(published);
            let mut known_new = KnownNewInserts::new(
                preparation.referential.constraints.catalog,
                &id_column,
                stmt.on_conflict.is_some(),
            );
            for row in prepared_rows
                .drain_rows()
                .map_err(crate::physical::physical_exec_error)?
            {
                cancel.check()?;
                let PreparedInsertSpillRow {
                    target_table,
                    document,
                    conflict: prepared,
                } = decode_prepared_insert_spill_row(
                    row.map_err(crate::physical::physical_exec_error)?,
                )?;
                let inserted = known_new.identity(
                    &target_table,
                    &prepared,
                    &observed,
                    mutation.publication.identifiers,
                )?;
                apply_validated_prepared_insert(
                    mutation.publication,
                    &target_table,
                    document,
                    prepared,
                    inserted,
                    &mut publication,
                )?;
            }
            finish_mutation_publication(mutation.publication, &mut publication)?;
            statement_end::note_written_rows(&statement_commands, &mut publication);
            statement_end::end_command(
                &statement_commands,
                &triggers,
                &insert_statement_events(
                    &stmt.table,
                    insert_original_query,
                    conflict_update_columns.as_deref(),
                ),
                events.into_after_rows(),
            )?;
            if !stmt.returning.is_empty() {
                let shape = DmlReturningShape {
                    table: &stmt.table,
                    target_qualifier: &stmt.target_qualifier,
                    aliases: &stmt.returning_aliases,
                    returning: &stmt.returning,
                    params,
                    ctes: scope,
                    supplemental_schema: None,
                };
                return dml_returning_result(
                    preparation.returning,
                    shape,
                    returning_rows,
                    affected,
                );
            }
            Ok(MutationOutput::from_affected(affected))
        };
    let result = match statement_snapshot.as_deref() {
        Some(snapshot) => with_mutation_snapshot(context.snapshots, snapshot, execute_read),
        None => execute_read(context),
    };
    statement_end::finish_statement(
        context,
        params,
        &stmt.ctes,
        statement_scope.as_mut(),
        result,
    )
}