Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tianmu): Improve Tianmu code (#11) #757

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 129 additions & 86 deletions storage/tianmu/core/compiled_query.cpp

Large diffs are not rendered by default.

114 changes: 60 additions & 54 deletions storage/tianmu/core/compiled_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
#include <vector>

#include "common/common_definitions.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/just_a_table.h"
#include "core/temp_table.h"

namespace Tianmu {
namespace core {

/*
CompiledQuery - for storing execution plan of a query (sequence of
CompiledQuery - for storing execution plan of a query (sequence of
primitive operations) and output data definition
*/
class MysqlExpression;
Expand Down Expand Up @@ -61,9 +62,9 @@ class CompiledQuery final {
class CQStep {
public:
StepType type;
TabID t1;
TabID t2;
TabID t3;
TableID t1;
TableID t2;
TableID t3;
AttrID a1, a2;
CondID c1, c2, c3;
CQTerm e1, e2, e3;
Expand All @@ -74,8 +75,8 @@ class CompiledQuery final {
char *alias;
std::vector<MysqlExpression *> mysql_expr;
std::vector<int> virt_cols;
std::vector<TabID> tables1;
std::vector<TabID> tables2;
std::vector<TableID> tables1;
std::vector<TableID> tables2;
int64_t n1, n2; // additional parameter (e.g. descending order, TOP n,
// LIMIT n1..n2)
SI si;
Expand Down Expand Up @@ -104,7 +105,11 @@ class CompiledQuery final {

CQStep(const CQStep &);

~CQStep() { delete[] alias; }
~CQStep() {
delete[] alias;
alias = nullptr;
}

CQStep &operator=(const CQStep &);
/*! \brief Swap contents with another instance of CQStep.
* \param s - another instance of CQStep.
Expand All @@ -120,34 +125,34 @@ class CompiledQuery final {
return x;
}
};
// Initialization

// Initialization
CompiledQuery();
CompiledQuery(CompiledQuery const &);
CompiledQuery &operator=(CompiledQuery const &) = delete;
~CompiledQuery() = default;

// Add a new step to the execution plan

void TableAlias(TabID &t_out, const TabID &n, const char *tab_name = NULL, int id = -1);
void TmpTable(TabID &t_out, const TabID &t1, bool for_subq = false);
void CreateConds(CondID &c_out, const TabID &t1, CQTerm e1, common::Operator pr, CQTerm e2, CQTerm e3 = CQTerm(),
void TableAlias(TableID &t_out, const TableID &n, const char *tab_name = NULL, int id = -1);
void TmpTable(TableID &t_out, const TableID &t1, bool for_subq = false);
void CreateConds(CondID &c_out, const TableID &t1, CQTerm e1, common::Operator pr, CQTerm e2, CQTerm e3 = CQTerm(),
bool is_or_subtree = false, char like_esc = '\\');
void CreateConds(CondID &c_out, const TabID &t1, const CondID &c1, bool is_or_subtree = false);
void And(const CondID &c1, const TabID &t, const CondID &c2);
void Or(const CondID &c1, const TabID &t, const CondID &c2);
void And(const CondID &c1, const TabID &t, CQTerm e1, common::Operator pr, CQTerm e2 = CQTerm(),
void CreateConds(CondID &c_out, const TableID &t1, const CondID &c1, bool is_or_subtree = false);
void And(const CondID &c1, const TableID &t, const CondID &c2);
void Or(const CondID &c1, const TableID &t, const CondID &c2);
void And(const CondID &c1, const TableID &t, CQTerm e1, common::Operator pr, CQTerm e2 = CQTerm(),
CQTerm e3 = CQTerm());
void Or(const CondID &c1, const TabID &t, CQTerm e1, common::Operator pr, CQTerm e2 = CQTerm(), CQTerm e3 = CQTerm());
void Mode(const TabID &t1, TMParameter mode, int64_t n1 = 0, int64_t n2 = 0);
void Join(const TabID &t1, const TabID &t2);
void LeftJoinOn(const TabID &temp_table, std::vector<TabID> &left_tables, std::vector<TabID> &right_tables,
void Or(const CondID &c1, const TableID &t, CQTerm e1, common::Operator pr, CQTerm e2 = CQTerm(),
CQTerm e3 = CQTerm());
void Mode(const TableID &t1, TMParameter mode, int64_t n1 = 0, int64_t n2 = 0);
void Join(const TableID &t1, const TableID &t2);
void LeftJoinOn(const TableID &temp_table, std::vector<TableID> &left_tables, std::vector<TableID> &right_tables,
const CondID &cond_id);
void InnerJoinOn(const TabID &temp_table, std::vector<TabID> &left_tables, std::vector<TabID> &right_tables,
void InnerJoinOn(const TableID &temp_table, std::vector<TableID> &left_tables, std::vector<TableID> &right_tables,
const CondID &cond_id);
void AddConds(const TabID &t1, const CondID &c1, CondType cond_type);
void ApplyConds(const TabID &t1);
void AddColumn(AttrID &a_out, const TabID &t1, CQTerm e1, common::ColOperation op, char const alias[] = 0,
void AddConds(const TableID &t1, const CondID &c1, CondType cond_type);
void ApplyConds(const TableID &t1);
void AddColumn(AttrID &a_out, const TableID &t1, CQTerm e1, common::ColOperation op, char const alias[] = 0,
bool distinct = false, SI *si = NULL);

/*! \brief Create compilation step CREATE_VC for mysql expression
Expand All @@ -157,8 +162,8 @@ class CompiledQuery final {
* \param src_table - src_table == t1 if expression works on aggregation
* results, == common::NULL_VALUE_32 otherwise \return void
*/
void CreateVirtualColumn(AttrID &a_out, const TabID &t1, MysqlExpression *expr,
const TabID &src_table = TabID(common::NULL_VALUE_32));
void CreateVirtualColumn(AttrID &a_out, const TableID &t1, MysqlExpression *expr,
const TableID &src_table = TableID(common::NULL_VALUE_32));

/*! \brief Create compilation step CREATE_VC for subquery
* \param a_out - id of created virtual column
Expand All @@ -169,99 +174,99 @@ class CompiledQuery final {
* representing output columns of t1 (\e true), e.g. in case of subquery in
* HAVING \return void
*/
void CreateVirtualColumn(AttrID &a_out, const TabID &t1, const TabID &subquery, bool on_result);
void CreateVirtualColumn(AttrID &a_out, const TabID &t1, std::vector<int> &vcs, const AttrID &vc_id);
void CreateVirtualColumn(int &a_out, const TabID &t1, const TabID &table_alias, const AttrID &col_number);
void Add_Order(const TabID &t1, const AttrID &a1,
void CreateVirtualColumn(AttrID &a_out, const TableID &t1, const TableID &subquery, bool on_result);
void CreateVirtualColumn(AttrID &a_out, const TableID &t1, std::vector<int> &vcs, const AttrID &vc_id);
void CreateVirtualColumn(int &a_out, const TableID &t1, const TableID &table_alias, const AttrID &col_number);
void Add_Order(const TableID &t1, const AttrID &a1,
int d = 0); // d=1 for descending
void Add_Order(const TabID &p_t, ptrdiff_t p_c) {
void Add_Order(const TableID &p_t, ptrdiff_t p_c) {
int a_c(static_cast<int>(p_c));
if (p_c == a_c) this->Add_Order(p_t, AttrID(-abs(a_c)), a_c < 0);
}
void Union(TabID &t_out, const TabID &t2, const TabID &t3, int all = 0);
void Result(const TabID &t1);
void Union(TableID &t_out, const TableID &t2, const TableID &t3, int all = 0);
void Result(const TableID &t1);

// Informations

int NumOfTabs() { return no_tabs; }
int NumOfAttrs(const TabID &tab) { return no_attrs[-tab.n - 1]; }
int NumOfAttrs(const TableID &tab) { return no_attrs[-tab.n - 1]; }
int NumOfConds() { return no_conds; }
int NumOfVirtualColumns(const TabID &tt) {
int NumOfVirtualColumns(const TableID &tt) {
if (no_virt_cols.find(tt) != no_virt_cols.end()) return no_virt_cols[tt];
return 0;
}
void Print(Query *); // display the CompiledQuery

int NumOfSteps() { return (int)steps.size(); }
CQStep &Step(int i) { return steps[i]; }
bool CountColumnOnly(const TabID &table);
bool CountColumnOnly(const TableID &table);

/*! \brief verify is given table alias is used in the query defined by a temp
* table \param tab_id alias of a searched table \param tmp_table alias of the
* temp table to be searched through returns true if tab_id == tmp_table or
* tab_id is one of the source tables used in tmp_table
*/
bool ExistsInTempTable(const TabID &tab_id, const TabID &tmp_table);
bool ExistsInTempTable(const TableID &tab_id, const TableID &tmp_table);

/*! \brief Returns set of all dimensions used by output columns (ADD_COLUMN
* step) for a given TempTable. \param table_id - id of given TempTable \param
* ta - vector of pointers to JustATables necessary to get access to virtual
* column inside relevant TempTable \return set<int>
*/
std::set<int> GetUsedDims(const TabID &table_id, std::vector<std::shared_ptr<JustATable>> &ta);
std::set<int> GetUsedDims(const TableID &table_id, std::vector<std::shared_ptr<JustATable>> &ta);

/*! \brief Finds the first TempTable in compilation steps that contains given
* table as source table \param tab_id - id of search table \param tmp_table -
* id of TempTable to start search from \param is_group_by - flag set to true
* if searched table (one that is returned) represents GROUP BY query \return
* returns id of TempTable containing tab_id as source table
*/
TabID FindSourceOfParameter(const TabID &tab_id, const TabID &tmp_table, bool &is_group_by);
TableID FindSourceOfParameter(const TableID &tab_id, const TableID &tmp_table, bool &is_group_by);

/*! \brief Finds if TempTable represents query with GroupBy clause
* \param tab_id - TempTable to be checked
* \return true if it is GroupBy query, false otherwise
*/
bool IsGroupByQuery(const TabID &tab_id);
bool IsResultTable(const TabID &t);
bool IsOrderedBy(const TabID &t);
bool IsGroupByQuery(const TableID &tab_id);
bool IsResultTable(const TableID &t);
bool IsOrderedBy(const TableID &t);
int64_t FindLimit(int table); // return LIMIT value for table, or -1
bool FindDistinct(int table); // return true if there is DISTINCT flag for this query
bool NoAggregationOrderingAndDistinct(int table);
std::pair<int64_t, int64_t> GetGlobalLimit();

int GetNumOfDimens(const TabID &tab_id);
TabID GetTableOfCond(const CondID &cond_id);
int GetNumOfDimens(const TableID &tab_id);
TableID GetTableOfCond(const CondID &cond_id);

void BuildTableIDStepsMap();
void AddGroupByStep(CQStep s) { steps_group_by_cols.push_back(s); }

private:
// NOTE: new objects' IDs (e.g. while declaring new aliases, filters etc.) are
// reserved and obtained by these functions.
TabID NextTabID() {
TableID NextTabID() {
no_tabs++;
no_attrs.push_back(0);
return TabID(-no_tabs);
return TableID(-no_tabs);
}
AttrID NextVCID(const TabID &tt) { return AttrID(no_virt_cols[tt]++); }
AttrID NextAttrID(const TabID &tab) {
AttrID NextVCID(const TableID &tt) { return AttrID(no_virt_cols[tt]++); }
AttrID NextAttrID(const TableID &tab) {
no_attrs[-tab.n - 1]++;
return AttrID(-no_attrs[-tab.n - 1]);
}
CondID NextCondID() { return CondID(no_conds++); } // IDs of tables and attributes start with -1 and decrease;
/*! \brief Checks if given TabID represents TempTable or just some table alias
* \param t - TabID of table to be checked
/*! \brief Checks if given TableID represents TempTable or just some table alias
* \param t - TableID of table to be checked
* \return true if t is an alias of TempTable, false otherwise
*/
bool IsTempTable(const TabID &t);
bool IsTempTable(const TableID &t);
int FindRootTempTable(int x);

// IDs of params and filters start with 0 and increase.
int no_tabs; // counters: which IDs are not in use?
int no_conds; // counters: which IDs are not in use?

std::map<TabID, int> no_virt_cols;
std::map<TableID, int> no_virt_cols;

std::vector<int> no_attrs; // repository of attr_ids for each table
std::vector<CQStep> steps; // repository of steps to be executed
Expand All @@ -271,8 +276,9 @@ class CompiledQuery final {
// container (steps).
std::vector<CQStep> steps_tmp_tables;
std::vector<CQStep> steps_group_by_cols;
std::multimap<TabID, CQStep> TabIDSteps;
std::multimap<TableID, CQStep> TabIDSteps;
};

} // namespace core
} // namespace Tianmu

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ class Hash64;
} // namespace utils

namespace core {

struct AttrID {
int n;
AttrID() : n(common::NULL_VALUE_32) {}
explicit AttrID(int _n) : n(_n) {}
};

struct TabID {
struct TableID {
int n;
TabID() : n(common::NULL_VALUE_32) {}
explicit TabID(int _n) : n(_n) {}
TableID() : n(common::NULL_VALUE_32) {}
explicit TableID(int _n) : n(_n) {}
bool IsNullID() const { return (n == common::NULL_VALUE_32); }
bool operator==(const TabID &other) const { return (n == other.n) && (!IsNullID()); }
bool operator<(const TabID &other) const { return (n < other.n) && (!IsNullID()); }
bool operator!=(const TabID &other) const { return !(operator==(other)); }
bool operator==(const TableID &other) const { return (n == other.n) && (!IsNullID()); }
bool operator<(const TableID &other) const { return (n < other.n) && (!IsNullID()); }
bool operator!=(const TableID &other) const { return !(operator==(other)); }
};

struct CondID {
Expand Down
4 changes: 3 additions & 1 deletion storage/tianmu/core/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

#include <set>

#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/descriptor.h"

namespace Tianmu {
namespace core {

class Condition {
protected:
std::vector<Descriptor> descriptors;
Expand Down Expand Up @@ -59,6 +60,7 @@ class SingleTreeCondition : public Condition {
DescTree *GetTree() { return tree; }
bool IsType_Tree() override { return true; }
};

} // namespace core
} // namespace Tianmu

Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/condition_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

#include "condition_encoder.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/descriptor.h"
#include "core/just_a_table.h"
#include "core/query.h"
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <vector>

#include "common/common_definitions.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/dimension_group.h"

namespace Tianmu {
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/just_a_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "just_a_table.h"

#include "common/assert.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/filter.h"
#include "core/temp_table.h"

Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/multi_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#pragma once

#include "core/bin_tools.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/dimension_group.h"
#include "core/filter.h"
#include "core/index_table.h"
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/multi_index_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <vector>

#include "core/bin_tools.h"
#include "core/cq_term.h"
#include "core/compiled_query_term.h"
#include "core/dimension_group.h"
#include "core/filter.h"
#include "core/index_table.h"
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/parameterized_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#define TIANMU_CORE_PARAMETERIZED_FILTER_H_
#pragma once

#include "core/compiled_query_term.h"
#include "core/condition.h"
#include "core/cq_term.h"
#include "core/joiner.h"
#include "core/multi_index.h"

Expand Down
Loading