Move index update from table logic to interpreter

This commit is contained in:
Yuriy Dupyn 2023-12-29 07:45:40 +01:00
parent e9d3df7a22
commit d87c95f1e1
2 changed files with 23 additions and 29 deletions

View file

@ -120,7 +120,7 @@ impl State {
let column_position: ColumnPosition = table.schema.column_position_from_column_name(&column_name)?;
let mut index: ColumnIndex = ColumnIndex::new();
let _ = index.update_from_table(&table, column_position)?;
let _ = update_index_from_table(&mut index, &table, column_position)?;
table.attach_index(column_position, index);
Ok(Response::IndexCreated)
@ -129,6 +129,27 @@ impl State {
}
}
// Should be used in the case when an indexed is created after the table has existed for a
// while. In such a case you need to build the index from the already existing rows.
fn update_index_from_table(column_index: &mut ColumnIndex, table: &Table, column_position: ColumnPosition) -> DbResult<()> {
for (id, row) in &table.rows {
let value = match row.get(column_position) {
Some(Value::Indexable(value)) => {
value.clone()
},
Some(_) => {
let column_name: ColumnName = table.schema.column_name_from_column_position(column_position)?;
return Err(Error::AttemptToIndexNonIndexableColumn(table.schema.table_name.to_string(), column_name))
},
None => {
return Err(Error::ColumnPositionDoesNotExist(table.schema.table_name.to_string(), column_position))
}
};
column_index.add(value, *id)
}
Ok(())
}
// TODO: Give a better name to something that you can respond to with rows
trait SqlResponseConsumer {
// TODO: