Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db/table/delete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::db::table::helpers::common::get_row_indicies_matching_clauses;


pub fn delete(table: &mut Table, statement: DeleteStatement) -> Result<(), String> {
let row_indicies_to_delete = get_row_indicies_matching_clauses(table, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;
let row_indicies_to_delete = get_row_indicies_matching_clauses(table, None, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;
swap_remove_bulk(table, row_indicies_to_delete)?;
Ok(())
}
Expand Down
36 changes: 35 additions & 1 deletion src/db/table/helpers/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::collections::HashSet;

use crate::db::table::{Table, Value, DataType};
use crate::interpreter::ast::{SelectStatementColumns, WhereStackElement, OrderByClause, LimitClause};
use crate::db::table::helpers::where_stack::matches_where_stack;
use crate::db::table::helpers::{order_by_clause::get_ordered_row_indicies, limit_clause::get_limited_rows};

pub struct DistinctOn<'a> {
pub columns: &'a SelectStatementColumns,
}

pub fn validate_and_clone_row(table: &Table, row: &Vec<Value>) -> Result<Vec<Value>, String> {
if row.len() != table.width() {
return Err(format!("Rows have incorrect width"));
Expand Down Expand Up @@ -62,9 +68,14 @@ pub fn get_columns_from_row(table: &Table, row: &Vec<Value>, selected_columns: &
return Ok(row_values);
}

pub fn get_row_indicies_matching_clauses(table: &Table, where_clause: &Option<Vec<WhereStackElement>>, order_by_clause: &Option<Vec<OrderByClause>>, limit_clause: &Option<LimitClause>) -> Result<Vec<usize>, String> {
pub fn get_row_indicies_matching_clauses(table: &Table, mode: Option<DistinctOn>, where_clause: &Option<Vec<WhereStackElement>>, order_by_clause: &Option<Vec<OrderByClause>>, limit_clause: &Option<LimitClause>) -> Result<Vec<usize>, String> {
let mut row_indicies = get_row_indicies_matching_where_clause(table, where_clause)?;

if let Some(mode) = mode {
row_indicies = remove_duplicate_rows_from_indicies(table, row_indicies, &mode.columns)?;

}

if let Some(order_by_clause) = order_by_clause {
row_indicies = get_ordered_row_indicies(table, row_indicies, &order_by_clause)?;
}
Expand All @@ -74,5 +85,28 @@ pub fn get_row_indicies_matching_clauses(table: &Table, where_clause: &Option<Ve
return Ok(result.to_vec());
}

return Ok(row_indicies);
}

pub fn remove_duplicate_rows(rows: Vec<Vec<Value>>) -> Vec<Vec<Value>> {
let set = rows.into_iter().collect::<HashSet<Vec<Value>>>();
let result = set.into_iter().collect::<Vec<Vec<Value>>>();
return result;
}

pub fn remove_duplicate_rows_from_indicies(table: &Table, mut row_indicies: Vec<usize>, columns: &SelectStatementColumns) -> Result<Vec<usize>, String> {
let mut set = HashSet::new();
let mut index = row_indicies.len();
while index > 0 {
index -= 1;
let row = get_columns_from_row(table, &table.rows[row_indicies[index]], columns)?;
if set.contains(&row) {
row_indicies.swap_remove(index);
}
else {
set.insert(row);
}

}
return Ok(row_indicies);
}
8 changes: 7 additions & 1 deletion src/db/table/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn select_statement_stack(database: &Database, statement: SelectStatementSta
mod tests {
use super::*;
use crate::db::table::test_utils::default_database;
use crate::interpreter::ast::{SelectStatement, SelectStatementColumns, WhereStackElement, WhereCondition, Operand, Operator, LogicalOperator};
use crate::interpreter::ast::{SelectStatement, SelectStatementColumns, WhereStackElement, WhereCondition, Operand, Operator, LogicalOperator, SelectMode};


#[test]
Expand All @@ -85,6 +85,7 @@ mod tests {
columns: SelectStatementColumns::All,
elements: vec![SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
Expand Down Expand Up @@ -112,6 +113,7 @@ mod tests {
elements: vec![
SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: Some(vec![WhereStackElement::Condition(WhereCondition {
l_side: Operand::Identifier("id".to_string()),
Expand All @@ -123,6 +125,7 @@ mod tests {
}),
SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
Expand All @@ -148,13 +151,15 @@ mod tests {
columns: SelectStatementColumns::All,
elements: vec![SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
limit_clause: None,
}),
SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: Some(vec![WhereStackElement::Condition(WhereCondition {
l_side: Operand::Identifier("id".to_string()),
Expand All @@ -174,6 +179,7 @@ mod tests {
SelectStatementStackElement::SetOperator(SetOperator::Intersect),
SelectStatementStackElement::SelectStatement(SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: Some(vec![WhereStackElement::Condition(WhereCondition {
l_side: Operand::Identifier("id".to_string()),
Expand Down
57 changes: 52 additions & 5 deletions src/db/table/select/select_statement.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
use crate::db::table::{Table, Value};
use crate::interpreter::ast::{SelectStatement};
use crate::db::table::helpers::common::{get_row_indicies_matching_clauses, get_row_columns_from_indicies};
use crate::interpreter::ast::{SelectStatement, SelectMode};
use crate::db::table::helpers::common::{get_row_indicies_matching_clauses, get_row_columns_from_indicies, DistinctOn};




pub fn select_statement(table: &Table, statement: &SelectStatement) -> Result<Vec<Vec<Value>>, String> {
let row_indicies = get_row_indicies_matching_clauses(table, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;

let mode = match statement.mode {
SelectMode::All => None,
SelectMode::Distinct => Some(DistinctOn { columns: &statement.columns }),
};
let row_indicies = get_row_indicies_matching_clauses(table, mode, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;
return Ok(get_row_columns_from_indicies(table, row_indicies, Some(&statement.columns))?);
}

#[cfg(test)]
mod tests {
use super::*;
use crate::db::table::Value;
use crate::db::table::ColumnDefinition;
use crate::db::table::DataType;
use crate::interpreter::ast::{SelectStatementColumns, LimitClause, OrderByClause, OrderByDirection, Operator};
use crate::interpreter::ast::WhereStackElement;
use crate::interpreter::ast::WhereCondition;
use crate::interpreter::ast::Operand;
use crate::db::table::test_utils::default_table;
use crate::db::table::test_utils::{default_table, assert_table_rows_eq_unordered};
use crate::interpreter::ast::SelectMode;

#[test]
fn select_with_all_tokens_is_generated_correctly() {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
Expand All @@ -46,6 +54,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::Specific(vec!["name".to_string(), "age".to_string()]),
where_clause: None,
order_by_clause: None,
Expand All @@ -67,6 +76,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: Some(vec![
WhereStackElement::Condition(WhereCondition {
Expand All @@ -91,6 +101,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::Specific(vec!["name".to_string(), "age".to_string()]),
where_clause: Some(vec![
WhereStackElement::Condition(WhereCondition {
Expand All @@ -115,6 +126,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
Expand All @@ -136,6 +148,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: Some(vec![
WhereStackElement::Condition(WhereCondition {
Expand All @@ -157,6 +170,7 @@ mod tests {
let table = default_table();
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: Some(vec![OrderByClause {column: "money".to_string(), direction: OrderByDirection::Desc}]),
Expand All @@ -172,4 +186,37 @@ mod tests {
];
assert_eq!(expected, result.unwrap());
}

#[test]
fn select_with_distinct_mode_is_generated_correctly() {
let table = Table {
name: "users".to_string(),
columns: vec![
ColumnDefinition {name: "id".to_string(), data_type: DataType::Integer, constraints: vec![]},
ColumnDefinition {name: "name".to_string(), data_type: DataType::Text, constraints: vec![]},
],
rows: vec![
vec![Value::Integer(1), Value::Text("John".to_string())],
vec![Value::Integer(2), Value::Text("Jane".to_string())],
vec![Value::Integer(3), Value::Text("Jane".to_string())],
vec![Value::Integer(4), Value::Null],
],
};
let statement = SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::Distinct,
columns: SelectStatementColumns::Specific(vec!["name".to_string()]),
where_clause: None,
order_by_clause: None,
limit_clause: None,
};
let result = select_statement(&table, &statement);
assert!(result.is_ok());
let expected = vec![
vec![Value::Text("John".to_string())],
vec![Value::Text("Jane".to_string())],
vec![Value::Null],
];
assert_table_rows_eq_unordered(expected, result.unwrap());
}
}
5 changes: 2 additions & 3 deletions src/db/table/select/set_operator_evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use crate::db::table::Value;
use crate::db::table::{helpers::common::remove_duplicate_rows, Value};

pub struct SetOperatorEvaluator {
pub stack: Vec<Vec<Vec<Value>>>,
Expand Down Expand Up @@ -32,8 +32,7 @@ impl SetOperatorEvaluator {
let second = self.pop()?;
let mut first = self.pop()?;
first.extend(second.into_iter());
let set = first.into_iter().collect::<HashSet<Vec<Value>>>();
let result = set.into_iter().collect::<Vec<Vec<Value>>>();
let result = remove_duplicate_rows(first);
self.push(result);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/db/table/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::db::table::helpers::common::get_row_indicies_matching_clauses;
use crate::db::table::DataType;

pub fn update(table: &mut Table, statement: UpdateStatement) -> Result<(), String> {
let row_indicies = get_row_indicies_matching_clauses(table, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;
let row_indicies = get_row_indicies_matching_clauses(table, None, &statement.where_clause, &statement.order_by_clause, &statement.limit_clause)?;
update_rows_from_indicies(table, row_indicies, statement.update_values)?;
Ok(())
}
Expand Down
41 changes: 40 additions & 1 deletion src/interpreter/ast/helpers/select_statement.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{interpreter::{
ast::{
parser::Parser, SelectStatement, SelectStatementColumns, WhereStackElement,
parser::Parser, SelectStatement, SelectStatementColumns, WhereStackElement, SelectMode,
helpers::{
common::{tokens_to_identifier_list, get_table_name, expect_token_type},
order_by_clause::get_order_by, where_stack::get_where_clause, limit_clause::get_limit
Expand All @@ -11,6 +11,13 @@ use crate::{interpreter::{

pub fn get_statement(parser: &mut Parser) -> Result<SelectStatement, String> {
parser.advance()?;
let mode = match parser.current_token()?.token_type {
TokenTypes::Distinct => {
parser.advance()?;
SelectMode::Distinct
}
_ => SelectMode::All
};
let columns = get_columns(parser)?;
expect_token_type(parser, TokenTypes::From)?;
parser.advance()?;
Expand All @@ -21,6 +28,7 @@ pub fn get_statement(parser: &mut Parser) -> Result<SelectStatement, String> {

return Ok(SelectStatement {
table_name: table_name,
mode: mode,
columns: columns,
where_clause: where_clause,
order_by_clause: order_by_clause,
Expand Down Expand Up @@ -69,6 +77,7 @@ mod tests {
let statement = result.unwrap();
assert_eq!(statement, SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::All,
where_clause: None,
order_by_clause: None,
Expand All @@ -92,6 +101,7 @@ mod tests {
let statement = result.unwrap();
assert_eq!(statement, SelectStatement {
table_name: "guests".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::Specific(vec![
"id".to_string(),
]),
Expand Down Expand Up @@ -119,6 +129,7 @@ mod tests {
let statement = result.unwrap();
assert_eq!(statement, SelectStatement {
table_name: "users".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::Specific(vec![
"id".to_string(),
"name".to_string(),
Expand Down Expand Up @@ -162,6 +173,7 @@ mod tests {
let statement = result.unwrap();
let expected = SelectStatement {
table_name: "guests".to_string(),
mode: SelectMode::All,
columns: SelectStatementColumns::Specific(vec![
"id".to_string(),
]),
Expand Down Expand Up @@ -193,4 +205,31 @@ mod tests {
};
assert_eq!(expected, statement);
}

#[test]
fn select_statement_with_distinct_mode_is_generated_correctly() {
// SELECT DISTINCT id FROM guests;
let tokens = vec![
token(TokenTypes::Select, "SELECT"),
token(TokenTypes::Distinct, "DISTINCT"),
token(TokenTypes::Identifier, "id"),
token(TokenTypes::From, "FROM"),
token(TokenTypes::Identifier, "guests"),
token(TokenTypes::SemiColon, ";"),
];
let mut parser = Parser::new(tokens);
let result = get_statement(&mut parser);
assert!(result.is_ok());
let statement = result.unwrap();
assert_eq!(statement, SelectStatement {
table_name: "guests".to_string(),
mode: SelectMode::Distinct,
columns: SelectStatementColumns::Specific(vec![
"id".to_string(),
]),
where_clause: None,
order_by_clause: None,
limit_clause: None,
});
}
}
Loading
Loading