From 04c0ed3f034460b52b9bf95ad3b423ea643b1e17 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Sun, 19 Dec 2021 12:07:26 +0100 Subject: [PATCH] some TODO items squezed --- debug.h | 3 ++- table.cpp | 13 ++++++++----- table.h | 1 + usql.cpp | 30 ++++++++++++++++++++---------- usql_dml.cpp | 37 ++++++++++++++++++------------------- 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/debug.h b/debug.h index 9018fc8..9b5ca77 100644 --- a/debug.h +++ b/debug.h @@ -32,6 +32,7 @@ std::vector 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" }; diff --git a/table.cpp b/table.cpp index 80df9a9..aa92412 100644 --- a/table.cpp +++ b/table.cpp @@ -147,7 +147,7 @@ void Table::create_row_from_vector(const std::vector &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(&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) { diff --git a/table.h b/table.h index e890955..736de59 100644 --- a/table.h +++ b/table.h @@ -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()) {} diff --git a/usql.cpp b/usql.cpp index 392cef4..dc901fe 100644 --- a/usql.cpp +++ b/usql.cpp @@ -174,9 +174,9 @@ std::unique_ptr 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(); + throw Exception("eval_function_value_node, no function parameter or first is null, function: " + fnc->function); + // return std::make_unique(); // TODO use some enum if (fnc->function == "lower") return lower_function(evaluatedPars); @@ -216,8 +216,8 @@ std::unique_ptr USql::eval_arithmetic_operator(ColumnType outType, Ar return std::make_unique(); 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(l + r); @@ -232,8 +232,8 @@ std::unique_ptr 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(l + r); @@ -248,17 +248,27 @@ std::unique_ptr 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(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(l + r); + case ArithmeticalOperatorType::minus_operator: + return std::make_unique(l - r); + default: + throw Exception("eval_arithmetic_operator, date_type type implement me!!"); + } + } throw Exception("eval_arithmetic_operator, implement me!!"); } diff --git a/usql_dml.cpp b/usql_dml.cpp index ec2b192..0e810fe 100644 --- a/usql_dml.cpp +++ b/usql_dml.cpp @@ -88,26 +88,25 @@ 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()) - rslt_row = &rslt_table->m_rows[0]; - else - rslt_row = &rslt_table->create_empty_row(); + if (is_aggregated && !rslt_table->empty()) + rslt_row = &rslt_table->m_rows[0]; + else + rslt_row = &rslt_table->create_empty_row(); - for (auto idx = 0; idx < rslt_table->columns_count(); idx++) { - auto src_table_col_idx = src_table_col_index[idx]; + for (auto idx = 0; idx < rslt_table->columns_count(); idx++) { + auto src_table_col_idx = src_table_col_index[idx]; - if (src_table_col_idx == FUNCTION_CALL) { - auto evaluated_value = eval_value_node(src_table, *src_row, where_node.cols_names->operator[](idx).value.get(), - const_cast(&rslt_tbl_col_defs[idx]), &rslt_row->operator[](idx)); - ValueNode *col_value = evaluated_value.get(); + if (src_table_col_idx == FUNCTION_CALL) { + auto evaluated_value = eval_value_node(src_table, *src_row, where_node.cols_names->operator[](idx).value.get(), + const_cast(&rslt_tbl_col_defs[idx]), &rslt_row->operator[](idx)); + ValueNode *col_value = evaluated_value.get(); - rslt_row->setColumnValue((ColDefNode *) &rslt_tbl_col_defs[idx], col_value); - } else { - ColValue &col_value = src_row->operator[](src_table_col_idx); - rslt_row->setColumnValue((ColDefNode *) &rslt_tbl_col_defs[idx], col_value); - } - } + rslt_row->setColumnValue((ColDefNode *) &rslt_tbl_col_defs[idx], col_value); + } else { + ColValue &col_value = src_row->operator[](src_table_col_idx); + rslt_row->setColumnValue((ColDefNode *) &rslt_tbl_col_defs[idx], col_value); + } + } // for aggregate is validated more than needed rslt_table->commit_row(*rslt_row); @@ -265,7 +264,7 @@ std::tuple 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 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 new_val = eval_arithmetic_operator(col_def.type, static_cast(*node.values[col_idx]), table, *row);