usql update

This commit is contained in:
VaclavT 2021-07-27 19:31:41 +02:00
parent 281f7d8700
commit db0e280371
7 changed files with 3132 additions and 61 deletions

View File

@ -68,6 +68,7 @@
"vector": "cpp", "vector": "cpp",
"thread": "cpp", "thread": "cpp",
"numeric": "cpp", "numeric": "cpp",
"stack": "cpp" "stack": "cpp",
"list": "cpp"
} }
} }

View File

@ -1,11 +1,10 @@
### TODO ### TODO
- drop table command - support for *
- add affecter rows and timing into result table - support for order by, offset, limit
- add exceptions and rename it to UsqlException
- class members should have prefix m_
- maybe to create iterator on table
- add pipe | token - add pipe | token
- add count min and max functions, eg aggregate functions - add count min and max functions, eg aggregate functions
- add logging - maybe to create iterator on table
- add exceptions and rename it to UsqlException
- class members should have prefix m_
- add const wherever should be - add const wherever should be

View File

@ -3,13 +3,10 @@
// https://dev.to/joaoh82/what-would-sqlite-look-like-if-written-in-rust-part-1-2np4 // https://dev.to/joaoh82/what-would-sqlite-look-like-if-written-in-rust-part-1-2np4
// parser should get m_lexer as param and table executor to be able translate * or get types or so using namespace std::chrono;
// podporovat create as select
// drop table
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
std::vector<std::string> sql_commands{ std::vector<std::string> sql_commands{
"load prices from 'prices.csv'",
"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, upper('one'))", "insert into a (i, s) values(1, upper('one'))",
"update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'", "update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
@ -41,16 +38,21 @@ int main(int argc, char *argv[]) {
"select i, s, f from a where i > 300", "select i, s, f from a where i > 300",
"select i, to_string(i, '%d.%m.%Y'), s, f from a where i > 300", "select i, to_string(i, '%d.%m.%Y'), s, f from a where i > 300",
"create table prices (datetime integer, symbol varchar(8), prev_close float, open float, price float, change float, change_prct varchar(16))", "create table prices (datetime integer, symbol varchar(8), prev_close float, open float, price float, change float, change_prct varchar(16))",
"load prices from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/prices.csv'",
"insert into prices (datetime, symbol, prev_close, open, price, change, change_prct) values (1626979443, 'MPC', 54.08, 53.82, 53.63, -0.832101, '-0.83 %')", "insert into prices (datetime, symbol, prev_close, open, price, change, change_prct) values (1626979443, 'MPC', 54.08, 53.82, 53.63, -0.832101, '-0.83 %')",
"select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices" "select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices where symbol = 'SYF'"
}; };
usql::USql uSql{}; usql::USql uSql{};
for (auto command : sql_commands) { for (auto command : sql_commands) {
std::cout << command << std::endl; time_point<high_resolution_clock> start_time = high_resolution_clock::now();
auto result = uSql.execute(command); auto result = uSql.execute(command);
time_point<high_resolution_clock> end_time = high_resolution_clock::now();
std::cout << command << std::endl;
std::cout << "run time: " << duration_cast<milliseconds>(end_time - start_time).count() << " ms " << std::endl;
result->print(); result->print();
} }

3061
usql/prices.csv Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -13,7 +13,7 @@ std::unique_ptr<Table> USql::execute(const std::string &command) {
return execute(*node); return execute(*node);
} catch (std::exception &e) { } catch (std::exception &e) {
return create_stmt_result_table(-1, e.what()); return create_stmt_result_table(-1, e.what(), 0);
} }
} }
@ -40,7 +40,7 @@ std::unique_ptr<Table> USql::execute(Node &node) {
case NodeType::drop_table: case NodeType::drop_table:
return execute_drop(static_cast<DropTableNode &>(node)); return execute_drop(static_cast<DropTableNode &>(node));
default: default:
return create_stmt_result_table(-1, "unknown statement"); return create_stmt_result_table(-1, "unknown statement", 0);
} }
} }
@ -51,7 +51,7 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
Table table{node.table_name, node.cols_defs}; Table table{node.table_name, node.cols_defs};
m_tables.push_back(table); m_tables.push_back(table);
return create_stmt_result_table(0, "table created"); return create_stmt_result_table(0, "table created", 0);
} }
@ -73,7 +73,7 @@ std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNo
select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode
return create_stmt_result_table(0, "table created"); return create_stmt_result_table(0, "table created", table->m_rows.size());
} }
@ -90,7 +90,7 @@ std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
// load rows // load rows
auto rows_cnt = table_def->load_csv_string(content); auto rows_cnt = table_def->load_csv_string(content);
return create_stmt_result_table(0, "load succeeded"); return create_stmt_result_table(0, "load succeeded", rows_cnt);
} }
@ -106,7 +106,7 @@ std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
file << csv_string; file << csv_string;
file.close(); file.close();
return create_stmt_result_table(0, "save succeeded"); return create_stmt_result_table(0, "save succeeded", 0);
} }
std::unique_ptr<Table> USql::execute_drop(DropTableNode &node) { std::unique_ptr<Table> USql::execute_drop(DropTableNode &node) {
@ -115,7 +115,7 @@ std::unique_ptr<Table> USql::execute_drop(DropTableNode &node) {
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp); auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
if (table_def != std::end(m_tables)) { if (table_def != std::end(m_tables)) {
m_tables.erase(table_def); m_tables.erase(table_def);
return create_stmt_result_table(0, "drop succeeded"); return create_stmt_result_table(0, "drop succeeded", 0);
} }
throw Exception("table not found (" + node.table_name + ")"); throw Exception("table not found (" + node.table_name + ")");
@ -133,7 +133,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// copy values // copy values
for (size_t i = 0; i < node.cols_names.size(); i++) { for (size_t i = 0; i < node.cols_names.size(); i++) {
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name); ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
auto col_value = evalValueNode(table_def, new_row, node.cols_values[i].get()); auto col_value = eval_value_node(table_def, new_row, node.cols_values[i].get());
new_row.setColumnValue(&col_def, col_value.get()); new_row.setColumnValue(&col_def, col_value.get());
} }
@ -141,7 +141,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// append new_row // append new_row
table_def->add_row(new_row); table_def->add_row(new_row);
return create_stmt_result_table(0, "insert succeeded"); return create_stmt_result_table(0, "insert succeeded", 1);
} }
@ -167,7 +167,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// execute access plan // execute access plan
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) { for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
// eval where for row // eval where for row
if (evalWhere(node.where.get(), table, *row)) { if (eval_where(node.where.get(), table, *row)) {
// prepare empty row // prepare empty row
Row new_row = result->create_empty_row(); Row new_row = result->create_empty_row();
@ -176,7 +176,8 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
auto row_col_index = source_table_col_index[idx]; auto row_col_index = source_table_col_index[idx];
if (row_col_index == -1) { // TODO introduce constant here if (row_col_index == -1) { // TODO introduce constant here
auto evaluated_value = evalValueNode(table, *row, node.cols_names->operator[](idx).value.get()); auto evaluated_value = eval_value_node(table, *row, node.cols_names->operator[](
idx).value.get());
ValueNode *col_value = evaluated_value.get(); ValueNode *col_value = evaluated_value.get();
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
@ -223,16 +224,18 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
Table *table = find_table(node.table_name); Table *table = find_table(node.table_name);
// execute access plan // execute access plan
int affected_rows = 0;
auto it = table->m_rows.begin(); auto it = table->m_rows.begin();
for (; it != table->m_rows.end();) { for (; it != table->m_rows.end();) {
if (evalWhere(node.where.get(), table, *it)) { if (eval_where(node.where.get(), table, *it)) {
it = table->m_rows.erase(it); it = table->m_rows.erase(it);
affected_rows++;
} else { } else {
++it; ++it;
} }
} }
return create_stmt_result_table(0, "delete succeeded"); return create_stmt_result_table(0, "delete succeeded", affected_rows);
} }
@ -241,35 +244,38 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
Table *table = find_table(node.table_name); Table *table = find_table(node.table_name);
// execute access plan // execute access plan
int affected_rows = 0;
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) { for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
// eval where for row // eval where for row
if (evalWhere(node.where.get(), table, *row)) { if (eval_where(node.where.get(), table, *row)) {
int i = 0; int i = 0;
for (const auto& col : node.cols_names) { for (const auto& col : node.cols_names) {
ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select
std::unique_ptr<ValueNode> new_val = evalArithmeticOperator(col_def.type, std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]), table, *row); static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, *row);
table->validate_column(&col_def, new_val.get()); table->validate_column(&col_def, new_val.get());
row->setColumnValue(&col_def, new_val.get()); row->setColumnValue(&col_def, new_val.get());
i++; i++;
} }
affected_rows++;
// TODO tady je problem, ze kdyz to zfajluje na jednom radku ostatni by se nemely provest // TODO tady je problem, ze kdyz to zfajluje na jednom radku ostatni by se nemely provest
} }
} }
return create_stmt_result_table(0, "delete succeeded"); return create_stmt_result_table(0, "update succeeded", affected_rows);
} }
bool USql::evalWhere(Node *where, Table *table, Row &row) const { bool USql::eval_where(Node *where, Table *table, Row &row) const {
switch (where->node_type) { // no where clause switch (where->node_type) { // no where clause
case NodeType::true_node: case NodeType::true_node:
return true; return true;
case NodeType::relational_operator: // just one condition case NodeType::relational_operator: // just one condition
return evalRelationalOperator(*((RelationalOperatorNode *) where), table, row); return eval_relational_operator(*((RelationalOperatorNode *) where), table, row);
case NodeType::logical_operator: case NodeType::logical_operator:
return evalLogicalOperator(*((LogicalOperatorNode *) where), table, row); return eval_logical_operator(*((LogicalOperatorNode *) where), table, row);
default: default:
throw Exception("Wrong node type"); throw Exception("Wrong node type");
} }
@ -278,9 +284,9 @@ bool USql::evalWhere(Node *where, Table *table, Row &row) const {
} }
bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const { bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const {
std::unique_ptr<ValueNode> left_value = evalValueNode(table, row, filter.left.get()); std::unique_ptr<ValueNode> left_value = eval_value_node(table, row, filter.left.get());
std::unique_ptr<ValueNode> right_value = evalValueNode(table, row, filter.right.get()); std::unique_ptr<ValueNode> right_value = eval_value_node(table, row, filter.right.get());
double comparator; double comparator;
@ -315,15 +321,15 @@ bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *t
} }
std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) { std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
return evalDatabaseValueNode(table, row, node); return eval_database_value_node(table, row, node);
} else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) { } else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) {
return evalLiteralValueNode(table, row, node); return eval_literal_value_node(table, row, node);
} else if (node->node_type == NodeType::function) { } else if (node->node_type == NodeType::function) {
return evalFunctionValueNode(table, row, node); return eval_function_value_node(table, row, node);
} else if (node->node_type == NodeType::null_value) { } else if (node->node_type == NodeType::null_value) {
return std::make_unique<NullValueNode>(); return std::make_unique<NullValueNode>();
} }
@ -331,7 +337,7 @@ std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *nod
} }
std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, Node *node) { std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row, Node *node) {
auto *dvl = static_cast<DatabaseValueNode *>(node); auto *dvl = static_cast<DatabaseValueNode *>(node);
ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once
auto db_value = row.ith_column(col_def.order); auto db_value = row.ith_column(col_def.order);
@ -349,7 +355,7 @@ std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, N
} }
std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, Node *node) { std::unique_ptr<ValueNode> USql::eval_literal_value_node(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::int_value) { if (node->node_type == NodeType::int_value) {
auto *ivl = static_cast<IntValueNode *>(node); auto *ivl = static_cast<IntValueNode *>(node);
return std::make_unique<IntValueNode>(ivl->value); return std::make_unique<IntValueNode>(ivl->value);
@ -368,12 +374,12 @@ std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, No
} }
std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, Node *node) { std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row, Node *node) {
auto *fnc = static_cast<FunctionNode *>(node); auto *fnc = static_cast<FunctionNode *>(node);
std::vector<std::unique_ptr<ValueNode>> evaluatedPars; std::vector<std::unique_ptr<ValueNode>> evaluatedPars;
for(auto & param : fnc->params) { for(auto & param : fnc->params) {
evaluatedPars.push_back(evalValueNode(table, row, param.get())); evaluatedPars.push_back(eval_value_node(table, row, param.get()));
} }
// TODO use some enum // TODO use some enum
@ -405,24 +411,24 @@ std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, N
} }
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const { bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row); bool left = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left)) if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
return left; return left;
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, row); bool right = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, row);
return right; return right;
} }
std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const { std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const {
if (node.op == ArithmeticalOperatorType::copy_value) { if (node.op == ArithmeticalOperatorType::copy_value) {
return evalValueNode(table, row, node.left.get()); return eval_value_node(table, row, node.left.get());
} }
std::unique_ptr<ValueNode> left = evalValueNode(table, row, node.left.get()); std::unique_ptr<ValueNode> left = eval_value_node(table, row, node.left.get());
std::unique_ptr<ValueNode> right = evalValueNode(table, row, node.right.get()); std::unique_ptr<ValueNode> right = eval_value_node(table, row, node.right.get());
if (outType == ColumnType::float_type) { if (outType == ColumnType::float_type) {
double l = ((ValueNode *) left.get())->getDoubleValue(); double l = ((ValueNode *) left.get())->getDoubleValue();
@ -471,16 +477,18 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
} }
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string& text) { std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) {
std::vector<ColDefNode> result_tbl_col_defs{}; std::vector<ColDefNode> result_tbl_col_defs{};
result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false)); result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false));
result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false)); result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
result_tbl_col_defs.push_back(ColDefNode("affected_rows", ColumnType::integer_type, 0, 1, true));
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs); auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
Row new_row = table_def->create_empty_row(); Row new_row = table_def->create_empty_row();
new_row.setColumnValue(0, code); new_row.setColumnValue(0, code);
new_row.setColumnValue(1, text); new_row.setColumnValue(1, text);
new_row.setColumnValue(2, affected_rows);
table_def->add_row(new_row); table_def->add_row(new_row);
return std::move(table_def); return std::move(table_def);

View File

@ -32,20 +32,20 @@ private:
private: private:
bool evalWhere(Node *where, Table *table, Row &row) const; bool eval_where(Node *where, Table *table, Row &row) const;
static std::unique_ptr<ValueNode> evalValueNode(Table *table, Row &row, Node *node); static std::unique_ptr<ValueNode> eval_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> evalDatabaseValueNode(Table *table, Row &row, Node *node); static std::unique_ptr<ValueNode> eval_database_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> evalLiteralValueNode(Table *table, Row &row, Node *node); static std::unique_ptr<ValueNode> eval_literal_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> evalFunctionValueNode(Table *table, Row &row, Node *node); static std::unique_ptr<ValueNode> eval_function_value_node(Table *table, Row &row, Node *node);
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const; bool eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const;
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const; bool eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const;
std::unique_ptr<ValueNode> evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const; std::unique_ptr<ValueNode> eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const;
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string& text); static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, long affected_rows);
static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ; static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ;
Table *find_table(const std::string &name); Table *find_table(const std::string &name);