Skip to content

Commit

Permalink
Plan create index v2
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 31, 2024
1 parent 988bcd3 commit 3f3cb0e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
8 changes: 4 additions & 4 deletions bustubx/src/planner/logical_plan_v2/create_index.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::planner::logical_plan_v2::OrderByExpr;

#[derive(derive_new::new, Debug, Clone)]
pub struct CreateIndex {
pub index_name: String,
pub table_name: String,
pub table_schema: SchemaRef,
pub key_attrs: Vec<u32>,
pub table: TableReference,
pub columns: Vec<OrderByExpr>,
}
28 changes: 25 additions & 3 deletions bustubx/src/planner/logical_planner/logical_planner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::BustubxResult;
use sqlparser::ast::{JoinConstraint, JoinOperator, Statement, TableFactor, TableWithJoins};
use crate::{BustubxError, BustubxResult};
use sqlparser::ast::{Ident, JoinConstraint, JoinOperator, Statement, TableFactor, TableWithJoins};

Check warning on line 2 in bustubx/src/planner/logical_planner/logical_planner.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `Ident`
use std::sync::Arc;

use crate::catalog::{Catalog, DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'a> LogicalPlanner<'a> {
}
}

pub fn plan_order_by(
pub fn plan_order_by_v2(
&self,
order_by: &sqlparser::ast::OrderByExpr,
) -> BustubxResult<OrderByExpr> {
Expand All @@ -209,4 +209,26 @@ impl<'a> LogicalPlanner<'a> {
nulls_first: order_by.nulls_first.unwrap_or(false),
})
}

pub fn plan_table_name(
&self,
table_name: &sqlparser::ast::ObjectName,
) -> BustubxResult<TableReference> {
match table_name.0.as_slice() {
[table] => Ok(TableReference::bare(table.value.clone())),
[schema, table] => Ok(TableReference::partial(
schema.value.clone(),
table.value.clone(),
)),
[catalog, schema, table] => Ok(TableReference::full(
catalog.value.clone(),
schema.value.clone(),
table.value.clone(),
)),
_ => Err(BustubxError::Plan(format!(
"Fail to plan table name: {}",
table_name
))),
}
}
}
29 changes: 29 additions & 0 deletions bustubx/src/planner/logical_planner/plan_create_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::common::table_ref::TableReference;
use crate::expression::{ColumnExpr, Expr};
use crate::planner::logical_plan::LogicalPlan;
use crate::planner::logical_plan_v2::{CreateIndex, LogicalPlanV2};
use crate::planner::operator::LogicalOperator;
use crate::{BustubxError, BustubxResult};
use itertools::Itertools;
use sqlparser::ast::{ObjectName, OrderByExpr};

use super::LogicalPlanner;
Expand Down Expand Up @@ -37,4 +41,29 @@ impl<'a> LogicalPlanner<'a> {
children: Vec::new(),
}
}

pub fn plan_create_index_v2(
&self,
index_name: &ObjectName,
table_name: &ObjectName,
columns: &Vec<OrderByExpr>,
) -> BustubxResult<LogicalPlanV2> {
let index_name = index_name
.0
.get(0)
.map_or(Err(BustubxError::Plan("".to_string())), |ident| {
Ok(ident.value.clone())
})?;
let table = self.plan_table_name(table_name)?;
let mut columns_expr = vec![];
for col in columns.iter() {
let col_expr = self.plan_order_by_v2(&col)?;
columns_expr.push(col_expr);
}
Ok(LogicalPlanV2::CreateIndex(CreateIndex {
index_name,
table,
columns: columns_expr,
}))
}
}

0 comments on commit 3f3cb0e

Please sign in to comment.