quill-sql 0.3.0

An educational Rust relational database (RDBMS) inspired by CMU 15445
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
use log::debug;
use serde::Serialize;
use sqlparser::ast::TransactionAccessMode;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use tempfile::TempDir;

use crate::catalog::{load_catalog_data, Catalog, TableStatistics};
use crate::error::{QuillSQLError, QuillSQLResult};
use crate::execution::physical_plan::PhysicalPlan;
use crate::execution::ExecutionEngine;
use crate::optimizer::LogicalOptimizer;
use crate::plan::logical_plan::{LogicalPlan, TransactionScope};
use crate::plan::{LogicalPlanner, PhysicalPlanner, PlannerContext};
use crate::session::SessionContext;
use crate::storage::engine::TableHandle;
use crate::storage::holt::{HoltStore, HoltTableHandle};
use crate::storage::tuple::Tuple;
use crate::storage::HoltStorage;
use crate::transaction::{
    CommandId, IsolationLevel, LockDebugSnapshot, TransactionManager, TransactionStatus,
    TxnDebugSnapshot,
};
use crate::utils::table_ref::TableReference;
use crate::utils::util::{pretty_format_logical_plan, pretty_format_physical_plan};

#[derive(Debug, Clone, Default)]
pub struct DatabaseOptions {
    pub holt: HoltOptions,
    pub default_isolation_level: Option<IsolationLevel>,
}

#[derive(Debug, Default, Clone)]
pub struct HoltOptions {
    pub directory: Option<PathBuf>,
}

#[derive(Clone)]
enum DatabaseLocation {
    OnDisk(String),
    Temporary,
}

pub struct Database {
    pub(crate) catalog: Catalog,
    pub(crate) transaction_manager: Arc<TransactionManager>,
    pub(crate) holt_store: Arc<HoltStore>,
    default_isolation: IsolationLevel,
    storage: Arc<HoltStorage>,
    debug_trace: Arc<Mutex<Option<DebugTrace>>>,
    // Must drop after every Holt owner so temporary databases can checkpoint
    // before their directory is removed.
    _temp_dir: Option<TempDir>,
}

struct PreparedStatement {
    optimized_logical_plan: LogicalPlan,
    physical_plan: PhysicalPlan,
}

#[derive(Debug, Clone, Serialize)]
pub struct DebugTrace {
    pub logical_plan: String,
    pub physical_plan: String,
    pub rows: usize,
    pub duration_ms: u128,
    pub logical_tree: DebugPlanNode,
    pub physical_tree: DebugPlanNode,
}

#[derive(Debug, Clone, Serialize)]
pub struct DebugPlanNode {
    pub op: String,
    pub children: Vec<DebugPlanNode>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DebugPlanSnapshot {
    pub logical: DebugPlanNode,
    pub physical: DebugPlanNode,
}

#[derive(Debug, Clone, Serialize)]
pub struct MvccVersionSample {
    pub table: String,
    pub rid: String,
    pub insert_txn: u64,
    pub delete_txn: u64,
    pub visible: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct MvccVersionsDebug {
    pub samples: Vec<MvccVersionSample>,
    pub note: String,
}

impl DebugPlanNode {
    pub fn from_physical(plan: &PhysicalPlan) -> Self {
        Self {
            op: plan.display_name(),
            children: plan
                .inputs()
                .iter()
                .map(|child| Self::from_physical(child))
                .collect(),
        }
    }

    pub fn from_logical(plan: &LogicalPlan) -> Self {
        Self {
            op: plan.to_string(),
            children: plan
                .inputs()
                .iter()
                .map(|child| Self::from_logical(child))
                .collect(),
        }
    }
}

impl Database {
    pub fn new_on_disk(db_path: &str) -> QuillSQLResult<Self> {
        Self::new_on_disk_with_options(db_path, DatabaseOptions::default())
    }

    pub fn new_on_disk_with_options(
        db_path: &str,
        options: DatabaseOptions,
    ) -> QuillSQLResult<Self> {
        Self::new_with_location(DatabaseLocation::OnDisk(db_path.to_string()), options)
    }

    pub fn new_temp() -> QuillSQLResult<Self> {
        Self::new_temp_with_options(DatabaseOptions::default())
    }

    pub fn new_temp_with_options(options: DatabaseOptions) -> QuillSQLResult<Self> {
        Self::new_with_location(DatabaseLocation::Temporary, options)
    }

    fn new_with_location(
        location: DatabaseLocation,
        options: DatabaseOptions,
    ) -> QuillSQLResult<Self> {
        let (holt_dir, temp_dir) = holt_directory_for_location(&location, &options.holt)?;
        let holt_store = Arc::new(HoltStore::open(holt_dir)?);
        let transaction_manager = Arc::new(TransactionManager::new());
        seed_holt_transaction_statuses(&holt_store, &transaction_manager)?;

        let catalog = Catalog::new(holt_store.clone());
        let storage = Arc::new(HoltStorage::new(holt_store.clone()));

        let mut db = Self {
            catalog,
            transaction_manager,
            holt_store,
            default_isolation: options
                .default_isolation_level
                .unwrap_or(IsolationLevel::ReadUncommitted),
            storage,
            debug_trace: Arc::new(Mutex::new(None)),
            _temp_dir: temp_dir,
        };
        load_catalog_data(&mut db)?;
        Ok(db)
    }

    pub fn run(&mut self, sql: &str) -> QuillSQLResult<Vec<Tuple>> {
        let mut session = SessionContext::new(self.default_isolation);
        self.run_with_session(&mut session, sql)
    }

    pub fn run_with_session(
        &mut self,
        session: &mut SessionContext,
        sql: &str,
    ) -> QuillSQLResult<Vec<Tuple>> {
        let start = Instant::now();
        let PreparedStatement {
            optimized_logical_plan,
            physical_plan,
        } = self.plan_statement(sql)?;
        let logical_plan_str = pretty_format_logical_plan(&optimized_logical_plan);
        let physical_plan_str = pretty_format_physical_plan(&physical_plan);
        let logical_tree = DebugPlanNode::from_logical(&optimized_logical_plan);
        let physical_tree = DebugPlanNode::from_physical(&physical_plan);

        if let Some(result) = self.execute_transaction_control(session, &optimized_logical_plan)? {
            return Ok(result);
        }

        let result = self.execute_physical_plan(session, physical_plan)?;

        if let Ok(mut guard) = self.debug_trace.lock() {
            *guard = Some(DebugTrace {
                logical_plan: logical_plan_str,
                physical_plan: physical_plan_str,
                logical_tree,
                physical_tree,
                rows: result.len(),
                duration_ms: start.elapsed().as_millis(),
            });
        }

        Ok(result)
    }

    pub fn default_isolation(&self) -> IsolationLevel {
        self.default_isolation
    }

    pub fn create_logical_plan(&mut self, sql: &str) -> QuillSQLResult<LogicalPlan> {
        let stmts = crate::sql::parser::parse_sql(sql)?;
        if stmts.len() != 1 {
            return Err(QuillSQLError::NotSupport(
                "only support one sql statement".to_string(),
            ));
        }
        let stmt = &stmts[0];
        let mut planner = LogicalPlanner {
            context: PlannerContext {
                catalog: &self.catalog,
            },
        };
        planner.plan(stmt)
    }

    pub fn analyze_table(&mut self, table_ref: &TableReference) -> QuillSQLResult<TableStatistics> {
        self.catalog.analyze_table(table_ref)
    }

    pub fn flush(&self) -> QuillSQLResult<()> {
        self.holt_store
            .db()
            .checkpoint()
            .map_err(crate::storage::holt::map_holt_err)
    }

    #[cfg(test)]
    pub(crate) fn table_binding(
        &self,
        table_ref: &TableReference,
    ) -> QuillSQLResult<crate::storage::TableBinding> {
        self.storage.table(&self.catalog, table_ref)
    }

    pub fn debug_last_trace(&self) -> Option<DebugTrace> {
        self.debug_trace.lock().ok().and_then(|guard| guard.clone())
    }

    pub fn debug_lock_snapshot(&self) -> LockDebugSnapshot {
        self.transaction_manager.lock_manager_arc().debug_snapshot()
    }

    pub fn debug_txn_snapshot(&self) -> TxnDebugSnapshot {
        self.transaction_manager.debug_snapshot()
    }

    pub fn debug_mvcc_versions(&self) -> QuillSQLResult<MvccVersionsDebug> {
        let snapshot = self
            .transaction_manager
            .snapshot(self.transaction_manager.next_txn_id_hint());
        let mut samples = Vec::new();
        let max_samples = 20usize;
        for (schema_name, schema) in &self.catalog.schemas {
            if schema_name == crate::catalog::INFORMATION_SCHEMA_NAME {
                continue;
            }
            for (table_name, table) in &schema.tables {
                let table_ref = TableReference::Full {
                    catalog: crate::catalog::DEFAULT_CATALOG_NAME.to_string(),
                    schema: schema_name.clone(),
                    table: table_name.clone(),
                };
                let handle = HoltTableHandle::new(
                    table_ref.clone(),
                    table.schema.clone(),
                    table.table_id,
                    self.holt_store.clone(),
                );
                let mut stream = handle.full_scan()?;
                while let Some((rid, meta, _tuple)) = stream.next()? {
                    if samples.len() >= max_samples {
                        break;
                    }
                    let visible = snapshot.is_visible(&meta, 0 as CommandId, |tid| {
                        self.transaction_manager.transaction_status(tid)
                    });
                    samples.push(MvccVersionSample {
                        table: table_ref.to_string(),
                        rid: rid.to_string(),
                        insert_txn: meta.insert_txn_id,
                        delete_txn: meta.delete_txn_id,
                        visible,
                    });
                }
                if samples.len() >= max_samples {
                    break;
                }
            }
            if samples.len() >= max_samples {
                break;
            }
        }

        Ok(MvccVersionsDebug {
            samples,
            note: format!("sampled up to {} tuples from Holt tables", max_samples),
        })
    }

    pub fn debug_last_plan(&self) -> Option<DebugPlanSnapshot> {
        self.debug_trace
            .lock()
            .ok()
            .and_then(|opt| opt.clone())
            .map(|trace| DebugPlanSnapshot {
                logical: trace.logical_tree,
                physical: trace.physical_tree,
            })
    }

    pub fn table_statistics(
        &self,
        table_ref: &TableReference,
    ) -> Option<&crate::catalog::TableStatistics> {
        self.catalog.table_statistics(table_ref)
    }

    pub fn transaction_manager(&self) -> Arc<TransactionManager> {
        self.transaction_manager.clone()
    }

    fn plan_statement(&mut self, sql: &str) -> QuillSQLResult<PreparedStatement> {
        let logical_plan = self.create_logical_plan(sql)?;
        debug!(
            "Logical Plan: \n{}",
            pretty_format_logical_plan(&logical_plan)
        );

        let optimized_logical_plan = self.optimize_logical_plan(&logical_plan)?;
        debug!(
            "Optimized Logical Plan: \n{}",
            pretty_format_logical_plan(&optimized_logical_plan)
        );

        let physical_plan = self.build_physical_plan(&optimized_logical_plan);
        debug!(
            "Physical Plan: \n{}",
            pretty_format_physical_plan(&physical_plan)
        );

        Ok(PreparedStatement {
            optimized_logical_plan,
            physical_plan,
        })
    }

    fn optimize_logical_plan(&self, logical_plan: &LogicalPlan) -> QuillSQLResult<LogicalPlan> {
        LogicalOptimizer::new().optimize(logical_plan)
    }

    fn build_physical_plan(&self, logical_plan: &LogicalPlan) -> PhysicalPlan {
        let physical_planner = PhysicalPlanner::with_catalog(&self.catalog);
        physical_planner.create_physical_plan(logical_plan.clone())
    }

    fn execute_transaction_control(
        &self,
        session: &mut SessionContext,
        plan: &LogicalPlan,
    ) -> QuillSQLResult<Option<Vec<Tuple>>> {
        match plan {
            LogicalPlan::BeginTransaction(modes) => {
                if session.has_active_transaction() {
                    return Err(QuillSQLError::Execution(
                        "transaction already active".to_string(),
                    ));
                }
                let txn = self.transaction_manager.begin(
                    modes.unwrap_effective_isolation(session.default_isolation()),
                    modes
                        .access_mode
                        .unwrap_or(TransactionAccessMode::ReadWrite),
                )?;
                session.set_active_transaction(txn)?;
                Ok(Some(vec![]))
            }
            LogicalPlan::CommitTransaction => {
                let txn_ref = session
                    .active_txn_mut()
                    .ok_or_else(|| QuillSQLError::Execution("no active transaction".to_string()))?;
                let txn_id = txn_ref.id();
                self.transaction_manager.commit(txn_ref)?;
                self.holt_store
                    .put_txn_status(txn_id, TransactionStatus::Committed)?;
                session.clear_active_transaction();
                Ok(Some(vec![]))
            }
            LogicalPlan::RollbackTransaction => {
                let txn_ref = session
                    .active_txn_mut()
                    .ok_or_else(|| QuillSQLError::Execution("no active transaction".to_string()))?;
                let txn_id = txn_ref.id();
                self.transaction_manager.abort(txn_ref)?;
                self.holt_store
                    .put_txn_status(txn_id, TransactionStatus::Aborted)?;
                session.clear_active_transaction();
                Ok(Some(vec![]))
            }
            LogicalPlan::SetTransaction { scope, modes } => {
                match scope {
                    TransactionScope::Session => session.apply_session_modes(modes),
                    TransactionScope::Transaction => session.apply_transaction_modes(modes),
                }
                Ok(Some(vec![]))
            }
            _ => Ok(None),
        }
    }

    fn execute_physical_plan(
        &mut self,
        session: &mut SessionContext,
        physical_plan: PhysicalPlan,
    ) -> QuillSQLResult<Vec<Tuple>> {
        let needs_cleanup = !session.has_active_transaction();
        let autocommit = session.autocommit();
        let result = {
            let txn = session.ensure_active_transaction(&self.transaction_manager)?;
            let context = crate::execution::ExecutionContext::new(
                &mut self.catalog,
                txn,
                self.transaction_manager.clone(),
                self.storage.clone(),
            );
            let mut engine = ExecutionEngine { context };
            engine.execute(Rc::new(physical_plan))?
        };

        if autocommit && needs_cleanup {
            if let Some(txn) = session.active_txn_mut() {
                let txn_id = txn.id();
                self.transaction_manager.commit(txn)?;
                self.holt_store
                    .put_txn_status(txn_id, TransactionStatus::Committed)?;
            }
            session.clear_active_transaction();
        }

        Ok(result)
    }
}

fn holt_directory_for_location(
    location: &DatabaseLocation,
    overrides: &HoltOptions,
) -> QuillSQLResult<(PathBuf, Option<TempDir>)> {
    if let Some(directory) = &overrides.directory {
        return Ok((directory.clone(), None));
    }
    match location {
        DatabaseLocation::OnDisk(path) => Ok((PathBuf::from(path), None)),
        DatabaseLocation::Temporary => {
            let temp_dir = TempDir::new()?;
            let holt_dir = temp_dir.path().join("holt");
            Ok((holt_dir, Some(temp_dir)))
        }
    }
}

fn seed_holt_transaction_statuses(
    holt_store: &HoltStore,
    transaction_manager: &TransactionManager,
) -> QuillSQLResult<()> {
    let mut next_txn_id = 1;
    for (txn_id, status) in holt_store.recover_txn_statuses()? {
        transaction_manager.record_recovered_status(txn_id, status);
        next_txn_id = next_txn_id.max(txn_id.saturating_add(1));
    }
    transaction_manager.ensure_next_txn_id_at_least(next_txn_id);
    Ok(())
}