Skip to content

Commit

Permalink
fix(binder): insert column count validation (#111)
Browse files Browse the repository at this point in the history
* fix(binder): insert column count validation

* fix(binder): values length check

When column names are not specified, the length of values ​​cannot exceed the number of columns of the table.
When column names are specified, values ​​need to be the same length to given column names.
  • Loading branch information
Joeyscat authored Dec 20, 2023
1 parent 33a310c commit 585dbeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ impl<'a, T: Transaction> Binder<'a, T> {

if let Some(table) = self.context.table(table_name.clone()) {
let mut columns = Vec::new();
let values_len = expr_rows[0].len();

if idents.is_empty() {
columns = table.all_columns();
if values_len > columns.len() {
return Err(BindError::ValuesLenMismatch(columns.len(), values_len));
}
} else {
let bind_table_name = Some(table_name.to_string());
for ident in idents {
Expand All @@ -40,9 +44,15 @@ impl<'a, T: Transaction> Binder<'a, T> {
_ => unreachable!(),
}
}
if values_len != columns.len() {
return Err(BindError::ValuesLenMismatch(columns.len(), values_len));
}
}
let mut rows = Vec::with_capacity(expr_rows.len());
for expr_row in expr_rows {
if expr_row.len() != values_len {
return Err(BindError::ValuesLenNotSame());
}
let mut row = Vec::with_capacity(expr_row.len());

for (i, expr) in expr_row.iter().enumerate() {
Expand Down
4 changes: 4 additions & 0 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ pub enum BindError {
InvalidColumn(String),
#[error("ambiguous column {0}")]
AmbiguousColumn(String),
#[error("values length not match, expect {0}, got {1}")]
ValuesLenMismatch(usize, usize),
#[error("values list must all be the same length")]
ValuesLenNotSame(),
#[error("binary operator types mismatch: {0} != {1}")]
BinaryOpTypeMismatch(String, String),
#[error("subquery error: {0}")]
Expand Down

0 comments on commit 585dbeb

Please sign in to comment.