Skip to content

Commit

Permalink
Implement Display for expr and logical plan
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 2, 2024
1 parent a1bbafd commit 0b31a3e
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bustubx/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Database {

pub fn run(&mut self, sql: &str) -> BustubxResult<Vec<Tuple>> {
let logical_plan = self.build_logical_plan(sql)?;
// println!("{:?}", logical_plan);
println!("logical plan: \n{}", logical_plan);

// logical plan -> physical plan
let physical_plan = PhysicalPlanner::new().create_physical_plan(logical_plan);
Expand Down
6 changes: 6 additions & 0 deletions bustubx/src/expression/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ impl ExprTrait for Alias {
)))
}
}

impl std::fmt::Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} AS {}", self.expr, self.name)
}
}
12 changes: 12 additions & 0 deletions bustubx/src/expression/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ impl TryFrom<&sqlparser::ast::BinaryOperator> for BinaryOp {
}
}
}

impl std::fmt::Display for BinaryExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {} {}", self.left, self.op, self.right)
}
}

impl std::fmt::Display for BinaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
6 changes: 6 additions & 0 deletions bustubx/src/expression/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ impl ExprTrait for Cast {
)))
}
}

impl std::fmt::Display for Cast {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CAST {} AS {}", self.expr, self.data_type)
}
}
9 changes: 9 additions & 0 deletions bustubx/src/expression/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ impl ExprTrait for ColumnExpr {
})
}
}

impl std::fmt::Display for ColumnExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(relation) = self.relation.as_ref() {
write!(f, "{}.", relation)?;
}
write!(f, "{}", self.name)
}
}
6 changes: 6 additions & 0 deletions bustubx/src/expression/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ impl ExprTrait for Literal {
))
}
}

impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
12 changes: 12 additions & 0 deletions bustubx/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,15 @@ impl ExprTrait for Expr {
}
}
}

impl std::fmt::Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expr::Alias(e) => write!(f, "{e}"),
Expr::Column(e) => write!(f, "{e}"),
Expr::Literal(e) => write!(f, "{e}"),
Expr::BinaryExpr(e) => write!(f, "{e}"),
Expr::Cast(e) => write!(f, "{e}"),
}
}
}
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/create_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pub struct CreateIndex {
pub table_schema: SchemaRef,
pub columns: Vec<OrderByExpr>,
}

impl std::fmt::Display for CreateIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CreateIndex: {}", self.index_name)
}
}
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ pub struct CreateTable {
pub name: TableReference,
pub columns: Vec<Column>,
}

impl std::fmt::Display for CreateTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CreateTable: {}", self.name)
}
}
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/empty_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ pub struct EmptyRelation {
/// The schema description of the output
pub schema: SchemaRef,
}

impl std::fmt::Display for EmptyRelation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "EmptyRelation")
}
}
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pub struct Filter {
/// The incoming logical plan
pub input: Arc<LogicalPlan>,
}

impl std::fmt::Display for Filter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Filter: {}", self.predicate)
}
}
16 changes: 16 additions & 0 deletions bustubx/src/planner/logical_plan/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ pub struct Insert {
pub projected_schema: SchemaRef,
pub input: Arc<LogicalPlan>,
}

impl std::fmt::Display for Insert {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Insert: {} ({})",
self.table,
self.projected_schema
.columns
.iter()
.map(|c| c.name.clone())
.collect::<Vec<_>>()
.join(", ")
)
}
}
18 changes: 17 additions & 1 deletion bustubx/src/planner/logical_plan/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,21 @@ pub enum JoinType {
FullOuter,
// select * from x, y
// select * from x cross join y
CrossJoin,
Cross,
}

impl std::fmt::Display for Join {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} Join", self.join_type)?;
if let Some(condition) = self.condition.as_ref() {
write!(f, ": On {condition}")?;
}
Ok(())
}
}

impl std::fmt::Display for JoinType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
11 changes: 11 additions & 0 deletions bustubx/src/planner/logical_plan/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ pub struct Limit {
pub offset: usize,
pub input: Arc<LogicalPlan>,
}

impl std::fmt::Display for Limit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Limit: {}, offset: {}",
self.limit.map_or("None".to_string(), |v| v.to_string()),
self.offset
)
}
}
18 changes: 18 additions & 0 deletions bustubx/src/planner/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,21 @@ impl LogicalPlan {
}
}
}

impl std::fmt::Display for LogicalPlan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogicalPlan::CreateTable(v) => write!(f, "{v}"),
LogicalPlan::CreateIndex(v) => write!(f, "{v}"),
LogicalPlan::Filter(v) => write!(f, "{v}"),
LogicalPlan::Insert(v) => write!(f, "{v}"),
LogicalPlan::Join(v) => write!(f, "{v}"),
LogicalPlan::Limit(v) => write!(f, "{v}"),
LogicalPlan::Project(v) => write!(f, "{v}"),
LogicalPlan::TableScan(v) => write!(f, "{v}"),
LogicalPlan::Sort(v) => write!(f, "{v}"),
LogicalPlan::Values(v) => write!(f, "{v}"),
LogicalPlan::EmptyRelation(v) => write!(f, "{v}"),
}
}
}
14 changes: 14 additions & 0 deletions bustubx/src/planner/logical_plan/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ pub struct Project {
pub input: Arc<LogicalPlan>,
pub schema: SchemaRef,
}

impl std::fmt::Display for Project {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Project: {}",
self.exprs
.iter()
.map(|e| format!("{e}"))
.collect::<Vec<_>>()
.join(", ")
)
}
}
30 changes: 30 additions & 0 deletions bustubx/src/planner/logical_plan/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,33 @@ pub struct OrderByExpr {
/// Whether to put Nulls before all other data values
pub nulls_first: bool,
}

impl std::fmt::Display for OrderByExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {}",
self.expr,
if self.asc { "ASC" } else { "DESC" },
if self.nulls_first {
"NULLS FIRST"
} else {
"NULLS LAST"
}
)
}
}

impl std::fmt::Display for Sort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Sort: {}",
self.expr
.iter()
.map(|e| format!("{e}"))
.collect::<Vec<_>>()
.join(", ")
)
}
}
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/table_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pub struct TableScan {
pub filters: Vec<Expr>,
pub limit: Option<usize>,
}

impl std::fmt::Display for TableScan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TableScan: {}", self.table_ref)
}
}
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_plan/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn build_join_schema(
let right_cols = &right.columns;

let columns: Vec<ColumnRef> = match join_type {
JoinType::Inner | JoinType::CrossJoin => {
JoinType::Inner | JoinType::Cross => {
left_cols.iter().chain(right_cols.iter()).cloned().collect()
}
JoinType::LeftOuter => left_cols
Expand Down
6 changes: 6 additions & 0 deletions bustubx/src/planner/logical_plan/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ pub struct Values {
pub schema: SchemaRef,
pub values: Vec<Vec<Expr>>,
}

impl std::fmt::Display for Values {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Values")
}
}
4 changes: 2 additions & 2 deletions bustubx/src/planner/logical_planner/plan_set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ impl LogicalPlanner<'_> {
let schema = Arc::new(build_join_schema(
left.schema(),
right.schema(),
JoinType::CrossJoin,
JoinType::Cross,
)?);
Ok(LogicalPlan::Join(Join {
left: Arc::new(left),
right: Arc::new(right),
join_type: JoinType::CrossJoin,
join_type: JoinType::Cross,
condition: None,
schema,
}))
Expand Down

0 comments on commit 0b31a3e

Please sign in to comment.