quill_sql/execution/
mod.rs1pub mod physical_plan;
2use crate::catalog::SchemaRef;
3use crate::error::{QuillSQLError, QuillSQLResult};
4use crate::execution::physical_plan::PhysicalPlan;
5use crate::expression::{Expr, ExprTrait};
6use crate::storage::{
7 engine::{HoltStorage, TableBinding},
8 tuple::Tuple,
9};
10use crate::transaction::{Transaction, TransactionManager, TxnContext};
11use crate::utils::scalar::ScalarValue;
12use crate::{catalog::Catalog, utils::table_ref::TableReference};
13use std::rc::Rc;
14use std::sync::Arc;
15
16pub trait VolcanoExecutor {
17 fn init(&self, _context: &mut ExecutionContext) -> QuillSQLResult<()> {
18 Ok(())
19 }
20
21 fn next(&self, context: &mut ExecutionContext) -> QuillSQLResult<Option<Tuple>>;
22
23 fn output_schema(&self) -> SchemaRef;
24}
25
26pub struct ExecutionContext<'a> {
29 pub catalog: &'a mut Catalog,
31 storage: Arc<HoltStorage>,
33 txn: TxnContext<'a>,
35}
36
37impl<'a> ExecutionContext<'a> {
38 pub fn new(
39 catalog: &'a mut Catalog,
40 txn: &'a mut Transaction,
41 txn_mgr: Arc<TransactionManager>,
42 storage: Arc<HoltStorage>,
43 ) -> Self {
44 Self {
45 catalog,
46 storage,
47 txn: TxnContext::new(txn_mgr, txn),
48 }
49 }
50
51 pub fn eval_predicate(&self, expr: &Expr, tuple: &Tuple) -> QuillSQLResult<bool> {
53 match expr.evaluate(tuple)? {
54 ScalarValue::Boolean(Some(v)) => Ok(v),
55 ScalarValue::Boolean(None) => Ok(false),
56 other => Err(QuillSQLError::Execution(format!(
57 "predicate value must be boolean, got {}",
58 other
59 ))),
60 }
61 }
62
63 pub fn eval_expr(&self, expr: &Expr, tuple: &Tuple) -> QuillSQLResult<ScalarValue> {
65 expr.evaluate(tuple)
66 }
67
68 pub fn table(&self, table: &TableReference) -> QuillSQLResult<TableBinding> {
70 self.storage.table(self.catalog, table)
71 }
72
73 pub fn txn_ctx(&self) -> &TxnContext<'a> {
74 &self.txn
75 }
76
77 pub fn txn_ctx_mut(&mut self) -> &mut TxnContext<'a> {
78 &mut self.txn
79 }
80}
81
82pub struct ExecutionEngine<'a> {
83 pub context: ExecutionContext<'a>,
84}
85impl<'a> ExecutionEngine<'a> {
86 pub fn execute(&mut self, plan: Rc<PhysicalPlan>) -> QuillSQLResult<Vec<Tuple>> {
87 plan.init(&mut self.context)?;
88 let mut result = Vec::new();
89 loop {
90 let next_tuple = plan.next(&mut self.context)?;
91 if let Some(tuple) = next_tuple {
92 result.push(tuple);
93 } else {
94 break;
95 }
96 }
97 Ok(result)
98 }
99}