small refactorings

This commit is contained in:
VaclavT 2021-12-19 16:31:54 +01:00
parent 2a138d0c81
commit 498d1f17aa
7 changed files with 41 additions and 39 deletions

View File

@ -19,8 +19,8 @@ MlValue uSQL::ivaluize(const usql::Table *table) {
for (auto row : table->m_rows) {
columns.clear();
for (int i = 0; i < table->columns_count(); i++) {
auto & c = row[i];
auto type = table->m_col_defs[i].type;
const auto & c = row[i];
const auto type = table->m_col_defs[i].type;
if (c.isNull()) {
columns.push_back(MlValue::nil());
} else if (type == ColumnType::integer_type || type == ColumnType::date_type) {

View File

@ -250,9 +250,9 @@ void Table::create_index(const Index& index) {
bool Table::drop_index(const std::string &index_name) {
auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&index_name](const Index &idx) {
return idx.get_index_name() == index_name;
});
[&index_name](const Index &idx) {
return idx.get_index_name() == index_name;
});
if (it != m_indexes.end()) {
m_indexes.erase(it);
@ -317,24 +317,25 @@ 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) {
return idx.get_index_name() == index_name;
});
[&index_name](const Index &idx) {
return idx.get_index_name() == index_name;
});
return (it != m_indexes.end()) ? &(*it) : nullptr;
}
Index * Table::get_index_for_column(const std::string &col_name) {
auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&col_name](const Index &idx) {
return idx.get_column_name() == col_name;
});
[&col_name](const Index &idx) {
return idx.get_column_name() == col_name;
});
return (it != m_indexes.end()) ? &(*it) : nullptr;
}
bool Table::empty() {
bool Table::empty() const {
if (m_rows.empty()) return true;
for (const auto & r : m_rows)
if (r.is_visible()) return false;

View File

@ -36,8 +36,8 @@ struct Table {
void print();
std::string m_name;
std::vector<ColDefNode> m_col_defs;
std::string m_name;
std::vector<ColDefNode> m_col_defs;
std::vector<Row> m_rows;
std::vector<Index> m_indexes;
@ -59,7 +59,7 @@ struct Table {
Index * get_index(const std::string &index_name);
Index * get_index_for_column(const std::string &col_name);
bool empty();
bool empty() const;
struct rows_scanner {
explicit rows_scanner(Table *tbl) : m_use_rowids(false), m_table(tbl), m_fscan_itr(tbl->m_rows.begin()) {}
@ -72,7 +72,7 @@ struct Table {
Table * m_table;
std::vector<Row>::iterator m_fscan_itr;
std::vector<rowid_t> m_rowids;
size_t m_rowids_idx{};
size_t m_rowids_idx{};
};
};

View File

@ -84,10 +84,10 @@ private:
static std::unique_ptr<ValueNode> count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars);
static void select_row(SelectFromTableNode &where_node,
Table *src_table, Row *src_row,
Table *rslt_table,
const std::vector<ColDefNode> &rslt_tbl_col_defs, const std::vector<int> &src_table_col_index,
bool is_aggregated) ;
Table *src_table, Row *src_row,
Table *rslt_table,
const std::vector<ColDefNode> &rslt_tbl_col_defs, const std::vector<int> &src_table_col_index,
bool is_aggregated);
std::pair<bool, std::vector<rowid_t>> probe_index_scan(const Node *where, Table *table) const;
std::pair<bool, std::vector<rowid_t>> look_for_usable_index(const Node *where, Table *table) const;

View File

@ -4,8 +4,9 @@
#include <algorithm>
namespace usql {
namespace usql {
std::pair<bool, std::vector<rowid_t>> USql::probe_index_scan(const Node *where, Table *table) const {
bool indexscan_possible = normalize_where(where);
@ -85,28 +86,28 @@ void USql::select_row(SelectFromTableNode &where_node,
const std::vector<int> &src_table_col_index,
bool is_aggregated) {
Row *rslt_row = nullptr;
Row *rslt_row = nullptr;
// when aggregate functions in rslt_table only one row exists
if (is_aggregated && !rslt_table->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<ColDefNode *>(&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<ColDefNode *>(&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);

View File

@ -1,7 +1,7 @@
#!/bin/sh
gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c
c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib -I./ --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/tcpnet.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp usql/usql_ddl.cpp usql/usql_dml.cpp usql/settings.cpp
c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib -I./ --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/tcpnet.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp usql/usql_ddl.cpp usql/usql_dml.cpp usql/usql_function.cpp usql/settings.cpp
c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o
cp stdlib/*.lsp /usr/local/var/mlisp/

View File

@ -24,7 +24,7 @@ fi
echo "Building ml"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib -I./ --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/tcpnet.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp usql/usql_ddl.cpp usql/usql_dml.cpp usql/settings.cpp"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib -I./ --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/tcpnet.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp usql/usql_ddl.cpp usql/usql_dml.cpp usql/usql_function.cpp usql/settings.cpp"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o"