diff --git a/csvreader.cpp b/csvreader.cpp index 90f3954..e765f02 100644 --- a/csvreader.cpp +++ b/csvreader.cpp @@ -70,7 +70,7 @@ int CsvReader::parseCSV(const std::string &filename, std::vector &co field.clear(); line.clear(); // DEBUG -// if (row_cnt > 1000) break; +// if (row_cnt > 10000) break; // } diff --git a/main.cpp b/main.cpp index 49332bb..2346ac9 100644 --- a/main.cpp +++ b/main.cpp @@ -134,10 +134,10 @@ void debug() { // "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 ticker, calendar_date from sf1 where calendar_date > to_date('2019-01-01', '%Y-%m-%d') limit 1", -// "select ticker, dimension, calendar_date, eps, dps from sf1 where (ticker = 'AIG' or ticker = 'AI') and dimension = 'MRY' order by 3 desc", +// "select ticker, calendar_date from sf1 where calendar_date > to_date('2019-01-01', '%Y-%m-%d') limit 1", +// "select ticker, dimension, calendar_date, eps, dps, roa*100 as roa, roe*100 as roe, pp(revenue), netinc from sf1 where (ticker = 'AIG' or ticker = 'WFC') and dimension = 'MRY' and calendar_date > to_date('2019-01-01', '%Y-%m-%d') order by 3 desc + "select ticker, dimension, calendar_date, eps, dps, roa*100 as roa, roe*100 as roe, pp(revenue) as revenue, pp(netinc) as netinc from sf1 where (ticker != 'AIG' and ticker != 'WFC') and dimension = 'MRY' and calendar_date > to_date('2019-01-01', '%Y-%m-%d') 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", // "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, f) values(1 + 10000, upper('one'), 'Y', 3.1415)", diff --git a/usql.cpp b/usql.cpp index 84028b2..493cebd 100644 --- a/usql.cpp +++ b/usql.cpp @@ -1,6 +1,7 @@ #include "usql.h" #include "exception.h" #include "ml_date.h" +#include "ml_string.h" #include #include @@ -284,6 +285,9 @@ std::tuple USql::get_node_definition(Table *table, Node * node, } else if (func_node->function == "to_date") { ColDefNode col_def = ColDefNode{col_name, ColumnType::integer_type, col_order, 1, true}; return std::make_tuple(-1, col_def); + } else if (func_node->function == "pp") { + ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 10, true}; + return std::make_tuple(-1, col_def); } throw Exception("Unsupported function"); @@ -524,6 +528,44 @@ std::unique_ptr USql::eval_function_value_node(Table *table, Row &row std::string formatted_date = date_to_string(date, format); return std::make_unique(formatted_date); } + if (fnc->function == "pp") { + 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}; + double value = parsed_value->getDoubleValue(); + + if (format == "100%") + 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(parsed_value->getStringValue()); + + std::string s {buf}; + return std::make_unique(string_padd(s.erase(s.find_last_not_of(" ")+1), 10, ' ', false)); + } + + return std::make_unique(parsed_value->getStringValue()); + } + throw Exception("invalid function"); }