ColNameNode removed, a bit more powerfull inserts etc

BEWARE insert into t (i) values(1+1) WILL FAIL - it is not "1" "+" "1" but "1" "+1"
This commit is contained in:
VaclavT 2021-08-13 16:34:51 +02:00
parent ee0cbf64ff
commit 6921421a65
5 changed files with 44440 additions and 74 deletions

View File

@ -139,8 +139,9 @@ int main(int argc, char *argv[]) {
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'WFC') and dimension = 'MRY' order by 3 desc",
"create table a (i integer not null, s varchar(64), f float null, d date null, b boolean)",
"insert into a (i, s, b) values(1, upper('one'), 'Y')",
"select i, s from a where i < to_date('20.12.1973', '%d.%m.%Y')",
// xxxxxxxx "select i+2, s, b from a where i >=1 order by 1 desc offset 0 limit 1",
"insert into a (i, s, b) values(1 + 10000, upper('one'), 'Y')",
// "select to_string(i, '%d.%m.%Y %H:%M:%S'), i, s from a where i < to_date('20.12.2019', '%d.%m.%Y')",
"select i + 2, s, b from a where i >=1 order by 1 desc offset 0 limit 1",
// "update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
"update table a set i = null",
"insert into a (i, s) values(2, 'two')",

View File

@ -61,7 +61,7 @@ namespace usql {
m_lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
std::string database_value;
ColumnType column_type;
int column_len = 1;
bool column_nullable = true;
@ -70,7 +70,7 @@ namespace usql {
if (m_lexer.tokenType() != TokenType::identifier) {
throw Exception("syntax error, expected identifier");
}
column_name = m_lexer.consumeCurrentToken().token_string;
database_value = m_lexer.consumeCurrentToken().token_string;
// column type and optionally len
if (m_lexer.tokenType() == TokenType::keyword_integer) {
@ -107,7 +107,7 @@ namespace usql {
m_lexer.nextToken();
}
cols_def.emplace_back(column_name, column_type, column_order++, column_len, column_nullable);
cols_def.emplace_back(database_value, column_type, column_order++, column_len, column_nullable);
m_lexer.skipTokenOptional(TokenType::comma);
@ -172,7 +172,7 @@ namespace usql {
}
std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<ColNameNode> column_names{};
std::vector<DatabaseValueNode> database_values{};
std::vector<std::unique_ptr<Node>> column_values{};
m_lexer.skipToken(TokenType::keyword_insert);
@ -186,7 +186,7 @@ namespace usql {
m_lexer.skipToken(TokenType::open_paren);
do {
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_names.emplace_back(m_lexer.consumeCurrentToken().token_string);
database_values.emplace_back(m_lexer.consumeCurrentToken().token_string);
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
@ -197,45 +197,14 @@ namespace usql {
// column values
m_lexer.skipToken(TokenType::open_paren);
do {
auto col_value = parse_value();
auto col_value = parse_expression();
column_values.push_back(std::move(col_value));
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, column_names, std::move(column_values));
}
std::unique_ptr<Node> Parser::parse_value() {
if (m_lexer.tokenType() == TokenType::int_number) {
return std::make_unique<IntValueNode>(std::stoi(m_lexer.consumeCurrentToken().token_string));
}
if (m_lexer.tokenType() == TokenType::double_number) {
return std::make_unique<DoubleValueNode>(std::stof(m_lexer.consumeCurrentToken().token_string));
}
if (m_lexer.tokenType() == TokenType::string_literal) {
return std::make_unique<StringValueNode>(m_lexer.consumeCurrentToken().token_string);
}
if (m_lexer.tokenType() == TokenType::identifier && m_lexer.nextTokenType() == TokenType::open_paren) {
// function
std::string function_name = m_lexer.consumeCurrentToken().token_string;
std::vector<std::unique_ptr<Node>> pars;
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(function_name, std::move(pars));
}
if (m_lexer.tokenType() == TokenType::identifier) {
std::string name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<ColNameNode>(name);
}
throw Exception("Syntax error, current token: " + m_lexer.currentToken().token_string);
return std::make_unique<InsertIntoTableNode>(table_name, database_values, std::move(column_values));
}
std::unique_ptr<Node> Parser::parse_select_from_table() {
@ -253,15 +222,15 @@ namespace usql {
while (m_lexer.tokenType() != TokenType::keyword_from) {
if (m_lexer.tokenType()==TokenType::multiply) {
std::string name = m_lexer.consumeCurrentToken().token_string;
auto multiply_char = std::make_unique<ColNameNode>(name);
auto multiply_char = std::make_unique<DatabaseValueNode>(name);
cols->push_back(SelectColNode{std::move(multiply_char), "*"});
} else {
auto column_value = parse_value();
auto column_value = parse_expression();
std::string column_alias;
if (column_value->node_type == NodeType::column_name) {
column_alias = ((ColNameNode*) column_value.get())->name;
if (column_value->node_type == NodeType::database_value) {
column_alias = ((DatabaseValueNode*) column_value.get())->col_name;
} else {
column_alias = "c" + std::to_string(i);
i++;
@ -306,17 +275,17 @@ namespace usql {
m_lexer.skipToken(TokenType::keyword_set);
std::vector<ColNameNode> cols_names;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
do {
cols_names.emplace_back(m_lexer.consumeCurrentToken().token_string);
m_lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node();
std::unique_ptr<Node> left = Parser::parse_value();
if (Lexer::isArithmeticalOperator(m_lexer.tokenType())) {
ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node();
std::unique_ptr<Node> right = Parser::parse_value();
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left), std::move(right)));
} else {
@ -415,7 +384,7 @@ namespace usql {
}
std::unique_ptr<Node> Parser::parse_expression() {
std::unique_ptr<Node> left = parse_operand_node();
std::unique_ptr<Node> left = parse_value();
return parse_expression(std::move(left));
}
@ -423,7 +392,7 @@ namespace usql {
std::unique_ptr<Node> Parser::parse_expression(std::unique_ptr<Node> left) {
if (Lexer::isRelationalOperator(m_lexer.tokenType())) {
auto operation = parse_relational_operator();
auto right = parse_operand_node();
auto right = parse_value();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
} else if (Lexer::isLogicalOperator(m_lexer.tokenType())) {
auto operation = parse_logical_operator();
@ -431,17 +400,17 @@ namespace usql {
return std::make_unique<LogicalOperatorNode>(operation, std::move(left), std::move(right));
} else if (Lexer::isArithmeticalOperator(m_lexer.tokenType())) {
auto operation = parse_arithmetical_operator();
auto right = parse_operand_node();
auto right = parse_value();
return std::make_unique<ArithmeticalOperatorNode>(operation, std::move(left), std::move(right));
} else if (m_lexer.tokenType() == TokenType::int_number || m_lexer.tokenType() == TokenType::double_number ||m_lexer.tokenType() == TokenType::string_literal ||m_lexer.tokenType() == TokenType::identifier || m_lexer.tokenType() == TokenType::keyword_null || m_lexer.tokenType() == TokenType::open_paren) {
return parse_operand_node();
return parse_value();
}
return left;
}
std::unique_ptr<Node> Parser::parse_operand_node() {
std::unique_ptr<Node> Parser::parse_value() {
auto token_type = m_lexer.tokenType();
// parenthised expression
@ -463,7 +432,7 @@ namespace usql {
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
pars.push_back(parse_operand_node());
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::close_paren);
@ -540,7 +509,7 @@ namespace usql {
}
std::unique_ptr<Node> Parser::parse_relational_expression() {
auto left = parse_operand_node();
auto left = parse_value();
if (!Lexer::isRelationalOperator(m_lexer.tokenType()))
{
@ -551,7 +520,7 @@ std::unique_ptr<Node> Parser::parse_relational_expression() {
}
auto operation = parse_relational_operator();
auto right = parse_operand_node();
auto right = parse_value();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
}

View File

@ -25,7 +25,6 @@ namespace usql {
float_value,
string_value,
bool_value,
database_value,
logical_operator,
relational_operator,
arithmetical_operator,
@ -40,7 +39,7 @@ namespace usql {
drop_table,
set,
show,
column_name,
database_value,
offset_limit,
column_order,
column_value,
@ -55,11 +54,6 @@ namespace usql {
Node(const NodeType type) : node_type(type) {}
};
struct ColNameNode : Node {
std::string name;
ColNameNode(const std::string col_name) : Node(NodeType::column_name), name(col_name) {}
};
struct ColOrderNode : Node {
std::string col_name;
@ -67,7 +61,7 @@ namespace usql {
bool ascending;
ColOrderNode(const std::string name, bool asc) : Node(NodeType::column_order), col_name(name), col_index(-1), ascending(asc) {}
ColOrderNode(int index, bool asc) : Node(NodeType::column_name), col_name(""), col_index(index), ascending(asc) {}
ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_name(""), col_index(index), ascending(asc) {}
};
@ -85,7 +79,7 @@ namespace usql {
std::string name;
SelectColNode(std::unique_ptr<Node> column, std::string alias) :
Node(NodeType::column_name), value(std::move(column)), name(alias) {}
Node(NodeType::database_value), value(std::move(column)), name(alias) {}
};
struct ColDefNode : Node {
@ -256,10 +250,10 @@ namespace usql {
struct InsertIntoTableNode : Node {
std::string table_name;
std::vector<ColNameNode> cols_names;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> values) :
InsertIntoTableNode(const std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) :
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(std::move(values)) {}
};
@ -285,11 +279,11 @@ namespace usql {
struct UpdateTableNode : Node {
std::string table_name;
std::vector<ColNameNode> cols_names;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
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<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> vals,
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)) {}
@ -369,7 +363,6 @@ namespace usql {
std::unique_ptr<Node> parse_expression();
std::unique_ptr<Node> parse_expression(std::unique_ptr<Node> left);
std::unique_ptr<Node> parse_operand_node();
std::unique_ptr<Node> parse_value();
RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator();

44395
tickers.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -148,7 +148,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// copy values
for (size_t i = 0; i < node.cols_names.size(); i++) {
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].col_name);
auto col_value = eval_value_node(table_def, new_row, node.cols_values[i].get());
new_row.setColumnValue(&col_def, col_value.get());
@ -169,7 +169,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
if (node.cols_names->size()==1 && node.cols_names->operator[](0).name == "*") {
node.cols_names->clear();
for(auto col : table->m_col_defs) {
node.cols_names->push_back(SelectColNode{std::move(std::make_unique<ColNameNode>(col.name)), col.name});
node.cols_names->push_back(SelectColNode{std::move(std::make_unique<DatabaseValueNode>(col.name)), col.name});
}
}
@ -263,7 +263,7 @@ void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) {
std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) {
std::string new_col_name = select_col_node->name;
if (select_col_node->value->node_type == NodeType::column_name) {
if (select_col_node->value->node_type == NodeType::database_value) {
ColDefNode src_cdef = table->get_column_def(new_col_name);
ColDefNode cdef = ColDefNode{new_col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null};
return std::make_tuple(src_cdef.order, cdef);
@ -279,6 +279,11 @@ std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColN
return std::make_tuple(-1, cdef);
}
throw Exception("Unsupported function");
} else if (select_col_node->value->node_type == NodeType::arithmetical_operator) {
// TODO return correct type
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::float_type, col_order, 1, true};
return std::make_tuple(-1, cdef);
}
throw Exception("Unsupported node type");
}
@ -313,7 +318,7 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
if (eval_where(node.where.get(), table, *row)) {
int i = 0;
for (const auto& col : node.cols_names) {
ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select
ColDefNode col_def = table->get_column_def(col.col_name); // TODO cache it like in select
std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, *row);
@ -390,7 +395,7 @@ bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table
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) {
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);
@ -398,6 +403,9 @@ std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *n
return eval_function_value_node(table, row, node);
} else if (node->node_type == NodeType::null_value) {
return std::make_unique<NullValueNode>();
} else if (node->node_type == NodeType::arithmetical_operator) {
// TODO correct type here
return eval_arithmetic_operator(ColumnType::float_type, static_cast<ArithmeticalOperatorNode &>(*node), table, row);
}
throw Exception("unsupported node type");
}