Skip to content

Commit

Permalink
Add nullable in Column::new method
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 5, 2024
1 parent 7aa59d7 commit 911d42a
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 68 deletions.
30 changes: 15 additions & 15 deletions bustubx/src/catalog/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ mod tests {

let table_name = "test_table1".to_string();
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("c".to_string(), DataType::Int32),
Column::new("a".to_string(), DataType::Int8, true),
Column::new("b".to_string(), DataType::Int16, true),
Column::new("c".to_string(), DataType::Int32, true),
]));
let table_info = catalog
.create_table(table_name.clone(), schema.clone())
Expand All @@ -211,9 +211,9 @@ mod tests {

let table_name = "test_table2".to_string();
let schema = Arc::new(Schema::new(vec![
Column::new("d".to_string(), DataType::Int32),
Column::new("e".to_string(), DataType::Int16),
Column::new("f".to_string(), DataType::Int8),
Column::new("d".to_string(), DataType::Int32, true),
Column::new("e".to_string(), DataType::Int16, true),
Column::new("f".to_string(), DataType::Int8, true),
]));
let table_info = catalog
.create_table(table_name.clone(), schema.clone())
Expand All @@ -236,17 +236,17 @@ mod tests {

let table_name1 = "test_table1".to_string();
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("c".to_string(), DataType::Int32),
Column::new("a".to_string(), DataType::Int8, true),
Column::new("b".to_string(), DataType::Int16, true),
Column::new("c".to_string(), DataType::Int32, true),
]));
let _ = catalog.create_table(table_name1.clone(), schema);

let table_name2 = "test_table2".to_string();
let schema = Arc::new(Schema::new(vec![
Column::new("d".to_string(), DataType::Int32),
Column::new("e".to_string(), DataType::Int16),
Column::new("f".to_string(), DataType::Int8),
Column::new("d".to_string(), DataType::Int32, true),
Column::new("e".to_string(), DataType::Int16, true),
Column::new("f".to_string(), DataType::Int8, true),
]));
let _ = catalog.create_table(table_name2.clone(), schema);

Expand Down Expand Up @@ -286,9 +286,9 @@ mod tests {

let table_name = "test_table1".to_string();
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("c".to_string(), DataType::Int32),
Column::new("a".to_string(), DataType::Int8, true),
Column::new("b".to_string(), DataType::Int16, true),
Column::new("c".to_string(), DataType::Int32, true),
]));
let _ = catalog.create_table(table_name.clone(), schema);

Expand Down
5 changes: 2 additions & 3 deletions bustubx/src/catalog/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ impl PartialEq for Column {
impl Eq for Column {}

impl Column {
// TODO set nullable
pub fn new(name: String, data_type: DataType) -> Self {
pub fn new(name: String, data_type: DataType, nullable: bool) -> Self {
Self {
name,
data_type,
nullable: false,
nullable,
}
}
}
2 changes: 1 addition & 1 deletion bustubx/src/catalog/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub type SchemaRef = Arc<Schema>;
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)]
vec![Column::new("insert_rows".to_string(), DataType::Int32, false)]
));
}

Expand Down
10 changes: 5 additions & 5 deletions bustubx/src/expression/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ impl ExprTrait for ColumnExpr {
}

fn to_column(&self, input_schema: &Schema) -> BustubxResult<Column> {
Ok(Column {
name: self.name.clone(),
data_type: self.data_type(input_schema)?,
nullable: self.nullable(input_schema)?,
})
Ok(Column::new(
self.name.clone(),
self.data_type(input_schema)?,
self.nullable(input_schema)?,
))
}
}

Expand Down
1 change: 1 addition & 0 deletions bustubx/src/expression/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl ExprTrait for Literal {
Ok(Column::new(
format!("{}", self.value),
self.data_type(input_schema)?,
self.nullable(input_schema)?,
))
}
}
Expand Down
1 change: 1 addition & 0 deletions bustubx/src/planner/logical_planner/plan_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<'a> LogicalPlanner<'a> {
columns.push(Column::new(
col_def.name.value.clone(),
(&col_def.data_type).try_into()?,
false,
))
}
Ok(LogicalPlan::CreateTable(CreateTable { name, columns }))
Expand Down
5 changes: 3 additions & 2 deletions bustubx/src/planner/logical_planner/plan_set_expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::catalog::{Column, Schema};
use crate::catalog::{Column, Schema, EMPTY_SCHEMA_REF};
use crate::expression::{Alias, ColumnExpr, Expr, ExprTrait};
use crate::planner::logical_plan::JoinType;
use crate::planner::logical_plan::{
Expand Down Expand Up @@ -258,7 +258,8 @@ impl LogicalPlanner<'_> {
for (idx, item) in first_row.iter().enumerate() {
columns.push(Column::new(
idx.to_string(),
item.data_type(&Schema::empty())?,
item.data_type(&EMPTY_SCHEMA_REF)?,
item.nullable(&EMPTY_SCHEMA_REF)?,
))
}

Expand Down
16 changes: 8 additions & 8 deletions bustubx/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,10 @@ mod tests {
"test_index".to_string(),
"test_table".to_string(),
Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("c".to_string(), DataType::Int8),
Column::new("d".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
Column::new("c".to_string(), DataType::Int8, false),
Column::new("d".to_string(), DataType::Int16, false),
])),
vec![1, 3],
);
Expand All @@ -819,8 +819,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let index_metadata = IndexMetadata::new(
"test_index".to_string(),
Expand Down Expand Up @@ -904,8 +904,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let index_metadata = IndexMetadata::new(
"test_index".to_string(),
Expand Down
32 changes: 16 additions & 16 deletions bustubx/src/storage/index_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ mod tests {
#[test]
pub fn test_internal_page_from_to_bytes() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("a".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("a".to_string(), DataType::Int16, false),
]));
let mut ori_page = BPlusTreeInternalPage::new(key_schema.clone(), 5);
ori_page.insert(Tuple::empty(key_schema.clone()), 0, &key_schema);
Expand Down Expand Up @@ -634,8 +634,8 @@ mod tests {
#[test]
pub fn test_leaf_page_from_to_bytes() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("a".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("a".to_string(), DataType::Int16, false),
]));
let mut ori_page = BPlusTreeLeafPage::new(key_schema.clone(), 5);
ori_page.insert(
Expand Down Expand Up @@ -665,8 +665,8 @@ mod tests {
#[test]
pub fn test_internal_page_insert() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut internal_page = BPlusTreeInternalPage::new(key_schema.clone(), 3);
internal_page.insert(Tuple::empty(key_schema.clone()), 0, &key_schema);
Expand Down Expand Up @@ -695,8 +695,8 @@ mod tests {
#[test]
pub fn test_leaf_page_insert() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut leaf_page = BPlusTreeLeafPage::new(key_schema.clone(), 3);
leaf_page.insert(
Expand Down Expand Up @@ -726,8 +726,8 @@ mod tests {
#[test]
pub fn test_internal_page_look_up() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut internal_page = BPlusTreeInternalPage::new(key_schema.clone(), 5);
internal_page.insert(Tuple::empty(key_schema.clone()), 0, &key_schema);
Expand Down Expand Up @@ -808,8 +808,8 @@ mod tests {
#[test]
pub fn test_leaf_page_look_up() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut leaf_page = BPlusTreeLeafPage::new(key_schema.clone(), 5);
leaf_page.insert(
Expand Down Expand Up @@ -913,8 +913,8 @@ mod tests {
#[test]
pub fn test_internal_page_delete() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut internal_page = BPlusTreeInternalPage::new(key_schema.clone(), 5);
internal_page.insert(Tuple::empty(key_schema.clone()), 0, &key_schema);
Expand Down Expand Up @@ -980,8 +980,8 @@ mod tests {
#[test]
pub fn test_leaf_page_delete() {
let key_schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut leaf_page = BPlusTreeLeafPage::new(key_schema.clone(), 5);
leaf_page.insert(
Expand Down
16 changes: 8 additions & 8 deletions bustubx/src/storage/table_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
Expand Down Expand Up @@ -275,8 +275,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
Expand Down Expand Up @@ -324,8 +324,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
Expand Down Expand Up @@ -384,8 +384,8 @@ mod tests {
let temp_path = temp_dir.path().join("test.db");

let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));

let disk_manager = DiskManager::try_new(&temp_path).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions bustubx/src/storage/table_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ mod tests {
#[test]
pub fn test_table_page_insert() {
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut table_page = super::TablePage::new(schema.clone(), 0);
let meta = super::TupleMeta {
Expand Down Expand Up @@ -270,8 +270,8 @@ mod tests {
#[test]
pub fn test_table_page_get_tuple() {
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut table_page = super::TablePage::new(schema.clone(), 0);
let meta = super::TupleMeta {
Expand Down Expand Up @@ -307,8 +307,8 @@ mod tests {
#[test]
pub fn test_table_page_update_tuple_meta() {
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut table_page = super::TablePage::new(schema.clone(), 0);
let meta = super::TupleMeta {
Expand Down Expand Up @@ -344,8 +344,8 @@ mod tests {
#[test]
pub fn test_table_page_from_to_bytes() {
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let mut table_page = super::TablePage::new(schema.clone(), 1);
let meta = super::TupleMeta {
Expand Down
4 changes: 2 additions & 2 deletions bustubx/src/storage/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ mod tests {
#[test]
pub fn test_compare() {
let schema = Arc::new(Schema::new(vec![
Column::new("a".to_string(), DataType::Int8),
Column::new("b".to_string(), DataType::Int16),
Column::new("a".to_string(), DataType::Int8, false),
Column::new("b".to_string(), DataType::Int16, false),
]));
let tuple1 = super::Tuple::new(schema.clone(), vec![1i8.into(), 2i16.into()]);
let tuple2 = super::Tuple::new(schema.clone(), vec![1i8.into(), 2i16.into()]);
Expand Down

0 comments on commit 911d42a

Please sign in to comment.