OmniSciDB  471d68cefb
 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 getCurrentNodeMapCardinality () const
 
JoinColumnsInfo getJoinColumnsInfoString (const Analyzer::Expr *join_expr, JoinColumnSide target_side, bool extract_only_col_id)
 
JoinColumnsInfo translateColVarsToInfoString (std::vector< const Analyzer::ColumnVar * > &col_vars, bool col_id_only) const
 
void clearQueryPlanCache ()
 
void printDag ()
 

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 85 of file QueryPlanDagCache.h.

Constructor & Destructor Documentation

QueryPlanDagCache::QueryPlanDagCache ( size_t  max_node_cache_size = MAX_NODE_CACHE_SIZE)
inline

Definition at line 87 of file QueryPlanDagCache.h.

88  : 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 26 of file QueryPlanDagCache.cpp.

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

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

26  {
27  std::lock_guard<std::mutex> cache_lock(cache_lock_);
28  auto key = node->toHash();
29  auto const result = node_map_.emplace(key, getCurrentNodeMapCardinality());
30  if (result.second) {
31  // key did not already exist in node_map_. Check max size wasn't hit.
34  // unfortunately our DAG cache becomes full if we add this node to it
35  // so we skip to cache this plan DAG and clear cache map
36  // b/c this can be happen in a middle of dag extraction
37  node_map_.clear();
38  cached_query_plan_dag_.graph().clear();
39  // assume we cannot keep 'InvalidQueryPlanHash' nodes for our DAG cache
40  return std::nullopt;
41  }
42  }
43  return result.first->second;
44 }
#define SIZE_MAX
QueryPlanDag cached_query_plan_dag_
size_t getCurrentNodeMapCardinality() const
size_t getCurrentNodeMapSize() 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 141 of file QueryPlanDagCache.cpp.

References cache_lock_, cached_query_plan_dag_, and node_map_.

141  {
142  std::lock_guard<std::mutex> cache_lock(cache_lock_);
143  node_map_.clear();
144  cached_query_plan_dag_.graph().clear();
145 }
QueryPlanDag cached_query_plan_dag_
std::vector< const Analyzer::ColumnVar * > QueryPlanDagCache::collectColVars ( const Analyzer::Expr target)

Definition at line 147 of file QueryPlanDagCache.cpp.

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

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

148  {
149  if (target) {
150  return col_var_visitor_.visit(target);
151  }
152  return {};
153 }
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 46 of file QueryPlanDagCache.cpp.

References cache_lock_, and cached_query_plan_dag_.

Referenced by QueryPlanDagExtractor::registerNodeToDagCache().

47  {
48  std::lock_guard<std::mutex> cache_lock(cache_lock_);
49  boost::add_vertex(parent_id, cached_query_plan_dag_);
50  boost::add_vertex(child_id, cached_query_plan_dag_);
51  add_edge_by_label(parent_id, child_id, cached_query_plan_dag_);
52 }
QueryPlanDag cached_query_plan_dag_

+ Here is the caller graph for this function:

size_t QueryPlanDagCache::getCurrentNodeMapCardinality ( ) const

Definition at line 137 of file QueryPlanDagCache.cpp.

References node_map_.

Referenced by addNodeIfAbsent().

137  {
138  return node_map_.size();
139 }

+ Here is the caller graph for this function:

size_t QueryPlanDagCache::getCurrentNodeMapSize ( ) const

Definition at line 133 of file QueryPlanDagCache.cpp.

References elem_size_, and node_map_.

Referenced by addNodeIfAbsent().

133  {
134  return node_map_.size() * elem_size_;
135 }
constexpr size_t elem_size_

+ Here is the caller graph for this function:

JoinColumnsInfo QueryPlanDagCache::getJoinColumnsInfoString ( const Analyzer::Expr join_expr,
JoinColumnSide  target_side,
bool  extract_only_col_id 
)

Definition at line 80 of file QueryPlanDagCache.cpp.

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

83  {
84  // this function returns qual_bin_oper's info depending on the requested context
85  // such as extracted col_id of inner join cols
86  // (target_side = JoinColumnSide::kInner, extract_only_col_id = true)
87  // and extract all infos of an entire join quals
88  // (target_side = JoinColumnSide::kQual, extract_only_col_id = false)
89  // todo (yoonmin): we may need to use a whole "EXPR" contents in a future
90  // to support a join qual with more general expression like A.a + 1 = (B.b * 2) / 2
91  if (!join_expr) {
92  return "";
93  }
94  auto get_sorted_col_info = [&](const Analyzer::Expr* join_cols) -> JoinColumnsInfo {
95  auto join_col_vars = collectColVars(join_cols);
96  if (join_col_vars.empty()) {
97  return "";
98  }
99  return translateColVarsToInfoString(join_col_vars, extract_only_col_id);
100  };
101 
102  if (target_side == JoinColumnSide::kQual) {
103  auto qual_bin_oper = reinterpret_cast<const Analyzer::BinOper*>(join_expr);
104  CHECK(qual_bin_oper);
105  auto inner_join_col_info = get_sorted_col_info(qual_bin_oper->get_left_operand());
106  auto outer_join_col_info = get_sorted_col_info(qual_bin_oper->get_right_operand());
107  return outer_join_col_info + "|" + inner_join_col_info;
108  } else if (target_side == JoinColumnSide::kInner) {
109  auto qual_bin_oper = reinterpret_cast<const Analyzer::BinOper*>(join_expr);
110  CHECK(qual_bin_oper);
111  return get_sorted_col_info(qual_bin_oper->get_left_operand());
112  } else if (target_side == JoinColumnSide::kOuter) {
113  auto qual_bin_oper = reinterpret_cast<const Analyzer::BinOper*>(join_expr);
114  CHECK(qual_bin_oper);
115  return get_sorted_col_info(qual_bin_oper->get_right_operand());
116  } else {
117  CHECK(target_side == JoinColumnSide::kDirect);
118  return get_sorted_col_info(join_expr);
119  }
120 }
std::string JoinColumnsInfo
JoinColumnsInfo translateColVarsToInfoString(std::vector< const Analyzer::ColumnVar * > &col_vars, bool col_id_only) const
#define CHECK(condition)
Definition: Logger.h:209
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 54 of file QueryPlanDagCache.cpp.

References cache_lock_, and max_node_map_size_.

54  {
55  std::lock_guard<std::mutex> cache_lock(cache_lock_);
56  max_node_map_size_ = map_size;
57 }
JoinColumnsInfo QueryPlanDagCache::translateColVarsToInfoString ( std::vector< const Analyzer::ColumnVar * > &  col_vars,
bool  col_id_only 
) const

Definition at line 59 of file QueryPlanDagCache.cpp.

References Analyzer::ColumnVar::get_column_id(), gpu_enabled::sort(), and toString().

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

61  {
62  // we need to sort col ids to prevent missing data reuse case in multi column qual
63  // scenarios like a) A.a = B.b and A.c = B.c and b) A.c = B.c and A.a = B.a
64  std::sort(col_vars.begin(),
65  col_vars.end(),
66  [](const Analyzer::ColumnVar* lhs, const Analyzer::ColumnVar* rhs) {
67  return lhs->get_column_id() < rhs->get_column_id();
68  });
69  if (col_id_only) {
70  std::vector<int> sorted_col_ids;
71  for (auto cv : col_vars) {
72  sorted_col_ids.push_back(cv->get_column_id());
73  }
74  return ::toString(sorted_col_ids);
75  } else {
76  return ::toString(col_vars);
77  }
78 }
std::string toString(const ExtArgumentType &sig_type)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
int get_column_id() const
Definition: Analyzer.h:194

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 123 of file QueryPlanDagCache.h.

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

ColumnVarsVisitor QueryPlanDagCache::col_var_visitor_
private

Definition at line 129 of file QueryPlanDagCache.h.

Referenced by collectColVars().

size_t QueryPlanDagCache::max_node_map_size_
private

Definition at line 126 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: