usql/executor.cpp

258 lines
10 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));
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, 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->ithColum(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::evalWhere(const SelectFromTableNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const {
if (node.where->node_type == NodeType::true_node) { // no where clause
return true;
}
if (node.where.get()->node_type == NodeType::relational_operator) {
RelationalOperatorNode &filter = static_cast<RelationalOperatorNode &>(*node.where);
return evalRelationalOperator(filter, table, row);
}
// if (node.where.get()->node_type == NodeType::logical_operator) {
// LogicalOperatorNode &filter = static_cast<LogicalOperatorNode &>(*node.where);
// return evalLogicalOperator(filter, table, row);
// }
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) {
IntValueNode *lvalue = static_cast<IntValueNode *>(left_value.get());
IntValueNode *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) {
IntValueNode *lvalue = static_cast<IntValueNode *>(left_value.get());
FloatValueNode *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) {
IntValueNode *lvalue = static_cast<IntValueNode *>(left_value.get());
StringValueNode *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) {
FloatValueNode *lvalue = static_cast<FloatValueNode *>(left_value.get());
IntValueNode *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) {
FloatValueNode *lvalue = static_cast<FloatValueNode *>(left_value.get());
FloatValueNode *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) {
FloatValueNode *lvalue = static_cast<FloatValueNode *>(left_value.get());
StringValueNode *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 *filter) const {
if (filter->node_type == NodeType::database_value) {
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(filter);
ColDefNode col_def = table->get_column_def(dvl->col_name); // TODO optimize it to just get this def once
auto db_value = row->ithColum(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 (filter->node_type == NodeType::int_value) {
IntValueNode *ivl = static_cast<IntValueNode *>(filter);
return std::make_unique<IntValueNode>(ivl->value);
} else if (filter->node_type == NodeType::float_value) {
FloatValueNode *ivl = static_cast<FloatValueNode*>(filter);
return std::make_unique<FloatValueNode>(ivl->value);
} else if (filter->node_type == NodeType::string_value) {
StringValueNode *ivl = static_cast<StringValueNode*>(filter);
return std::make_unique<StringValueNode>(ivl->value);
}
throw Exception("invalid type");
}