#include "usql.h" #include "exception.h" #include "ml_date.h" #include #include namespace usql { std::unique_ptr USql::execute(const std::string &command) { try { std::unique_ptr node = m_parser.parse(command); return execute(*node); } catch (std::exception &e) { return create_stmt_result_table(-1, e.what(), 0); } } std::unique_ptr
USql::execute(Node &node) { // TODO optimize execution nodes here switch (node.node_type) { case NodeType::create_table: return execute_create_table(static_cast(node)); case NodeType::create_table_as_select: return execute_create_table_as_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)); case NodeType::delete_from: return execute_delete(static_cast(node)); case NodeType::update_table: return execute_update(static_cast(node)); case NodeType::load_table: return execute_load(static_cast(node)); case NodeType::save_table: return execute_save(static_cast(node)); case NodeType::drop_table: return execute_drop(static_cast(node)); default: return create_stmt_result_table(-1, "unknown statement", 0); } } std::unique_ptr
USql::execute_create_table(CreateTableNode &node) { check_table_not_exists(node.table_name); Table table{node.table_name, node.cols_defs}; m_tables.push_back(table); return create_stmt_result_table(0, "table created", 0); } std::unique_ptr
USql::execute_create_table_as_table(CreateTableAsSelectNode &node) { check_table_not_exists(node.table_name); auto select = execute_select((SelectFromTableNode &) *node.select_table); // create table Table new_table{node.table_name, select->m_col_defs}; m_tables.push_back(new_table); // copy rows // must be here, if rows are put into new_table, they are lost during m_tables.push_table Table *table = find_table(node.table_name); for( Row& orig_row : select->m_rows) { table->add_copy_of_row(orig_row); } select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode return create_stmt_result_table(0, "table created", table->m_rows.size()); } std::unique_ptr
USql::execute_load(LoadIntoTableNode &node) { // find source table Table *table_def = find_table(node.table_name); // read data std::ifstream ifs(node.filename); std::string content((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); // load rows auto rows_cnt = table_def->load_csv_string(content); return create_stmt_result_table(0, "load succeeded", rows_cnt); } std::unique_ptr
USql::execute_save(SaveTableNode &node) { // find source table Table *table_def = find_table(node.table_name); // make csv string std::string csv_string = table_def->csv_string(); // save data std::ofstream file(node.filename); file << csv_string; file.close(); return create_stmt_result_table(0, "save succeeded", table_def->rows_count()); } std::unique_ptr
USql::execute_drop(DropTableNode &node) { auto name_cmp = [node](const Table& t) { return t.m_name == node.table_name; }; auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp); if (table_def != std::end(m_tables)) { m_tables.erase(table_def); return create_stmt_result_table(0, "drop succeeded", 0); } throw Exception("table not found (" + node.table_name + ")"); } std::unique_ptr
USql::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->create_empty_row(); // copy values for (size_t i = 0; i < node.cols_names.size(); i++) { ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name); auto col_value = eval_value_node(table_def, new_row, node.cols_values[i].get()); new_row.setColumnValue(&col_def, col_value.get()); } // append new_row table_def->add_row(new_row); return create_stmt_result_table(0, "insert succeeded", 1); } std::unique_ptr
USql::execute_select(SelectFromTableNode &node) { // 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{}; for (int i = 0; i < node.cols_names->size(); i++) { auto [ src_tbl_col_index, rst_tbl_col_def ] = get_column_definition(table, &node.cols_names->operator[](i), i); source_table_col_index.push_back(src_tbl_col_index); result_tbl_col_defs.push_back(rst_tbl_col_def); } auto result = std::make_unique
("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 (eval_where(node.where.get(), table, *row)) { // prepare empty row Row new_row = result->create_empty_row(); // copy column values for (auto idx = 0; idx < result->columns_count(); idx++) { auto row_col_index = source_table_col_index[idx]; if (row_col_index == -1) { // TODO introduce constant here auto evaluated_value = eval_value_node(table, *row, node.cols_names->operator[](idx).value.get()); ValueNode *col_value = evaluated_value.get(); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value); } else { ColValue *col_value = row->ith_column(row_col_index); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value); } } // add row to result result->m_rows.push_back(new_row); } } // order by execute_order_by(node, table, result.get()); // offset & limit execute_offset_limit(node.offset_limit, result.get()); return std::move(result); } void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *result) const { if (node.order_by.size() == 0) return; auto compare_rows = [&node, &result, this](const Row &a, const Row &b) { for(auto order_by_col_def : node.order_by) { ColDefNode col_def = result->get_column_def(order_by_col_def.col_index - 1); // TODO validate index ColValue *a_val = a.ith_column(col_def.order); ColValue *b_val = b.ith_column(col_def.order); if (a_val->isNull() && b_val->isNull()) return true; // both is null so a goes to end if (!a_val->isNull() && b_val->isNull()) return true; // b is null so goes to end if (a_val->isNull() && !b_val->isNull()) return false; // a is null so goes to end int compare = compare_col_values(col_def, a_val, b_val); if (compare < 0) return order_by_col_def.ascending ? true : false; if (compare > 0) return order_by_col_def.ascending ? false : true; } return false; }; std::sort(result->m_rows.begin(), result->m_rows.end(), compare_rows); } void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) const { if (node.offset > 0) result->m_rows.erase(result->m_rows.begin(), result->rows_count() > node.offset ? result->m_rows.begin() + node.offset : result->m_rows.end()); if (node.limit > 0 && node.limit < result->rows_count()) result->m_rows.erase(result->m_rows.begin() + node.limit, result->m_rows.end()); } int USql::compare_col_values(const ColDefNode &col_def, ColValue *a_val, ColValue *b_val) const { double c; switch (col_def.type) { case (ColumnType::integer_type): return a_val->getIntValue() - b_val->getIntValue(); case (ColumnType::float_type): c = a_val->getDoubleValue() - b_val->getDoubleValue(); return c < 0 ? -1 : c==0.0 ? 0 : 1; case (ColumnType::varchar_type): return a_val->getStringValue().compare(b_val->getStringValue()); default: throw Exception("Unsupported data type"); } } std::tuple USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) { std::string new_col_name = select_col_node->name; if (select_col_node->value->node_type == NodeType::column_name) { ColDefNode src_cdef = table->get_column_def(new_col_name); ColDefNode cdef = ColDefNode{new_col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null}; return std::make_tuple(src_cdef.order, cdef); } else if (select_col_node->value->node_type == NodeType::function) { auto node = static_cast(select_col_node->value.get()); if (node->function == "to_string") { ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, col_order, 32, true}; return std::make_tuple(-1, cdef); } else if (node->function == "to_date") { ColDefNode cdef = ColDefNode{new_col_name, ColumnType::integer_type, col_order, 1, true}; return std::make_tuple(-1, cdef); } throw Exception("Unsupported function"); } throw Exception("Unsupported node type"); } std::unique_ptr
USql::execute_delete(DeleteFromTableNode &node) { // find source table Table *table = find_table(node.table_name); // execute access plan int affected_rows = table->rows_count(); table->m_rows.erase( std::remove_if(table->m_rows.begin(), table->m_rows.end(), [&node, table, this](Row &row){return eval_where(node.where.get(), table, row);}), table->m_rows.end()); affected_rows -= table->rows_count(); return create_stmt_result_table(0, "delete succeeded", affected_rows); } std::unique_ptr
USql::execute_update(UpdateTableNode &node) { // find source table Table *table = find_table(node.table_name); // execute access plan int affected_rows = 0; for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) { // eval where for row if (eval_where(node.where.get(), table, *row)) { int i = 0; for (const auto& col : node.cols_names) { ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select std::unique_ptr new_val = eval_arithmetic_operator(col_def.type, static_cast(*node.values[i]), table, *row); table->validate_column(&col_def, new_val.get()); row->setColumnValue(&col_def, new_val.get()); i++; } affected_rows++; // TODO tady je problem, ze kdyz to zfajluje na jednom radku ostatni by se nemely provest } } return create_stmt_result_table(0, "update succeeded", affected_rows); } bool USql::eval_where(Node *where, Table *table, Row &row) const { switch (where->node_type) { // no where clause case NodeType::true_node: return true; case NodeType::relational_operator: // just one condition return eval_relational_operator(*((RelationalOperatorNode *) where), table, row); case NodeType::logical_operator: return eval_logical_operator(*((LogicalOperatorNode *) where), table, row); default: throw Exception("Wrong node type"); } return false; } bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const { std::unique_ptr left_value = eval_value_node(table, row, filter.left.get()); std::unique_ptr right_value = eval_value_node(table, row, filter.right.get()); double comparator; if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) { comparator = left_value->getIntValue() - right_value->getIntValue(); } else if ((left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) || (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) || (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value)) { comparator = left_value->getDoubleValue() - right_value->getDoubleValue(); } else if (left_value->node_type == NodeType::string_value || right_value->node_type == NodeType::string_value) { comparator = left_value->getStringValue().compare(right_value->getStringValue()); } else { // TODO throw exception } 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 USql::eval_value_node(Table *table, Row &row, Node *node) { if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit return eval_database_value_node(table, row, node); } else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) { return eval_literal_value_node(table, row, node); } else if (node->node_type == NodeType::function) { return eval_function_value_node(table, row, node); } else if (node->node_type == NodeType::null_value) { return std::make_unique(); } throw Exception("unsupported node type"); } std::unique_ptr USql::eval_database_value_node(Table *table, Row &row, Node *node) { auto *dvl = static_cast(node); ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once auto db_value = row.ith_column(col_def.order); if (col_def.type == ColumnType::integer_type) { return std::make_unique(db_value->getIntValue()); } if (col_def.type == ColumnType::float_type) { return std::make_unique(db_value->getDoubleValue()); } if (col_def.type == ColumnType::varchar_type) { return std::make_unique(db_value->getStringValue()); } throw Exception("unknown database value type"); } std::unique_ptr USql::eval_literal_value_node(Table *table, Row &row, Node *node) { if (node->node_type == NodeType::int_value) { auto *ivl = static_cast(node); return std::make_unique(ivl->value); } else if (node->node_type == NodeType::float_value) { auto *ivl = static_cast(node); return std::make_unique(ivl->value); } else if (node->node_type == NodeType::string_value) { auto *ivl = static_cast(node); return std::make_unique(ivl->value); } throw Exception("invalid type"); } std::unique_ptr USql::eval_function_value_node(Table *table, Row &row, Node *node) { auto *fnc = static_cast(node); std::vector> evaluatedPars; for(auto & param : fnc->params) { evaluatedPars.push_back(eval_value_node(table, row, param.get())); } // TODO use some enum if (fnc->function == "lower") { std::string str = evaluatedPars[0]->getStringValue(); std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return std::tolower(c); }); return std::make_unique(str); } if (fnc->function == "upper") { std::string str = evaluatedPars[0]->getStringValue(); std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return std::toupper(c); }); return std::make_unique(str); } if (fnc->function == "to_date") { std::string date = evaluatedPars[0]->getStringValue(); std::string format = evaluatedPars[1]->getStringValue(); long epoch_time = string_to_date(date, format); return std::make_unique(epoch_time); } if (fnc->function == "to_string") { long date = evaluatedPars[0]->getIntValue(); std::string format = evaluatedPars[1]->getStringValue(); std::string formated_date = date_to_string(date, format); return std::make_unique(formated_date); } throw Exception("invalid function"); } bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const { bool left = eval_relational_operator(static_cast(*node.left), pTable, row); if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left)) return left; bool right = eval_relational_operator(static_cast(*node.right), pTable, row); return right; } std::unique_ptr USql::eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const { if (node.op == ArithmeticalOperatorType::copy_value) { return eval_value_node(table, row, node.left.get()); } std::unique_ptr left = eval_value_node(table, row, node.left.get()); std::unique_ptr right = eval_value_node(table, row, node.right.get()); if (outType == ColumnType::float_type) { double l = ((ValueNode *) left.get())->getDoubleValue(); double r = ((ValueNode *) right.get())->getDoubleValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); case ArithmeticalOperatorType::minus_operator: return std::make_unique(l - r); case ArithmeticalOperatorType::multiply_operator: return std::make_unique(l * r); case ArithmeticalOperatorType::divide_operator: return std::make_unique(l / r); default: throw Exception("implement me!!"); } } else if (outType == ColumnType::integer_type) { long l = ((ValueNode *) left.get())->getIntValue(); long r = ((ValueNode *) right.get())->getIntValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); case ArithmeticalOperatorType::minus_operator: return std::make_unique(l - r); case ArithmeticalOperatorType::multiply_operator: return std::make_unique(l * r); case ArithmeticalOperatorType::divide_operator: return std::make_unique(l / r); default: throw Exception("implement me!!"); } } else if (outType == ColumnType::varchar_type) { std::string l = ((ValueNode *) left.get())->getStringValue(); std::string r = ((ValueNode *) right.get())->getStringValue(); switch (node.op) { case ArithmeticalOperatorType::plus_operator: return std::make_unique(l + r); default: throw Exception("implement me!!"); } } throw Exception("implement me!!"); } std::unique_ptr
USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) { std::vector result_tbl_col_defs{}; result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false)); result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 48, false)); result_tbl_col_defs.push_back(ColDefNode("affected_rows", ColumnType::integer_type, 0, 1, true)); auto table_def = std::make_unique
("result", result_tbl_col_defs); Row new_row = table_def->create_empty_row(); new_row.setColumnValue(0, code); new_row.setColumnValue(1, text); new_row.setColumnValue(2, affected_rows); table_def->add_row(new_row); return std::move(table_def); } Table *USql::find_table(const std::string &name) { auto name_cmp = [name](const 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 + ")"); } } void USql::check_table_not_exists(const std::string &name) { auto name_cmp = [name](const 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)) { throw Exception("table already exists"); } } } // namespace