330 lines
13 KiB
C++
330 lines
13 KiB
C++
#include "executor.h"
|
|
#include "exception.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
Executor::Executor() {
|
|
m_tables.clear();
|
|
}
|
|
|
|
Table* Executor::find_table(const std::string name) {
|
|
auto name_cmp = [name](Table t){ return t.m_name == name; };
|
|
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp );
|
|
if (table_def != std::end(m_tables)) {
|
|
return table_def.operator->();
|
|
} else {
|
|
throw Exception("table not found (" + name + ")");
|
|
}
|
|
}
|
|
|
|
|
|
bool Executor::execute(Node& node) {
|
|
// TODO optimize node here
|
|
switch (node.node_type) {
|
|
case NodeType::create_table:
|
|
return execute_create_table(static_cast<CreateTableNode &>(node));
|
|
case NodeType::insert_into:
|
|
return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node));
|
|
case NodeType::select_from:
|
|
return execute_select(static_cast<SelectFromTableNode &>(node));
|
|
case NodeType::delete_from:
|
|
return execute_delete(static_cast<DeleteFromTableNode &>(node));
|
|
case NodeType::update_table:
|
|
return execute_update(static_cast<UpdateTableNode&>(node));
|
|
default:
|
|
// TODO error message
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
bool Executor::execute_create_table(CreateTableNode& node) {
|
|
// TODO check table does not exists
|
|
Table table{node.table_name, node.cols_defs};
|
|
m_tables.push_back(table);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
|
|
// TODO check column names.size = values.size
|
|
|
|
// find table
|
|
Table* table_def = find_table(node.table_name);
|
|
|
|
// prepare empty new_row
|
|
Row new_row = table_def->createEmptyRow();
|
|
|
|
// copy values
|
|
for(size_t i=0; i<node.cols_names.size(); i++) {
|
|
auto colNameNode = node.cols_names[i];
|
|
ColDefNode col_def = table_def->get_column_def(colNameNode.name);
|
|
|
|
// TODO validate value
|
|
|
|
if (col_def.type == ColumnType::integer_type) {
|
|
new_row.setColumnValue(col_def.order, std::stoi(node.cols_values[i].value));
|
|
} else if (col_def.type == ColumnType::float_type) {
|
|
new_row.setColumnValue(col_def.order, std::stof(node.cols_values[i].value));
|
|
} else {
|
|
new_row.setColumnValue(col_def.order, node.cols_values[i].value);
|
|
}
|
|
}
|
|
|
|
// TODO check not null columns
|
|
|
|
// append new_row
|
|
table_def->addRow(new_row);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_select(SelectFromTableNode& node) {
|
|
// TODO create plan for accessing rows
|
|
|
|
// find source table
|
|
Table* table = find_table(node.table_name);
|
|
|
|
// create result table
|
|
std::vector<ColDefNode> result_tbl_col_defs{};
|
|
std::vector<int> source_table_col_index{};
|
|
int i = 0; // new column order
|
|
for(ColNameNode rc : node.cols_names) {
|
|
ColDefNode cdef = table->get_column_def(rc.name);
|
|
source_table_col_index.push_back(cdef.order);
|
|
|
|
auto col = ColDefNode(rc.name, cdef.type, i, cdef.length, cdef.null);
|
|
result_tbl_col_defs.push_back(col);
|
|
|
|
i++;
|
|
}
|
|
Table result {"result", result_tbl_col_defs};
|
|
|
|
// execute access plan
|
|
for (auto row = begin (table->m_rows); row != end (table->m_rows); ++row) {
|
|
// eval where for row
|
|
if (evalWhere(node.where.get(), table, row)) {
|
|
// prepare empty row
|
|
Row new_row = result.createEmptyRow();
|
|
|
|
// copy column values
|
|
for(auto idx=0; idx<result.columns_count(); idx++) {
|
|
auto row_col_index = source_table_col_index[idx];
|
|
ColValue *col_value = row->ithColumn(row_col_index);
|
|
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
|
|
new_row.setColumnValue(idx, ((ColIntegerValue*)col_value)->integerValue());
|
|
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
|
|
new_row.setColumnValue(idx, col_value->floatValue());
|
|
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
|
|
new_row.setColumnValue(idx, col_value->stringValue());
|
|
}
|
|
|
|
// add row to result
|
|
result.m_rows.push_back(new_row);
|
|
}
|
|
}
|
|
|
|
result.print();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_delete(DeleteFromTableNode& node) {
|
|
// TODO create plan for accessing rows
|
|
|
|
// find source table
|
|
Table* table = find_table(node.table_name);
|
|
|
|
// execute access plan
|
|
auto it = table->m_rows.begin();
|
|
for ( ; it != table->m_rows.end(); ) {
|
|
if (evalWhere(node.where.get(), table, it)) {
|
|
// TODO this can be really expensive operation
|
|
it = table->m_rows.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Executor::execute_update(UpdateTableNode &node) {
|
|
// TODO create plan for accessing rows
|
|
|
|
// find source table
|
|
Table* table = find_table(node.table_name);
|
|
|
|
// execute access plan
|
|
for (auto row = begin (table->m_rows); row != end (table->m_rows); ++row) {
|
|
// eval where for row
|
|
if (evalWhere(node.where.get(), table, row)) {
|
|
// TODO do update
|
|
int i = 0;
|
|
for(auto col : node.cols_names) {
|
|
// TODO cache it like in select
|
|
ColDefNode cdef = table->get_column_def(col.name);
|
|
|
|
std::unique_ptr<Node> new_val = evalArithmetic(static_cast<ArithmeticalOperatorNode &>(*node.values[i]), table, row);
|
|
|
|
if (cdef.type == ColumnType::integer_type) {
|
|
row->setColumnValue(cdef.order, ((IntValueNode*)new_val.get())->value);
|
|
} else if (cdef.type == ColumnType::float_type) {
|
|
row->setColumnValue(cdef.order, ((FloatValueNode*)new_val.get())->value);
|
|
} else {
|
|
throw Exception("Implement me!");
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Executor::evalWhere(Node *where, Table *table,
|
|
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
|
switch (where->node_type) { // no where clause
|
|
case NodeType::true_node:
|
|
return true;
|
|
case NodeType::relational_operator: // just one condition
|
|
return evalRelationalOperator(*((RelationalOperatorNode *)where), table, row);
|
|
case NodeType::logical_operator:
|
|
return evalLogicalOperator(*((LogicalOperatorNode *)where), table, row);
|
|
default:
|
|
throw Exception("Wrong node type");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
|
std::unique_ptr<Node> left_value = evalNode(table, row, filter.left.get());
|
|
std::unique_ptr<Node> right_value = evalNode(table, row, filter.right.get());
|
|
|
|
double comparator;
|
|
|
|
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) {
|
|
auto lvalue = static_cast<IntValueNode *>(left_value.get());
|
|
auto rvalue = static_cast<IntValueNode *>(right_value.get());
|
|
comparator = lvalue->value - rvalue->value;
|
|
}
|
|
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) {
|
|
auto *lvalue = static_cast<IntValueNode *>(left_value.get());
|
|
auto *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
|
comparator = (double)lvalue->value - rvalue->value;
|
|
}
|
|
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::string_value) {
|
|
auto *lvalue = static_cast<IntValueNode *>(left_value.get());
|
|
auto *rvalue = static_cast<StringValueNode *>(right_value.get());
|
|
comparator = std::to_string(lvalue->value).compare(rvalue->value);
|
|
}
|
|
|
|
|
|
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) {
|
|
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
|
auto *rvalue = static_cast<IntValueNode *>(right_value.get());
|
|
comparator = lvalue->value - (double)rvalue->value;
|
|
}
|
|
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value) {
|
|
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
|
auto *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
|
comparator = lvalue->value - rvalue->value;
|
|
}
|
|
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::string_value) {
|
|
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
|
auto *rvalue = static_cast<StringValueNode *>(right_value.get());
|
|
comparator = std::to_string(lvalue->value).compare(rvalue->value);
|
|
}
|
|
|
|
|
|
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::int_value) {
|
|
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
|
IntValueNode *rvalue = static_cast<IntValueNode *>(right_value.get());
|
|
comparator = lvalue->value.compare(std::to_string(rvalue->value));
|
|
}
|
|
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::float_value) {
|
|
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
|
FloatValueNode *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
|
comparator = lvalue->value.compare(std::to_string(rvalue->value));
|
|
}
|
|
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::string_value) {
|
|
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
|
StringValueNode *rvalue = static_cast<StringValueNode *>(right_value.get());
|
|
comparator = lvalue->value.compare(rvalue->value);
|
|
}
|
|
|
|
|
|
switch (filter.op) {
|
|
case RelationalOperatorType::equal:
|
|
return comparator == 0.0;
|
|
case RelationalOperatorType::not_equal:
|
|
return comparator != 0.0;
|
|
case RelationalOperatorType::greater:
|
|
return comparator > 0.0;
|
|
case RelationalOperatorType::greater_equal:
|
|
return comparator >= 0.0;
|
|
case RelationalOperatorType::lesser:
|
|
return comparator < 0.0;
|
|
case RelationalOperatorType::lesser_equal:
|
|
return comparator <= 0.0;
|
|
}
|
|
|
|
throw Exception("invalid relational operator");
|
|
}
|
|
|
|
std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
|
if (node->node_type == NodeType::database_value) {
|
|
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node);
|
|
ColDefNode col_def = table->get_column_def(dvl->col_name); // TODO optimize it to just get this def once
|
|
auto db_value = row->ithColumn(col_def.order);
|
|
|
|
if (col_def.type == ColumnType::integer_type) {
|
|
return std::make_unique<IntValueNode>(db_value->integerValue());
|
|
}
|
|
if (col_def.type == ColumnType::float_type) {
|
|
return std::make_unique<FloatValueNode>(db_value->floatValue());
|
|
}
|
|
if (col_def.type == ColumnType::varchar_type) {
|
|
return std::make_unique<StringValueNode>(db_value->stringValue());
|
|
}
|
|
|
|
} else if (node->node_type == NodeType::int_value) {
|
|
IntValueNode *ivl = static_cast<IntValueNode *>(node);
|
|
return std::make_unique<IntValueNode>(ivl->value);
|
|
|
|
} else if (node->node_type == NodeType::float_value) {
|
|
FloatValueNode *ivl = static_cast<FloatValueNode*>(node);
|
|
return std::make_unique<FloatValueNode>(ivl->value);
|
|
|
|
} else if (node->node_type == NodeType::string_value) {
|
|
StringValueNode *ivl = static_cast<StringValueNode*>(node);
|
|
return std::make_unique<StringValueNode>(ivl->value);
|
|
}
|
|
|
|
throw Exception("invalid type");
|
|
}
|
|
|
|
bool Executor::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
|
std::vector<Row, std::allocator<Row>>::iterator &iter) const {
|
|
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
|
|
|
|
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
|
|
return left;
|
|
|
|
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter);
|
|
return right;
|
|
}
|
|
|
|
std::unique_ptr<Node> Executor::evalArithmetic(ArithmeticalOperatorNode &node, Table *table,
|
|
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
|
|
|
switch (node.op) {
|
|
case ArithmeticalOperatorType::copy_value:
|
|
return evalNode(table, row, node.left.get());
|
|
default:
|
|
throw Exception("implement me!!");
|
|
}
|
|
} |