usql update

This commit is contained in:
2021-12-19 13:33:47 +01:00
parent 37d0d9b3f5
commit 5c925f2608
23 changed files with 1570 additions and 1124 deletions

View File

@@ -1,10 +1,8 @@
#include "usql.h"
#include "exception.h"
#include "ml_date.h"
#include "ml_string.h"
#include <algorithm>
#include <fstream>
namespace usql {
@@ -20,10 +18,11 @@ std::unique_ptr<Table> USql::execute(const std::string &command) {
}
std::unique_ptr<Table> USql::execute(Node &node) {
// TODO optimize execution nodes here
switch (node.node_type) {
case NodeType::create_table:
return execute_create_table(static_cast<CreateTableNode &>(node));
case NodeType::create_index:
return execute_create_index(static_cast<CreateIndexNode &>(node));
case NodeType::create_table_as_select:
return execute_create_table_as_table(static_cast<CreateTableAsSelectNode &>(node));
case NodeType::drop_table:
@@ -65,7 +64,7 @@ bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table
return !all_null;
return false;
} else if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) {
comparator = left_value->getIntegerValue() - right_value->getIntegerValue();
comparator = (double)(left_value->getIntegerValue() - right_value->getIntegerValue());
} 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)) {
@@ -108,7 +107,7 @@ std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *n
if (node->node_type == NodeType::database_value) {
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 || node->node_type == NodeType::bool_value) {
return eval_literal_value_node(table, row, node);
return eval_literal_value_node(row, node);
} else if (node->node_type == NodeType::function) {
return eval_function_value_node(table, row, node, col_def_node, agg_func_value);
} else if (node->node_type == NodeType::null_value) {
@@ -129,7 +128,7 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
return std::make_unique<NullValueNode>();
if (col_def.type == ColumnType::integer_type)
return std::make_unique<IntValueNode>(db_value.getIntValue());
return std::make_unique<IntValueNode>(db_value.getIntegerValue());
if (col_def.type == ColumnType::float_type)
return std::make_unique<DoubleValueNode>(db_value.getDoubleValue());
if (col_def.type == ColumnType::varchar_type)
@@ -137,13 +136,13 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
if (col_def.type == ColumnType::bool_type)
return std::make_unique<BooleanValueNode>(db_value.getBoolValue());
if (col_def.type == ColumnType::date_type)
return std::make_unique<IntValueNode>(db_value.getIntValue());
return std::make_unique<IntValueNode>(db_value.getIntegerValue());
throw Exception("unknown database value type");
}
std::unique_ptr<ValueNode> USql::eval_literal_value_node(Table *table, Row &row, Node *node) {
std::unique_ptr<ValueNode> USql::eval_literal_value_node(Row &row, Node *node) {
if (node->node_type == NodeType::int_value) {
auto *ivl = static_cast<IntValueNode *>(node);
return std::make_unique<IntValueNode>(ivl->value);
@@ -166,8 +165,7 @@ std::unique_ptr<ValueNode> USql::eval_literal_value_node(Table *table, Row &row,
}
std::unique_ptr<ValueNode>
USql::eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value) {
std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *col_def_node, ColValue *agg_func_value) {
auto *fnc = static_cast<FunctionNode *>(node);
std::vector<std::unique_ptr<ValueNode>> evaluatedPars;
@@ -177,7 +175,8 @@ USql::eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *c
// at this moment no functions without parameter(s) or first param can be null
if (evaluatedPars.empty() || evaluatedPars[0]->isNull())
return std::make_unique<NullValueNode>();
throw Exception("eval_function_value_node, no function parameter or first is null, function: " + fnc->function);
// return std::make_unique<NullValueNode>();
// TODO use some enum
if (fnc->function == "lower") return lower_function(evaluatedPars);
@@ -193,23 +192,13 @@ USql::eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *c
throw Exception("invalid function: " + fnc->function);
}
std::unique_ptr<ValueNode> USql::count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
long c = 1;
if (!agg_func_value->isNull()) {
c = agg_func_value->getIntValue() + 1;
}
return std::make_unique<IntValueNode>(c);
}
bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) {
//bool left = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
bool left = eval_where(&(*node.left), pTable, row);
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
return left;
//bool right = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, row);
bool right = eval_where(&(*node.right), pTable, row);
return right;
}
@@ -227,8 +216,8 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
return std::make_unique<NullValueNode>();
if (outType == ColumnType::float_type) {
double l = ((ValueNode *) left.get())->getDoubleValue();
double r = ((ValueNode *) right.get())->getDoubleValue();
auto l = left->getDoubleValue();
auto r = right->getDoubleValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<DoubleValueNode>(l + r);
@@ -239,12 +228,12 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
case ArithmeticalOperatorType::divide_operator:
return std::make_unique<DoubleValueNode>(l / r);
default:
throw Exception("implement me!!");
throw Exception("eval_arithmetic_operator, float type implement me!!");
}
} else if (outType == ColumnType::integer_type) {
long l = ((ValueNode *) left.get())->getIntegerValue();
long r = ((ValueNode *) right.get())->getIntegerValue();
auto l = left->getIntegerValue();
auto r = right->getIntegerValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r);
@@ -255,179 +244,57 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
case ArithmeticalOperatorType::divide_operator:
return std::make_unique<IntValueNode>(l / r);
default:
throw Exception("implement me!!");
throw Exception("eval_arithmetic_operator, integer type implement me!!");
}
} else if (outType == ColumnType::varchar_type) {
std::string l = ((ValueNode *) left.get())->getStringValue();
std::string r = ((ValueNode *) right.get())->getStringValue();
auto l = left->getStringValue();
auto r = right->getStringValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<StringValueNode>(l + r);
default:
throw Exception("implement me!!");
throw Exception("eval_arithmetic_operator, varchar type implement me!!");
}
} else if (outType == ColumnType::date_type) {
auto l = left->getDateValue();
auto r = right->getDateValue();
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);
default:
throw Exception("eval_arithmetic_operator, date_type type implement me!!");
}
}
// TODO date node should support addition and subtraction
throw Exception("implement me!!");
throw Exception("eval_arithmetic_operator, implement me!!");
}
std::unique_ptr<ValueNode> USql::to_string_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
long date = evaluatedPars[0]->getDateValue();
std::string format = evaluatedPars[1]->getStringValue();
std::string formatted_date = date_to_string(date, format);
return std::make_unique<StringValueNode>(formatted_date);
}
std::unique_ptr<ValueNode> USql::to_date_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
std::string date = evaluatedPars[0]->getStringValue();
std::string format = evaluatedPars[1]->getStringValue();
long epoch_time = string_to_date(date, format);
return std::make_unique<IntValueNode>(epoch_time); // No DateValueNode for now
}
std::unique_ptr<ValueNode> USql::date_add_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
long datetime = evaluatedPars[0]->getDateValue();
long quantity = evaluatedPars[1]->getIntegerValue();
std::string part = evaluatedPars[2]->getStringValue();
long new_date = add_to_date(datetime, quantity, part);
return std::make_unique<IntValueNode>(new_date); // No DateValueNode for now
}
std::unique_ptr<ValueNode> USql::upper_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
std::string str = evaluatedPars[0]->getStringValue();
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return toupper(c); });
return std::make_unique<StringValueNode>(str);
}
std::unique_ptr<ValueNode> USql::lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
std::string str = evaluatedPars[0]->getStringValue();
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
return std::make_unique<StringValueNode>(str);
}
std::unique_ptr<ValueNode> USql::pp_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
auto &parsed_value = evaluatedPars[0];
if (parsed_value->node_type == NodeType::int_value || parsed_value->node_type == NodeType::float_value) {
std::string format = evaluatedPars.size() > 1 ? evaluatedPars[1]->getStringValue() : "";
char buf[20] {0}; // TODO constant here
double value = parsed_value->getDoubleValue();
if (format == "100%")
std::snprintf(buf, 20, "%.2f%%", value);
else if (format == "%.2f")
std::snprintf(buf, 20, "%.2f", value);
else if (value >= 1000000000000)
std::snprintf(buf, 20, "%7.2fT", value/1000000000000);
else if (value >= 1000000000)
std::sprintf(buf, "%7.2fB", value/1000000000);
else if (value >= 1000000)
std::snprintf(buf, 20, "%7.2fM", value/1000000);
else if (value >= 100000)
std::snprintf(buf, 20, "%7.2fM", value/100000); // 0.12M
else if (value <= -1000000000000)
std::snprintf(buf, 20, "%7.2fT", value/1000000000000);
else if (value <= -1000000000)
std::snprintf(buf, 20, "%7.2fB", value/1000000000);
else if (value <= -1000000)
std::snprintf(buf, 20, "%7.2fM", value/1000000);
else if (value <= -100000)
std::snprintf(buf, 20, "%7.2fM", value/100000); // 0.12M
else if (value == 0)
buf[0]='0';
else
return std::make_unique<StringValueNode>(parsed_value->getStringValue().substr(0, 10));
// TODO introduce constant for 10
std::string s {buf};
return std::make_unique<StringValueNode>(string_padd(s.erase(s.find_last_not_of(" ")+1), 10, ' ', false));
}
return std::make_unique<StringValueNode>(parsed_value->getStringValue());
}
std::unique_ptr<ValueNode>
USql::max_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node,
ColValue *agg_func_value) {
if (col_def_node->type == ColumnType::integer_type || col_def_node->type == ColumnType::date_type) {
if (!evaluatedPars[0]->isNull()) {
long val = evaluatedPars[0]->getIntegerValue();
if (agg_func_value->isNull()) {
return std::make_unique<IntValueNode>(val);
} else {
return std::make_unique<IntValueNode>(std::max(val, agg_func_value->getIntValue()));
}
} else {
return std::make_unique<IntValueNode>(agg_func_value->getIntValue());
}
} else if (col_def_node->type == ColumnType::float_type) {
if (!evaluatedPars[0]->isNull()) {
double val = evaluatedPars[0]->getDoubleValue();
if (agg_func_value->isNull()) {
return std::make_unique<DoubleValueNode>(val);
} else {
return std::make_unique<DoubleValueNode>(std::max(val, agg_func_value->getDoubleValue()));
}
} else {
return std::make_unique<DoubleValueNode>(agg_func_value->getDoubleValue());
}
}
// TODO string and boolean
throw Exception("unsupported data type for max function");
}
std::unique_ptr<ValueNode>
USql::min_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node,
ColValue *agg_func_value) {
if (col_def_node->type == ColumnType::integer_type || col_def_node->type == ColumnType::date_type) {
if (!evaluatedPars[0]->isNull()) {
long val = evaluatedPars[0]->getIntegerValue();
if (agg_func_value->isNull()) {
return std::make_unique<IntValueNode>(val);
} else {
return std::make_unique<IntValueNode>(std::min(val, agg_func_value->getIntValue()));
}
} else {
return std::make_unique<IntValueNode>(agg_func_value->getIntValue());
}
} else if (col_def_node->type == ColumnType::float_type) {
if (!evaluatedPars[0]->isNull()) {
double val = evaluatedPars[0]->getDoubleValue();
if (agg_func_value->isNull()) {
return std::make_unique<DoubleValueNode>(val);
} else {
return std::make_unique<DoubleValueNode>(std::min(val, agg_func_value->getDoubleValue()));
}
} else {
return std::make_unique<DoubleValueNode>(agg_func_value->getDoubleValue());
}
}
// TODO string and boolean
throw Exception("unsupported data type for min function");
}
Table *USql::find_table(const std::string &name) {
Table *USql::find_table(const std::string &name) const {
auto name_cmp = [name](const Table& t) { return t.m_name == name; };
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
if (table_def != std::end(m_tables)) {
return table_def.operator->();
} else {
throw Exception("table not found (" + name + ")");
}
if (table_def != std::end(m_tables))
return const_cast<Table *>(table_def.operator->());
throw Exception("table not found (" + name + ")");
}
void USql::check_table_not_exists(const std::string &name) {
void USql::check_table_not_exists(const std::string &name) const {
auto name_cmp = [name](const Table& t) { return t.m_name == name; };
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
if (table_def != std::end(m_tables)) {
if (table_def != std::end(m_tables))
throw Exception("table already exists");
}
}
} // namespace
void USql::check_index_not_exists(const std::string &index_name) {
for (auto &table : m_tables)
if (table.get_index(index_name) != nullptr)
throw Exception("index already exists");
}
} // namespace