Skip to content

Commit

Permalink
Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 1, 2024
1 parent 52c65a4 commit 909877b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 28 deletions.
6 changes: 0 additions & 6 deletions bustubx/src/catalog/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ impl Column {
nullable: false,
}
}

pub fn from_sqlparser_column(column_def: &ColumnDef) -> Self {
let column_name = column_def.name.to_string();
let column_type: DataType = (&column_def.data_type).try_into().unwrap();
Self::new(column_name, column_type)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use crate::planner::LogicalPlanner;
use crate::{BustubxError, BustubxResult};

impl LogicalPlanner<'_> {
pub fn plan_expr(&self, sql: &sqlparser::ast::Expr) -> BustubxResult<Expr> {
pub fn bind_expr(&self, sql: &sqlparser::ast::Expr) -> BustubxResult<Expr> {
match sql {
sqlparser::ast::Expr::Identifier(ident) => Ok(Expr::Column(ColumnExpr {
relation: None,
name: ident.value.clone(),
})),
sqlparser::ast::Expr::BinaryOp { left, op, right } => {
let left = Box::new(self.plan_expr(left)?);
let right = Box::new(self.plan_expr(right)?);
let left = Box::new(self.bind_expr(left)?);
let right = Box::new(self.bind_expr(right)?);
Ok(Expr::BinaryExpr(BinaryExpr {
left,
op: op.try_into()?,
right,
}))
}
sqlparser::ast::Expr::Value(value) => self.plan_value(value),
sqlparser::ast::Expr::Value(value) => self.bind_value(value),
sqlparser::ast::Expr::CompoundIdentifier(idents) => match idents.as_slice() {
[col] => Ok(Expr::Column(ColumnExpr {
relation: None,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl LogicalPlanner<'_> {
}
}

pub fn plan_value(&self, value: &sqlparser::ast::Value) -> BustubxResult<Expr> {
pub fn bind_value(&self, value: &sqlparser::ast::Value) -> BustubxResult<Expr> {
match value {
sqlparser::ast::Value::Number(s, _) => {
let num: i64 = s.parse::<i64>().map_err(|e| {
Expand Down
6 changes: 3 additions & 3 deletions bustubx/src/planner/logical_planner/logical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ impl<'a> LogicalPlanner<'a> {
}
}

pub fn plan_order_by_expr(
pub fn bind_order_by_expr(
&self,
order_by: &sqlparser::ast::OrderByExpr,
) -> BustubxResult<OrderByExpr> {
let expr = self.plan_expr(&order_by.expr)?;
let expr = self.bind_expr(&order_by.expr)?;
Ok(OrderByExpr {
expr: Box::new(expr),
asc: order_by.asc.unwrap_or(true),
nulls_first: order_by.nulls_first.unwrap_or(false),
})
}

pub fn plan_table_name(
pub fn bind_table_name(
&self,
table_name: &sqlparser::ast::ObjectName,
) -> BustubxResult<TableReference> {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod bind_expr;
mod logical_planner;
mod plan_create_index;
mod plan_create_table;
mod plan_expr;
mod plan_insert;
mod plan_query;
mod plan_set_expr;
Expand Down
4 changes: 2 additions & 2 deletions bustubx/src/planner/logical_planner/plan_create_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ impl<'a> LogicalPlanner<'a> {
.map_or(Err(BustubxError::Plan("".to_string())), |ident| {
Ok(ident.value.clone())
})?;
let table = self.plan_table_name(table_name)?;
let table = self.bind_table_name(table_name)?;
let mut columns_expr = vec![];
for col in columns.iter() {
let col_expr = self.plan_order_by_expr(&col)?;
let col_expr = self.bind_order_by_expr(&col)?;
columns_expr.push(col_expr);
}
let table_schema = self
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/plan_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a> LogicalPlanner<'a> {
name: &sqlparser::ast::ObjectName,
column_defs: &Vec<sqlparser::ast::ColumnDef>,
) -> BustubxResult<LogicalPlan> {
let name = self.plan_table_name(name)?;
let name = self.bind_table_name(name)?;
let mut columns = vec![];
for col_def in column_defs {
columns.push(Column::new(
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/plan_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<'a> LogicalPlanner<'a> {
source: &sqlparser::ast::Query,
) -> BustubxResult<LogicalPlan> {
let values = self.plan_set_expr(source.body.as_ref())?;
let table = self.plan_table_name(table_name)?;
let table = self.bind_table_name(table_name)?;
let table_schema = self
.context
.catalog
Expand Down
6 changes: 3 additions & 3 deletions bustubx/src/planner/logical_planner/plan_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a> LogicalPlanner<'a> {

let mut order_by_exprs = vec![];
for order in order_by {
order_by_exprs.push(self.plan_order_by_expr(order)?);
order_by_exprs.push(self.bind_order_by_expr(order)?);
}

Ok(LogicalPlan::Sort(Sort {
Expand All @@ -48,7 +48,7 @@ impl<'a> LogicalPlanner<'a> {
let limit = match limit {
None => None,
Some(limit_expr) => {
let n = match self.plan_expr(&limit_expr)? {
let n = match self.bind_expr(&limit_expr)? {
Expr::Literal(lit) => match lit.value {
ScalarValue::Int64(Some(v)) if v >= 0 => Ok(v as usize),
_ => Err(BustubxError::Plan(format!(
Expand All @@ -67,7 +67,7 @@ impl<'a> LogicalPlanner<'a> {

let offset = match offset {
None => 0,
Some(offset_expr) => match self.plan_expr(&offset_expr.value)? {
Some(offset_expr) => match self.bind_expr(&offset_expr.value)? {
Expr::Literal(lit) => match lit.value {
ScalarValue::Int64(Some(v)) => {
if v < 0 {
Expand Down
12 changes: 6 additions & 6 deletions bustubx/src/planner/logical_planner/plan_set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl LogicalPlanner<'_> {
let mut exprs = vec![];
for select_item in project {
match select_item {
sqlparser::ast::SelectItem::UnnamedExpr(expr) => exprs.push(self.plan_expr(expr)?),
sqlparser::ast::SelectItem::UnnamedExpr(expr) => exprs.push(self.bind_expr(expr)?),
sqlparser::ast::SelectItem::ExprWithAlias { expr, alias } => {
exprs.push(Expr::Alias(Alias {
name: alias.value.clone(),
expr: Box::new(self.plan_expr(expr)?),
expr: Box::new(self.bind_expr(expr)?),
}))
}
sqlparser::ast::SelectItem::Wildcard(_) => {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl LogicalPlanner<'_> {
match selection {
None => Ok(input),
Some(predicate) => {
let predicate = self.plan_expr(predicate)?;
let predicate = self.bind_expr(predicate)?;
Ok(LogicalPlan::Filter(Filter {
input: Arc::new(input),
predicate,
Expand Down Expand Up @@ -162,7 +162,7 @@ impl LogicalPlanner<'_> {
) -> BustubxResult<LogicalPlan> {
match constraint {
sqlparser::ast::JoinConstraint::On(expr) => {
let expr = self.plan_expr(expr)?;
let expr = self.bind_expr(expr)?;
let schema = Arc::new(build_join_schema(left.schema(), right.schema(), join_type)?);
Ok(LogicalPlan::Join(Join {
left: Arc::new(left),
Expand Down Expand Up @@ -205,7 +205,7 @@ impl LogicalPlanner<'_> {
match relation {
sqlparser::ast::TableFactor::Table { name, alias, .. } => {
// TODO handle alias
let table_ref = self.plan_table_name(name)?;
let table_ref = self.bind_table_name(name)?;
// TODO get schema by full table name
let schema = self
.context
Expand Down Expand Up @@ -241,7 +241,7 @@ impl LogicalPlanner<'_> {
for row in values.rows.iter() {
let mut record = vec![];
for item in row {
record.push(self.plan_expr(item)?);
record.push(self.bind_expr(item)?);
}
result.push(record);
}
Expand Down

0 comments on commit 909877b

Please sign in to comment.