diff --git a/csvreader.cpp b/csvreader.cpp index eacf324..ecba6a4 100644 --- a/csvreader.cpp +++ b/csvreader.cpp @@ -18,27 +18,27 @@ namespace usql { } - int CsvReader::parseCSV(const std::string &filename, std::vector &cols_def, Table& table) { +int CsvReader::parseCSV(const std::string &filename, std::vector &cols_def, Table &table) { std::vector cdefs; cdefs.reserve(cols_def.size()); - for(auto & cd : cols_def) { + for (auto &cd : cols_def) { cdefs.emplace_back(table.get_column_def(cd.name)); } - int row_cnt = 0; - bool inQuote(false); - std::string field; - std::string csvSource; + int row_cnt = 0; + bool inQuote(false); + std::string field; + std::string csvSource; std::vector line; line.reserve(32); std::fstream newfile; newfile.open(filename, std::ios::in); - /// if (newfile.is_open()){ //checking whether the file is open + /// if (newfile.is_open()){ //checking whether the file is open - while(getline(newfile, csvSource)){ + while (getline(newfile, csvSource)) { if (skip_header && !header_skiped) { header_skiped = true; continue; @@ -69,13 +69,13 @@ namespace usql { field.clear(); line.clear(); - +// DEBUG if (row_cnt > 1000) break; } - newfile.close(); + newfile.close(); return row_cnt; - } +} int CsvReader::parseCSV2(const std::string &csvSource, std::vector &cols_def, Table& table) { int row_cnt = 0; diff --git a/main.cpp b/main.cpp index 2bf35cc..436c03c 100644 --- a/main.cpp +++ b/main.cpp @@ -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 sql_commands{ +void debug() { + std::__1::vector 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(); diff --git a/parser.cpp b/parser.cpp index 4b791a8..1b77a88 100644 --- a/parser.cpp +++ b/parser.cpp @@ -444,7 +444,7 @@ namespace usql { std::vector> 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 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(operation, std::move(left), std::move(right)); -} - } // namespace diff --git a/parser.h b/parser.h index 0354d1e..982bb4d 100644 --- a/parser.h +++ b/parser.h @@ -368,10 +368,8 @@ namespace usql { LogicalOperatorType parse_logical_operator(); ArithmeticalOperatorType parse_arithmetical_operator(); - std::unique_ptr parse_relational_expression(); - private: Lexer m_lexer; }; -} +} // namespace diff --git a/row.cpp b/row.cpp index e2738fb..c6a94aa 100644 --- a/row.cpp +++ b/row.cpp @@ -1,120 +1,126 @@ +#include #include "row.h" namespace usql { - int ColNullValue::compare(ColValue * other) { - return other->isNull() ? 0 : -1; // null goes to end - } +int ColNullValue::compare(ColValue *other) { + return other->isNull() ? 0 : -1; // null goes to end +} - int ColIntegerValue::compare(ColValue * other) { - long r = m_integer - other->getIntValue(); - return other->isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1; - } +int ColIntegerValue::compare(ColValue *other) { + long r = m_integer - other->getIntValue(); + return other->isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1; +} - int ColDoubleValue::compare(ColValue * other) { - if (other->isNull()) return 1; // null goes to end +int ColDoubleValue::compare(ColValue *other) { + if (other->isNull()) return 1; // null goes to end - double c = m_double - other->getDoubleValue(); - return c < 0 ? -1 : c == 0.0 ? 0 : 1; - } + double c = m_double - other->getDoubleValue(); + return c < 0 ? -1 : c == 0.0 ? 0 : 1; +} - int ColStringValue::compare(ColValue * other) { - return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end - } +int ColStringValue::compare(ColValue *other) { + return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end +} - int ColDateValue::compare(ColValue * other) { +int ColDateValue::compare(ColValue *other) { long r = m_date - other->getIntValue(); return other->isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1; - } +} - int ColBooleanValue::compare(ColValue * other) { - if (other->isNull()) return 1; // null goes to end +int ColBooleanValue::compare(ColValue *other) { + if (other->isNull()) return 1; // null goes to end - return m_bool==other->getBoolValue() ? 0 : m_bool && !other->getBoolValue() ? -1 : 1; // true first - } + return m_bool == other->getBoolValue() ? 0 : m_bool && !other->getBoolValue() ? -1 : 1; // true first +} - 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()); - } - } +Row::Row(int cols_count) { + m_columns.reserve(cols_count); + for (int i = 0; i < cols_count; i++) { + m_columns.emplace_back(std::make_unique()); + } +} - Row::Row(const Row &other) { - m_columns.reserve(other.m_columns.size()); - // PERF here we first set cols null and then immediately replace it - for (int i = 0; i < other.m_columns.size(); i++) { - m_columns.emplace_back(std::make_unique()); - } +Row::Row(const Row &other) { + m_columns.reserve(other.m_columns.size()); + // PERF here we first set cols null and then immediately replace it + for (int i = 0; i < other.m_columns.size(); i++) { + m_columns.emplace_back(std::make_unique()); + } - for (int i = 0; i < other.m_columns.size(); i++) { + for (int i = 0; i < other.m_columns.size(); i++) { if (other.m_columns[i]->isNull()) - continue; // for null NOP + continue; // for null NOP - ColumnType col_type = other.m_columns[i]->getColType(); - switch (col_type) { + ColumnType col_type = other.m_columns[i]->getColType(); + switch (col_type) { case ColumnType::integer_type : - setIntColumnValue(i, (static_cast(other.m_columns[i].get())->getIntValue())); + setIntColumnValue(i, + (static_cast(other.m_columns[i].get())->getIntValue())); break; case ColumnType::float_type : - setFloatColumnValue(i, (static_cast(other.m_columns[i].get())->getDoubleValue())); + setFloatColumnValue(i, + (static_cast(other.m_columns[i].get())->getDoubleValue())); break; case ColumnType::varchar_type : - setStringColumnValue(i, (static_cast(other.m_columns[i].get())->getStringValue())); + setStringColumnValue(i, + (static_cast(other.m_columns[i].get())->getStringValue())); break; case ColumnType::date_type : - setDateColumnValue(i, (static_cast(other.m_columns[i].get())->getDateValue())); + setDateColumnValue(i, + (static_cast(other.m_columns[i].get())->getDateValue())); break; case ColumnType::bool_type : - setBoolColumnValue(i, (static_cast(other.m_columns[i].get())->getBoolValue())); - break; + setBoolColumnValue(i, + (static_cast(other.m_columns[i].get())->getBoolValue())); + break; default: - throw Exception("unsupported column type"); + throw Exception("unsupported column type"); } - } - } + } +} - Row &Row::operator=(Row other) { - std::swap(m_columns, other.m_columns); - return *this; - } +Row &Row::operator=(Row other) { + std::swap(m_columns, other.m_columns); + return *this; +} - void Row::setColumnNull(int col_index) { +void Row::setColumnNull(int col_index) { m_columns[col_index] = std::make_unique(); - } +} - void Row::setIntColumnValue(int col_index, long value) { - m_columns[col_index] = std::make_unique(value); - } +void Row::setIntColumnValue(int col_index, long value) { + m_columns[col_index] = std::make_unique(value); +} - void Row::setFloatColumnValue(int col_index, double value) { - m_columns[col_index] = std::make_unique(value); - } +void Row::setFloatColumnValue(int col_index, double value) { + m_columns[col_index] = std::make_unique(value); +} - void Row::setStringColumnValue(int col_index, const std::string &value) { - m_columns[col_index] = std::make_unique(value); - } +void Row::setStringColumnValue(int col_index, const std::string &value) { + m_columns[col_index] = std::make_unique(value); +} - void Row::setDateColumnValue(int col_index, long value) { - m_columns[col_index] = std::make_unique(value); - } +void Row::setDateColumnValue(int col_index, long value) { + m_columns[col_index] = std::make_unique(value); +} - void Row::setDateColumnValue(int col_index, const std::string &value) { +void Row::setDateColumnValue(int col_index, const std::string &value) { m_columns[col_index] = std::make_unique(Settings::string_to_date(value)); - } +} - void Row::setBoolColumnValue(int col_index, bool value) { - m_columns[col_index] = std::make_unique(value); - } +void Row::setBoolColumnValue(int col_index, bool value) { + m_columns[col_index] = std::make_unique(value); +} - void Row::setBoolColumnValue(int col_index, const std::string &value) { - bool v = (value=="Y" || value=="1"); - m_columns[col_index] = std::make_unique(v); - } +void Row::setBoolColumnValue(int col_index, const std::string &value) { + bool v = (value == "Y" || value == "1"); + m_columns[col_index] = std::make_unique(v); +} - void Row::setColumnValue(ColDefNode *col_def, ColValue *col_value) { +void Row::setColumnValue(ColDefNode *col_def, ColValue *col_value) { if (!col_value->isNull()) { if (col_def->type == ColumnType::integer_type) setIntColumnValue(col_def->order, col_value->getIntValue()); @@ -129,7 +135,7 @@ namespace usql { } else { setColumnNull(col_def->order); } - } +} void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) { if (!col_value->isNull()) { @@ -150,25 +156,22 @@ void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) { } } - int Row::compare(const Row & other) const { - for (int ci = 0; ci < m_columns.size(); ci++) { - int cmp = m_columns[ci]->compare(other.ith_column(ci)); - if (cmp != 0) return cmp; - } - return 0; - } +int Row::compare(const Row &other) const { + for (int ci = 0; ci < m_columns.size(); ci++) { + int cmp = m_columns[ci]->compare(other.ith_column(ci)); + if (cmp != 0) return cmp; + } + return 0; +} - void Row::print(const std::vector & col_char_sizes) { - std::string out{"| "}; +void Row::print(const std::vector &col_char_sizes) { + std::string out{"| "}; - for (int ci = 0; ci < m_columns.size(); ci++) { - auto value = m_columns[ci]->getStringValue(); + for (int ci = 0; ci < m_columns.size(); ci++) { + out.append(string_padd(m_columns[ci]->getStringValue(), col_char_sizes[ci], ' ', false) + " | "); + } - // TODO use rpad string function handle len - out.append(value + std::string(col_char_sizes[ci] - value.size(), ' ') + " | "); - } + std::cout << out << std::endl; +} - std::cout << out << std::endl; - } - -} \ No newline at end of file +} // namespace diff --git a/table.cpp b/table.cpp index c724a04..8008767 100644 --- a/table.cpp +++ b/table.cpp @@ -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 &colDefs, const Row& new_row = create_empty_row(); // copy values - for (size_t i = 0; i < std::min(columns_count(), csv_line.size()); i++) { + for (int i = 0; i < std::min(columns_count(), csv_line.size()); i++) { const ColDefNode & col_def = colDefs[i]; if (csv_line[i].empty()) { diff --git a/table.h b/table.h index b3ce303..e4fce1e 100644 --- a/table.h +++ b/table.h @@ -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); diff --git a/usql.cpp b/usql.cpp index 09de491..924d031 100644 --- a/usql.cpp +++ b/usql.cpp @@ -138,11 +138,12 @@ std::unique_ptr USql::execute_show(ShowNode &node) { } std::unique_ptr
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
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(col.name)), col.name}); + node.cols_names->emplace_back(SelectColNode{std::make_unique(col.name), col.name}); } } @@ -179,7 +181,7 @@ std::unique_ptr
USql::execute_select(SelectFromTableNode &node) { std::vector source_table_col_index{}; for (int i = 0; i < node.cols_names->size(); i++) { - auto [ src_tbl_col_index, rst_tbl_col_def ] = get_column_definition(table, &node.cols_names->operator[](i), i); + auto [src_tbl_col_index, rst_tbl_col_def] = get_column_definition(table, &node.cols_names->operator[](i), i); source_table_col_index.push_back(src_tbl_col_index); result_tbl_col_defs.push_back(rst_tbl_col_def); @@ -297,7 +299,7 @@ std::unique_ptr
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 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
USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) { +std::unique_ptr
USql::create_stmt_result_table(long code, const std::string &text, size_t affected_rows) { std::vector 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); diff --git a/usql.h b/usql.h index 17110e7..fa5b4e5 100644 --- a/usql.h +++ b/usql.h @@ -46,7 +46,7 @@ private: static std::unique_ptr eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) ; - static std::unique_ptr
create_stmt_result_table(long code, const std::string &text, long affected_rows); + static std::unique_ptr
create_stmt_result_table(long code, const std::string &text, size_t affected_rows); static std::tuple get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ; Table *find_table(const std::string &name);