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

@ -30,6 +30,18 @@ pub enum IndexableValue {
// TODO: what about null? // TODO: what about null?
} }
impl DbType {
pub fn is_indexable(&self) -> bool {
match self {
Self::String => true,
Self::Int => true,
Self::Number => false,
Self::Uuid => true,
}
}
}
impl Value { impl Value {
pub fn to_type(&self) -> DbType { pub fn to_type(&self) -> DbType {
match self { match self {

View file

@ -20,6 +20,8 @@ pub enum ValidationError {
PrimaryKeyMissing(TableName), PrimaryKeyMissing(TableName),
#[error("multiple primary keys found in table {0}")] #[error("multiple primary keys found in table {0}")]
MultiplePrimaryKeysFound(TableName), 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:?})")] #[error("type mismatch at column `{column_name:?}` (expected {expected_type:?}, found {received_type:?})")]
TypeMismatch { TypeMismatch {
column_name: ColumnName, 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> { 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)?; let (table_position, schema) = validate_table_exists(db_schema, &table_name)?;
schema schema
.get_column_position(&column_name) .get_column(&column_name)
.map_or_else( .map_or_else(
|| Err(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()])), || 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))
}
}
) )
} }