Disallow indexing of non-indexable columns

This commit is contained in:
Yuriy Dupyn 2024-01-28 17:20:50 +01:00
parent 7b5b2bf9f3
commit 377c19cf32
2 changed files with 22 additions and 3 deletions

View file

@ -20,6 +20,8 @@ pub enum ValidationError {
PrimaryKeyMissing(TableName),
#[error("multiple primary keys found in table {0}")]
MultiplePrimaryKeysFound(TableName),
#[error("attempt to index non-indexable column {1} in table {0}")]
AttemptToIndexNonIndexableColumn(TableName, ColumnName),
#[error("type mismatch at column `{column_name:?}` (expected {expected_type:?}, found {received_type:?})")]
TypeMismatch {
column_name: ColumnName,
@ -202,13 +204,18 @@ fn validate_condition(condition: Option<syntax::Condition>, schema: &TableSchema
}
fn validate_create_index(table_name: TableName, column_name: ColumnName, db_schema: &DbSchema) -> Result<Operation, ValidationError> {
// TODO: You should disallow indexing of Number columns.
let (table_position, schema) = validate_table_exists(db_schema, &table_name)?;
schema
.get_column_position(&column_name)
.get_column(&column_name)
.map_or_else(
|| Err(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()])),
|column| Ok(Operation::CreateIndex(table_position, column))
|(column, type_)| {
if type_.is_indexable() {
Ok(Operation::CreateIndex(table_position, column))
} else {
Err(ValidationError::AttemptToIndexNonIndexableColumn(column_name.clone(), table_name))
}
}
)
}