uqa-execution 0.3.5

Volcano physical operators with row-batch pipelines
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! SQL batch scheduling over live cache, transaction and statement inputs.

use super::{
    plan_executor::UnifiedPlanExecutor,
    transactions::{
        abort_explicit_statement_error, rollback_after_statement_error, rollback_implicit_statement,
    },
};
use crate::query::locking::query_has_row_locks;
use context::BatchExecutionContext;
use std::sync::Arc;
use uqa_sql::{
    plan::UnifiedPlan,
    semantics::effects::{
        is_transaction_control, query_may_mutate_engine, query_requires_statement_transaction,
        transaction_blocks::{no_active_transaction_error, transaction_requires_explicit_block},
    },
    SQLError, SQLParam, SQLResult,
};

pub mod context;

#[cfg(test)]
mod tests;

pub fn execute<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    sql: &str,
    params: &[SQLParam],
) -> Result<SQLResult, SQLError> {
    execute_with_context(context, sql, params, false, &mut None)
}

pub fn execute_nested<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    sql: &str,
    params: &[SQLParam],
) -> Result<SQLResult, SQLError> {
    execute_with_context(context, sql, params, true, &mut None)
}

type ResultConsumer<'a> = Option<&'a mut dyn FnMut(&SQLResult) -> Result<(), SQLError>>;

enum StatementInput<'sql> {
    Cached(Arc<uqa_sql::Statement>),
    Parsed(uqa_sql::ParsedStatement<'sql>),
}

impl StatementInput<'_> {
    fn compile(self) -> Result<uqa_sql::Statement, SQLError> {
        match self {
            Self::Cached(statement) => Ok(statement.as_ref().clone()),
            Self::Parsed(statement) => statement.compile(),
        }
    }
}

pub fn execute_simple_query<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    sql: &str,
    params: &[SQLParam],
    nested_statement: bool,
    consume: &mut dyn FnMut(&SQLResult) -> Result<(), SQLError>,
) -> Result<(), SQLError> {
    let mut consumer: ResultConsumer<'_> = Some(consume);
    let result = execute_with_context(context, sql, params, nested_statement, &mut consumer)?;
    consume_result(context, &result, &mut consumer)
}

fn consume_result<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    result: &SQLResult,
    consumer: &mut ResultConsumer<'_>,
) -> Result<(), SQLError> {
    if let Some(consume) = consumer {
        consume(result)
            .map_err(|error| abort_explicit_statement_error(context.transactions, error))?;
    }
    Ok(())
}

fn execute_with_context<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    sql: &str,
    params: &[SQLParam],
    nested_statement: bool,
    consumer: &mut ResultConsumer<'_>,
) -> Result<SQLResult, SQLError> {
    // Reject cancelled tokens up-front so a stale cancel signal does
    // not leak into a fresh batch. Callers that want the
    // cancellation flag preserved across statements should use
    // their session cancellation-reset API explicitly between calls.
    if let Err(error) = context.runtime.cancellation.check() {
        return Err(abort_explicit_statement_error(
            context.transactions,
            error.into(),
        ));
    }
    if !context.persistent_backend && context.transactions.transaction_depth() == 0 {
        if let Some(plan) = context.cache.cached_optimized_sql_plan(sql) {
            let can_execute_without_transaction = match plan.as_ref() {
                uqa_sql::plan::UnifiedPlan::Query(query) => !query_requires_statement_transaction(
                    &context.effects.query_effect_context(),
                    query,
                )?,
                uqa_sql::plan::UnifiedPlan::Command(_) => false,
            };
            if can_execute_without_transaction {
                return UnifiedPlanExecutor::with_nested_statement(
                    context.statements.statement_execution_context(),
                    params,
                    nested_statement,
                )
                .with_source_sql(sql)
                .execute(plan.as_ref());
            }
        }
    }
    execute_uncached_or_snapshot_scoped(context, sql, params, nested_statement, consumer)
}

#[inline(never)]
#[expect(
    clippy::too_many_lines,
    reason = "preserves statement transaction order"
)]
fn execute_uncached_or_snapshot_scoped<S: Clone + Send + Sync + 'static>(
    context: &BatchExecutionContext<'_, S>,
    sql: &str,
    params: &[SQLParam],
    nested_statement: bool,
    consumer: &mut ResultConsumer<'_>,
) -> Result<SQLResult, SQLError> {
    // Parse an uncached batch completely before executing its first statement.
    // This preserves syntax atomicity. Exact single-statement cache hits reuse
    // the parsed AST and logical plan; batches still lower each statement only
    // when its turn arrives so earlier DDL, SET, ANALYZE, and function commands
    // can affect the following statement's semantics.
    let cached_statement = context.cache.cached_sql_statement(sql);
    let (statements, mut cached_entry) = match cached_statement {
        Some(cached) => (
            vec![StatementInput::Cached(cached.statement.clone())],
            Some(cached),
        ),
        None => match uqa_sql::parse_statements(sql) {
            Ok(statements) => (
                statements.into_iter().map(StatementInput::Parsed).collect(),
                None,
            ),
            Err(error) => return Err(abort_explicit_statement_error(context.transactions, error)),
        },
    };
    if statements.is_empty() {
        return Ok(SQLResult::empty());
    }
    let is_single_statement = statements.len() == 1;
    let final_statement_index = statements.len() - 1;
    let simple_query_batch = !is_single_statement;
    let mut implicit_segment_open = false;
    let execution = (|| -> Result<SQLResult, SQLError> {
        let mut last = SQLResult::empty();
        for (statement_index, statement) in statements.into_iter().enumerate() {
            if let Err(error) = context.runtime.cancellation.check() {
                return Err(abort_explicit_statement_error(
                    context.transactions,
                    error.into(),
                ));
            }
            let statement = statement
                .compile()
                .map_err(|error| abort_explicit_statement_error(context.transactions, error))?;
            let transaction = match &statement {
                uqa_sql::ast::Statement::Transaction(transaction) => Some(transaction.clone()),
                _ => None,
            };
            if simple_query_batch
                && transaction
                    .as_ref()
                    .is_some_and(transaction_requires_explicit_block)
                && (implicit_segment_open || context.transactions.transaction_depth() == 0)
            {
                return Err(no_active_transaction_error(
                    transaction.as_ref().expect("checked transaction command"),
                ));
            }
            if simple_query_batch
                && implicit_segment_open
                && transaction.as_ref().is_some_and(|transaction| {
                    matches!(
                        transaction,
                        uqa_sql::ast::TransactionStmt::Begin
                            | uqa_sql::ast::TransactionStmt::BeginWithCharacteristics(_)
                    )
                })
            {
                // PostgreSQL promotes the simple-query message's implicit
                // transaction to an explicit block. The preceding statements
                // stay uncommitted; a later COMMIT or ROLLBACK controls them.
                context.transactions.promote_simple_query_transaction()?;
                if let Some(uqa_sql::ast::TransactionStmt::BeginWithCharacteristics(options)) =
                    transaction
                {
                    context.transactions.run_transaction_statement(
                        uqa_sql::ast::TransactionStmt::SetCharacteristics(options),
                    )?;
                }
                implicit_segment_open = false;
                last = SQLResult::empty();
                last.command_tag = Some("BEGIN".into());
                if statement_index != final_statement_index {
                    consume_result(context, &last, consumer)?;
                }
                continue;
            }
            if simple_query_batch
                && transaction.is_none()
                && context.transactions.transaction_depth() == 0
                && !implicit_segment_open
            {
                context.transactions.begin_simple_query_transaction()?;
                implicit_segment_open = true;
            }
            let (initial_plan, cached_optimized_plan) = if is_single_statement {
                if let Some(cached) = cached_entry.take() {
                    (cached.logical_plan, cached.optimized_plan)
                } else {
                    let plan = Arc::new(UnifiedPlan::lower_with(
                        statement.clone(),
                        context.aggregates,
                    ));
                    context.cache.cache_sql_statement(
                        sql.to_string(),
                        Arc::new(statement.clone()),
                        Arc::clone(&plan),
                    );
                    (plan, None)
                }
            } else {
                (
                    Arc::new(UnifiedPlan::lower_with(
                        statement.clone(),
                        context.aggregates,
                    )),
                    None,
                )
            };
            if is_transaction_control(initial_plan.as_ref()) {
                // SQL COMMIT/ROLLBACK outside a block warn and succeed; the direct Rust transaction API keeps reporting misuse as an error.
                if context.transactions.transaction_depth() == 0
                    && transaction.as_ref().is_some_and(|transaction| {
                        matches!(
                            transaction,
                            uqa_sql::ast::TransactionStmt::Commit
                                | uqa_sql::ast::TransactionStmt::Rollback
                        )
                    })
                {
                    context.runtime.notices.lock().push((
                        "WARNING".into(),
                        "there is no transaction in progress".into(),
                    ));
                    last = SQLResult::empty();
                    last.command_tag = Some(
                        uqa_sql::result::completion::transaction_completion(
                            transaction.as_ref().expect("checked transaction command"),
                            false,
                        )
                        .into(),
                    );
                    if statement_index != final_statement_index {
                        consume_result(context, &last, consumer)?;
                    }
                    continue;
                }
                if simple_query_batch
                    && implicit_segment_open
                    && transaction.as_ref().is_some_and(|transaction| {
                        matches!(
                            transaction,
                            uqa_sql::ast::TransactionStmt::Commit
                                | uqa_sql::ast::TransactionStmt::Rollback
                        )
                    })
                {
                    context.runtime.notices.lock().push((
                        "WARNING".into(),
                        "there is no transaction in progress".into(),
                    ));
                }
                last = UnifiedPlanExecutor::with_nested_statement(
                    context.statements.statement_execution_context(),
                    params,
                    nested_statement || simple_query_batch,
                )
                .with_source_sql(sql)
                .execute(initial_plan.as_ref())?;
                if simple_query_batch
                    && transaction.as_ref().is_some_and(|transaction| {
                        matches!(
                            transaction,
                            uqa_sql::ast::TransactionStmt::Commit
                                | uqa_sql::ast::TransactionStmt::Rollback
                        )
                    })
                {
                    implicit_segment_open = false;
                }
                if statement_index != final_statement_index {
                    consume_result(context, &last, consumer)?;
                }
                continue;
            }
            let (has_row_locks, needs_row_lock_statement) = match initial_plan.as_ref() {
                uqa_sql::plan::UnifiedPlan::Query(query) => {
                    let has_row_locks = query_has_row_locks(query);
                    (has_row_locks, has_row_locks)
                }
                uqa_sql::plan::UnifiedPlan::Command(_) => (false, true),
            };
            let _row_lock_statement =
                needs_row_lock_statement.then(|| context.row_locks.begin_row_lock_statement());

            if context.transactions.transaction_depth() != 0 {
                context.transactions.ensure_transaction_usable()?;
                if has_row_locks {
                    context
                        .row_locks
                        .statement_row_lock_cache()
                        .map_err(|error| context.transactions.abort_after_error(error))?;
                }
                context
                    .transactions
                    .prepare_explicit_statement_snapshot(
                        uqa_sql::semantics::effects::read_only::plan_sets_transaction_snapshot(
                            initial_plan.as_ref(),
                        ),
                    )
                    .map_err(|error| context.transactions.abort_after_error(error))?;
                // The statement was lowered immediately above, after any earlier
                // BEGIN or catalog-changing statement in this batch completed.
                let mut plan = UnifiedPlan::lower_with(statement.clone(), context.aggregates);
                if is_single_statement {
                    context.cache.cache_sql_statement(
                        sql.to_string(),
                        Arc::new(statement.clone()),
                        Arc::new(plan.clone()),
                    );
                }
                let mutating_query = match &plan {
                    uqa_sql::plan::UnifiedPlan::Query(query) => {
                        query_may_mutate_engine(&context.effects.query_effect_context(), query)
                    }
                    uqa_sql::plan::UnifiedPlan::Command(_) => Ok(false),
                }
                .map_err(|error| context.transactions.abort_after_error(error))?;
                if mutating_query
                    && !has_row_locks
                    && context
                        .transactions
                        .prepare_explicit_transaction_writer()
                        .map_err(|error| context.transactions.abort_after_error(error))?
                {
                    plan = UnifiedPlan::lower_with(statement.clone(), context.aggregates);
                    if is_single_statement {
                        context.cache.cache_sql_statement(
                            sql.to_string(),
                            Arc::new(statement.clone()),
                            Arc::new(plan.clone()),
                        );
                    }
                }
                let optimized = match context.planning.plan_for_execution(plan, params) {
                    Ok(plan) => plan,
                    Err(error) => {
                        return Err(context.transactions.abort_after_error(error));
                    }
                };
                let mut executor = UnifiedPlanExecutor::with_nested_statement(
                    context.statements.statement_execution_context(),
                    params,
                    nested_statement || simple_query_batch,
                )
                .with_source_sql(sql);
                match executor.execute(&optimized) {
                    Ok(result) => last = result,
                    Err(error) => return Err(context.transactions.abort_after_error(error)),
                }
                if statement_index != final_statement_index {
                    consume_result(context, &last, consumer)?;
                }
                continue;
            }

            // Every persistent SQL statement owns one storage transaction when
            // the caller has not opened an explicit transaction. This is the
            // actual autocommit boundary: catalog, document, FTS, btree, vector,
            // graph, and registry writes either commit together or are all rolled
            // back. Memory commands use the same boundary so a fallible multi-row
            // mutation restores its pre-statement snapshot; read-only memory
            // queries avoid copying the whole database.
            let (is_read_query, requires_statement_transaction) = match initial_plan.as_ref() {
                uqa_sql::plan::UnifiedPlan::Query(query) => {
                    let mutates =
                        query_may_mutate_engine(&context.effects.query_effect_context(), query)?;
                    (
                        !mutates,
                        mutates
                            || query_requires_statement_transaction(
                                &context.effects.query_effect_context(),
                                query,
                            )?,
                    )
                }
                uqa_sql::plan::UnifiedPlan::Command(_) => (false, true),
            };
            let runs_outside_transaction = matches!(
                initial_plan.as_ref(),
                uqa_sql::plan::UnifiedPlan::Command(command)
                    if matches!(
                        command.as_ref(),
                        uqa_sql::plan::CommandPlan::Discard { .. }
                            | uqa_sql::plan::CommandPlan::Vacuum(_)
                    )
            );
            let needs_implicit_transaction = !runs_outside_transaction
                && (context.persistent_backend || requires_statement_transaction || has_row_locks);
            if needs_implicit_transaction {
                if has_row_locks {
                    context.row_locks.statement_row_lock_cache()?;
                }
                context
                    .transactions
                    .begin_implicit_statement_transaction(is_read_query)?;
                // Catalog/table refresh intentionally invalidates cached logical
                // plans even when the in-process generation did not move: a
                // sibling SQLite writer can release its lock immediately before
                // publishing that generation. Re-lower the parsed statement while
                // the database snapshot is pinned, then optimize that exact plan.
                let mut plan = UnifiedPlan::lower_with(statement.clone(), context.aggregates);
                if is_single_statement {
                    context.cache.cache_sql_statement(
                        sql.to_string(),
                        Arc::new(statement.clone()),
                        Arc::new(plan.clone()),
                    );
                }
                let must_restart_as_writer =
                    if is_read_query && context.persistent_backend && !has_row_locks {
                        match &plan {
                            uqa_sql::plan::UnifiedPlan::Query(query) => {
                                match query_may_mutate_engine(
                                    &context.effects.query_effect_context(),
                                    query,
                                ) {
                                    Ok(mutates) => mutates,
                                    Err(error) => {
                                        return rollback_after_statement_error(
                                            context.transactions,
                                            error,
                                        )
                                    }
                                }
                            }
                            uqa_sql::plan::UnifiedPlan::Command(_) => false,
                        }
                    } else {
                        false
                    };
                if must_restart_as_writer {
                    rollback_implicit_statement(
                        context.transactions,
                        "restart read transaction as writer",
                    )?;
                    context
                        .transactions
                        .begin_implicit_statement_transaction(false)?;
                    plan = UnifiedPlan::lower_with(statement.clone(), context.aggregates);
                    if is_single_statement {
                        context.cache.cache_sql_statement(
                            sql.to_string(),
                            Arc::new(statement.clone()),
                            Arc::new(plan.clone()),
                        );
                    }
                }
                let mutating_query = match &plan {
                    uqa_sql::plan::UnifiedPlan::Query(query) => {
                        query_may_mutate_engine(&context.effects.query_effect_context(), query)
                    }
                    uqa_sql::plan::UnifiedPlan::Command(_) => Ok(false),
                };
                let mutating_query = match mutating_query {
                    Ok(mutates) => mutates,
                    Err(error) => {
                        return rollback_after_statement_error(context.transactions, error)
                    }
                };
                if mutating_query && context.persistent_backend && !has_row_locks {
                    match context.transactions.prepare_explicit_transaction_writer() {
                        Ok(true) => {
                            plan = UnifiedPlan::lower_with(statement.clone(), context.aggregates);
                            if is_single_statement {
                                context.cache.cache_sql_statement(
                                    sql.to_string(),
                                    Arc::new(statement.clone()),
                                    Arc::new(plan.clone()),
                                );
                            }
                        }
                        Ok(false) => {}
                        Err(error) => {
                            return rollback_after_statement_error(context.transactions, error)
                        }
                    }
                }
                let optimized = match context.planning.plan_for_execution(plan, params) {
                    Ok(plan) => plan,
                    Err(error) => {
                        return rollback_after_statement_error(context.transactions, error)
                    }
                };
                let mut executor = UnifiedPlanExecutor::with_nested_statement(
                    context.statements.statement_execution_context(),
                    params,
                    nested_statement || simple_query_batch,
                )
                .with_source_sql(sql);
                match executor.execute(&optimized) {
                    Ok(result) => {
                        // Commit failure cleanup is owned by the transaction
                        // layer, including cache restoration and stack reset.
                        context
                            .transactions
                            .run_transaction_statement(uqa_sql::ast::TransactionStmt::Commit)?;
                        last = result;
                    }
                    Err(statement_error) => {
                        return rollback_after_statement_error(
                            context.transactions,
                            statement_error,
                        )
                    }
                }
            } else {
                // In-memory read-only queries run without a transaction snapshot.
                // Their cache generation is invalidated by every table, catalog,
                // search-path, and function-registry change, so both parsing and
                // physical optimization are reusable until that generation moves.
                let optimized = if let Some(plan) = cached_optimized_plan {
                    plan
                } else {
                    let plan = Arc::new(
                        context
                            .planning
                            .plan_for_execution(initial_plan.as_ref().clone(), params)?,
                    );
                    if is_single_statement {
                        context
                            .cache
                            .cache_optimized_sql_plan(sql, Arc::clone(&plan));
                    }
                    plan
                };
                last = UnifiedPlanExecutor::with_nested_statement(
                    context.statements.statement_execution_context(),
                    params,
                    nested_statement || simple_query_batch,
                )
                .with_source_sql(sql)
                .execute(optimized.as_ref())?;
            }
            if statement_index != final_statement_index {
                consume_result(context, &last, consumer)?;
            }
        }
        Ok(last)
    })();
    if !simple_query_batch {
        return execution;
    }
    match execution {
        Ok(result) if implicit_segment_open => {
            context
                .transactions
                .run_transaction_statement(uqa_sql::ast::TransactionStmt::Commit)?;
            Ok(result)
        }
        Ok(result) => Ok(result),
        Err(error) if implicit_segment_open && context.transactions.transaction_depth() != 0 => {
            rollback_after_statement_error(context.transactions, error)
        }
        Err(error) => Err(error),
    }
}