OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryPlanDagCache Class Reference

#include <QueryPlanDagCache.h>

+ Collaboration diagram for QueryPlanDagCache:

Public Member Functions

 QueryPlanDagCache (size_t max_node_cache_size=MAX_NODE_CACHE_SIZE)
 
 QueryPlanDagCache (QueryPlanDagCache &&other)=delete
 
QueryPlanDagCacheoperator= (QueryPlanDagCache &&other)=delete
 
 QueryPlanDagCache (const QueryPlanDagCache &)=delete
 
QueryPlanDagCacheoperator= (const QueryPlanDagCache &)=delete
 
std::optional< RelNodeIdaddNodeIfAbsent (const RelAlgNode *)
 
void connectNodes (const RelNodeId parent_id, const RelNodeId child_id)
 
std::vector< const
Analyzer::ColumnVar * > 
collectColVars (const Analyzer::Expr *target)
 
size_t getCurrentNodeMapSize () const
 
void setNodeMapMaxSize (const size_t map_size)
 
size_t getJoinColumnsInfoHash (const Analyzer::Expr *join_expr, JoinColumnSide target_side, bool extract_only_col_id)
 
size_t translateColVarsToInfoHash (std::vector< const Analyzer::ColumnVar * > &col_vars, bool col_id_only) const
 
void clearQueryPlanCache ()
 
void printDag ()
 

Private Member Functions

size_t getCurrentNodeMapSizeUnlocked () const
 
size_t getCurrentNodeMapCardinality () const
 

Private Attributes

RelNodeMap node_map_
 
QueryPlanDag cached_query_plan_dag_
 
size_t max_node_map_size_
 
std::mutex cache_lock_
 
ColumnVarsVisitor col_var_visitor_
 

Detailed Description

Definition at line 110 of file QueryPlanDagCache.h.

Constructor & Destructor Documentation

QueryPlanDagCache::QueryPlanDagCache ( size_t  max_node_cache_size = MAX_NODE_CACHE_SIZE)
inline

Definition at line 112 of file QueryPlanDagCache.h.

113  : max_node_map_size_(max_node_cache_size) {}
QueryPlanDagCache::QueryPlanDagCache ( QueryPlanDagCache &&  other)
delete
QueryPlanDagCache::QueryPlanDagCache ( const QueryPlanDagCache )
delete

Member Function Documentation

std::optional< RelNodeId > QueryPlanDagCache::addNodeIfAbsent ( const RelAlgNode node)

Definition at line 25 of file QueryPlanDagCache.cpp.

References cache_lock_, cached_query_plan_dag_, getCurrentNodeMapCardinality(), getCurrentNodeMapSizeUnlocked(), max_node_map_size_, node_map_, run_benchmark_import::result, and RelAlgNode::toHash().

Referenced by QueryPlanDagExtractor::extractQueryPlanDagImpl(), QueryPlanDagExtractor::handleTranslatedJoin(), and QueryPlanDagExtractor::register_and_visit().

25  {
26  std::lock_guard<std::mutex> cache_lock(cache_lock_);
27  auto key = node->toHash();
28  auto const result = node_map_.emplace(key, getCurrentNodeMapCardinality());
29  if (result.second) {
31  getCurrentNodeMapCardinality() == SIZE_MAX) {
32  // unfortunately our DAG cache becomes full
33  // so clear the internal status and skip the query plan DAG extraction
34  node_map_.clear();
35  cached_query_plan_dag_.graph().clear();
36  return std::nullopt;
37  }
38  }
39  return result.first->second;
40 }
size_t getCurrentNodeMapSizeUnlocked() const
QueryPlanDag cached_query_plan_dag_
size_t getCurrentNodeMapCardinality() const
virtual size_t toHash() const =0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QueryPlanDagCache::clearQueryPlanCache ( )

Definition at line 146 of file QueryPlanDagCache.cpp.

References cache_lock_, cached_query_plan_dag_, and node_map_.

146  {
147  std::lock_guard<std::mutex> cache_lock(cache_lock_);
148  node_map_.clear();
149  cached_query_plan_dag_.graph().clear();
150 }
QueryPlanDag cached_query_plan_dag_
std::vector< const Analyzer::ColumnVar * > QueryPlanDagCache::collectColVars ( const Analyzer::Expr target)

Definition at line 152 of file QueryPlanDagCache.cpp.

References col_var_visitor_, and ScalarExprVisitor< T >::visit().

Referenced by QueryPlanDagExtractor::getColVar(), and getJoinColumnsInfoHash().

153  {
154  if (target) {
155  return col_var_visitor_.visit(target);
156  }
157  return {};
158 }
T visit(const Analyzer::Expr *expr) const
ColumnVarsVisitor col_var_visitor_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void QueryPlanDagCache::connectNodes ( const RelNodeId  parent_id,
const RelNodeId  child_id 
)

Definition at line 42 of file QueryPlanDagCache.cpp.

References cache_lock_, and cached_query_plan_dag_.

Referenced by QueryPlanDagExtractor::registerNodeToDagCache().

43  {
44  std::lock_guard<std::mutex> cache_lock(cache_lock_);
45  boost::add_vertex(parent_id, cached_query_plan_dag_);
46  boost::add_vertex(child_id, cached_query_plan_dag_);
47  add_edge_by_label(parent_id, child_id, cached_query_plan_dag_);
48 }
QueryPlanDag cached_query_plan_dag_

+ Here is the caller graph for this function:

size_t QueryPlanDagCache::getCurrentNodeMapCardinality ( ) const
private

Definition at line 142 of file QueryPlanDagCache.cpp.

References node_map_.

Referenced by addNodeIfAbsent().

142  {
143  return node_map_.size();
144 }

+ Here is the caller graph for this function:

size_t QueryPlanDagCache::getCurrentNodeMapSize ( ) const

Definition at line 133 of file QueryPlanDagCache.cpp.

References cache_lock_, and getCurrentNodeMapSizeUnlocked().

133  {
134  std::lock_guard<std::mutex> cache_lock(cache_lock_);
136 }
size_t getCurrentNodeMapSizeUnlocked() const

+ Here is the call graph for this function:

size_t QueryPlanDagCache::getCurrentNodeMapSizeUnlocked ( ) const
private

Definition at line 138 of file QueryPlanDagCache.cpp.

References elem_size_, and node_map_.

Referenced by addNodeIfAbsent(), and getCurrentNodeMapSize().

138  {
139  return node_map_.size() * elem_size_;
140 }
constexpr size_t elem_size_

+ Here is the caller graph for this function:

size_t QueryPlanDagCache::getJoinColumnsInfoHash ( const Analyzer::Expr join_expr,
JoinColumnSide  target_side,
bool  extract_only_col_id 
)

Definition at line 78 of file QueryPlanDagCache.cpp.

References CHECK, collectColVars(), EMPTY_HASHED_PLAN_DAG_KEY, kDirect, kInner, kOuter, kQual, and translateColVarsToInfoHash().

80  {
81  // this function returns qual_bin_oper's info depending on the requested context
82  // such as extracted col_id of inner join cols
83  // (target_side = JoinColumnSide::kInner, extract_only_col_id = true)
84  // and extract all infos of an entire join quals
85  // (target_side = JoinColumnSide::kQual, extract_only_col_id = false)
86  // todo (yoonmin): we may need to use a whole "EXPR" contents in a future
87  // to support a join qual with more general expression like A.a + 1 = (B.b * 2) / 2
88  if (!join_expr) {
90  }
91  auto get_sorted_col_info = [=](const Analyzer::Expr* join_cols) -> size_t {
92  auto join_col_vars = collectColVars(join_cols);
93  CHECK(!join_col_vars.empty())
94  << "Join expression should have at least one join column variable";
95  return translateColVarsToInfoHash(join_col_vars, extract_only_col_id);
96  };
97  auto hashed_join_col_info = EMPTY_HASHED_PLAN_DAG_KEY;
98  if (target_side == JoinColumnSide::kQual) {
99  auto qual_bin_oper = dynamic_cast<const Analyzer::BinOper*>(join_expr);
100  CHECK(qual_bin_oper);
101  boost::hash_combine(hashed_join_col_info,
102  get_sorted_col_info(qual_bin_oper->get_left_operand()));
103  boost::hash_combine(hashed_join_col_info,
104  get_sorted_col_info(qual_bin_oper->get_right_operand()));
105  } else if (target_side == JoinColumnSide::kInner) {
106  auto qual_bin_oper = dynamic_cast<const Analyzer::BinOper*>(join_expr);
107  CHECK(qual_bin_oper);
108  boost::hash_combine(hashed_join_col_info,
109  get_sorted_col_info(qual_bin_oper->get_left_operand()));
110  } else if (target_side == JoinColumnSide::kOuter) {
111  auto qual_bin_oper = dynamic_cast<const Analyzer::BinOper*>(join_expr);
112  CHECK(qual_bin_oper);
113  boost::hash_combine(hashed_join_col_info,
114  get_sorted_col_info(qual_bin_oper->get_right_operand()));
115  } else {
116  CHECK(target_side == JoinColumnSide::kDirect);
117  boost::hash_combine(hashed_join_col_info, get_sorted_col_info(join_expr));
118  }
119  return hashed_join_col_info;
120 }
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
size_t translateColVarsToInfoHash(std::vector< const Analyzer::ColumnVar * > &col_vars, bool col_id_only) const
#define CHECK(condition)
Definition: Logger.h:291
std::vector< const Analyzer::ColumnVar * > collectColVars(const Analyzer::Expr *target)

+ Here is the call graph for this function:

QueryPlanDagCache& QueryPlanDagCache::operator= ( QueryPlanDagCache &&  other)
delete
QueryPlanDagCache& QueryPlanDagCache::operator= ( const QueryPlanDagCache )
delete
void QueryPlanDagCache::printDag ( )

Definition at line 122 of file QueryPlanDagCache.cpp.

References cached_query_plan_dag_, and node_map_.

122  {
123  std::cout << "Edge list:" << std::endl;
124  boost::print_graph(cached_query_plan_dag_.graph());
125  std::ostringstream os;
126  os << "\n\nNodeMap:\n";
127  for (auto& kv : node_map_) {
128  os << "[" << kv.second << "] " << kv.first << "\n";
129  }
130  std::cout << os.str() << std::endl;
131 }
QueryPlanDag cached_query_plan_dag_
void QueryPlanDagCache::setNodeMapMaxSize ( const size_t  map_size)

Definition at line 50 of file QueryPlanDagCache.cpp.

References cache_lock_, and max_node_map_size_.

50  {
51  std::lock_guard<std::mutex> cache_lock(cache_lock_);
52  max_node_map_size_ = map_size;
53 }
size_t QueryPlanDagCache::translateColVarsToInfoHash ( std::vector< const Analyzer::ColumnVar * > &  col_vars,
bool  col_id_only 
) const

Definition at line 55 of file QueryPlanDagCache.cpp.

References EMPTY_HASHED_PLAN_DAG_KEY, Analyzer::ColumnVar::getColumnKey(), and gpu_enabled::sort().

Referenced by getJoinColumnsInfoHash(), and QueryPlanDagExtractor::handleTranslatedJoin().

57  {
58  // we need to sort col ids to prevent missing data reuse case in multi column qual
59  // scenarios like a) A.a = B.b and A.c = B.c and b) A.c = B.c and A.a = B.a
60  std::sort(col_vars.begin(),
61  col_vars.end(),
62  [](const Analyzer::ColumnVar* lhs, const Analyzer::ColumnVar* rhs) {
63  return lhs->getColumnKey() < rhs->getColumnKey();
64  });
65  size_t col_vars_info_hash = EMPTY_HASHED_PLAN_DAG_KEY;
66  using Hasher = std::function<void(Analyzer::ColumnVar const*)>;
67  Hasher hash_col_id = [&col_vars_info_hash](auto const* cv) {
68  boost::hash_combine(col_vars_info_hash, cv->getColumnKey().column_id);
69  };
70  Hasher hash_cv_string = [&col_vars_info_hash](auto const* cv) {
71  boost::hash_combine(col_vars_info_hash, cv->toString());
72  };
73  std::for_each(
74  col_vars.begin(), col_vars.end(), col_id_only ? hash_col_id : hash_cv_string);
75  return col_vars_info_hash;
76 }
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::mutex QueryPlanDagCache::cache_lock_
mutableprivate
QueryPlanDag QueryPlanDagCache::cached_query_plan_dag_
private

Definition at line 148 of file QueryPlanDagCache.h.

Referenced by addNodeIfAbsent(), clearQueryPlanCache(), connectNodes(), and printDag().

ColumnVarsVisitor QueryPlanDagCache::col_var_visitor_
private

Definition at line 155 of file QueryPlanDagCache.h.

Referenced by collectColVars().

size_t QueryPlanDagCache::max_node_map_size_
private

Definition at line 151 of file QueryPlanDagCache.h.

Referenced by addNodeIfAbsent(), and setNodeMapMaxSize().

RelNodeMap QueryPlanDagCache::node_map_
private

The documentation for this class was generated from the following files: