some TODO items squezed

This commit is contained in:
2021-12-19 12:07:26 +01:00
parent eebb270f47
commit 04c0ed3f03
5 changed files with 49 additions and 35 deletions

View File

@@ -32,6 +32,7 @@ std::vector<std::string> k_debug_sql_commands {
"update a set i = 5 where i = 2",
"select * from a where i = 5",
// "select max(i) from a where s = 'two'"
"select min(i), max(i), count(i) from a"
"select min(i), max(i), count(i) from a",
"select i + 1000 from a"
};

View File

@@ -147,7 +147,7 @@ void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const
commit_row(new_row);
}
void Table::print() {
void Table::print() {
std::string out{"| "};
std::string out2{"+-"};
@@ -261,7 +261,6 @@ bool Table::drop_index(const std::string &index_name) {
return false;
}
void Table::index_row(Index &index, const ColDefNode &col_def, const Row &row, const size_t rowid) {
index.insert(reinterpret_cast<ColValue *>(&row[col_def.order]), rowid);
}
@@ -275,7 +274,6 @@ void Table::reindex_row(Index &index, const ColDefNode &col_def, const Row &old_
index_row(index, col_def, new_row, rowid);
}
void Table::index_row(const Row &row) {
if (!m_indexes.empty()) {
const size_t rowid = get_rowid(row);
@@ -306,7 +304,6 @@ void Table::reindex_row(const Row &old_row, const Row &new_row) {
}
}
void Table::index_rows(const std::string &index_name) {
auto index = get_index(index_name);
@@ -318,7 +315,6 @@ void Table::index_rows(const std::string &index_name) {
}
}
Index * Table::get_index(const std::string &index_name) {
auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&index_name](const Index &idx) {
@@ -337,6 +333,13 @@ Index * Table::get_index_for_column(const std::string &col_name) {
return (it != m_indexes.end()) ? &(*it) : nullptr;
}
bool Table::empty() {
if (m_rows.empty()) return true;
for (const auto & r : m_rows)
if (r.is_visible()) return false;
return true;
}
Row *Table::rows_scanner::next() {
if (m_use_rowids) {

View File

@@ -59,6 +59,7 @@ struct Table {
Index * get_index(const std::string &index_name);
Index * get_index_for_column(const std::string &col_name);
bool empty();
struct rows_scanner {
explicit rows_scanner(Table *tbl) : m_use_rowids(false), m_table(tbl), m_fscan_itr(tbl->m_rows.begin()) {}

View File

@@ -174,9 +174,9 @@ std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row
}
// at this moment no functions without parameter(s) or first param can be null
// TODO exception here or better parsing
if (evaluatedPars.empty() || evaluatedPars[0]->isNull())
return std::make_unique<NullValueNode>();
throw Exception("eval_function_value_node, no function parameter or first is null, function: " + fnc->function);
// return std::make_unique<NullValueNode>();
// TODO use some enum
if (fnc->function == "lower") return lower_function(evaluatedPars);
@@ -216,8 +216,8 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
return std::make_unique<NullValueNode>();
if (outType == ColumnType::float_type) {
double l = left->getDoubleValue();
double r = right->getDoubleValue();
auto l = left->getDoubleValue();
auto r = right->getDoubleValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<DoubleValueNode>(l + r);
@@ -232,8 +232,8 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
}
} else if (outType == ColumnType::integer_type) {
long l = left->getIntegerValue();
long r = right->getIntegerValue();
auto l = left->getIntegerValue();
auto r = right->getIntegerValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r);
@@ -248,17 +248,27 @@ std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, Ar
}
} else if (outType == ColumnType::varchar_type) {
std::string l = left->getStringValue();
std::string r = right->getStringValue();
auto l = left->getStringValue();
auto r = right->getStringValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<StringValueNode>(l + r);
default:
throw Exception("eval_arithmetic_operator, varchar type implement me!!");
}
}
// TODO date node should support addition and subtraction
} else if (outType == ColumnType::date_type) {
auto l = left->getDateValue();
auto r = right->getDateValue();
switch (node.op) {
case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator:
return std::make_unique<IntValueNode>(l - r);
default:
throw Exception("eval_arithmetic_operator, date_type type implement me!!");
}
}
throw Exception("eval_arithmetic_operator, implement me!!");
}

View File

@@ -88,8 +88,7 @@ void USql::select_row(SelectFromTableNode &where_node,
Row *rslt_row = nullptr;
// when aggregate functions in rslt_table only one row exists
// TODO add function to get rows count
if (is_aggregated && !rslt_table->m_rows.empty())
if (is_aggregated && !rslt_table->empty())
rslt_row = &rslt_table->m_rows[0];
else
rslt_row = &rslt_table->create_empty_row();
@@ -265,7 +264,7 @@ std::tuple<int, ColDefNode> USql::get_node_definition(Table *table, Node * node,
auto [left_col_index, left_tbl_col_def] = get_node_definition(table, ari_node->left.get(), col_name, col_order );
auto [right_col_index, right_tbl_col_def] = get_node_definition(table, ari_node->right.get(), col_name, col_order );
ColumnType col_type; // TODO handle varchar and it len
ColumnType col_type; // TODO handle varchar and its len
if (left_tbl_col_def.type==ColumnType::float_type || right_tbl_col_def.type==ColumnType::float_type)
col_type = ColumnType::float_type;
else
@@ -357,7 +356,7 @@ std::unique_ptr<Table> USql::execute_update(const UpdateTableNode &node) {
int col_idx = 0;
for (const auto& col : node.cols_names) {
// TODO cache it like in select
// PERF cache it like in select
ColDefNode col_def = table->get_column_def(col.col_name);
std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[col_idx]), table, *row);