Skip to content

Commit

Permalink
Fix/integer cast (#218)
Browse files Browse the repository at this point in the history
* fix: add `natual_join.slt` & fix `Integer` cast & `Wildcard` resulting in early exit

* chore: `assert!` -> `debug_assert!`
  • Loading branch information
KKould authored Sep 1, 2024
1 parent 5e7e47a commit a4cdca4
Show file tree
Hide file tree
Showing 33 changed files with 643 additions and 421 deletions.
2 changes: 1 addition & 1 deletion src/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl FileFormat {
for opt in options {
match opt {
CopyOption::Format(fmt) => {
assert_eq!(fmt.value.to_lowercase(), "csv", "only support CSV format")
debug_assert_eq!(fmt.value.to_lowercase(), "csv", "only support CSV format")
}
CopyOption::Delimiter(c) => delimiter = *c,
CopyOption::Header(b) => header = *b,
Expand Down
14 changes: 7 additions & 7 deletions src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ mod tests {

match plan1.operator {
Operator::CreateTable(op) => {
assert_eq!(op.table_name, Arc::new("t1".to_string()));
assert_eq!(op.columns[0].name(), "id");
assert_eq!(op.columns[0].nullable, false);
assert_eq!(
debug_assert_eq!(op.table_name, Arc::new("t1".to_string()));
debug_assert_eq!(op.columns[0].name(), "id");
debug_assert_eq!(op.columns[0].nullable, false);
debug_assert_eq!(
op.columns[0].desc,
ColumnDesc::new(LogicalType::Integer, true, false, None)
);
assert_eq!(op.columns[1].name(), "name");
assert_eq!(op.columns[1].nullable, true);
assert_eq!(
debug_assert_eq!(op.columns[1].name(), "name");
debug_assert_eq!(op.columns[1].nullable, true);
debug_assert_eq!(
op.columns[1].desc,
ColumnDesc::new(
LogicalType::Varchar(Some(10), CharLengthUnits::Characters),
Expand Down
16 changes: 8 additions & 8 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,13 @@ pub mod test {

#[test]
pub fn test_valid_identifier() {
assert!(is_valid_identifier("valid_table"));
assert!(is_valid_identifier("valid_column"));
assert!(is_valid_identifier("_valid_column"));
assert!(is_valid_identifier("valid_column_1"));

assert!(!is_valid_identifier("invalid_name&"));
assert!(!is_valid_identifier("1_invalid_name"));
assert!(!is_valid_identifier("____"));
debug_assert!(is_valid_identifier("valid_table"));
debug_assert!(is_valid_identifier("valid_column"));
debug_assert!(is_valid_identifier("_valid_column"));
debug_assert!(is_valid_identifier("valid_column_1"));

debug_assert!(!is_valid_identifier("invalid_name&"));
debug_assert!(!is_valid_identifier("1_invalid_name"));
debug_assert!(!is_valid_identifier("____"));
}
}
9 changes: 6 additions & 3 deletions src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
) -> Result<Vec<ScalarExpression>, DatabaseError> {
let mut select_items = vec![];

for item in items.iter().enumerate() {
match item.1 {
for item in items.iter() {
match item {
SelectItem::UnnamedExpr(expr) => select_items.push(self.bind_expr(expr)?),
SelectItem::ExprWithAlias { expr, alias } => {
let expr = self.bind_expr(expr)?;
Expand All @@ -428,7 +428,10 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
}
SelectItem::Wildcard(_) => {
if let Operator::Project(op) = &plan.operator {
return Ok(op.exprs.clone());
for expr in op.exprs.iter() {
select_items.push(expr.clone());
}
continue;
}
let mut join_used = HashSet::with_capacity(self.context.using.len());

Expand Down
16 changes: 8 additions & 8 deletions src/catalog/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,24 @@ mod tests {
let col_catalogs = vec![col0, col1];
let table_catalog = TableCatalog::new(Arc::new("test".to_string()), col_catalogs).unwrap();

assert_eq!(table_catalog.contains_column(&"a".to_string()), true);
assert_eq!(table_catalog.contains_column(&"b".to_string()), true);
assert_eq!(table_catalog.contains_column(&"c".to_string()), false);
debug_assert_eq!(table_catalog.contains_column(&"a".to_string()), true);
debug_assert_eq!(table_catalog.contains_column(&"b".to_string()), true);
debug_assert_eq!(table_catalog.contains_column(&"c".to_string()), false);

let col_a_id = table_catalog
.get_column_id_by_name(&"a".to_string())
.unwrap();
let col_b_id = table_catalog
.get_column_id_by_name(&"b".to_string())
.unwrap();
assert!(col_a_id < col_b_id);
debug_assert!(col_a_id < col_b_id);

let column_catalog = table_catalog.get_column_by_id(&col_a_id).unwrap();
assert_eq!(column_catalog.name(), "a");
assert_eq!(*column_catalog.datatype(), LogicalType::Integer,);
debug_assert_eq!(column_catalog.name(), "a");
debug_assert_eq!(*column_catalog.datatype(), LogicalType::Integer,);

let column_catalog = table_catalog.get_column_by_id(&col_b_id).unwrap();
assert_eq!(column_catalog.name(), "b");
assert_eq!(*column_catalog.datatype(), LogicalType::Boolean,);
debug_assert_eq!(column_catalog.name(), "b");
debug_assert_eq!(*column_catalog.datatype(), LogicalType::Boolean,);
}
}
58 changes: 49 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ mod test {
use crate::catalog::{ColumnCatalog, ColumnDesc};
use crate::db::{DataBaseBuilder, DatabaseError};
use crate::storage::{Storage, TableCache, Transaction};
use crate::types::tuple::create_table;
use crate::types::tuple::{create_table, Tuple};
use crate::types::value::DataValue;
use crate::types::LogicalType;
use chrono::{Datelike, Local};
use std::sync::Arc;
use tempfile::TempDir;

Expand Down Expand Up @@ -356,6 +357,23 @@ mod test {
let (schema, tuples) = fnck_sql.run("select current_date()")?;
println!("{}", create_table(&schema, &tuples));

debug_assert_eq!(
schema,
Arc::new(vec![Arc::new(ColumnCatalog::new(
"current_date()".to_string(),
true,
ColumnDesc::new(LogicalType::Date, false, false, None)
))])
);
debug_assert_eq!(
tuples,
vec![Tuple {
id: None,
values: vec![Arc::new(DataValue::Date32(Some(
Local::now().num_days_from_ce()
)))],
}]
);
Ok(())
}

Expand All @@ -369,6 +387,28 @@ mod test {
)?;
println!("{}", create_table(&schema, &tuples));

let mut column = ColumnCatalog::new(
"number".to_string(),
true,
ColumnDesc::new(LogicalType::Integer, false, false, None),
);
column.set_table_name(Arc::new("a".to_string()));
column.set_id(0);

debug_assert_eq!(schema, Arc::new(vec![Arc::new(column)]));
debug_assert_eq!(
tuples,
vec![
Tuple {
id: None,
values: vec![Arc::new(DataValue::Int32(Some(3)))],
},
Tuple {
id: None,
values: vec![Arc::new(DataValue::Int32(Some(4)))],
},
]
);
Ok(())
}

Expand All @@ -391,32 +431,32 @@ mod test {
let (_, tuples_1) = tx_1.run("select * from t1")?;
let (_, tuples_2) = tx_2.run("select * from t1")?;

assert_eq!(tuples_1.len(), 2);
assert_eq!(tuples_2.len(), 2);
debug_assert_eq!(tuples_1.len(), 2);
debug_assert_eq!(tuples_2.len(), 2);

assert_eq!(
debug_assert_eq!(
tuples_1[0].values,
vec![
Arc::new(DataValue::Int32(Some(0))),
Arc::new(DataValue::Int32(Some(0)))
]
);
assert_eq!(
debug_assert_eq!(
tuples_1[1].values,
vec![
Arc::new(DataValue::Int32(Some(1))),
Arc::new(DataValue::Int32(Some(1)))
]
);

assert_eq!(
debug_assert_eq!(
tuples_2[0].values,
vec![
Arc::new(DataValue::Int32(Some(0))),
Arc::new(DataValue::Int32(Some(0)))
]
);
assert_eq!(
debug_assert_eq!(
tuples_2[1].values,
vec![
Arc::new(DataValue::Int32(Some(3))),
Expand All @@ -426,11 +466,11 @@ mod test {

tx_1.commit()?;

assert!(tx_2.commit().is_err());
debug_assert!(tx_2.commit().is_err());

let mut tx_3 = fnck_sql.new_transaction()?;
let res = tx_3.run("create table t2 (a int primary key, b int)");
assert!(res.is_err());
debug_assert!(res.is_err());

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/execution/dml/copy_from_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
CoroutineState::Complete(()) => unreachable!(),
}
.unwrap();
assert_eq!(
debug_assert_eq!(
tuple,
TupleBuilder::build_result(format!("import {} rows", 2))
);
Expand Down
6 changes: 3 additions & 3 deletions src/execution/dql/aggregate/hash_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ mod test {
)
);

assert_eq!(tuples.len(), 2);
debug_assert_eq!(tuples.len(), 2);

let vec_values = tuples.into_iter().map(|tuple| tuple.values).collect_vec();

assert!(vec_values.contains(&build_integers(vec![Some(3), Some(0)])));
assert!(vec_values.contains(&build_integers(vec![Some(5), Some(1)])));
debug_assert!(vec_values.contains(&build_integers(vec![Some(3), Some(0)])));
debug_assert!(vec_values.contains(&build_integers(vec![Some(5), Some(1)])));

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/execution/dql/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct SumAccumulator {

impl SumAccumulator {
pub fn new(ty: &LogicalType) -> Result<Self, DatabaseError> {
assert!(ty.is_numeric());
debug_assert!(ty.is_numeric());

Ok(Self {
result: DataValue::none(ty),
Expand Down
Loading

0 comments on commit a4cdca4

Please sign in to comment.