radixdb-executor 1.1.0

SQL binding, planning, and execution engine for RadixDB
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
//! Statement classification, fencing, routing, and statement atomicity.

use std::sync::atomic::{AtomicU64, Ordering};

use radixdb_core::{Error, Result};
use radixdb_sql::ast::*;
use radixdb_storage::traits::QueryResult;

use crate::context::ExecutionContext;
use crate::mutation::copy::CopyExecutorExt;
use crate::mutation::ddl::DdlExecutorExt;
use crate::mutation::dml::DmlExecutorExt;
use crate::mutation::extension::ExtensionDdlExecutorExt;
use crate::mutation::external_type::ExternalTypeDdlExecutorExt;
use crate::mutation::host::MutationHost;
use crate::mutation::operator::OperatorDdlExecutorExt;

use super::transaction::TransactionControlExt;

static NEXT_STATEMENT_SAVEPOINT_ID: AtomicU64 = AtomicU64::new(1);

/// Internal dispatch seam between statement coordination and concrete owners.
pub trait StatementDispatchHost: MutationHost {
    type NavigationPlan;

    fn dispatch_ddl_fence_already_held(&self) -> bool;

    fn dispatch_authorize_statement(
        &self,
        statement: &Statement,
        context: &ExecutionContext,
    ) -> Result<()>;

    fn dispatch_bind_navigation(
        &self,
        statement: &Statement,
    ) -> Result<Option<Self::NavigationPlan>>;

    fn dispatch_select(
        &self,
        statement: &SelectStatement,
        navigation: Option<&Self::NavigationPlan>,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_call(
        &self,
        statement: &CallStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;

    fn dispatch_set(
        &self,
        statement: &SetStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_show_tables(
        &self,
        statement: &ShowTablesStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_show_views(
        &self,
        statement: &ShowViewsStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_show_create_table(
        &self,
        statement: &ShowCreateTableStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_show_create_view(
        &self,
        statement: &ShowCreateViewStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_show_indexes(
        &self,
        statement: &ShowIndexesStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_describe(
        &self,
        statement: &DescribeStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_pragma(
        &self,
        statement: &PragmaStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_expression(
        &self,
        statement: &ExpressionStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_explain(
        &self,
        statement: &ExplainStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_analyze(
        &self,
        statement: &AnalyzeStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
    fn dispatch_vacuum(
        &self,
        statement: &VacuumStatement,
        context: &ExecutionContext,
    ) -> Result<Box<dyn QueryResult>>;
}

pub fn execute_statement<H: StatementDispatchHost + ?Sized>(
    host: &H,
    statement: &Statement,
    context: &ExecutionContext,
    navigation_checked: bool,
    mut navigation: Option<H::NavigationPlan>,
) -> Result<Box<dyn QueryResult>> {
    if matches!(statement, Statement::Expression(_)) {
        return Err(Error::parse(format!(
            "invalid SQL: unrecognised statement: {statement}"
        )));
    }

    let explicit_transaction = host.mutation_has_active_transaction();
    let transaction_end = matches!(statement, Statement::Commit(_) | Statement::Rollback(_));
    let engine_owns_fence = matches!(
        statement,
        Statement::Pragma(value)
            if value.name.value.eq_ignore_ascii_case("SNAPSHOT")
                || value.name.value.eq_ignore_ascii_case("RESTORE")
                || value.name.value.eq_ignore_ascii_case("CHECKPOINT")
    );
    let transaction_owns_fence = matches!(statement, Statement::CreateTable(_))
        || (!explicit_transaction
            && matches!(
                statement,
                Statement::CreateExtension(_)
                    | Statement::DropExtension(_)
                    | Statement::CreateExternalType(_)
                    | Statement::DropExternalType(_)
                    | Statement::CreateOperator(_)
                    | Statement::DropOperator(_)
                    | Statement::CreateOperatorClass(_)
                    | Statement::DropOperatorClass(_)
                    | Statement::CreatePlannerSupport(_)
                    | Statement::DropPlannerSupport(_)
                    | Statement::CreateRoutine(_)
                    | Statement::CreateTrigger(_)
                    | Statement::CreateJob(_)
                    | Statement::DropRoutine(_)
                    | Statement::DropTrigger(_)
                    | Statement::DropJob(_)
                    | Statement::AlterJob(_)
                    | Statement::CreateSchema(_)
                    | Statement::CreatePrincipal(_)
                    | Statement::CreateRole(_)
                    | Statement::AlterSecuritySubject(_)
                    | Statement::DropSecuritySubject(_)
                    | Statement::Grant(_)
                    | Statement::Revoke(_)
                    | Statement::AlterOwner(_)
                    | Statement::CreateIndex(_)
                    | Statement::DropTable(_)
                    | Statement::DropIndex(_)
                    | Statement::AlterIndex(_)
                    | Statement::CreateView(_)
                    | Statement::DropView(_)
            ))
        || matches!(statement, Statement::AlterTable(_));
    let coordinates_nested_statements = matches!(statement, Statement::Analyze(_));
    // Physical DDL takes `ddl_fence` internally, so auto-commit catalog
    // writers use an independent admission fence across the complete pin ->
    // stage -> commit interval. Otherwise two writers can pin the same
    // generation and the loser reports a stale catalog mutation.
    let _catalog_write_fence = (is_catalog_mutation(statement) && !explicit_transaction)
        .then(|| host.mutation_engine().acquire_catalog_write_fence());
    let _catalog_fence = (!host.dispatch_ddl_fence_already_held()
        && !transaction_end
        && !coordinates_nested_statements
        && !engine_owns_fence
        && !transaction_owns_fence)
        .then(|| {
            let exclusive = (is_ddl(statement) && !explicit_transaction)
                || matches!(statement, Statement::Truncate(_));
            host.mutation_engine()
                .acquire_ddl_statement_fence(exclusive)
        });

    host.dispatch_authorize_statement(statement, context)?;

    if !navigation_checked {
        navigation = host.dispatch_bind_navigation(statement)?;
    }

    let _statement_scope = context.enter_statement_scope();
    let _visibility_fence = matches!(statement, Statement::Select(_))
        .then(|| host.mutation_engine().acquire_statement_visibility_fence())
        .flatten();
    let context = host.mutation_active_transaction_id().map_or_else(
        || context.clone(),
        |id| context.with_transaction_id(id as u64),
    );

    let statement_savepoint = create_statement_savepoint(host, statement, explicit_transaction)?;
    let result = route_statement(host, statement, &context, navigation.as_ref());
    finalize_statement_savepoint(host, statement_savepoint, result)
}

fn is_catalog_mutation(statement: &Statement) -> bool {
    is_ddl(statement) && !matches!(statement, Statement::Truncate(_))
}

fn is_ddl(statement: &Statement) -> bool {
    matches!(
        statement,
        Statement::CreateTable(_)
            | Statement::CreateExtension(_)
            | Statement::DropExtension(_)
            | Statement::CreateExternalType(_)
            | Statement::DropExternalType(_)
            | Statement::CreateOperator(_)
            | Statement::DropOperator(_)
            | Statement::CreateOperatorClass(_)
            | Statement::DropOperatorClass(_)
            | Statement::CreateRoutine(_)
            | Statement::CreateTrigger(_)
            | Statement::CreateJob(_)
            | Statement::DropRoutine(_)
            | Statement::DropTrigger(_)
            | Statement::DropJob(_)
            | Statement::AlterJob(_)
            | Statement::CreateSchema(_)
            | Statement::CreatePrincipal(_)
            | Statement::CreateRole(_)
            | Statement::AlterSecuritySubject(_)
            | Statement::DropSecuritySubject(_)
            | Statement::Grant(_)
            | Statement::Revoke(_)
            | Statement::AlterOwner(_)
            | Statement::DropTable(_)
            | Statement::CreateIndex(_)
            | Statement::DropIndex(_)
            | Statement::AlterTable(_)
            | Statement::AlterIndex(_)
            | Statement::CreateView(_)
            | Statement::DropView(_)
            | Statement::Truncate(_)
    )
}

fn route_statement<H: StatementDispatchHost + ?Sized>(
    host: &H,
    statement: &Statement,
    context: &ExecutionContext,
    navigation: Option<&H::NavigationPlan>,
) -> Result<Box<dyn QueryResult>> {
    match statement {
        Statement::CreateExtension(value) => host.execute_create_extension(value, context),
        Statement::DropExtension(value) => host.execute_drop_extension(value, context),
        Statement::CreateExternalType(value) => host.execute_create_external_type(value, context),
        Statement::DropExternalType(value) => host.execute_drop_external_type(value, context),
        Statement::CreateOperator(value) => host.execute_create_operator(value, context),
        Statement::DropOperator(value) => host.execute_drop_operator(value, context),
        Statement::CreateOperatorClass(value) => host.execute_create_operator_class(value, context),
        Statement::DropOperatorClass(value) => host.execute_drop_operator_class(value, context),
        Statement::CreatePlannerSupport(value) => {
            host.execute_create_planner_support(value, context)
        }
        Statement::DropPlannerSupport(value) => host.execute_drop_planner_support(value, context),
        Statement::CreateTable(value) => host.execute_create_table(value, context),
        Statement::CreateRoutine(value) => host.execute_create_routine(value, context),
        Statement::CreateTrigger(value) => host.execute_create_trigger(value, context),
        Statement::CreateJob(value) => host.execute_create_job(value, context),
        Statement::DropRoutine(value) => host.execute_drop_routine(value, context),
        Statement::DropTrigger(value) => host.execute_drop_trigger(value, context),
        Statement::DropJob(value) => host.execute_drop_job(value, context),
        Statement::AlterJob(value) => host.execute_alter_job(value, context),
        Statement::CreateSchema(value) => host.execute_create_schema(value, context),
        Statement::CreatePrincipal(value) => host.execute_create_principal(value, context),
        Statement::CreateRole(value) => host.execute_create_role(value, context),
        Statement::AlterSecuritySubject(value) => {
            host.execute_alter_security_subject(value, context)
        }
        Statement::DropSecuritySubject(value) => host.execute_drop_security_subject(value, context),
        Statement::Grant(value) => host.execute_grant(value, context),
        Statement::Revoke(value) => host.execute_revoke(value, context),
        Statement::AlterOwner(value) => host.execute_alter_owner(value, context),
        Statement::DropTable(value) => host.execute_drop_table(value, context),
        Statement::CreateIndex(value) => host.execute_create_index(value, context),
        Statement::DropIndex(value) => host.execute_drop_index(value, context),
        Statement::AlterTable(value) => host.execute_alter_table(value, context),
        Statement::AlterIndex(value) => host.execute_alter_index(value, context),
        Statement::CreateView(value) => host.execute_create_view(value, context),
        Statement::DropView(value) => host.execute_drop_view(value, context),
        Statement::Insert(value) => host.execute_insert(value, context),
        Statement::Update(value) => host.execute_update(value, context),
        Statement::Delete(value) => host.execute_delete(value, context),
        Statement::Truncate(value) => host.execute_truncate(value, context),
        Statement::Select(value) => host.dispatch_select(value, navigation, context),
        Statement::Call(value) => host.dispatch_call(value, context),
        Statement::Begin(value) => host.execute_begin(value, context),
        Statement::Commit(value) => host.execute_commit_stmt(value, context),
        Statement::Rollback(value) => host.execute_rollback_stmt(value, context),
        Statement::Savepoint(value) => host.execute_savepoint(value, context),
        Statement::ReleaseSavepoint(value) => host.execute_release_savepoint(value, context),
        Statement::Set(value) => host.dispatch_set(value, context),
        Statement::ShowTables(value) => host.dispatch_show_tables(value, context),
        Statement::ShowViews(value) => host.dispatch_show_views(value, context),
        Statement::ShowCreateTable(value) => host.dispatch_show_create_table(value, context),
        Statement::ShowCreateView(value) => host.dispatch_show_create_view(value, context),
        Statement::ShowIndexes(value) => host.dispatch_show_indexes(value, context),
        Statement::Describe(value) => host.dispatch_describe(value, context),
        Statement::Pragma(value) => host.dispatch_pragma(value, context),
        Statement::Expression(value) => host.dispatch_expression(value, context),
        Statement::Explain(value) => host.dispatch_explain(value, context),
        Statement::Analyze(value) => host.dispatch_analyze(value, context),
        Statement::Vacuum(value) => host.dispatch_vacuum(value, context),
        Statement::Copy(value) => host.execute_copy(value, context),
    }
}

fn create_statement_savepoint<H: StatementDispatchHost + ?Sized>(
    host: &H,
    statement: &Statement,
    explicit_transaction: bool,
) -> Result<Option<String>> {
    if !explicit_transaction
        || !matches!(
            statement,
            Statement::Insert(_)
                | Statement::Update(_)
                | Statement::Delete(_)
                | Statement::AlterTable(_)
        )
    {
        return Ok(None);
    }

    let id = NEXT_STATEMENT_SAVEPOINT_ID.fetch_add(1, Ordering::Relaxed);
    let name = format!("\0radixdb-statement-{id}");
    let mut active = host.mutation_active_transaction().lock().unwrap();
    let state = active.as_mut().ok_or_else(|| {
        Error::internal("explicit transaction disappeared before statement execution")
    })?;
    state.create_savepoint(&name)?;
    Ok(Some(name))
}

fn finalize_statement_savepoint<H: StatementDispatchHost + ?Sized>(
    host: &H,
    savepoint: Option<String>,
    result: Result<Box<dyn QueryResult>>,
) -> Result<Box<dyn QueryResult>> {
    let Some(name) = savepoint else {
        return result;
    };
    let mut active = host.mutation_active_transaction().lock().unwrap();
    let state = active.as_mut().ok_or_else(|| {
        Error::internal("explicit transaction disappeared during statement execution")
    })?;

    match result {
        Ok(value) => match state.release_savepoint(&name) {
            Ok(()) => Ok(value),
            Err(release_error) => {
                let rollback_error = state.rollback_to_savepoint(&name).err();
                Err(Error::internal(format!(
                    "statement completed but its atomic savepoint could not be released: {release_error}{}",
                    rollback_error.map_or_else(String::new, |error| format!(
                        "; rollback also failed: {error}"
                    ))
                )))
            }
        },
        Err(statement_error) => {
            if let Err(rollback_error) = state.rollback_to_savepoint(&name) {
                return Err(Error::internal(format!(
                    "statement failed: {statement_error}; atomic rollback failed: {rollback_error}"
                )));
            }
            if let Err(release_error) = state.release_savepoint(&name) {
                return Err(Error::internal(format!(
                    "statement failed: {statement_error}; rolled back but could not release its savepoint: {release_error}"
                )));
            }
            Err(statement_error)
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Barrier};

    use radixdb_storage::mvcc::MVCCEngine;
    use radixdb_storage::traits::Engine;

    use crate::Executor;

    #[test]
    fn concurrent_autocommit_catalog_writers_serialize_pin_to_commit() {
        let engine = Arc::new(MVCCEngine::in_memory());
        engine.open_engine().unwrap();
        let barrier = Arc::new(Barrier::new(3));
        let mut writers = Vec::new();

        for writer in 0..2 {
            let engine = Arc::clone(&engine);
            let barrier = Arc::clone(&barrier);
            writers.push(std::thread::spawn(move || {
                let executor = Executor::new(engine);
                barrier.wait();
                for table in 0..64 {
                    executor
                        .execute(&format!(
                            "CREATE TABLE concurrent_catalog_{writer}_{table} (id INTEGER PRIMARY KEY)"
                        ))
                        .unwrap();
                }
            }));
        }

        barrier.wait();
        for writer in writers {
            writer.join().unwrap();
        }

        for writer in 0..2 {
            for table in 0..64 {
                assert!(engine
                    .table_exists(&format!("concurrent_catalog_{writer}_{table}"))
                    .unwrap());
            }
        }
    }
}