166 lines
6.8 KiB
C++
166 lines
6.8 KiB
C++
#include "usql.h"
|
|
#include "exception.h"
|
|
#include "ml_date.h"
|
|
#include "ml_string.h"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace usql {
|
|
|
|
std::unique_ptr<ValueNode> USql::to_string_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
|
long date = evaluatedPars[0]->getDateValue();
|
|
std::string format = evaluatedPars[1]->getStringValue();
|
|
std::string formatted_date = date_to_string(date, format);
|
|
return std::make_unique<StringValueNode>(formatted_date);
|
|
}
|
|
|
|
std::unique_ptr<ValueNode> USql::to_date_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
|
std::string date = evaluatedPars[0]->getStringValue();
|
|
std::string format = evaluatedPars[1]->getStringValue();
|
|
long epoch_time = string_to_date(date, format);
|
|
return std::make_unique<IntValueNode>(epoch_time); // No DateValueNode for now
|
|
}
|
|
|
|
std::unique_ptr<ValueNode> USql::date_add_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
|
long datetime = evaluatedPars[0]->getDateValue();
|
|
long quantity = evaluatedPars[1]->getIntegerValue();
|
|
std::string part = evaluatedPars[2]->getStringValue();
|
|
|
|
long new_date = add_to_date(datetime, quantity, part);
|
|
return std::make_unique<IntValueNode>(new_date); // No DateValueNode for now
|
|
}
|
|
|
|
|
|
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); });
|
|
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::pp_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
|
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}; // TODO constant here
|
|
double value = parsed_value->getDoubleValue();
|
|
|
|
if (format == "100%")
|
|
std::snprintf(buf, 20, "%.2f%%", value);
|
|
else if (format == "%.2f")
|
|
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<StringValueNode>(parsed_value->getStringValue().substr(0, 10));
|
|
// TODO introduce constant for 10
|
|
std::string s {buf};
|
|
return std::make_unique<StringValueNode>(string_padd(s.erase(s.find_last_not_of(' ')+1), 10, ' ', false));
|
|
}
|
|
return std::make_unique<StringValueNode>(parsed_value->getStringValue());
|
|
}
|
|
|
|
std::unique_ptr<ValueNode> USql::max_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node, ColValue *agg_func_value) {
|
|
if (col_def_node->type == ColumnType::integer_type || col_def_node->type == ColumnType::date_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
auto val = evaluatedPars[0]->getIntegerValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<IntValueNode>(val);
|
|
else
|
|
return std::make_unique<IntValueNode>(std::max(val, agg_func_value->getIntegerValue()));
|
|
} else {
|
|
return std::make_unique<IntValueNode>(agg_func_value->getIntegerValue());
|
|
}
|
|
} else if (col_def_node->type == ColumnType::float_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
auto val = evaluatedPars[0]->getDoubleValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<DoubleValueNode>(val);
|
|
else
|
|
return std::make_unique<DoubleValueNode>(std::max(val, agg_func_value->getDoubleValue()));
|
|
} else {
|
|
return std::make_unique<DoubleValueNode>(agg_func_value->getDoubleValue());
|
|
}
|
|
} else if (col_def_node->type == ColumnType::varchar_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
auto val = evaluatedPars[0]->getStringValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<StringValueNode>(val);
|
|
else
|
|
return std::make_unique<StringValueNode>(std::max(val, agg_func_value->getStringValue()));
|
|
} else {
|
|
return std::make_unique<StringValueNode>(agg_func_value->getStringValue());
|
|
}
|
|
}
|
|
|
|
throw Exception("unsupported data type for max function");
|
|
}
|
|
|
|
std::unique_ptr<ValueNode> USql::min_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars, const ColDefNode *col_def_node,
|
|
ColValue *agg_func_value) {
|
|
if (col_def_node->type == ColumnType::integer_type || col_def_node->type == ColumnType::date_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
long val = evaluatedPars[0]->getIntegerValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<IntValueNode>(val);
|
|
else
|
|
return std::make_unique<IntValueNode>(std::min(val, agg_func_value->getIntegerValue()));
|
|
} else {
|
|
return std::make_unique<IntValueNode>(agg_func_value->getIntegerValue());
|
|
}
|
|
} else if (col_def_node->type == ColumnType::float_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
double val = evaluatedPars[0]->getDoubleValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<DoubleValueNode>(val);
|
|
else
|
|
return std::make_unique<DoubleValueNode>(std::min(val, agg_func_value->getDoubleValue()));
|
|
} else {
|
|
return std::make_unique<DoubleValueNode>(agg_func_value->getDoubleValue());
|
|
}
|
|
} else if (col_def_node->type == ColumnType::varchar_type) {
|
|
if (!evaluatedPars[0]->isNull()) {
|
|
auto val = evaluatedPars[0]->getStringValue();
|
|
if (agg_func_value->isNull())
|
|
return std::make_unique<StringValueNode>(val);
|
|
else
|
|
return std::make_unique<StringValueNode>(std::min(val, agg_func_value->getStringValue()));
|
|
} else {
|
|
return std::make_unique<StringValueNode>(agg_func_value->getStringValue());
|
|
}
|
|
}
|
|
|
|
throw Exception("unsupported data type for min function");
|
|
}
|
|
|
|
std::unique_ptr<ValueNode> USql::count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
|
long c = 1;
|
|
if (!agg_func_value->isNull()) {
|
|
c = agg_func_value->getIntegerValue() + 1;
|
|
}
|
|
return std::make_unique<IntValueNode>(c);
|
|
}
|
|
|
|
} // namespace
|