Skip to content

Commit

Permalink
Pretty print tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 2, 2024
1 parent 6845e9a commit 241f835
Show file tree
Hide file tree
Showing 25 changed files with 81 additions and 98 deletions.
35 changes: 0 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions bustubx-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bustubx::Database;
use bustubx::{pretty_format_tuples, BustubxResult, Database};
use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Result};
use std::io::Write;
Expand Down Expand Up @@ -35,7 +35,15 @@ fn main() -> Result<()> {
println!("bye!");
break;
}
let _ = db.run(&line);
let result = db.run(&line);
match result {
Ok(tuples) => {
if !tuples.is_empty() {
println!("{}", pretty_format_tuples(&tuples))
}
}
Err(e) => println!("{}", e),
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
Expand Down
3 changes: 0 additions & 3 deletions bustubx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ doctest = false

[dependencies]
sqlparser = "0.34.0"
petgraph = "0.6.3"
lazy_static = "1.4.0"
itertools = "0.11.0"
comfy-table = "7.0.1"
derive-new = "0.5.9"
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-chrome = "0.7.1"
thiserror = "1.0.56"
tempfile = "3"
derive-with = "0.5.0"
2 changes: 1 addition & 1 deletion bustubx/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mod page;
mod replacer;

pub use buffer_pool::BufferPoolManager;
pub use page::{Page, PageId};
pub use page::{Page, PageId, INVALID_PAGE_ID};

Check warning on line 6 in bustubx/src/buffer/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `Page`
1 change: 1 addition & 0 deletions bustubx/src/buffer/page.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::common::config::BUSTUBX_PAGE_SIZE;

pub type PageId = u32;
pub const INVALID_PAGE_ID: PageId = u32::MAX;

#[derive(Debug, Clone)]
pub struct Page {
Expand Down
11 changes: 10 additions & 1 deletion bustubx/src/common/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use crate::buffer::PageId;

Check warning on line 1 in bustubx/src/common/config.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::buffer::PageId`
use crate::catalog::{Column, DataType, Schema, SchemaRef};
use std::sync::Arc;

// 数据页的大小(字节)
pub const BUSTUBX_PAGE_SIZE: usize = 4096;
pub const INVALID_PAGE_ID: PageId = std::u32::MAX;

// table heap对应的缓冲池的大小(页)
pub const TABLE_HEAP_BUFFER_POOL_SIZE: usize = 100;

pub type TransactionId = u32;

lazy_static::lazy_static! {
pub static ref EMPTY_SCHEMA_REF: SchemaRef = Arc::new(Schema::empty());

pub static ref INSERT_OUTPUT_SCHEMA_REF: SchemaRef = Arc::new(Schema::new(
vec![Column::new("insert_rows".to_string(), DataType::Int32)]
));
}
3 changes: 2 additions & 1 deletion bustubx/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod config;
pub mod rid;
mod scalar;
pub mod table_ref;
mod table_ref;
pub mod util;

pub use scalar::ScalarValue;
pub use table_ref::TableReference;
33 changes: 19 additions & 14 deletions bustubx/src/common/util.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
use comfy_table::Cell;

use crate::{catalog::Schema, storage::Tuple};
use crate::storage::Tuple;

pub fn pretty_format_tuples(tuples: &Vec<Tuple>) -> comfy_table::Table {
let mut table = comfy_table::Table::new();
table.load_preset("||--+-++| ++++++");

pub fn print_tuples(tuples: &Vec<Tuple>, schema: &Schema) {
if tuples.is_empty() {
return;
return table;
}
let mut headers = Vec::new();
for column in &schema.columns {
headers.push(Cell::new(column.name.clone()));

let schema = &tuples[0].schema;

let mut header = Vec::new();
for column in schema.columns.iter() {
header.push(Cell::new(column.name.clone()));
}
let mut table = comfy_table::Table::new();
table.set_header(headers);
table.set_header(header);

for tuple in tuples {
let mut row = Vec::new();
tuple.all_values(schema).iter().for_each(|v| {
row.push(Cell::new(format!("{v:?}")));
});
table.add_row(row);
let mut cells = Vec::new();
for value in tuple.data.iter() {
cells.push(Cell::new(format!("{value}")));
}
table.add_row(cells);
}

println!("{}", table);
table
}
6 changes: 3 additions & 3 deletions bustubx/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ 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);

// logical plan -> physical plan
let physical_plan = PhysicalPlanner::new().create_physical_plan(logical_plan);
println!("{:?}", physical_plan);
// println!("{:?}", physical_plan);

let execution_ctx = ExecutionContext::new(&mut self.catalog);
let mut execution_engine = ExecutionEngine {
context: execution_ctx,
};
let tuples = execution_engine.execute(Arc::new(physical_plan))?;
println!("execution result: {:?}", tuples);
// println!("execution result: {:?}", tuples);
Ok(tuples)
}

Expand Down
5 changes: 3 additions & 2 deletions bustubx/src/execution/physical_plan/create_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::config::EMPTY_SCHEMA_REF;
use crate::common::TableReference;
use crate::planner::logical_plan::OrderByExpr;
use crate::{
catalog::Schema,

Check warning on line 6 in bustubx/src/execution/physical_plan/create_index.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `catalog::Schema`
Expand All @@ -23,7 +24,7 @@ impl VolcanoExecutor for PhysicalCreateIndex {
Ok(None)
}
fn output_schema(&self) -> SchemaRef {
Arc::new(Schema::empty())
EMPTY_SCHEMA_REF.clone()
}
}

Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/execution/physical_plan/create_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::{
catalog::Schema,
execution::{ExecutionContext, VolcanoExecutor},
Expand Down
24 changes: 12 additions & 12 deletions bustubx/src/execution/physical_plan/insert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::{atomic::AtomicU32, Arc};
use tracing::debug;

use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::config::INSERT_OUTPUT_SCHEMA_REF;
use crate::common::TableReference;
use crate::{
catalog::{Column, DataType, Schema},

Check warning on line 8 in bustubx/src/execution/physical_plan/insert.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `Column`, `DataType`, `Schema`
common::ScalarValue,
Expand Down Expand Up @@ -39,27 +41,28 @@ impl PhysicalInsert {
}
impl VolcanoExecutor for PhysicalInsert {
fn init(&self, context: &mut ExecutionContext) -> BustubxResult<()> {
println!("init insert executor");
debug!("init insert executor");
self.input.init(context)?;
self.insert_rows
.store(0, std::sync::atomic::Ordering::SeqCst);
self.input.init(context)
Ok(())
}
fn next(&self, context: &mut ExecutionContext) -> BustubxResult<Option<Tuple>> {
loop {
let next_tuple = self.input.next(context)?;
if next_tuple.is_none() {
// only return insert_rows when input exhausted
if self.insert_rows.load(std::sync::atomic::Ordering::SeqCst) == 0 {
return Ok(None);
return if self.insert_rows.load(std::sync::atomic::Ordering::SeqCst) == 0 {
Ok(None)
} else {
let insert_rows = self.insert_rows.load(std::sync::atomic::Ordering::SeqCst);
self.insert_rows
.store(0, std::sync::atomic::Ordering::SeqCst);
return Ok(Some(Tuple::new(
Ok(Some(Tuple::new(
self.output_schema(),
vec![ScalarValue::Int32(Some(insert_rows as i32))],
)));
}
)))
};
}
let tuple = next_tuple.unwrap();

Expand Down Expand Up @@ -93,10 +96,7 @@ impl VolcanoExecutor for PhysicalInsert {
}

fn output_schema(&self) -> SchemaRef {
Arc::new(Schema::new(vec![Column::new(
"insert_rows".to_string(),
DataType::Int32,
)]))
INSERT_OUTPUT_SCHEMA_REF.clone()
}
}

Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/execution/physical_plan/seq_scan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Mutex;

use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::{
execution::{ExecutionContext, VolcanoExecutor},
storage::{TableIterator, Tuple},
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/expression/column.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::catalog::Schema;
use crate::catalog::{Column, DataType};
use crate::common::table_ref::TableReference;
use crate::common::ScalarValue;
use crate::common::TableReference;
use crate::error::{BustubxError, BustubxResult};
use crate::expression::ExprTrait;
use crate::storage::Tuple;
Expand Down
1 change: 1 addition & 0 deletions bustubx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod parser;
mod planner;
mod storage;

pub use common::util::pretty_format_tuples;
pub use database::Database;
pub use error::{BustubxError, BustubxResult};
pub use storage::Tuple;
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_plan/create_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::planner::logical_plan::OrderByExpr;

#[derive(derive_new::new, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_plan/create_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::catalog::Column;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;

#[derive(Debug, Clone)]
pub struct CreateTable {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_plan/insert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::planner::logical_plan::LogicalPlan;
use std::sync::Arc;

Expand Down
7 changes: 4 additions & 3 deletions bustubx/src/planner/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod util;
mod values;

use crate::catalog::SchemaRef;
use crate::common::config::{EMPTY_SCHEMA_REF, INSERT_OUTPUT_SCHEMA_REF};
pub use create_index::CreateIndex;
pub use create_table::CreateTable;
pub use empty_relation::EmptyRelation;
Expand Down Expand Up @@ -43,10 +44,10 @@ pub enum LogicalPlan {
impl LogicalPlan {
pub fn schema(&self) -> &SchemaRef {
match self {
LogicalPlan::CreateTable(_) => todo!(),
LogicalPlan::CreateIndex(_) => todo!(),
LogicalPlan::CreateTable(_) => &EMPTY_SCHEMA_REF,
LogicalPlan::CreateIndex(_) => &EMPTY_SCHEMA_REF,
LogicalPlan::Filter(Filter { input, .. }) => input.schema(),
LogicalPlan::Insert(_) => todo!(),
LogicalPlan::Insert(_) => &INSERT_OUTPUT_SCHEMA_REF,
LogicalPlan::Join(Join { schema, .. }) => schema,
LogicalPlan::Limit(Limit { input, .. }) => input.schema(),
LogicalPlan::Project(Project { schema, .. }) => schema,
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_plan/table_scan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::catalog::SchemaRef;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::expression::Expr;

#[derive(derive_new::new, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/bind_expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::expression::{BinaryExpr, ColumnExpr, Expr, Literal};
use crate::planner::LogicalPlanner;
use crate::{BustubxError, BustubxResult};
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/logical_planner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{BustubxError, BustubxResult};

use crate::catalog::Catalog;
use crate::common::table_ref::TableReference;
use crate::common::TableReference;
use crate::planner::logical_plan::{LogicalPlan, OrderByExpr};

pub struct PlannerContext<'a> {
Expand Down
Loading

0 comments on commit 241f835

Please sign in to comment.