Skip to content

Commit

Permalink
Add sqllogictest
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 30, 2024
1 parent dc99a41 commit 863bb78
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 354 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

336 changes: 0 additions & 336 deletions bustubx/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,339 +91,3 @@ impl Database {
Ok(logical_plan)
}
}

mod tests {
use crate::{
catalog::{Column, DataType, Schema},
common::ScalarValue,
};

#[test]
pub fn test_crud_sql() {
let mut db = super::Database::new_temp();
// db.run("create table t1 (a int, b int)");
// db.run("create table t2 (a int, b int)");
// db.run("create table t3 (a int, b int)");
// db.run("create table t4 (a int, b int)");
// db.run("create index idx1 on t1 (a)");
// db.run("select * from t1 where a > 3");
// db.run("select * from t1, t2, t3 inner join t4 on t3.id = t4.id");
// db.run(&"select * from (t1 inner join t2 on t1.a = t2.a) inner join t3 on t1.a = t3.a ".to_string());
}

#[test]
pub fn test_create_table_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run("create table t1 (a int, b int)");

let table = db.catalog.get_table_by_name("t1");
assert!(table.is_some());
let table = table.unwrap();
assert_eq!(table.name, "t1");
assert_eq!(table.schema.columns.len(), 2);
assert_eq!(table.schema.columns[0].name, "a".to_string());
assert_eq!(table.schema.columns[0].data_type, DataType::Int32);
assert_eq!(table.schema.columns[1].name, "b".to_string());
assert_eq!(table.schema.columns[1].data_type, DataType::Int32);
}

#[test]
pub fn test_create_index_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run("create table t1 (a int, b int)");
db.run("create index idx1 on t1 (a)");

let index = db.catalog.get_index_by_name("t1", "idx1");
assert!(index.is_some());
let index = index.unwrap();
assert_eq!(index.name, "idx1");
assert_eq!(index.table_name, "t1");
assert_eq!(index.key_schema.column_count(), 1);
}

#[test]
pub fn test_insert_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run(&"create table t1 (a int, b int)".to_string());
let insert_rows = db
.run(&"insert into t1 values (1, 1), (2, 3), (5, 4)".to_string())
.unwrap();
assert_eq!(insert_rows.len(), 1);

let schema = Schema::new(vec![Column::new(
"insert_rows".to_string(),
DataType::Int32,
)]);
let insert_rows = insert_rows[0].get_value_by_col_id(&schema, 0);
assert_eq!(insert_rows, ScalarValue::Int32(Some(3)));
}

#[test]
pub fn test_select_wildcard_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run(&"create table t1 (a int, b bigint)".to_string());

let select_result = db.run(&"select * from t1".to_string()).unwrap();
assert_eq!(select_result.len(), 0);

db.run(&"insert into t1 values (1, 1), (2, 3), (5, 4)".to_string());

let select_result = db.run(&"select * from t1".to_string()).unwrap();
assert_eq!(select_result.len(), 3);

let schema = Schema::new(vec![
Column::new("a".to_string(), DataType::Int32),
Column::new("b".to_string(), DataType::Int64),
]);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(1))
);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 1),
ScalarValue::Int64(Some(1))
);
assert_eq!(
select_result[1].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(2))
);
assert_eq!(
select_result[1].get_value_by_col_id(&schema, 1),
ScalarValue::Int64(Some(3))
);
assert_eq!(
select_result[2].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(5))
);
assert_eq!(
select_result[2].get_value_by_col_id(&schema, 1),
ScalarValue::Int64(Some(4))
);
}

#[test]
pub fn test_select_where_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run(&"create table t1 (a int, b int)".to_string());
db.run(&"insert into t1 values (1, 1), (2, 3), (5, 4)".to_string());
let select_result = db
.run(&"select a from t1 where a <= b".to_string())
.unwrap();
assert_eq!(select_result.len(), 2);

let schema = Schema::new(vec![Column::new("a".to_string(), DataType::Int32)]);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(1))
);
assert_eq!(
select_result[1].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(2))
);
}

#[test]
pub fn test_select_limit_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run(&"create table t1 (a int, b int)".to_string());
db.run(&"insert into t1 values (1, 1), (2, 3), (5, 4)".to_string());
let select_result = db
.run(&"select * from t1 limit 1 offset 1".to_string())
.unwrap();
assert_eq!(select_result.len(), 1);

let schema = Schema::new(vec![
Column::new("a".to_string(), DataType::Int32),
Column::new("b".to_string(), DataType::Int32),
]);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(2))
);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 1),
ScalarValue::Int32(Some(3))
);
}

// TODO fix tests
// #[test]
// pub fn test_select_cross_join_sql() {
// let db_path = "test_select_cross_join_sql.db";
// let _ = std::fs::remove_file(db_path);
//
// let mut db = super::Database::new_on_disk(db_path);
// db.run(&"create table t1 (a int, b int)".to_string());
// db.run(&"create table t2 (a int, b int)".to_string());
// db.run(&"insert into t1 values (1, 2), (3, 4)".to_string());
// db.run(&"insert into t2 values (5, 6), (7, 8)".to_string());
// let select_result = db.run(&"select * from t1, t2".to_string());
// assert_eq!(select_result.len(), 4);
//
// let schema = Schema::new(vec![
// Column::new("a".to_string(), DataType::Int32, 0),
// Column::new("b".to_string(), DataType::Int32, 1),
// Column::new("a".to_string(), DataType::Int32, 0),
// Column::new("b".to_string(), DataType::Int32, 1),
// ]);
// // 1st row
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 0),
// Value::Integer(1)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 1),
// Value::Integer(2)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 2),
// Value::Integer(5)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 3),
// Value::Integer(6)
// );
//
// // 2nd row
// assert_eq!(
// select_result[1].get_value_by_col_id(&schema, 0),
// Value::Integer(1)
// );
// assert_eq!(
// select_result[1].get_value_by_col_id(&schema, 1),
// Value::Integer(2)
// );
// assert_eq!(
// select_result[1].get_value_by_col_id(&schema, 2),
// Value::Integer(7)
// );
// assert_eq!(
// select_result[1].get_value_by_col_id(&schema, 3),
// Value::Integer(8)
// );
//
// // 3rd row
// assert_eq!(
// select_result[2].get_value_by_col_id(&schema, 0),
// Value::Integer(3)
// );
// assert_eq!(
// select_result[2].get_value_by_col_id(&schema, 1),
// Value::Integer(4)
// );
// assert_eq!(
// select_result[2].get_value_by_col_id(&schema, 2),
// Value::Integer(5)
// );
// assert_eq!(
// select_result[2].get_value_by_col_id(&schema, 3),
// Value::Integer(6)
// );
//
// // 4th row
// assert_eq!(
// select_result[3].get_value_by_col_id(&schema, 0),
// Value::Integer(3)
// );
// assert_eq!(
// select_result[3].get_value_by_col_id(&schema, 1),
// Value::Integer(4)
// );
// assert_eq!(
// select_result[3].get_value_by_col_id(&schema, 2),
// Value::Integer(7)
// );
// assert_eq!(
// select_result[3].get_value_by_col_id(&schema, 3),
// Value::Integer(8)
// );
//
// let _ = std::fs::remove_file(db_path);
// }
//
// #[test]
// pub fn test_select_inner_join_sql() {
// let db_path = "test_select_inner_join_sql.db";
// let _ = std::fs::remove_file(db_path);
//
// let mut db = super::Database::new_on_disk(db_path);
// db.run(&"create table t1 (a int, b int)".to_string());
// db.run(&"create table t2 (a int, b int)".to_string());
// db.run(&"insert into t1 values (1, 2), (5, 6)".to_string());
// db.run(&"insert into t2 values (3, 4), (7, 8)".to_string());
// let select_result = db.run(&"select * from t1 inner join t2 on t1.a > t2.a".to_string());
// assert_eq!(select_result.len(), 1);
//
// let schema = Schema::new(vec![
// Column::new("a".to_string(), DataType::Int32, 0),
// Column::new("b".to_string(), DataType::Int32, 0),
// Column::new("a".to_string(), DataType::Int32, 0),
// Column::new("b".to_string(), DataType::Int32, 0),
// ]);
// // 1st row
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 0),
// Value::Integer(5)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 1),
// Value::Integer(6)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 2),
// Value::Integer(3)
// );
// assert_eq!(
// select_result[0].get_value_by_col_id(&schema, 3),
// Value::Integer(4)
// );
//
// let _ = std::fs::remove_file(db_path);
// }

#[test]
pub fn test_select_order_by_sql() {
let mut db = super::Database::new_temp().unwrap();
db.run(&"create table t1 (a int, b int)".to_string());
db.run(&"insert into t1 values (5, 6), (1, 2), (1, 4)".to_string());
let select_result = db
.run(&"select * from t1 order by a, b desc".to_string())
.unwrap();
assert_eq!(select_result.len(), 3);

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

// 1st row
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(1))
);
assert_eq!(
select_result[0].get_value_by_col_id(&schema, 1),
ScalarValue::Int32(Some(4))
);

// 2nd row
assert_eq!(
select_result[1].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(1))
);
assert_eq!(
select_result[1].get_value_by_col_id(&schema, 1),
ScalarValue::Int32(Some(2))
);

// 3th row
assert_eq!(
select_result[2].get_value_by_col_id(&schema, 0),
ScalarValue::Int32(Some(5))
);
assert_eq!(
select_result[2].get_value_by_col_id(&schema, 1),
ScalarValue::Int32(Some(6))
);
}
}
Loading

0 comments on commit 863bb78

Please sign in to comment.