some more TODOs resolved

This commit is contained in:
VaclavT 2021-08-14 15:07:41 +02:00
parent 0f586aab8a
commit fc0ce36e8d
9 changed files with 136 additions and 145 deletions

View File

@ -69,7 +69,7 @@ namespace usql {
field.clear();
line.clear();
// DEBUG if (row_cnt > 1000) break;
}
newfile.close();

View File

@ -79,6 +79,7 @@ void close_linenoise() {
linenoiseHistorySave(history_file.c_str());
}
void repl() {
std::string code;
std::string input;
@ -124,19 +125,19 @@ void repl() {
close_linenoise();
}
int main(int argc, char *argv[]) {
std::vector<std::string> sql_commands{
void debug() {
std::__1::vector<std::string> sql_commands{
// "create table ticker ( tablee varchar(5) not null, permaticker integer, ticker varchar(10) not null, name varchar(256) not null, exchange varchar(32), isdelisted boolean, category varchar(32), cusips varchar(256), siccode integer, sicsector varchar(256), sicindustry varchar(256), famasector varchar(256), famaindustry varchar(256), sector varchar(128), industry varchar(128), scalemarketcap varchar(64), scalerevenue varchar(64), relatedtickers varchar(128), currency varchar(3), location varchar(64), lastupdated date, firstadded date, firstpricedate date, lastpricedate date, firstquarter date, lastquarter date, secfilings varchar(256), companysite varchar(256))",
// "load ticker from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/tickers.csv')",
// "select * from ticker where ticker = 'WFC' and tablee = 'SF1'",
// "set 'DATE_FORMAT' = '%Y-%m-%d'",
// "show 'DATE_FORMAT'",
// "create table sf1 ( ticker varchar(8), dimension varchar(3), calendar_date date, date_key date, report_period date, last_updated date, accoci float, assets float, assetsavg float, assetsc float, assetsnc float, assetturnover float, bvps float, capex float, cashneq float, cashnequsd float, cor float, consolinc float, currentratio float, de float, debt float, debtc float, debtnc float, debtusd float, deferredrev float, depamor float, deposits float, divyield float, dps float, ebit float, ebitda float, ebitdamargin float, ebitdausd float, ebitusd float, ebt float, eps float, epsdil float, epsusd float, equity float, equityavg float, equityusd float, ev float, evebit float, evebitda float, fcf float, fcfps float, fxusd float, gp float, grossmargin float, intangibles float, intexp float, invcap float, invcapavg float, inventory float, investments float, investmentsc float, investmentsnc float, liabilities float, liabilitiesc float, liabilitiesnc float, marketcap float, ncf float, ncfbus float, ncfcommon float, ncfdebt float, ncfdiv float, ncff float, ncfi float, ncfinv float, ncfo float, ncfx float, netinc float, netinccmn float, netinccmnusd float, netincdis float, netincnci float, netmargin float, opex float, opinc float, payables float, payoutratio float, pb float, pe float, pe1 float, ppnenet float, prefdivis float, price float, ps float, ps1 float, receivables float, retearn float, revenue float, revenueusd float, rnd float, roa float, roe float, roic float, ros float, sbcomp float, sgna float, sharefactor float, sharesbas float, shareswa float, shareswadil float, sps float, tangibles float, taxassets float, taxexp float, taxliabilities float, tbvps float, workingcapital float)",
// "load sf1 from '/tmp/sf1.csv')",
"set 'DATE_FORMAT' = '%Y-%m-%d'",
"show 'DATE_FORMAT'",
"create table sf1 ( ticker varchar(8), dimension varchar(3), calendar_date date, date_key date, report_period date, last_updated date, accoci float, assets float, assetsavg float, assetsc float, assetsnc float, assetturnover float, bvps float, capex float, cashneq float, cashnequsd float, cor float, consolinc float, currentratio float, de float, debt float, debtc float, debtnc float, debtusd float, deferredrev float, depamor float, deposits float, divyield float, dps float, ebit float, ebitda float, ebitdamargin float, ebitdausd float, ebitusd float, ebt float, eps float, epsdil float, epsusd float, equity float, equityavg float, equityusd float, ev float, evebit float, evebitda float, fcf float, fcfps float, fxusd float, gp float, grossmargin float, intangibles float, intexp float, invcap float, invcapavg float, inventory float, investments float, investmentsc float, investmentsnc float, liabilities float, liabilitiesc float, liabilitiesnc float, marketcap float, ncf float, ncfbus float, ncfcommon float, ncfdebt float, ncfdiv float, ncff float, ncfi float, ncfinv float, ncfo float, ncfx float, netinc float, netinccmn float, netinccmnusd float, netincdis float, netincnci float, netmargin float, opex float, opinc float, payables float, payoutratio float, pb float, pe float, pe1 float, ppnenet float, prefdivis float, price float, ps float, ps1 float, receivables float, retearn float, revenue float, revenueusd float, rnd float, roa float, roe float, roic float, ros float, sbcomp float, sgna float, sharefactor float, sharesbas float, shareswa float, shareswadil float, sps float, tangibles float, taxassets float, taxexp float, taxliabilities float, tbvps float, workingcapital float)",
"load sf1 from '/tmp/sf1.csv')",
// "select * from sf1 where ticker = 'WFC'",
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'AI') and dimension = 'MRY' order by 3 desc",
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'AI') and (dimension = 'MRY' or dimension = 'MRQ') order by 3 desc",
// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'WFC') and dimension = 'MRY' order by 3 desc",
"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')",
"insert into a (i, s, b) values(1 + 10000, upper('one'), 'Y')",
@ -195,6 +196,11 @@ int main(int argc, char *argv[]) {
}
std::cout << std::endl << std::endl;
}
int main(int argc, char *argv[]) {
debug();
// repl();

View File

@ -444,7 +444,7 @@ namespace usql {
std::vector<std::unique_ptr<Node>> pars;
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
while (m_lexer.tokenType() != TokenType::close_paren && m_lexer.tokenType() != TokenType::eof) {
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
@ -521,22 +521,5 @@ namespace usql {
}
}
std::unique_ptr<Node> Parser::parse_relational_expression() {
auto left = parse_value();
if (!Lexer::isRelationalOperator(m_lexer.tokenType()))
{
if (left->node_type != NodeType::relational_operator)
throw Exception("syntax error in relational operation");
return left;
}
auto operation = parse_relational_operator();
auto right = parse_value();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
}
} // namespace

View File

@ -368,10 +368,8 @@ namespace usql {
LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator();
std::unique_ptr<Node> parse_relational_expression();
private:
Lexer m_lexer;
};
}
} // namespace

25
row.cpp
View File

@ -1,4 +1,5 @@
#include <ml_string.h>
#include "row.h"
namespace usql {
@ -37,7 +38,7 @@ namespace usql {
Row::Row(int cols_count) {
m_columns.reserve(cols_count);
for (int i = 0; i < cols_count; i++) {
m_columns.push_back(std::make_unique<ColNullValue>());
m_columns.emplace_back(std::make_unique<ColNullValue>());
}
}
@ -55,19 +56,24 @@ namespace usql {
ColumnType col_type = other.m_columns[i]->getColType();
switch (col_type) {
case ColumnType::integer_type :
setIntColumnValue(i, (static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
setIntColumnValue(i,
(static_cast<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
break;
case ColumnType::float_type :
setFloatColumnValue(i, (static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
setFloatColumnValue(i,
(static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
break;
case ColumnType::varchar_type :
setStringColumnValue(i, (static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
setStringColumnValue(i,
(static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
break;
case ColumnType::date_type :
setDateColumnValue(i, (static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
setDateColumnValue(i,
(static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
break;
case ColumnType::bool_type :
setBoolColumnValue(i, (static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
setBoolColumnValue(i,
(static_cast<ColBooleanValue *>(other.m_columns[i].get())->getBoolValue()));
break;
default:
throw Exception("unsupported column type");
@ -162,13 +168,10 @@ void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) {
std::string out{"| "};
for (int ci = 0; ci < m_columns.size(); ci++) {
auto value = m_columns[ci]->getStringValue();
// TODO use rpad string function handle len
out.append(value + std::string(col_char_sizes[ci] - value.size(), ' ') + " | ");
out.append(string_padd(m_columns[ci]->getStringValue(), col_char_sizes[ci], ' ', false) + " | ");
}
std::cout << out << std::endl;
}
}
} // namespace

View File

@ -1,4 +1,3 @@
#include "table.h"
#include "csvreader.h"
#include "ml_string.h"
@ -93,7 +92,7 @@ int Table::load_csv_file(const std::string &filename) {
if (infile.good()) {
std::string sLine;
std::getline(infile, sLine);
line_size = sLine.size();
line_size = (int)sLine.size();
}
infile.close();
@ -113,7 +112,7 @@ void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const
Row& new_row = create_empty_row();
// copy values
for (size_t i = 0; i < std::min<int>(columns_count(), csv_line.size()); i++) {
for (int i = 0; i < std::min<int>(columns_count(), csv_line.size()); i++) {
const ColDefNode & col_def = colDefs[i];
if (csv_line[i].empty()) {

View File

@ -15,8 +15,8 @@ namespace usql {
ColDefNode get_column_def(const std::string &col_name);
ColDefNode get_column_def(int col_index);
int columns_count() const { return m_col_defs.size(); }; // TODO size_t type?
int rows_count() const { return m_rows.size(); };
int columns_count() const { return (int) m_col_defs.size(); };
size_t rows_count() const { return m_rows.size(); };
Row& create_empty_row();
void commit_row(const Row &row);

View File

@ -138,11 +138,12 @@ std::unique_ptr<Table> USql::execute_show(ShowNode &node) {
}
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
// TODO check column names.size = values.size
// find table
Table *table_def = find_table(node.table_name);
if (node.cols_names.size() != node.cols_values.size())
throw Exception("Incorrect number of values");
// prepare empty new_row
Row& new_row = table_def->create_empty_row();
@ -168,8 +169,9 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// expand *
if (node.cols_names->size()==1 && node.cols_names->operator[](0).name == "*") {
node.cols_names->clear();
node.cols_names->reserve(table->columns_count());
for(auto col : table->m_col_defs) {
node.cols_names->push_back(SelectColNode{std::move(std::make_unique<DatabaseValueNode>(col.name)), col.name});
node.cols_names->emplace_back(SelectColNode{std::make_unique<DatabaseValueNode>(col.name), col.name});
}
}
@ -297,7 +299,7 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
Table *table = find_table(node.table_name);
// execute access plan
int affected_rows = table->rows_count();
auto affected_rows = table->rows_count();
table->m_rows.erase(
std::remove_if(table->m_rows.begin(), table->m_rows.end(),
@ -565,13 +567,13 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
throw Exception("implement me!!");
}
}
// TODO date node should support addition and substraction
// TODO date node should support addition and subtraction
throw Exception("implement me!!");
}
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) {
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string &text, size_t affected_rows) {
std::vector<ColDefNode> result_tbl_col_defs{};
result_tbl_col_defs.emplace_back("code", ColumnType::integer_type, 0, 1, false);
result_tbl_col_defs.emplace_back("desc", ColumnType::varchar_type, 1, 48, false);

2
usql.h
View File

@ -46,7 +46,7 @@ private:
static std::unique_ptr<ValueNode> eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) ;
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, long affected_rows);
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, size_t affected_rows);
static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ;
Table *find_table(const std::string &name);