basic version of coalesce function
This commit is contained in:
35
debug.h
35
debug.h
@@ -10,33 +10,36 @@ std::vector<std::string> k_debug_sql_commands {
|
||||
// "delete from history_earnings_dates where symbol='BABA' and datetime=to_date('2021-11-04', '%Y-%m-%d')",
|
||||
// "select * from history_earnings_dates"
|
||||
|
||||
"create table sf1 (symbol varchar(8) not null, 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)",
|
||||
"set 'DATE_FORMAT' = '%Y-%m-%d'",
|
||||
"create index sf1_symbol on sf1(symbol)",
|
||||
"load into sf1 '/srv/SHARADAR_SF1.csv'",
|
||||
"set 'USE_INDEXSCAN' = 'false'",
|
||||
"select dimension, to_string(calendar_date, '%d.%m.%Y'), pp(eps, \"%.2f\"), pp(shareswadil), pp(revenue), pp(netinc), pp(cashneq), pp(assets), pp(debt), pp(ncfdebt), pp(roe*100), pp(intangibles), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ' order by dimension, calendar_date desc limit 5",
|
||||
"set 'USE_INDEXSCAN' = 'true'",
|
||||
"select dimension, to_string(calendar_date, '%d.%m.%Y'), pp(eps, \"%.2f\"), pp(shareswadil), pp(revenue), pp(netinc), pp(cashneq), pp(assets), pp(debt), pp(ncfdebt), pp(roe*100), pp(intangibles), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ' order by dimension, calendar_date desc limit 5",
|
||||
"select max(calendar_date), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ'",
|
||||
"select max(calendar_date), min(calendar_date) from sf1 where symbol = 'MU' and dimension = 'ARQ'"
|
||||
// "create table sf1 (symbol varchar(8) not null, 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)",
|
||||
// "set 'DATE_FORMAT' = '%Y-%m-%d'",
|
||||
// "create index sf1_symbol on sf1(symbol)",
|
||||
// "load into sf1 '/srv/SHARADAR_SF1.csv'",
|
||||
// "set 'USE_INDEXSCAN' = 'false'",
|
||||
// "select dimension, to_string(calendar_date, '%d.%m.%Y'), pp(eps, \"%.2f\"), pp(shareswadil), pp(revenue), pp(netinc), pp(cashneq), pp(assets), pp(debt), pp(ncfdebt), pp(roe*100), pp(intangibles), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ' order by dimension, calendar_date desc limit 5",
|
||||
// "set 'USE_INDEXSCAN' = 'true'",
|
||||
// "select dimension, to_string(calendar_date, '%d.%m.%Y'), pp(eps, \"%.2f\"), pp(shareswadil), pp(revenue), pp(netinc), pp(cashneq), pp(assets), pp(debt), pp(ncfdebt), pp(roe*100), pp(intangibles), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ' order by dimension, calendar_date desc limit 5",
|
||||
// "select max(calendar_date), calendar_date from sf1 where symbol = 'MU' and dimension = 'ARQ'",
|
||||
// "select max(calendar_date), min(calendar_date) from sf1 where symbol = 'MU' and dimension = 'ARQ'"
|
||||
|
||||
// "create table a (i integer not null, s varchar(64))",
|
||||
// "create index a_i on a(i)",
|
||||
"create table a (i integer not null, s varchar(64))",
|
||||
"create index a_i on a(i)",
|
||||
// "insert into a (i, s) values(1, 'one')",
|
||||
// "insert into a (i, s) values(2, 'two')",
|
||||
// "insert into a (i, s) values(2, 'second two')",
|
||||
// "insert into a (i, s) values(3, 'three')",
|
||||
// "insert into a (i, s) values(4, 'four')",
|
||||
// "save a into '/tmp/a.csv'",
|
||||
// "set 'USE_INDEXSCAN' = 'true'",
|
||||
"insert into a (i) values(5)",
|
||||
"save a into '/tmp/a.csv'",
|
||||
"set 'USE_INDEXSCAN' = 'true'",
|
||||
// "select * from a where 1 = i",
|
||||
// "delete from a where i = 2 and s ='two'",
|
||||
// "select * from a where i = 2",
|
||||
// "update a set i = 5 where i = 2",
|
||||
// "select * from a where i = 5",
|
||||
// "select max(i) from a where s = 'two'"
|
||||
// "select max(i) from a where s = 'two'",
|
||||
// "select min(i), max(i), count(i) from a",
|
||||
// "select i + 1000 from a"
|
||||
// "select i + 1000 from a",
|
||||
// "select i, s from a where i = 5",
|
||||
"select i, s, coalesce(s, i) from a where i = 5"
|
||||
};
|
||||
|
||||
|
||||
5
parser.h
5
parser.h
@@ -132,6 +132,7 @@ struct FunctionNode : Node {
|
||||
pp,
|
||||
lower,
|
||||
upper,
|
||||
coalesce,
|
||||
min,
|
||||
max,
|
||||
count
|
||||
@@ -144,6 +145,7 @@ struct FunctionNode : Node {
|
||||
if (str=="pp") return Type::pp;
|
||||
if (str=="lower") return Type::lower;
|
||||
if (str=="upper") return Type::upper;
|
||||
if (str=="coalesce") return Type::coalesce;
|
||||
if (str=="min") return Type::min;
|
||||
if (str=="max") return Type::max;
|
||||
if (str=="count") return Type::count;
|
||||
@@ -158,6 +160,7 @@ struct FunctionNode : Node {
|
||||
if (type == Type::pp) return "pp";
|
||||
if (type == Type::lower) return "lower";
|
||||
if (type == Type::upper) return "upper";
|
||||
if (type == Type::coalesce) return "coalesce";
|
||||
if (type == Type::min) return "min";
|
||||
if (type == Type::max) return "max";
|
||||
if (type == Type::count) return "count";
|
||||
@@ -166,7 +169,7 @@ struct FunctionNode : Node {
|
||||
};
|
||||
|
||||
|
||||
Type function;
|
||||
Type function;
|
||||
std::vector<std::unique_ptr<Node>> params;
|
||||
|
||||
FunctionNode(std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
|
||||
|
||||
3
usql.cpp
3
usql.cpp
@@ -173,6 +173,9 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
|
||||
evaluatedPars.push_back(eval_value_node(table, row, param.get(), nullptr, nullptr));
|
||||
}
|
||||
|
||||
// coalesce function can have first parameter null, so must be calles before following "return null"
|
||||
if (fnc->function == FunctionNode::Type::coalesce) return coalesce_function(evaluatedPars);
|
||||
|
||||
if (evaluatedPars.empty() || evaluatedPars[0]->isNull())
|
||||
return std::make_unique<NullValueNode>();
|
||||
|
||||
|
||||
1
usql.h
1
usql.h
@@ -73,6 +73,7 @@ private:
|
||||
|
||||
static std::unique_ptr<ValueNode> lower_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> upper_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> coalesce_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> to_date_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> to_string_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
static std::unique_ptr<ValueNode> date_add_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
|
||||
|
||||
@@ -239,6 +239,13 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
|
||||
// TODO get length, use get_db_column_definition
|
||||
ColDefNode col_def = ColDefNode{col_name, ColumnType::varchar_type, col_order, 256, true};
|
||||
return std::make_tuple(FUNCTION_CALL, col_def);
|
||||
} else if (func_node->function == FunctionNode::Type::coalesce) {
|
||||
// TODO handle cases here
|
||||
if (func_node->params.empty()) throw Exception("Coalesce without parameters");
|
||||
if (func_node->params[0]->node_type != NodeType::database_value) throw Exception("Coalesce first parameter must be database column");
|
||||
ColDefNode tbl_col_def = get_db_column_definition(table, func_node->params[0].get());
|
||||
ColDefNode col_def = ColDefNode{col_name, tbl_col_def.type, col_order, tbl_col_def.length, true};
|
||||
return std::make_tuple(FUNCTION_CALL, col_def);
|
||||
} else if (func_node->function == FunctionNode::Type::min || func_node->function == FunctionNode::Type::max) {
|
||||
auto col_type= ColumnType::float_type;
|
||||
size_t col_len = 1;
|
||||
|
||||
@@ -34,6 +34,13 @@ std::unique_ptr<ValueNode> USql::date_add_function(const std::vector<std::unique
|
||||
}
|
||||
|
||||
|
||||
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::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); });
|
||||
@@ -41,11 +48,21 @@ std::unique_ptr<ValueNode> USql::upper_function(const std::vector<std::unique_pt
|
||||
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::coalesce_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
for(const auto & par : evaluatedPars) {
|
||||
if (!par->isNull()) {
|
||||
// TODO implement rest and take it out as a function
|
||||
if (par->node_type == NodeType::int_value)
|
||||
return std::make_unique<IntValueNode>(par->getIntegerValue());
|
||||
if (par->node_type == NodeType::float_value)
|
||||
return std::make_unique<DoubleValueNode>(par->getDoubleValue());
|
||||
if (par->node_type == NodeType::string_value)
|
||||
return std::make_unique<StringValueNode>(par->getStringValue());
|
||||
if (par->node_type == NodeType::bool_value)
|
||||
return std::make_unique<BooleanValueNode>(par->getBooleanValue());
|
||||
}
|
||||
}
|
||||
return std::make_unique<NullValueNode>();
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> USql::pp_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||
|
||||
Reference in New Issue
Block a user