changes here and there

This commit is contained in:
VaclavT 2021-07-12 18:45:51 +02:00
parent 5e69ce1047
commit 5e4480c767
18 changed files with 1692 additions and 1309 deletions

View File

@ -7,15 +7,15 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14") set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
project(msql) project(usql)
set(PROJECT_NAME msql) set(PROJECT_NAME usql)
set(SOURCE set(SOURCE
exception.cpp lexer.cpp parser.cpp executor.cpp main.cpp table.cpp table.h row.cpp row.h) exception.cpp lexer.cpp parser.cpp executor.cpp main.cpp table.cpp table.h row.cpp row.h csvreader.cpp csvreader.h)
add_executable(${PROJECT_NAME} ${SOURCE}) add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} stdc++ m) target_link_libraries(${PROJECT_NAME} stdc++ m)
target_compile_options(msql PRIVATE -g) target_compile_options(usql PRIVATE -g)

View File

@ -1,6 +1,5 @@
### TODO ### TODO
- rename it to usql
- rename Exception to UException, Table to UTable, Row to URow etc - rename Exception to UException, Table to UTable, Row to URow etc
- remove newlines from lexed string tokens - remove newlines from lexed string tokens
- unify using of float and double keywords - unify using of float and double keywords

88
csvreader.cpp Normal file
View File

@ -0,0 +1,88 @@
#include "csvreader.h"
#include <climits>
namespace usql {
CsvReader::CsvReader(bool skip_hdr, char field_sep, char quote_ch, char line_sep, char line_sep2) {
skip_header = skip_hdr;
field_separator = field_sep;
quote_character = quote_ch;
line_separator = line_sep;
line_separator2 = line_sep2;
header_skiped = false;
}
std::vector<std::vector<std::string>> CsvReader::parseCSV(const std::string &csvSource) {
int linesRead = 0;
bool inQuote(false);
bool newLine(false);
std::string field;
std::vector<std::vector<std::string>> parsed_data;
parsed_data.reserve(128);
std::vector<std::string> line;
line.reserve(32);
std::string::const_iterator aChar = csvSource.begin();
while (aChar != csvSource.end()) {
if (*aChar == quote_character) {
newLine = false;
inQuote = !inQuote;
} else if (*aChar == field_separator) {
newLine = false;
if (inQuote == true) {
field += *aChar;
} else {
line.push_back(field);
field.clear();
}
} else if (*aChar == line_separator || *aChar == line_separator2) {
if (inQuote == true) {
field += *aChar;
} else {
if (newLine == false) {
line.push_back(field);
add_line(line, parsed_data);
field.clear();
line.clear();
linesRead++;
if (linesRead == 16) {
int linesEstimation =
csvSource.size() /
(std::distance(csvSource.begin(), aChar) / linesRead);
if (linesEstimation > parsed_data.capacity())
parsed_data.reserve(linesEstimation);
}
newLine = true;
}
}
} else {
newLine = false;
field.push_back(*aChar);
}
aChar++;
}
if (field.size())
line.push_back(field);
add_line(line, parsed_data);
return parsed_data;
}
void CsvReader::add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines) {
if (skip_header && !header_skiped) {
header_skiped = true;
} else {
if (line.size())
lines.push_back(line);
}
}
}

31
csvreader.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <math.h>
#include <string>
#include <vector>
#include <regex>
namespace usql {
class CsvReader {
private:
char field_separator;
char line_separator;
char line_separator2;
char quote_character;
bool skip_header;
bool header_skiped;
public:
CsvReader(bool skip_hdr = false, char field_sep = ',', char quote_ch = '"', char line_sep = '\r',
char line_sep2 = '\n');
std::vector<std::vector<std::string>> parseCSV(const std::string &csvSource);
private:
void add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines);
};
}

3
data.csv Normal file
View File

@ -0,0 +1,3 @@
Ticker,Price
FDX,257.3
C,59.85
1 Ticker Price
2 FDX 257.3
3 C 59.85

View File

@ -1,9 +1,11 @@
#include "exception.h" #include "exception.h"
namespace usql {
Exception::Exception(const std::string &msg) { Exception::Exception(const std::string &msg) {
cause = msg; cause = msg;
} }
const char* Exception::what() const noexcept { return cause.c_str(); } const char *Exception::what() const noexcept { return cause.c_str(); }
}

View File

@ -4,12 +4,16 @@
#include <string> #include <string>
class Exception : public std::exception { namespace usql {
private:
std::string cause;
public: class Exception : public std::exception {
private:
std::string cause;
public:
Exception(const std::string &msg); Exception(const std::string &msg);
const char* what() const noexcept; const char *what() const noexcept;
}; };
}

View File

@ -1,330 +1,419 @@
#include "executor.h" #include "executor.h"
#include "exception.h" #include "exception.h"
#include "csvreader.h"
#include <algorithm> #include <algorithm>
#include <fstream>
namespace usql {
Executor::Executor() { Executor::Executor() {
m_tables.clear(); 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) { Table *Executor::find_table(const std::string name) {
auto *lvalue = static_cast<FloatValueNode *>(left_value.get()); auto name_cmp = [name](Table t) { return t.m_name == name; };
auto *rvalue = static_cast<IntValueNode *>(right_value.get()); auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
comparator = lvalue->value - (double)rvalue->value; if (table_def != std::end(m_tables)) {
} return table_def.operator->();
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value) { } else {
auto *lvalue = static_cast<FloatValueNode *>(left_value.get()); throw Exception("table not found (" + name + ")");
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) { std::unique_ptr<Table> Executor::create_stmt_result_table(int code, std::string text) {
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get()); std::vector<ColDefNode> result_tbl_col_defs{};
IntValueNode *rvalue = static_cast<IntValueNode *>(right_value.get()); result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false));
comparator = lvalue->value.compare(std::to_string(rvalue->value)); result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
}
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::float_value) { auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
FloatValueNode *rvalue = static_cast<FloatValueNode *>(right_value.get()); Row new_row = table_def->createEmptyRow();
comparator = lvalue->value.compare(std::to_string(rvalue->value)); new_row.setColumnValue(0, code);
} new_row.setColumnValue(1, text);
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::string_value) { table_def->addRow(new_row);
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
StringValueNode *rvalue = static_cast<StringValueNode *>(right_value.get()); return std::move(table_def);
comparator = lvalue->value.compare(rvalue->value);
} }
switch (filter.op) { std::unique_ptr<Table> Executor::execute(Node &node) {
case RelationalOperatorType::equal: // TODO optimize execution nodes here
return comparator == 0.0; switch (node.node_type) {
case RelationalOperatorType::not_equal: case NodeType::create_table:
return comparator != 0.0; return execute_create_table(static_cast<CreateTableNode &>(node));
case RelationalOperatorType::greater: case NodeType::insert_into:
return comparator > 0.0; return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node));
case RelationalOperatorType::greater_equal: case NodeType::select_from:
return comparator >= 0.0; return execute_select(static_cast<SelectFromTableNode &>(node));
case RelationalOperatorType::lesser: case NodeType::delete_from:
return comparator < 0.0; return execute_delete(static_cast<DeleteFromTableNode &>(node));
case RelationalOperatorType::lesser_equal: case NodeType::update_table:
return comparator <= 0.0; return execute_update(static_cast<UpdateTableNode &>(node));
case NodeType::load_table:
return execute_load(static_cast<LoadIntoTableNode &>(node));
default:
return create_stmt_result_table(-1, "unknown statement");
}
} }
throw Exception("invalid relational operator");
}
std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const { std::unique_ptr<Table> Executor::execute_create_table(CreateTableNode &node) {
if (node->node_type == NodeType::database_value) { // TODO check table does not exists
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node); Table table{node.table_name, node.cols_defs};
ColDefNode col_def = table->get_column_def(dvl->col_name); // TODO optimize it to just get this def once m_tables.push_back(table);
auto db_value = row->ithColumn(col_def.order);
if (col_def.type == ColumnType::integer_type) { return create_stmt_result_table(0, "table created");
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::unique_ptr<Table> Executor::execute_insert_into_table(InsertIntoTableNode &node) {
std::vector<Row, std::allocator<Row>>::iterator &iter) const { // TODO check column names.size = values.size
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left)) // find table
return left; Table *table_def = find_table(node.table_name);
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter); // prepare empty new_row
return right; Row new_row = table_def->createEmptyRow();
}
std::unique_ptr<Node> Executor::evalArithmetic(ArithmeticalOperatorNode &node, Table *table, // copy values
std::vector<Row, std::allocator<Row>>::iterator &row) const { for (size_t i = 0; i < node.cols_names.size(); i++) {
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
switch (node.op) { // TODO validate value
case ArithmeticalOperatorType::copy_value:
return evalNode(table, row, node.left.get()); if (col_def.type == ColumnType::integer_type) {
default: new_row.setColumnValue(col_def.order, std::stoi(node.cols_values[i].value));
throw Exception("implement me!!"); } 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);
}
}
// append new_row
table_def->addRow(new_row);
return create_stmt_result_table(0, "insert succeded");
} }
std::unique_ptr<Table> 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++;
}
auto result = std::make_unique<Table>("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);
}
}
return std::move(result);
}
std::unique_ptr<Table> 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 create_stmt_result_table(0, "delete succeded");
}
std::unique_ptr<Table> 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)) {
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<ValueNode> new_val = evalArithmetic(cdef.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, row);
if (cdef.type == ColumnType::integer_type) {
row->setColumnValue(cdef.order, new_val->getIntValue());
} else if (cdef.type == ColumnType::float_type) {
row->setColumnValue(cdef.order, new_val->getDoubleValue());
} else if (cdef.type == ColumnType::varchar_type) {
row->setColumnValue(cdef.order, new_val->getStringValue());
} else {
throw Exception("Implement me!");
}
i++;
}
}
}
return create_stmt_result_table(0, "delete succeeded");
}
std::unique_ptr<Table> Executor::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<char>(ifs)),
(std::istreambuf_iterator<char>()));
CsvReader csvparser{};
auto csv = csvparser.parseCSV(content);
std::vector<ColDefNode> &colDefs = table_def->m_col_defs;
for (auto it = csv.begin() + 1; it != csv.end(); ++it) {
std::vector<std::string> csv_line = *it;
// prepare empty new_row
Row new_row = table_def->createEmptyRow();
// copy values
for (size_t i = 0; i < table_def->columns_count(); i++) {
ColDefNode col_def = table_def->get_column_def(colDefs[i].name);
// TODO validate value
if (col_def.type == ColumnType::integer_type) {
new_row.setColumnValue(col_def.order, std::stoi(csv_line[i]));
} else if (col_def.type == ColumnType::float_type) {
new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
} else {
new_row.setColumnValue(col_def.order, csv_line[i]);
}
}
// append new_row
table_def->addRow(new_row);
}
return create_stmt_result_table(0, "load succeeded");
}
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<ValueNode> left_value = evalNode(table, row, filter.left.get());
std::unique_ptr<ValueNode> 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) {
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<ValueNode>
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<ValueNode>
Executor::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const {
if (node.op == ArithmeticalOperatorType::copy_value) {
return evalNode(table, row, node.left.get());
}
std::unique_ptr<ValueNode> left = evalNode(table, row, node.left.get());
std::unique_ptr<ValueNode> right = evalNode(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<FloatValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator:
return std::make_unique<FloatValueNode>(l - r);
case ArithmeticalOperatorType::multiply_operator:
return std::make_unique<FloatValueNode>(l * r);
case ArithmeticalOperatorType::divide_operator:
return std::make_unique<FloatValueNode>(l / r);
default:
throw Exception("implement me!!");
}
} else if (outType == ColumnType::integer_type) {
int l = ((ValueNode *) left.get())->getIntValue();
int r = ((ValueNode *) right.get())->getIntValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator:
return std::make_unique<IntValueNode>(l - r);
case ArithmeticalOperatorType::multiply_operator:
return std::make_unique<IntValueNode>(l * r);
case ArithmeticalOperatorType::divide_operator:
return std::make_unique<IntValueNode>(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<StringValueNode>(l + r);
default:
throw Exception("implement me!!");
}
}
throw Exception("implement me!!");
}
} }

View File

@ -5,38 +5,50 @@
#include <string> #include <string>
class Executor { namespace usql {
private:
public: class Executor {
Executor(); private:
bool execute(Node& node); public:
Executor();
private: std::unique_ptr<Table> execute(Node &node);
bool execute_create_table(CreateTableNode& node);
bool execute_insert_into_table(InsertIntoTableNode& node);
bool execute_select(SelectFromTableNode& node);
bool execute_delete(DeleteFromTableNode& node);
bool execute_update(UpdateTableNode& node);
Table* find_table(const std::string name); private:
private: std::unique_ptr<Table> execute_create_table(CreateTableNode &node);
std::vector<Table> m_tables;
bool evalWhere(Node *where, Table *table, std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
std::vector<Row, std::allocator<Row>>::iterator &row) const;
std::unique_ptr<Node> std::unique_ptr<Table> execute_select(SelectFromTableNode &node);
evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
Node *node) const;
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
std::vector<Row, std::allocator<Row>>::iterator &row) const;
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, std::unique_ptr<Table> execute_update(UpdateTableNode &node);
std::vector<Row, std::allocator<Row>>::iterator &iter) const;
std::unique_ptr<Node> evalArithmetic(ArithmeticalOperatorNode &node, Table *table, std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
std::vector<Row, std::allocator<Row>>::iterator &row) const;
}; Table *find_table(const std::string name);
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
private:
std::vector<Table> m_tables;
bool evalWhere(Node *where, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
Node *node) const;
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
std::vector<Row, std::allocator<Row>>::iterator &iter) const;
std::unique_ptr<ValueNode> evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
};
}

728
lexer.cpp
View File

@ -3,395 +3,415 @@
#include <algorithm> #include <algorithm>
namespace usql {
Token::Token(const std::string &token_str, TokenType typ) {
token_string = token_str;
type = typ;
}
Lexer::Lexer() {
k_words_regex =
"[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/"
",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n";
k_int_regex = "[0-9]+";
k_int_underscored_regex = "[0-9][0-9_]+[0-9]";
k_double_regex = "[0-9]+\\.[0-9]+";
k_identifier_regex = "[A-Za-z]+[A-Za-z0-9_#]*";
}
void Lexer::parse(const std::string &code) {
// TODO handle empty code
m_tokens.clear();
// PERF something like this to preallocate ??
if (code.size() > 100) {
m_tokens.reserve(code.size() / 10);
}
m_code_str = code;
if (!m_code_str.empty() && m_code_str.back() != '\n') {
m_code_str.append("\n"); // TODO temp solution to prevent possible situation when last line is a comment
}
auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), k_words_regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
TokenType token_type = type(match_str);
if (token_type == TokenType::string_literal)
match_str = stringLiteral(match_str);
if (token_type != TokenType::newline)
m_tokens.push_back(Token{match_str, token_type});
}
// DEBUG IT
// debugTokens();
m_index = 0;
}
void Lexer::debugTokens() {
int i = 0;
for (std::vector<Token>::iterator it = m_tokens.begin(); it != m_tokens.end(); ++it) {
std::cerr << i << "\t" << it->token_string << std::endl;
i++;
}
}
Token Lexer::currentToken() { return m_tokens[m_index]; }
Token Lexer::consumeCurrentToken() {
int i = m_index;
nextToken();
return m_tokens[i];
}
void Lexer::nextToken() {
if (m_index < m_tokens.size()) {
m_index++;
}
}
void Lexer::skipToken(TokenType type) {
if (tokenType() == type) {
nextToken();
} else {
throw Exception("ERROR unexpected token " + consumeCurrentToken().token_string + ", instead of " +
typeToString(type));
}
}
Token::Token(const std::string &token_str, TokenType typ) { void Lexer::skipTokenOptional(TokenType type) {
token_string = token_str; if (tokenType() == type) {
type = typ; nextToken();
} }
}
void Lexer::parse(const std::string &code) {
// TODO handle empty code
m_tokens.clear();
// PERF something like this to preallocate ??
if (code.size() > 100) {
m_tokens.reserve(code.size() / 10);
}
m_code_str = code;
if (!m_code_str.empty() && m_code_str.back() != '\n') {
m_code_str.append("\n"); // TODO tempo solution to prevent possible situation when last line is a comment
}
// TODO make it constant
std::regex words_regex("[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/"
",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n");
auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), words_regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
TokenType token_type = type(match_str);
if (token_type == TokenType::string_literal)
match_str = stringLiteral(match_str);
if (token_type != TokenType::newline)
m_tokens.push_back(Token{match_str, token_type});
}
// DEBUG IT
// debugTokens();
m_index = 0;
}
void Lexer::debugTokens() {
int i = 0;
for (std::vector<Token>::iterator it = m_tokens.begin(); it != m_tokens.end(); ++it) {
std::cerr << i << "\t" << it->token_string << std::endl;
i++;
}
}
Token Lexer::currentToken() { return m_tokens[m_index]; } TokenType Lexer::tokenType() { return m_index < m_tokens.size() ? currentToken().type : TokenType::eof; }
Token Lexer::consumeCurrentToken() {
int i = m_index;
nextToken();
return m_tokens[i];
}
void Lexer::nextToken() { TokenType Lexer::nextTokenType() {
if (m_index < m_tokens.size()) { return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof;
m_index++; }
}
}
void Lexer::skipToken(TokenType type) { TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; }
if (tokenType() == type) {
nextToken();
} else {
throw Exception("ERROR unexpected token " + consumeCurrentToken().token_string + ", instead of " + typeToString(type));
}
}
void Lexer::skipTokenOptional(TokenType type) { bool Lexer::isRelationalOperator(TokenType token_type) {
if (tokenType() == type) { return (token_type == TokenType::equal || token_type == TokenType::not_equal ||
nextToken(); token_type == TokenType::greater || token_type == TokenType::greater_equal ||
} token_type == TokenType::lesser || token_type == TokenType::lesser_equal);
} }
TokenType Lexer::tokenType() { return m_index < m_tokens.size() ? currentToken().type : TokenType::eof; } bool Lexer::isLogicalOperator(TokenType token_type) {
return (token_type == TokenType::logical_and || token_type == TokenType::logical_or);
}
TokenType Lexer::nextTokenType() { return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof; } bool Lexer::isArithmeticalOperator(TokenType token_type) {
return (token_type == TokenType::plus || token_type == TokenType::minus ||
token_type == TokenType::multiply ||
token_type == TokenType::divide);
}
TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; } TokenType Lexer::type(const std::string &token) {
// TODO, FIXME 'one is evaluated as identifier
if (token == ";")
return TokenType::semicolon;
bool Lexer::isRelationalOperator(TokenType token_type) { if (token == "+")
return (token_type == TokenType::equal || token_type == TokenType::not_equal || token_type == TokenType::greater || token_type == TokenType::greater_equal || return TokenType::plus;
token_type == TokenType::lesser || token_type == TokenType::lesser_equal);
}
bool Lexer::isLogicalOperator(TokenType token_type) { if (token == "-")
return (token_type == TokenType::logical_and || token_type == TokenType::logical_or); return TokenType::minus;
}
bool Lexer::isArithmeticalOperator(TokenType token_type) { if (token == "*")
return (token_type == TokenType::plus || token_type == TokenType::minus || token_type == TokenType::multiply || token_type == TokenType::divide); return TokenType::multiply;
}
TokenType Lexer::type(const std::string &token) { if (token == "/")
// TODO move it to class level not to reinit it again and again return TokenType::divide;
std::regex int_regex("[0-9]+");
std::regex int_underscored_regex("[0-9][0-9_]+[0-9]");
std::regex double_regex("[0-9]+\\.[0-9]+");
std::regex identifier_regex("[A-Za-z]+[A-Za-z0-9_#]*");
// TODO 'one is evaluated as identifier if (token == "(")
if (token == ";") return TokenType::open_paren;
return TokenType::semicolon;
if (token == "+") if (token == ")")
return TokenType::plus; return TokenType::close_paren;
if (token == "-") if (token == "=")
return TokenType::minus; return TokenType::equal;
if (token == "*") if (token == "!=")
return TokenType::multiply; return TokenType::not_equal;
if (token == "/") if (token == ">")
return TokenType::divide; return TokenType::greater;
if (token == "(") if (token == ">=")
return TokenType::open_paren; return TokenType::greater_equal;
if (token == ")") if (token == "<")
return TokenType::close_paren; return TokenType::lesser;
if (token == "=") if (token == "<=")
return TokenType::equal; return TokenType::lesser_equal;
if (token == "!=") if (token == "create")
return TokenType::not_equal; return TokenType::keyword_create;
if (token == ">") if (token == "where")
return TokenType::greater; return TokenType::keyword_where;
if (token == ">=") if (token == "from")
return TokenType::greater_equal; return TokenType::keyword_from;
if (token == "<") if (token == "delete")
return TokenType::lesser; return TokenType::keyword_delete;
if (token == "<=") if (token == "table")
return TokenType::lesser_equal; return TokenType::keyword_table;
if (token == "create") if (token == "insert")
return TokenType::keyword_create; return TokenType::keyword_insert;
if (token == "where") if (token == "into")
return TokenType::keyword_where; return TokenType::keyword_into;
if (token == "from") if (token == "values")
return TokenType::keyword_from; return TokenType::keyword_values;
if (token == "delete") if (token == "select")
return TokenType::keyword_delete; return TokenType::keyword_select;
if (token == "table") if (token == "set")
return TokenType::keyword_table; return TokenType::keyword_set;
if (token == "insert") if (token == "copy")
return TokenType::keyword_insert; return TokenType::keyword_copy;
if (token == "into") if (token == "update")
return TokenType::keyword_into; return TokenType::keyword_update;
if (token == "values") if (token == "load")
return TokenType::keyword_values; return TokenType::keyword_load;
if (token == "select") if (token == "not")
return TokenType::keyword_select; return TokenType::keyword_not;
if (token == "set") if (token == "null")
return TokenType::keyword_set; return TokenType::keyword_null;
if (token == "copy") if (token == "integer")
return TokenType::keyword_copy; return TokenType::keyword_int;
if (token == "update") if (token == "float")
return TokenType::keyword_update; return TokenType::keyword_float;
if (token == "not") if (token == "varchar")
return TokenType::keyword_not; return TokenType::keyword_varchar;
if (token == "null") if (token == "or")
return TokenType::keyword_null; return TokenType::logical_or;
if (token == "integer") if (token == "and")
return TokenType::keyword_int; return TokenType::logical_and;
if (token == "float") if (token == ",")
return TokenType::keyword_float; return TokenType::comma;
if (token == "varchar") if (token == "\n" || token == "\r\n" || token == "\r")
return TokenType::keyword_varchar; return TokenType::newline;
if (token == "or") if (token.length() > 1 && token.at(0) == '%' &&
return TokenType::logical_or; (token.at(token.length() - 1) == '\n' || token.at(token.length() - 1) == '\r'))
return TokenType::comment;
if (token == "and")
return TokenType::logical_and; // if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"')
// return TokenType::string_literal;
if (token == ",")
return TokenType::comma; if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'')
return TokenType::string_literal;
if (token == "\n" || token == "\r\n" || token == "\r")
return TokenType::newline; if (std::regex_match(token, k_int_regex))
return TokenType::int_number;
if (token.length() > 1 && token.at(0) == '%' && (token.at(token.length() - 1) == '\n' || token.at(token.length() - 1) == '\r'))
return TokenType::comment; if (std::regex_match(token, k_int_underscored_regex))
return TokenType::int_number;
// if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"')
// return TokenType::string_literal; if (std::regex_match(token, k_double_regex))
return TokenType::double_number;
if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'')
return TokenType::string_literal; if (std::regex_match(token, k_identifier_regex))
return TokenType::identifier;
if (std::regex_match(token, int_regex))
return TokenType::int_number; if (m_index + 1 >= m_tokens.size())
return TokenType::eof;
if (std::regex_match(token, int_underscored_regex))
return TokenType::int_number; return TokenType::undef;
}
if (std::regex_match(token, double_regex))
return TokenType::double_number; std::string Lexer::stringLiteral(std::string token) {
// remove ' or " from the literal ends
if (std::regex_match(token, identifier_regex)) bool replace = token[0] == '\'' && token[token.size() - 1] == '\'';
return TokenType::identifier;
std::string str = token.substr(1, token.size() - 2);
if (m_index + 1 >= m_tokens.size()) if (!replace) {
return TokenType::eof; return str;
}
return TokenType::undef; std::string out = "";
} out.reserve(str.size());
std::string Lexer::stringLiteral(std::string token) {
// remove ' or " from the literal ends for (std::string::size_type i = 0; i < str.size(); ++i) {
bool replace = token[0]=='\'' && token[token.size()-1]=='\''; if (str[i] == '\'' && i < str.size() - 1) {
if (str[i + 1] == '\'') {
std::string str = token.substr(1, token.size() - 2); out.append(1, '\'');
if (!replace) { i++;
return str; } else {
} out.append(1, str[i]);
std::string out = ""; }
out.reserve(str.size()); } else if (str[i] == '\\' && i < str.size() - 1) {
if (str[i + 1] == 'n') {
out.append(1, '\n');
for(std::string::size_type i = 0; i < str.size(); ++i) { i++;
if (str[i] == '\'' && i < str.size() - 1) { } else if (str[i + 1] == 't') {
if (str[i+1] == '\'') { out.append(1, '\t');
out.append(1, '\''); i++;
i++; } else {
} else { out.append(1, str[i]);
out.append(1, str[i]); }
} } else {
} else if (str[i] == '\\' && i < str.size() - 1) { out.append(1, str[i]);
if (str[i+1] == 'n') { }
out.append(1, '\n'); }
i++; return out;
} else if (str[i+1] == 't') { }
out.append(1, '\t');
i++; std::string Lexer::typeToString(TokenType token_type) {
} else { std::string txt;
out.append(1, str[i]); switch (token_type) {
} case TokenType::undef:
} else { txt = "undef";
out.append(1, str[i]); break;
} case TokenType::identifier:
} txt = "identifier";
return out; break;
} case TokenType::plus:
txt = "+";
std::string Lexer::typeToString(TokenType token_type) { break;
std::string txt; case TokenType::minus:
switch (token_type) { txt = "-";
case TokenType::undef: break;
txt = "undef"; case TokenType::multiply:
break; txt = "*";
case TokenType::identifier: break;
txt = "identifier"; case TokenType::divide:
break; txt = "/";
case TokenType::plus: break;
txt = "+"; case TokenType::equal:
break; txt = "==";
case TokenType::minus: break;
txt = "-"; case TokenType::not_equal:
break; txt = "!=";
case TokenType::multiply: break;
txt = "*"; case TokenType::greater:
break; txt = ">";
case TokenType::divide: break;
txt = "/"; case TokenType::greater_equal:
break; txt = ">=";
case TokenType::equal: break;
txt = "=="; case TokenType::lesser:
break; txt = "<";
case TokenType::not_equal: break;
txt = "!="; case TokenType::lesser_equal:
break; txt = "<=";
case TokenType::greater: break;
txt = ">"; case TokenType::keyword_create:
break; txt = "create";
case TokenType::greater_equal: break;
txt = ">="; case TokenType::keyword_where:
break; txt = "where";
case TokenType::lesser: break;
txt = "<"; case TokenType::keyword_table:
break; txt = "table";
case TokenType::lesser_equal: break;
txt = "<="; case TokenType::keyword_into:
break; txt = "into";
case TokenType::keyword_create: break;
txt = "create"; case TokenType::keyword_values:
break; txt = "values";
case TokenType::keyword_where: break;
txt = "where"; case TokenType::keyword_select:
break; txt = "select";
case TokenType::keyword_table: break;
txt = "table"; case TokenType::keyword_set:
break; txt = "set";
case TokenType::keyword_into: break;
txt = "into"; case TokenType::keyword_copy:
break; txt = "copy";
case TokenType::keyword_values: break;
txt = "values"; case TokenType::keyword_update:
break; txt = "update";
case TokenType::keyword_select: break;
txt = "select"; case TokenType::keyword_load:
break; txt = "load";
case TokenType::keyword_set: break;
txt = "set"; case TokenType::keyword_not:
break; txt = "not";
case TokenType::keyword_copy: break;
txt = "copy"; case TokenType::keyword_null:
break; txt = "null";
case TokenType::keyword_not: break;
txt = "not"; case TokenType::keyword_int:
break; txt = "integer";
case TokenType::keyword_null: break;
txt = "null"; case TokenType::keyword_float:
break; txt = "float";
case TokenType::keyword_int: break;
txt = "integer"; case TokenType::keyword_varchar:
break; txt = "varchar";
case TokenType::keyword_float: break;
txt = "float"; case TokenType::int_number:
break; txt = "int number";
case TokenType::keyword_varchar: break;
txt = "varchar"; case TokenType::double_number:
break; txt = "double number";
case TokenType::int_number: break;
txt = "int number"; case TokenType::string_literal:
break; txt = "string literal";
case TokenType::double_number: break;
txt = "double number"; case TokenType::open_paren:
break; txt = "(";
case TokenType::string_literal: break;
txt = "string literal"; case TokenType::close_paren:
break; txt = ")";
case TokenType::open_paren: break;
txt = "("; case TokenType::logical_and:
break; txt = "and";
case TokenType::close_paren: break;
txt = ")"; case TokenType::logical_or:
break; txt = "or";
case TokenType::logical_and: break;
txt = "and"; case TokenType::semicolon:
break; txt = ";";
case TokenType::logical_or: break;
txt = "or"; case TokenType::comma:
break; txt = ",";
case TokenType::semicolon: break;
txt = ";"; case TokenType::newline:
break; txt = "newline";
case TokenType::comma: break;
txt = ","; case TokenType::comment:
break; txt = "comment";
case TokenType::newline: break;
txt = "newline"; case TokenType::eof:
break; txt = "eof";
case TokenType::comment: break;
txt = "comment"; default:
break; txt = "FIXME, unknown token type";
case TokenType::eof: break;
txt = "eof"; }
break; return txt;
default: }
txt = "FIXME, unknown token type";
break; }
}
return txt;
}

58
lexer.h
View File

@ -5,7 +5,9 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
enum class TokenType { namespace usql {
enum class TokenType {
undef, undef,
identifier, identifier,
plus, plus,
@ -21,8 +23,9 @@ enum class TokenType {
keyword_create, keyword_create,
keyword_table, keyword_table,
keyword_where, keyword_where,
keyword_delete, keyword_delete,
keyword_update, keyword_update,
keyword_load,
keyword_from, keyword_from,
keyword_insert, keyword_insert,
keyword_into, keyword_into,
@ -48,46 +51,63 @@ enum class TokenType {
newline, newline,
comment, comment,
eof eof
}; };
struct Token { struct Token {
std::string token_string; std::string token_string;
TokenType type; TokenType type;
Token(const std::string &token_str, TokenType typ);
};
class Lexer { Token(const std::string &token_str, TokenType typ);
public: };
Lexer() {};
class Lexer {
public:
Lexer();
void parse(const std::string &code); void parse(const std::string &code);
void debugTokens(); void debugTokens();
Token currentToken(); Token currentToken();
Token consumeCurrentToken(); Token consumeCurrentToken();
void nextToken(); void nextToken();
void skipToken(TokenType type); void skipToken(TokenType type);
void skipTokenOptional(TokenType type); void skipTokenOptional(TokenType type);
TokenType tokenType(); TokenType tokenType();
TokenType nextTokenType(); TokenType nextTokenType();
TokenType prevTokenType(); TokenType prevTokenType();
static bool isRelationalOperator(TokenType token_type); static bool isRelationalOperator(TokenType token_type);
static bool isLogicalOperator(TokenType token_type);
static bool isLogicalOperator(TokenType token_type);
static bool isArithmeticalOperator(TokenType token_type); static bool isArithmeticalOperator(TokenType token_type);
private: private:
TokenType type(const std::string &token); TokenType type(const std::string &token);
std::string stringLiteral(std::string token); std::string stringLiteral(std::string token);
static std::string typeToString(TokenType token_type); static std::string typeToString(TokenType token_type);
private: private:
std::string m_code_str; std::string m_code_str;
std::vector<Token> m_tokens; std::vector<Token> m_tokens;
int m_index = 0; int m_index = 0;
};
std::regex k_words_regex;
std::regex k_int_regex;
std::regex k_int_underscored_regex;
std::regex k_double_regex;
std::regex k_identifier_regex;
};
}

View File

@ -8,34 +8,41 @@
// drop table // drop table
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Parser parser{}; usql::Parser parser{};
Executor executor{}; usql::Executor executor{};
std::vector<std::string> sql_commands { std::vector<std::string> sql_commands{
"create table a (i integer not null, s varchar(64), f float null)", "create table a (i integer not null, s varchar(64), f float null)",
"insert into a (i, s) values(1, 'one')", "insert into a (i, s) values(1, 'one')",
"insert into a (i, s) values(2, 'two')", "insert into a (i, s) values(2, 'two')",
"insert into a (i, s) values(3, 'two')", "insert into a (i, s) values(3, 'two')",
"insert into a (i, s) values(4, 'four')", "insert into a (i, s) values(4, 'four')",
"insert into a (i, s) values(5, 'five')", "insert into a (i, s) values(5, 'five')",
"select i, s from a where i > 2", "select i, s from a where i > 2",
"select i, s from a where i = 1", "select i, s from a where i = 1",
"select i, s from a where s = 'two'", "select i, s from a where s = 'two'",
"select i, s from a where i <= 3 and s = 'one'", "select i, s from a where i <= 3 and s = 'one'",
"select i, s from a where i > 0", "select i, s from a where i > 0",
"delete from a where i = 4", "delete from a where i = 4",
"select i, s from a where i > 0", "select i, s from a where i > 0",
"update a set f = 9.99 where i = 3", "update a set f = 9.99 where i = 3",
// "update a set s = 'three', f = 1.0 + 2.0 where i = 3", "select i, s, f from a where i = 3",
"select i, s, f from a where i = 3" "update a set s = 'three', f = f + 0.01 where i = 3",
// "select i, s from a where i > 0" "select i, s, f from a where i = 3",
}; "create table data (ticker varchar(8), price float null)",
"load data from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/data.csv')",
"select ticker, price from data"
};
for(auto command : sql_commands) { for (auto command : sql_commands) {
auto node = parser.parse(command); std::cout << command << std::endl;
executor.execute(*node); auto node = parser.parse(command);
} auto result = executor.execute(*node);
result->print();
// std::cout << std::endl;
}
return 0; return 0;
} }

View File

@ -1,279 +1,314 @@
#include "parser.h" #include "parser.h"
#include "exception.h" #include "exception.h"
namespace usql {
// TOOD handle premature eof // TOOD handle premature eof
Parser::Parser() { Parser::Parser() {
lexer = Lexer{}; lexer = Lexer{};
}
std::unique_ptr<Node> Parser::parse(const std::string &code) {
lexer.parse(code);
// lexer.debugTokens();
if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
return parse_create_table();
} if (lexer.tokenType() == TokenType::keyword_insert) {
return parse_insert_into_table();
} if (lexer.tokenType() == TokenType::keyword_select) {
return parse_select_from_table();
} if (lexer.tokenType() == TokenType::keyword_delete) {
return parse_delete_from_table();
} if (lexer.tokenType() == TokenType::keyword_update) {
return parse_update_table();
}
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error);
}
std::unique_ptr<Node> Parser::parse_create_table() {
std::vector<ColDefNode> cols_def {};
lexer.skipToken(TokenType::keyword_create);
lexer.skipToken(TokenType::keyword_table);
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
ColumnType column_type;
int column_len {1};
bool column_nullable {true};
// column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
// column type and optionaly len
if (lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
if (lexer.tokenType() == TokenType::keyword_not) {
lexer.nextToken();
lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (lexer.tokenType() == TokenType::keyword_null) {
lexer.nextToken();
}
cols_def.push_back(ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
lexer.skipTokenOptional(TokenType::comma);
// TODO in future constraints
} while (lexer.tokenType() != TokenType::close_paren);
return std::make_unique<CreateTableNode>(table_name, cols_def);
}
std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<Node> exec_code {};
std::vector<ColNameNode> cols_names {};
std::vector<ColValueNode> cols_values {};
lexer.skipToken(TokenType::keyword_insert);
lexer.skipToken(TokenType::keyword_into);
// table name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
// column names
lexer.skipToken(TokenType::open_paren);
do {
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
lexer.skipToken(TokenType::keyword_values);
// column values
lexer.skipToken(TokenType::open_paren);
do {
cols_values.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values);
}
std::unique_ptr<Node> Parser::parse_select_from_table() {
std::vector<ColNameNode> cols_names {};
lexer.skipToken(TokenType::keyword_select);
while (lexer.tokenType() != TokenType::keyword_from) {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} }
lexer.skipToken(TokenType::keyword_from); std::unique_ptr<Node> Parser::parse(const std::string &code) {
std::string table_name = lexer.consumeCurrentToken().token_string; lexer.parse(code);
// lexer.debugTokens();
std::unique_ptr<Node> where_node = parse_where_clause(); if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
return parse_create_table();
}
if (lexer.tokenType() == TokenType::keyword_insert) {
return parse_insert_into_table();
}
if (lexer.tokenType() == TokenType::keyword_select) {
return parse_select_from_table();
}
if (lexer.tokenType() == TokenType::keyword_delete) {
return parse_delete_from_table();
}
if (lexer.tokenType() == TokenType::keyword_update) {
return parse_update_table();
}
if (lexer.tokenType() == TokenType::keyword_load) {
return parse_load_table();
}
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error);
}
std::unique_ptr<Node> Parser::parse_create_table() {
std::vector<ColDefNode> cols_def{};
lexer.skipToken(TokenType::keyword_create);
lexer.skipToken(TokenType::keyword_table);
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
ColumnType column_type;
int column_len{1};
bool column_nullable{true};
// column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
// column type and optionally len
if (lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
if (lexer.tokenType() == TokenType::keyword_not) {
lexer.nextToken();
lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (lexer.tokenType() == TokenType::keyword_null) {
lexer.nextToken();
}
cols_def.push_back(
ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
lexer.skipTokenOptional(TokenType::comma);
// TODO in future constraints
} while (lexer.tokenType() != TokenType::close_paren);
return std::make_unique<CreateTableNode>(table_name, cols_def);
}
std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<Node> exec_code{};
std::vector<ColNameNode> cols_names{};
std::vector<ColValueNode> cols_values{};
lexer.skipToken(TokenType::keyword_insert);
lexer.skipToken(TokenType::keyword_into);
// table name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
// column names
lexer.skipToken(TokenType::open_paren);
do {
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
lexer.skipToken(TokenType::keyword_values);
// column values
lexer.skipToken(TokenType::open_paren);
do {
cols_values.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values);
}
std::unique_ptr<Node> Parser::parse_select_from_table() {
std::vector<ColNameNode> cols_names{};
lexer.skipToken(TokenType::keyword_select);
while (lexer.tokenType() != TokenType::keyword_from) {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
}
lexer.skipToken(TokenType::keyword_from);
std::string table_name = lexer.consumeCurrentToken().token_string;
std::unique_ptr<Node> where_node = parse_where_clause();
// if (lexer.tokenType() == TokenType::keyword_order_by) {} // if (lexer.tokenType() == TokenType::keyword_order_by) {}
// if (lexer.tokenType() == TokenType::keyword_offset) {} // if (lexer.tokenType() == TokenType::keyword_offset) {}
// if (lexer.tokenType() == TokenType::keyword_limit) {} // if (lexer.tokenType() == TokenType::keyword_limit) {}
return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node)); return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node));
}
std::unique_ptr<Node> Parser::parse_delete_from_table() {
lexer.skipToken(TokenType::keyword_delete);
lexer.skipToken(TokenType::keyword_from);
std::string table_name = lexer.consumeCurrentToken().token_string;
std::unique_ptr<Node> where_node = parse_where_clause();
return std::make_unique<DeleteFromTableNode>(table_name, std::move(where_node));
}
std::unique_ptr<Node> Parser::parse_update_table() {
lexer.skipToken(TokenType::keyword_update);
lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::keyword_set);
std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
do {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node();
if (Lexer::isArithmeticalOperator(lexer.tokenType())) {
ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node();
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left), std::move(right)));
} else {
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0);
values.push_back(std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value, std::move(left), std::move(right)));
}
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::keyword_where && lexer.tokenType() != TokenType::eof);
std::unique_ptr<Node> where_node = parse_where_clause();
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
}
std::unique_ptr<Node> Parser::parse_where_clause() {
// TODO add support for multiple filters
// TODO add support for parenthesis
if (lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>();
} }
std::unique_ptr<Node> node; std::unique_ptr<Node> Parser::parse_delete_from_table() {
lexer.skipToken(TokenType::keyword_where); lexer.skipToken(TokenType::keyword_delete);
do { lexer.skipToken(TokenType::keyword_from);
node = parse_relational_expression();
if (Lexer::isLogicalOperator(lexer.tokenType())) { std::string table_name = lexer.consumeCurrentToken().token_string;
auto operation = parse_logical_operator();
std::unique_ptr<Node> node2 = parse_relational_expression();
node = std::make_unique<LogicalOperatorNode>(operation, std::move(node), std::move(node2));
}
} while (lexer.tokenType() != TokenType::eof); // until whole where clause parsed
return node; std::unique_ptr<Node> where_node = parse_where_clause();
}
std::unique_ptr<Node> Parser::parse_relational_expression() { return std::make_unique<DeleteFromTableNode>(table_name, std::move(where_node));
auto left = parse_operand_node();
auto operation = parse_relational_operator();
auto right = parse_operand_node();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
}
std::unique_ptr<Node> Parser::parse_operand_node() {
// while not end or order or limit
auto token_type = lexer.tokenType();
std::string tokenString = lexer.consumeCurrentToken().token_string;
switch (token_type) {
case TokenType::int_number:
return std::make_unique<IntValueNode>(std::stoi(tokenString));
case TokenType::double_number:
return std::make_unique<FloatValueNode>(std::stod(tokenString));
case TokenType::string_literal:
return std::make_unique<StringValueNode>(tokenString);
case TokenType::identifier:
return std::make_unique<DatabaseValueNode>(tokenString);
default: ;
throw Exception("Unknown operand node");
} }
}
RelationalOperatorType Parser::parse_relational_operator() { std::unique_ptr<Node> Parser::parse_update_table() {
auto op = lexer.consumeCurrentToken(); lexer.skipToken(TokenType::keyword_update);
switch (op.type) { lexer.skipTokenOptional(TokenType::keyword_table);
case TokenType::equal:
return RelationalOperatorType::equal;
case TokenType::not_equal:
return RelationalOperatorType::not_equal;
case TokenType::greater:
return RelationalOperatorType::greater;
case TokenType::greater_equal:
return RelationalOperatorType::greater_equal;
case TokenType::lesser:
return RelationalOperatorType::lesser;
case TokenType::lesser_equal:
return RelationalOperatorType::lesser_equal;
default:
throw Exception("Unknown relational operator");
}
}
LogicalOperatorType Parser::parse_logical_operator() {
auto op = lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::logical_and:
return LogicalOperatorType::and_operator;
case TokenType::logical_or:
return LogicalOperatorType::or_operator;
default:
throw Exception("Unknown logical operator");
}
}
ArithmeticalOperatorType Parser::parse_arithmetical_operator() { std::string table_name = lexer.consumeCurrentToken().token_string;
auto op = lexer.consumeCurrentToken();
switch (op.type) { lexer.skipToken(TokenType::keyword_set);
case TokenType::plus:
return ArithmeticalOperatorType::plus_operator; std::vector<ColNameNode> cols_names;
default: std::vector<std::unique_ptr<Node>> values;
throw Exception("Unknown arithmetical operator");
do {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node();
if (Lexer::isArithmeticalOperator(lexer.tokenType())) {
ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node();
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left),
std::move(right)));
} else {
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0);
values.push_back(
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
std::move(left), std::move(right)));
}
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::keyword_where && lexer.tokenType() != TokenType::eof);
std::unique_ptr<Node> where_node = parse_where_clause();
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
} }
std::unique_ptr<Node> Parser::parse_load_table() {
lexer.skipToken(TokenType::keyword_load);
lexer.skipTokenOptional(TokenType::keyword_into);
std::string table_name = lexer.consumeCurrentToken().token_string;
lexer.skipTokenOptional(TokenType::keyword_from);
std::string file_name = lexer.consumeCurrentToken().token_string;
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_where_clause() {
// TODO add support for multiple filters
// TODO add support for parenthesis
if (lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>();
}
std::unique_ptr<Node> node;
lexer.skipToken(TokenType::keyword_where);
do {
node = parse_relational_expression();
if (Lexer::isLogicalOperator(lexer.tokenType())) {
auto operation = parse_logical_operator();
std::unique_ptr<Node> node2 = parse_relational_expression();
node = std::make_unique<LogicalOperatorNode>(operation, std::move(node), std::move(node2));
}
} while (lexer.tokenType() != TokenType::eof); // until whole where clause parsed
return node;
}
std::unique_ptr<Node> Parser::parse_relational_expression() {
auto left = parse_operand_node();
auto operation = parse_relational_operator();
auto right = parse_operand_node();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
}
std::unique_ptr<Node> Parser::parse_operand_node() {
// while not end or order or limit
auto token_type = lexer.tokenType();
std::string tokenString = lexer.consumeCurrentToken().token_string;
switch (token_type) {
case TokenType::int_number:
return std::make_unique<IntValueNode>(std::stoi(tokenString));
case TokenType::double_number:
return std::make_unique<FloatValueNode>(std::stod(tokenString));
case TokenType::string_literal:
return std::make_unique<StringValueNode>(tokenString);
case TokenType::identifier:
return std::make_unique<DatabaseValueNode>(tokenString);
default:;
throw Exception("Unknown operand node");
}
}
RelationalOperatorType Parser::parse_relational_operator() {
auto op = lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::equal:
return RelationalOperatorType::equal;
case TokenType::not_equal:
return RelationalOperatorType::not_equal;
case TokenType::greater:
return RelationalOperatorType::greater;
case TokenType::greater_equal:
return RelationalOperatorType::greater_equal;
case TokenType::lesser:
return RelationalOperatorType::lesser;
case TokenType::lesser_equal:
return RelationalOperatorType::lesser_equal;
default:
throw Exception("Unknown relational operator");
}
}
LogicalOperatorType Parser::parse_logical_operator() {
auto op = lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::logical_and:
return LogicalOperatorType::and_operator;
case TokenType::logical_or:
return LogicalOperatorType::or_operator;
default:
throw Exception("Unknown logical operator");
}
}
ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
auto op = lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::plus:
return ArithmeticalOperatorType::plus_operator;
case TokenType::minus:
return ArithmeticalOperatorType::minus_operator;
case TokenType::multiply:
return ArithmeticalOperatorType::multiply_operator;
case TokenType::divide:
return ArithmeticalOperatorType::divide_operator;
default:
throw Exception("Unknown arithmetical operator");
}
}
} }

349
parser.h
View File

@ -6,223 +6,276 @@
#include <vector> #include <vector>
namespace usql {
enum class ColumnType {
enum class ColumnType {
integer_type, integer_type,
float_type, float_type,
varchar_type varchar_type
}; };
enum class NodeType { enum class NodeType {
true_node, true_node,
int_value, int_value,
float_value, float_value,
string_value, string_value,
database_value, database_value,
logical_operator, logical_operator,
relational_operator, relational_operator,
arithmetical_operator, arithmetical_operator,
create_table, create_table,
insert_into, insert_into,
select_from, select_from,
delete_from, delete_from,
update_table, update_table,
column_name, load_table,
column_name,
column_value, column_value,
column_def, column_def,
not_implemented_yet, not_implemented_yet,
error error
}; };
struct Node { struct Node {
NodeType node_type; NodeType node_type;
Node(const NodeType type) : node_type(type) {} Node(const NodeType type) : node_type(type) {}
}; };
struct ColNameNode : Node { struct ColNameNode : Node {
std::string name; std::string name;
ColNameNode(const std::string col_name) : ColNameNode(const std::string col_name) :
Node(NodeType::column_name), name(col_name) {} Node(NodeType::column_name), name(col_name) {}
}; };
struct ColValueNode : Node { struct ColValueNode : Node {
std::string value; std::string value;
ColValueNode(const std::string col_value) : ColValueNode(const std::string col_value) :
Node(NodeType::column_value), value(col_value) {} Node(NodeType::column_value), value(col_value) {}
}; };
// TODO add order in row // TODO add order in row
struct ColDefNode : Node { struct ColDefNode : Node {
std::string name; std::string name;
ColumnType type; ColumnType type;
int order; int order;
int length; int length;
bool null; bool null;
ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) : ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) :
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len), null(nullable) {} Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
}; null(nullable) {}
};
struct TrueNode : Node {
TrueNode() : Node(NodeType::true_node) {}
};
struct TrueNode : Node { struct ValueNode : Node {
TrueNode() : Node(NodeType::true_node) {} ValueNode(NodeType type) : Node(type) {}
};
struct IntValueNode : Node { virtual int getIntValue() = 0;
int value;
IntValueNode(int value) : Node(NodeType::int_value), value(value) {} virtual double getDoubleValue() = 0;
};
struct FloatValueNode : Node { virtual std::string getStringValue() = 0;
double value;
FloatValueNode(double value) : Node(NodeType::float_value), value(value) {} virtual ~ValueNode() {};
}; };
struct StringValueNode : Node { struct IntValueNode : ValueNode {
std::string value; int value;
StringValueNode(std::string value) : Node(NodeType::string_value), value(value) {} IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
};
struct DatabaseValueNode : Node { int getIntValue() { return value; };
std::string col_name;
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {} double getDoubleValue() { return (double) value; };
};
enum class LogicalOperatorType { std::string getStringValue() { return std::to_string(value); }
and_operator, };
or_operator,
not_operator
};
struct LogicalOperatorNode : Node { struct FloatValueNode : ValueNode {
LogicalOperatorType op; double value;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
};
enum class RelationalOperatorType { int getIntValue() { return (int) value; };
equal,
greater,
greater_equal,
lesser,
lesser_equal,
not_equal
// like
};
struct RelationalOperatorNode : Node { double getDoubleValue() { return value; };
RelationalOperatorType op;
std::unique_ptr<Node> left; std::string getStringValue() { return std::to_string(value); }
std::unique_ptr<Node> right; };
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : struct StringValueNode : ValueNode {
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {}; std::string value;
};
enum class ArithmeticalOperatorType { StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
copy_value, // just copy lef value and do nothing with it
plus_operator,
minus_operator,
multiply_operator,
divide_operator
};
struct ArithmeticalOperatorNode : Node { int getIntValue() { return std::stoi(value); };
ArithmeticalOperatorType op;
std::unique_ptr<Node> left; double getDoubleValue() { return std::stod(value); };
std::unique_ptr<Node> right;
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : std::string getStringValue() { return value; };
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {}; };
};
struct DatabaseValueNode : Node {
std::string col_name;
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
};
enum class LogicalOperatorType {
and_operator,
or_operator,
not_operator
};
struct LogicalOperatorNode : Node {
LogicalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
};
enum class RelationalOperatorType {
equal,
greater,
greater_equal,
lesser,
lesser_equal,
not_equal
// like
};
struct RelationalOperatorNode : Node {
RelationalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
};
enum class ArithmeticalOperatorType {
copy_value, // just copy lef value and do nothing with it
plus_operator,
minus_operator,
multiply_operator,
divide_operator
};
struct ArithmeticalOperatorNode : Node {
ArithmeticalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
};
struct CreateTableNode : Node { struct CreateTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColDefNode> cols_defs; std::vector<ColDefNode> cols_defs;
CreateTableNode(const std::string name, std::vector<ColDefNode> defs) : CreateTableNode(const std::string name, std::vector<ColDefNode> defs) :
Node(NodeType::create_table), table_name(name), cols_defs(defs) {} Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
}; };
struct InsertIntoTableNode : Node { struct InsertIntoTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::vector<ColValueNode> cols_values; std::vector<ColValueNode> cols_values;
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) : InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {} Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
}; };
struct SelectFromTableNode : Node { struct SelectFromTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::unique_ptr<Node> where; std::unique_ptr<Node> where;
SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) : SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) :
Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {} Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {}
}; };
struct UpdateTableNode : Node { struct UpdateTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> values; std::vector<std::unique_ptr<Node>> values;
std::unique_ptr<Node> where; std::unique_ptr<Node> where;
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals, UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
std::unique_ptr<Node> where_clause) : std::unique_ptr<Node> where_clause) :
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)), where(std::move(where_clause)) {} Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
}; where(std::move(where_clause)) {}
};
struct DeleteFromTableNode : Node { struct LoadIntoTableNode : Node {
std::string table_name; std::string table_name;
std::unique_ptr<Node> where; std::string filename;
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) : LoadIntoTableNode(const std::string name, std::string file) :
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {} Node(NodeType::load_table), table_name(name), filename(file) {}
}; };
struct DeleteFromTableNode : Node {
std::string table_name;
std::unique_ptr<Node> where;
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
};
class Parser {
private:
public:
class Parser {
private:
public:
Parser(); Parser();
std::unique_ptr<Node> parse(const std::string &code); std::unique_ptr<Node> parse(const std::string &code);
private: private:
std::unique_ptr<Node> parse_create_table(); std::unique_ptr<Node> parse_create_table();
std::unique_ptr<Node> parse_insert_into_table();
std::unique_ptr<Node> parse_select_from_table();
std::unique_ptr<Node> parse_delete_from_table();
std::unique_ptr<Node> parse_update_table();
std::unique_ptr<Node> parse_where_clause(); std::unique_ptr<Node> parse_insert_into_table();
std::unique_ptr<Node> parse_operand_node();
RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator();
private: std::unique_ptr<Node> parse_select_from_table();
std::unique_ptr<Node> parse_delete_from_table();
std::unique_ptr<Node> parse_update_table();
std::unique_ptr<Node> parse_load_table();
std::unique_ptr<Node> parse_where_clause();
std::unique_ptr<Node> parse_operand_node();
RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator();
private:
Lexer lexer; Lexer lexer;
std::unique_ptr<Node> parse_relational_expression(); std::unique_ptr<Node> parse_relational_expression();
}; };
}

94
row.cpp
View File

@ -1,53 +1,59 @@
#include "row.h" #include "row.h"
namespace usql {
Row::Row(int cols_count) { Row::Row(int cols_count) {
m_columns.reserve(cols_count); m_columns.reserve(cols_count);
for (int i = 0; i < cols_count; i++) { for (int i = 0; i < cols_count; i++) {
m_columns.push_back(std::make_unique<ColValue>()); m_columns.push_back(std::make_unique<ColValue>());
} }
}
Row::Row(const Row &other) {
m_columns.reserve(other.m_columns.size());
// TODO fixme this is nonsense
for (int i = 0; i < other.m_columns.size(); i++) {
m_columns.push_back(std::make_unique<ColNullValue>());
} }
for (int i = 0; i < other.m_columns.size(); i++) { Row::Row(const Row &other) {
if (ColIntegerValue* other_v = dynamic_cast<ColIntegerValue*>(other.m_columns[i].get())) { m_columns.reserve(other.m_columns.size());
setColumnValue(i, other_v->integerValue()); // TODO fixme this is nonsense
} for (int i = 0; i < other.m_columns.size(); i++) {
if (ColFloatValue* other_v = dynamic_cast<ColFloatValue*>(other.m_columns[i].get())) { m_columns.push_back(std::make_unique<ColNullValue>());
setColumnValue(i, other_v->floatValue()); }
}
if (ColStringValue* other_v = dynamic_cast<ColStringValue*>(other.m_columns[i].get())) { for (int i = 0; i < other.m_columns.size(); i++) {
setColumnValue(i, other_v->stringValue()); if (ColIntegerValue *other_v = dynamic_cast<ColIntegerValue *>(other.m_columns[i].get())) {
} setColumnValue(i, other_v->integerValue());
}
if (ColFloatValue *other_v = dynamic_cast<ColFloatValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->floatValue());
}
if (ColStringValue *other_v = dynamic_cast<ColStringValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->stringValue());
}
}
} }
}
Row& Row::operator=(Row other) { Row &Row::operator=(Row other) {
std::swap(m_columns, other.m_columns); std::swap(m_columns, other.m_columns);
return *this; return *this;
}
void Row::setColumnValue(int col_index, int value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}
void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColFloatValue>(value);
}
void Row::setColumnValue(int col_index, std::string value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value);
};
void Row::print() {
for(int i=0; i<m_columns.size(); i++) {
m_columns[i].get()->print();
} }
}
void Row::setColumnValue(int col_index, int value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}
void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColFloatValue>(value);
}
void Row::setColumnValue(int col_index, std::string value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value);
};
void Row::print() {
for (int ci = 0; ci < m_columns.size(); ci++) {
if (ci > 0) std::cout << ",";
auto v = m_columns[ci]->stringValue();
std::cout << v;
}
std::cout << std::endl;
}
}

150
row.h
View File

@ -5,116 +5,128 @@
#include <vector> #include <vector>
class ColumnValue { namespace usql {
class ColumnValue {
private: private:
ColumnType m_type; ColumnType m_type;
union { union {
int int_value; int int_value;
double float_value; double float_value;
};
}; };
};
struct ColValue {
struct ColValue { virtual bool isNull() { return false; };
virtual bool isNull() { return false; }; virtual bool isInteger() { return false; };
virtual bool isInteger() { return false; };
virtual bool isFloat() { return false; };
virtual bool isString() { return false; };
virtual int integerValue() { throw Exception("Not supported"); }; virtual bool isFloat() { return false; };
virtual double floatValue() { throw Exception("Not supported"); };
virtual std::string stringValue() { throw Exception("Not supported"); };
virtual void print() {std::cout << "ColValue:" << std::endl; }; virtual bool isString() { return false; };
};
virtual int integerValue() { throw Exception("Not supported"); };
virtual double floatValue() { throw Exception("Not supported"); };
virtual std::string stringValue() { throw Exception("Not supported"); };
};
struct ColNullValue : ColValue { struct ColNullValue : ColValue {
virtual bool isNull() { return true; }; virtual bool isNull() { return true; };
virtual void print() {std::cout << "ColNullValue:" << std::endl; }; virtual std::string stringValue() { return "null"; };
}; };
struct ColIntegerValue : ColValue { struct ColIntegerValue : ColValue {
ColIntegerValue(int value) : m_integer(value) {}; ColIntegerValue(int value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}
virtual bool isInteger() { return true; }; ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}
virtual int integerValue() { return m_integer; }; virtual bool isInteger() { return true; };
virtual double floatValue() { return (double) m_integer; };
virtual std::string stringValue() { return std::to_string(m_integer); };
virtual void print() {std::cout << "ColIntegerValue: " << m_integer <<std::endl; }; virtual int integerValue() { return m_integer; };
int m_integer; virtual double floatValue() { return (double) m_integer; };
};
virtual std::string stringValue() { return std::to_string(m_integer); };
int m_integer;
};
struct ColFloatValue : ColValue { struct ColFloatValue : ColValue {
ColFloatValue(double value) : m_float(value) {}; ColFloatValue(double value) : m_float(value) {};
ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {}
virtual bool isFloat() { return true; } ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {}
virtual int integerValue() { return (int) m_float; }; virtual bool isFloat() { return true; }
virtual double floatValue() { return m_float; };
virtual std::string stringValue() { return std::to_string(m_float); };
virtual void print() {std::cout << "ColFloatValue: " << m_float <<std::endl; }; virtual int integerValue() { return (int) m_float; };
double m_float; virtual double floatValue() { return m_float; };
};
virtual std::string stringValue() { return std::to_string(m_float); };
double m_float;
};
struct ColStringValue : ColValue { struct ColStringValue : ColValue {
ColStringValue(const std::string value) : m_string(value) {}; ColStringValue(const std::string value) : m_string(value) {};
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
virtual bool isString() { return true; } ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
virtual int integerValue() { return std::stoi(m_string); }; virtual bool isString() { return true; }
virtual double floatValue() { return std::stod(m_string); };
virtual std::string stringValue() { return m_string; };
virtual void print() {std::cout << "ColStringValue: " << m_string <<std::endl; }; virtual int integerValue() { return std::stoi(m_string); };
std::string m_string; virtual double floatValue() { return std::stod(m_string); };
};
virtual std::string stringValue() { return m_string; };
std::string m_string;
};
class Row {
class Row { public:
Row(int cols_count);
public: Row(const Row &other);
Row(int cols_count);
Row(const Row &other);
Row& operator=(Row other);
void setColumnValue(int col_index, int value); Row &operator=(Row other);
void setColumnValue(int col_index, double value);
void setColumnValue(int col_index, std::string value);
ColValue& operator[](int i) { void setColumnValue(int col_index, int value);
return *m_columns[i];
}
ColValue* ithColumn(int i) { void setColumnValue(int col_index, double value);
return m_columns[i].get();
}
void print(); void setColumnValue(int col_index, std::string value);
private: ColValue &operator[](int i) {
std::vector<std::unique_ptr<ColValue>> m_columns; return *m_columns[i];
}; }
ColValue *ithColumn(int i) {
return m_columns[i].get();
}
void print();
private:
std::vector<std::unique_ptr<ColValue>> m_columns;
};
}

View File

@ -1,46 +1,47 @@
#include "table.h" #include "table.h"
Table::Table(const std::string name, const std::vector<ColDefNode> columns) { namespace usql {
m_name = name;
m_col_defs = columns;
m_rows.clear();
}
ColDefNode Table::get_column_def(const std::string& col_name) { Table::Table(const std::string name, const std::vector<ColDefNode> columns) {
auto name_cmp = [col_name](ColDefNode cd){ return cd.name == col_name; }; m_name = name;
auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp ); m_col_defs = columns;
if (col_def != std::end(m_col_defs)) { m_rows.clear();
return *col_def;
} else {
throw Exception("column not exists (" + col_name + ")");
} }
}
ColDefNode Table::get_column_def(const std::string &col_name) {
Row Table::createEmptyRow() { auto name_cmp = [col_name](ColDefNode cd) { return cd.name == col_name; };
return Row(columns_count()); auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp);
} if (col_def != std::end(m_col_defs)) {
return *col_def;
} else {
void Table::print() { throw Exception("column not exists (" + col_name + ")");
std::cout << "** " << m_name << " **" << std::endl; }
for(auto row : m_rows) {
for(int ci = 0; ci < columns_count(); ci++) {
auto v = row[ci].stringValue();
std::cout << v << ",";
}
std::cout << std::endl;
} }
}
Table::Table(const Table& other) {
m_name = other.m_name;
m_col_defs = other.m_col_defs;
m_rows.clear(); // row not copied now
}
void Table::addRow(const Row &row) { Row Table::createEmptyRow() {
m_rows.push_back(row); return Row(columns_count());
} }
void Table::print() {
std::cout << "** " << m_name << " **" << std::endl;
for (auto row : m_rows) {
row.print();
}
}
Table::Table(const Table &other) {
m_name = other.m_name;
m_col_defs = other.m_col_defs;
m_rows.clear(); // row not copied now
}
void Table::addRow(const Row &row) {
// TODO validate for not null values
// todo validate for length etc
m_rows.push_back(row);
}
}

31
table.h
View File

@ -5,25 +5,26 @@
#include <vector> #include <vector>
// TODO make it a class namespace usql {
struct Table {
// public: struct Table {
Table(const Table& other);
Table(const std::string name, const std::vector<ColDefNode> columns); Table(const Table &other);
ColDefNode get_column_def(const std::string& col_name); Table(const std::string name, const std::vector<ColDefNode> columns);
int columns_count() { return m_col_defs.size(); };
Row createEmptyRow(); // TODO this means unnecessary copying ColDefNode get_column_def(const std::string &col_name);
void addRow(const Row &row);
void print(); int columns_count() { return m_col_defs.size(); };
Row createEmptyRow(); // TODO this means unnecessary copying
void addRow(const Row &row);
// private: void print();
std::string m_name;
std::vector<ColDefNode> m_col_defs; std::string m_name;
std::vector<Row> m_rows; std::vector<ColDefNode> m_col_defs;
}; std::vector<Row> m_rows;
};
}