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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! DELETE execution with snapshot qualification, tuple rechecks, and deferred publication.
use crate::mutation::output::MutationOutput;
use crate::mutation::statement::context::{with_mutation_snapshot, MutationStatementContext};
use crate::mutation::{
    candidate::{MutationCandidate, PhysicalDocumentIdentity, PhysicalMutationLockTarget},
    command_scope::MutationOverlayScope,
    prepared::decode_prepared_mutation_action_row,
    returning::DmlReturningShape,
    statement_end,
    triggers::queue::StatementEvent,
};
use crate::query::CteScope;
use std::collections::BTreeSet;
use uqa_sql::{
    plan::DeletePlan,
    semantics::{
        mutation_qualifiers::validate_dml_expression_qualifiers,
        returning::validate_returning_alias_relations,
    },
    SQLError, SQLParam,
};
mod qualification;
mod rows;
use qualification::{recheck_delete_candidate, DeleteCandidateRecheck, DeleteSourceRows};
#[expect(
    clippy::too_many_lines,
    reason = "preserves statement trigger, snapshot, row deletion, and publication order"
)]
pub fn run_table_delete<S: Clone + Send + Sync + 'static>(
    context: &MutationStatementContext<'_, S>,
    stmt: &DeletePlan,
    params: &[SQLParam],
    inherited_ctes: Option<&CteScope<S>>,
) -> Result<MutationOutput, SQLError> {
    let privilege_expressions =
        uqa_sql::semantics::mutation_privileges::ensure_delete_target_privileges(
            context.mutation.privileges,
            stmt,
        )?;
    let (statement_commands, _running_statement) =
        statement_end::statement_commands(inherited_ctes);
    let _trigger_scope = crate::mutation::triggers::TriggerStatementScope::enter();
    context.query.source.locking.session.lock_relation(
        &stmt.table,
        crate::row_locks::RelationLockMode::RowExclusive,
    )?;
    uqa_sql::semantics::rules::validate_rule_returning_contract(
        context.mutation.rules.rules.analysis.rules,
        &stmt.table,
        uqa_sql::ast::RuleEvent::Delete,
        !stmt.returning.is_empty(),
    )?;
    if let Some(view_returning) = &stmt.view_rule_returning {
        uqa_sql::semantics::rules::validate_rule_returning_contract(
            context.mutation.rules.rules.analysis.rules,
            &view_returning.relation,
            uqa_sql::ast::RuleEvent::Delete,
            !view_returning.returning.is_empty(),
        )?;
    }
    crate::mutation::routine_calls::delete(context, stmt, params, inherited_ctes)?;
    let has_before_statement_trigger = !context
        .mutation
        .preparation
        .referential
        .triggers
        .catalog
        .triggers_for(
            &stmt.table,
            uqa_sql::ast::TriggerTiming::Before,
            uqa_sql::ast::TriggerEvent::Delete,
            false,
            &[],
        )?
        .is_empty();
    let statement_snapshot = match inherited_ctes.and_then(CteScope::command_cte_snapshot) {
        Some(snapshot) => Some(snapshot),
        None if has_before_statement_trigger
            || stmt.ctes.iter().any(|cte| cte.body.modifies_data()) =>
        {
            Some(std::sync::Arc::new(context.snapshots.capture()?))
        }
        None => None,
    };
    let delete_statement =
        StatementEvent::new(&stmt.table, uqa_sql::ast::TriggerEvent::Delete, &[]);

    statement_commands.after_triggers().fire_before_statement(
        &context.mutation.preparation.referential.triggers,
        &delete_statement,
    )?;

    let mut statement_scope = None;
    let mut execute_read =
        |read_context: &MutationStatementContext<'_, S>| -> Result<MutationOutput, SQLError> {
            let mut affected = 0u64;
            let cancel = context.query.source.relational.runtime.cancellation_token();
            let mut deleted = rows::DeleteRows::new(context)?;
            let ctes = statement_scope.insert(read_context.mutation.scopes.command_scope(
                stmt.statement_privilege_subject.as_ref(),
                stmt.relations_bound,
            )?);
            if let Some(parent) = inherited_ctes {
                ctes.inherit_cte_bindings(parent);
            }
            ctes.set_statement_commands(std::sync::Arc::clone(&statement_commands));
            ctes.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(),
                        stmt.source_input(),
                        control,
                    )
                },
                params,
                ctes,
            )?;
            ctes.scalar_subqueries = stmt.subqueries.as_slice().into();
            if let Some(source) = stmt.source.as_deref() {
                crate::query::privileges::ensure_select_privileges_for_source_expressions(
                    source,
                    &privilege_expressions,
                    ctes,
                )?;
            }
            if stmt.source.is_none() {
                let allowed = BTreeSet::from([stmt.target_qualifier.clone()]);
                if let Some(predicate) = stmt.predicate.as_ref() {
                    validate_dml_expression_qualifiers(predicate, &allowed)?;
                }
            }
            // Retain the USING source, then qualify each joined row immediately before its mutation and RETURNING output.
            let using_rows: Option<crate::SharedSpill> = match stmt.source.as_deref() {
                Some(source) => Some(crate::query::sources::build_join_spill_with_ctes(
                    &read_context.query.source,
                    source,
                    params,
                    ctes,
                )?),
                None => None,
            };
            validate_returning_alias_relations(
                &stmt.target_qualifier,
                &stmt.returning_aliases,
                using_rows.as_ref().map(crate::SharedSpill::row_schema),
            )?;
            let has_runtime_scope = !ctes.rows.is_empty() || !ctes.scalar_subqueries.is_empty();
            // A non-volatile plain predicate can use the accelerated candidate set. A VOLATILE predicate must qualify rows in command order so each prior logical deletion is visible to the next callback.
            let predicate_is_volatile = stmt.predicate.as_ref().is_some_and(|predicate| {
                uqa_sql::semantics::volatility::expr_contains_volatile_function(
                    context.query.source.volatility,
                    predicate,
                )
            });
            let preselected = !has_runtime_scope
                && stmt.source.is_none()
                && stmt.predicate.is_some()
                && !predicate_is_volatile;
            let target_tables = read_context
                .mutation
                .preparation
                .referential
                .constraints
                .catalog
                .hierarchy_scan_tables(&stmt.table, stmt.include_descendants)?;
            let mut candidates = crate::mutation::candidate_ids::CandidateIds::new(
                context.mutation.rules.rules.memory.work_mem_bytes()?,
                cancel.clone(),
            );
            for (table_index, table) in target_tables.iter().enumerate() {
                if preselected {
                    let filter = stmt.predicate.as_ref().ok_or_else(|| {
                        SQLError::Internal("DELETE preselection is missing its predicate".into())
                    })?;
                    crate::query::block::where_filter::visit_where_doc_ids(
                        &read_context.query.source,
                        table,
                        &stmt.target_qualifier,
                        filter,
                        params,
                        ctes,
                        &mut |doc_id| candidates.push(table_index, doc_id),
                    )?;
                } else {
                    let tables = read_context.query.source.scans.tables;
                    let source = tables.table(table)?;
                    let changes = tables.command_overlay_changes(table)?.unwrap_or_default();
                    candidates.capture_table(
                        table_index,
                        source.read_documents().as_ref(),
                        &changes,
                    )?;
                }
            }
            let candidates = candidates.finish()?;
            let snapshot_ctes = ctes.returning_statement_snapshot_scope();
            let mut qualification = crate::mutation::expressions::MutationPredicate::new(
                read_context
                    .mutation
                    .preparation
                    .referential
                    .assignment
                    .expressions,
                &snapshot_ctes,
                stmt.predicate.as_ref(),
                params,
            );
            let overlay = MutationOverlayScope::new(context.mutation.state);
            let mut completed_ids = crate::mutation::row_membership::PhysicalRowBuffer::new(
                context.mutation.rules.rules.memory.work_mem_bytes()?,
                cancel.clone(),
                "deleted-row membership",
            );
            for candidate_id in candidates.read_rows()? {
                let (table_index, doc_id) = candidate_id?;
                let storage_table = target_tables
                    .get(table_index)
                    .ok_or_else(|| {
                        SQLError::Internal("mutation candidate has an invalid table ordinal".into())
                    })?
                    .clone();
                cancel.check()?;
                let Some(candidate_document) = read_context
                    .mutation
                    .preparation
                    .referential
                    .constraints
                    .reads
                    .get_document(&storage_table, doc_id)?
                else {
                    continue;
                };
                let target_row = crate::mutation::rows::target_row_for_storage(
                    read_context
                        .mutation
                        .preparation
                        .referential
                        .assignment
                        .rows,
                    &stmt.table,
                    &storage_table,
                    &stmt.target_qualifier,
                    doc_id,
                    &candidate_document,
                )?;
                for source_context in DeleteSourceRows::new(using_rows.as_ref())? {
                    let source_context = source_context?;
                    if !preselected {
                        let joined = source_context
                            .as_ref()
                            .map(|source| crate::mutation::rows::join_rows(&target_row, source));
                        if !qualification.qualifies(joined.as_ref().unwrap_or(&target_row))? {
                            continue;
                        }
                    }
                    let target = crate::mutation::locking::lock_physical_mutation_target(
                        context.mutation.preparation.referential.locking.session,
                        &storage_table,
                        &stmt.target_qualifier,
                        doc_id,
                        uqa_sql::ast::LockStrength::ForUpdate,
                    )?;
                    let PhysicalMutationLockTarget::Present { identity, recheck } = target else {
                        continue;
                    };
                    // Another command of the statement already wrote the row, which `PostgreSQL`'s `ExecDelete` skips as `TM_SelfModified`.
                    if ctes.statement_wrote(&identity) {
                        continue;
                    }
                    // A statement that the command's triggers or functions started wrote the row under a later command id; a BEFORE ROW trigger fetches the row through `GetTupleForTrigger`, whose error names an update.
                    if ctes.statement_triggered_write(&identity) {
                        let operation = if crate::mutation::triggers::has_before_row_triggers(
                            &context.mutation.preparation.referential.triggers,
                            &identity.table,
                            uqa_sql::ast::TriggerEvent::Delete,
                        )? {
                            "updated"
                        } else {
                            "deleted"
                        };
                        return Err(crate::mutation::errors::triggered_modification_error(
                            operation,
                        ));
                    }
                    let storage_table = identity.table;
                    let doc_id = identity.doc_id;
                    let qualified = if recheck {
                        context
                            .mutation
                            .preparation
                            .referential
                            .constraints
                            .transactions
                            .refresh_explicit_statement_snapshot()?;
                        recheck_delete_candidate(DeleteCandidateRecheck {
                            reads: context.mutation.preparation.referential.constraints.reads,
                            rows: context.mutation.preparation.referential.assignment.rows,
                            expressions: read_context
                                .mutation
                                .preparation
                                .referential
                                .assignment
                                .expressions,
                            stmt,
                            storage_table: &storage_table,
                            params,
                            ctes: &snapshot_ctes,
                            doc_id,
                            source_context: source_context.as_ref(),
                        })?
                    } else {
                        Some((candidate_document.clone(), source_context))
                    };
                    let Some((doc, returning_context)) = qualified else {
                        continue;
                    };
                    if completed_ids.contains(&storage_table, doc_id)? {
                        continue;
                    }
                    if crate::mutation::triggers::fire_before_row_triggers(
                        &context.mutation.preparation.referential.triggers,
                        &storage_table,
                        uqa_sql::ast::TriggerEvent::Delete,
                        doc_id,
                        Some(&doc),
                        None,
                        &[],
                    )?
                    .is_some()
                    {
                        let removed = deleted.prepare(
                            context,
                            stmt,
                            params,
                            &snapshot_ctes,
                            MutationCandidate {
                                identity: PhysicalDocumentIdentity {
                                    table: storage_table.clone(),
                                    doc_id,
                                },
                                document: doc,
                                context: returning_context,
                            },
                            &|_, _| Ok(false),
                        )?;
                        if removed {
                            completed_ids.insert(&storage_table, doc_id)?;
                            affected += 1;
                        }
                    }
                }
            }
            let published = overlay.finish();
            if deleted.prepared.rows() != 0 {
                context.mutation.state.prepare_writer()?;
                let mut publication =
                    statement_end::publication_batch(&statement_commands).with_published(published);
                for row in deleted
                    .prepared
                    .drain_rows()
                    .map_err(crate::physical::physical_exec_error)?
                {
                    cancel.check()?;
                    let action = decode_prepared_mutation_action_row(
                        row.map_err(crate::physical::physical_exec_error)?,
                    )?;
                    crate::mutation::publication::publish_prepared_mutation_action(
                        context.mutation.publication,
                        action,
                        crate::mutation::publication::InsertedIdentity::Unknown,
                        &mut publication,
                    )?;
                }
                crate::mutation::publication::finish_mutation_publication(
                    context.mutation.publication,
                    &mut publication,
                )?;
                statement_end::note_written_rows(&statement_commands, &mut publication);
            }
            statement_end::end_command(
                &statement_commands,
                &context.mutation.preparation.referential.triggers,
                std::slice::from_ref(&delete_statement),
                deleted.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,
                    supplemental_schema: using_rows.as_ref().map(crate::SharedSpill::row_schema),
                };

                return crate::mutation::returning::dml_returning_result(
                    context.mutation.preparation.returning,
                    shape,
                    deleted.returning,
                    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,
    )
}