-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use anyhow::Result; | ||
|
||
use crate::{plan::IndexScanPlan, tuple::Tuple}; | ||
|
||
use super::ExecutorContext; | ||
|
||
pub struct IndexScanExecutor<'a> { | ||
pub plan: IndexScanPlan, | ||
pub executor_context: &'a ExecutorContext, | ||
pub index_id: i64, | ||
} | ||
|
||
impl IndexScanExecutor<'_> { | ||
pub fn init(&mut self) -> Result<()> { | ||
let index = self | ||
.executor_context | ||
.catalog | ||
.lock() | ||
.map_err(|_| anyhow::anyhow!("Catalog lock error"))? | ||
.get_index(self.index_id, self.executor_context.transaction_id)?; | ||
println!("Index: {:?}", index); | ||
Ok(()) | ||
} | ||
pub fn next(&mut self) -> Result<Option<Tuple>> { | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use anyhow::Result; | ||
|
||
use crate::{common::PageID, value::Value}; | ||
|
||
#[derive(Debug)] | ||
pub struct Index { | ||
pub id: i64, | ||
pub name: String, | ||
pub table_name: String, | ||
pub first_page_id: PageID, | ||
pub columns: Vec<String>, | ||
} | ||
|
||
impl Index { | ||
pub fn from_system_table(values: Vec<Value>) -> Result<Self> { | ||
let id = if let Value::Integer(id) = &values[0] { | ||
id.0 | ||
} else { | ||
return Err(anyhow::anyhow!("Invalid id")); | ||
}; | ||
let name = if let Value::Varchar(name) = &values[1] { | ||
name.0.clone() | ||
} else { | ||
return Err(anyhow::anyhow!("Invalid name")); | ||
}; | ||
let table_name = if let Value::Varchar(table_name) = &values[2] { | ||
table_name.0.clone() | ||
} else { | ||
return Err(anyhow::anyhow!("Invalid table_name")); | ||
}; | ||
let first_page_id = if let Value::Integer(first_page_id) = &values[3] { | ||
PageID(first_page_id.0 as u32) | ||
} else { | ||
return Err(anyhow::anyhow!("Invalid first_page_id")); | ||
}; | ||
Ok(Self { | ||
id, | ||
name, | ||
table_name, | ||
first_page_id, | ||
columns: Vec::new(), | ||
}) | ||
} | ||
pub fn add_columns(&mut self, column: String) { | ||
self.columns.push(column); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::{ | ||
binder::BoundExpressionAST, | ||
catalog::Catalog, | ||
common::TransactionID, | ||
plan::{IndexScanPlan, Plan}, | ||
}; | ||
|
||
pub struct Optimizer { | ||
catalog: Arc<Mutex<Catalog>>, | ||
txn_id: TransactionID, | ||
} | ||
impl Optimizer { | ||
pub fn new(catalog: Arc<Mutex<Catalog>>, txn_id: TransactionID) -> Self { | ||
Self { catalog, txn_id } | ||
} | ||
pub fn optimize(&self, plan: Plan) -> Result<Plan> { | ||
let mut optimized_plan = plan; | ||
optimized_plan = self.optimize_filter_index_scan(optimized_plan)?; | ||
Ok(optimized_plan) | ||
} | ||
fn optimize_filter_index_scan(&self, plan: Plan) -> Result<Plan> { | ||
let mut children = Vec::new(); | ||
for child in plan.children() { | ||
let optimized_child = self.optimize_filter_index_scan(*child)?; | ||
children.push(optimized_child); | ||
} | ||
let mut source_plan = plan.clone(); | ||
source_plan.set_children(children); | ||
if let Plan::Filter(filter_plan) = plan { | ||
if let BoundExpressionAST::Binary(binary_expression) = filter_plan.condition { | ||
if let BoundExpressionAST::Path(path_expression) = *binary_expression.left { | ||
let indexes = self | ||
.catalog | ||
.lock() | ||
.map_err(|_| anyhow::anyhow!("Catalog lock error"))? | ||
.get_indexes_by_table_name(&path_expression.table_name, self.txn_id)?; | ||
for index in indexes { | ||
if index.columns.len() == 1 | ||
&& index.columns[0] == path_expression.column_name | ||
{ | ||
if let BoundExpressionAST::Literal(_) = *binary_expression.right { | ||
return Ok(Plan::IndexScan(IndexScanPlan { | ||
index_id: index.id, | ||
schema: filter_plan.schema, | ||
})); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(source_plan) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters