#include "executor.h" #include "exception.h" #include 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(node)); case NodeType::insert_into: return execute_insert_into_table(static_cast(node)); case NodeType::select_from: return execute_select(static_cast(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; iget_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 result_tbl_col_defs{}; std::vector 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; idxithColum(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>::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(*node.where); return evalRelationalOperator(filter, table, row); } // if (node.where.get()->node_type == NodeType::logical_operator) { // LogicalOperatorNode &filter = static_cast(*node.where); // return evalLogicalOperator(filter, table, row); // } return false; } bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::vector>::iterator &row) const { std::unique_ptr left_value = evalNode(table, row, filter.left.get()); std::unique_ptr 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(left_value.get()); IntValueNode *rvalue = static_cast(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(left_value.get()); FloatValueNode *rvalue = static_cast(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(left_value.get()); StringValueNode *rvalue = static_cast(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(left_value.get()); IntValueNode *rvalue = static_cast(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(left_value.get()); FloatValueNode *rvalue = static_cast(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(left_value.get()); StringValueNode *rvalue = static_cast(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(left_value.get()); IntValueNode *rvalue = static_cast(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(left_value.get()); FloatValueNode *rvalue = static_cast(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(left_value.get()); StringValueNode *rvalue = static_cast(right_value.get()); comparator = lvalue->value.compare(rvalue->value); } if (filter.op == RelationalOperatorType::greater) { return comparator > 0.0; } if (filter.op == RelationalOperatorType::equal) { return comparator == 0.0; } throw Exception("invalid relational operator"); } std::unique_ptr Executor::evalNode(Table *table, std::vector>::iterator &row, Node *filter) const { if (filter->node_type == NodeType::database_value) { DatabaseValueNode *dvl = static_cast(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(db_value->integerValue()); } if (col_def.type == ColumnType::float_type) { return std::make_unique(db_value->floatValue()); } if (col_def.type == ColumnType::varchar_type) { return std::make_unique(db_value->stringValue()); } } else if (filter->node_type == NodeType::int_value) { IntValueNode *ivl = static_cast(filter); return std::make_unique(ivl->value); } else if (filter->node_type == NodeType::float_value) { FloatValueNode *ivl = static_cast(filter); return std::make_unique(ivl->value); } else if (filter->node_type == NodeType::string_value) { StringValueNode *ivl = static_cast(filter); return std::make_unique(ivl->value); } throw Exception("invalid type"); }