diff --git a/minisql/src/interpreter2.rs b/minisql/src/interpreter2.rs index a312e1a..3944710 100644 --- a/minisql/src/interpreter2.rs +++ b/minisql/src/interpreter2.rs @@ -12,6 +12,7 @@ use bimap::BiMap; use storage_engine::store::Store; use storage_engine::cursor::{ReadCursor, WriteCursor}; use storage_engine::cursor_capabilities::traversal::CursorCanTraverse; +use storage_engine::cursor_capabilities::index_access::CursorCanReadIndex; // ==============Interpreter================ #[derive(Debug)] @@ -84,6 +85,16 @@ impl State { Ok(()) } + async fn select_eq(table: &Table, mut cursor: ReadCursor<'_, Value>, response_writer: &mut Writer, column_selection: ColumnSelection, column: Column, value: Value) -> DbResult<()> { + let entries = cursor.select_entries_where_eq(column as storage_engine::store::Column, &value).await.map_err(|e| RuntimeError::StorageEngineError(table.schema.table_name().to_string(), e))?; + for entry in entries { + let row: Row = From::from(entry); + let restricted_row = row.restrict_columns(&column_selection); + response_writer.write_table_row(&restricted_row).await.map_err(|e| RuntimeError::AnyhowError(e))?; + } + Ok(()) + } + pub async fn interpret(&mut self, response_writer: &mut Writer, operation: Operation) -> DbResult<()> { use Operation::*; @@ -99,7 +110,7 @@ impl State { } Some(Condition::Eq(eq_column, value)) => { - todo!() + Self::select_eq(&table, cursor, response_writer, column_selection, eq_column, value).await } } } diff --git a/minisql/src/type_system.rs b/minisql/src/type_system.rs index 8aa811d..155eb99 100644 --- a/minisql/src/type_system.rs +++ b/minisql/src/type_system.rs @@ -84,6 +84,42 @@ impl Ord for IndexableValue { } } +impl Eq for Value {} + +impl PartialOrd for Value { + fn partial_cmp(&self, other: &Self) -> Option { + todo!() + } +} + +// TODO: Make column know about indexable types +impl Ord for Value { + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + (Value::String(s0), Value::String(s1)) => s0.cmp(s1), + (Value::Int(n0), Value::Int(n1)) => n0.cmp(n1), + (Value::Uuid(id0), Value::Uuid(id1)) => id0.cmp(id1), + (Value::None(_), Value::None(_)) => Ordering::Equal, + (Value::None(_), Value::Some(_)) => Ordering::Less, + (Value::Some(_), Value::None(_)) => Ordering::Greater, + (Value::Some(v0), Value::Some(v1)) => v0.cmp(v1), + _ => + // SAFETY: + // We are using indexable values as keys in key-value maps. + // When validation is done, it can't happen that we will be comparing two values + // of different types. + // Ofcourse another option is to artificialy order e.g. + // None < Some(...) < String < Int < Uuid + // where ... is again None < Some(...) < String < Int < Uuid + // where ... + // infinitely deep total order. But this is pointless for our usecase. + { + unreachable!() + } + } + } +} + // impl Encode for Value { // fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { // todo!()