OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryPlanDagExtractor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "QueryPlanDagCache.h"
20 #include "RelAlgExecutionUnit.h"
21 #include "RelAlgExecutor.h"
22 #include "RelAlgTranslator.h"
23 
24 #include <unordered_map>
25 #include <vector>
26 
27 // a vector of explained join info
28 using TranslatedJoinInfo = std::vector<std::shared_ptr<RelTranslatedJoin>>;
29 
31  // extracted DAG starting from the root_node
33  // show whether this query plan is not available to extract a DAG
35 };
36 
38  // a map btw. join qual to an access path (as a query plan DAG) to corresponding
39  // hashtable
41  // a map btw. inner join col's input table id to corresponding rel node ptr
43  // show whether this query plan is not available to extract a DAG
44 };
45 
46 // set the bool flag be true when the InnerOuter qual is for loop join
48  std::pair<const Analyzer::Expr*, const Analyzer::Expr*> inner_outer;
49  bool loop_join_qual{false};
50 };
51 
53  public:
55  QueryPlanDagCache& global_dag,
56  std::unordered_map<unsigned, JoinQualsPerNestingLevel> left_deep_tree_infos,
57  Executor* executor,
58  bool analyze_join_ops)
59  : global_dag_(global_dag)
61  , left_deep_tree_infos_(left_deep_tree_infos)
62  , executor_(executor)
63  , analyze_join_ops_(analyze_join_ops) {
64  if (analyze_join_ops_) {
65  translated_join_info_ = std::make_shared<TranslatedJoinInfo>();
66  }
67  }
68 
70  const RelAlgNode* top_node,
71  std::optional<unsigned> left_deep_tree_id,
72  std::unordered_map<unsigned, JoinQualsPerNestingLevel> left_deep_tree_infos,
73  Executor* executor);
74 
76  Executor* executor);
77 
79 
81  std::optional<unsigned> left_deep_join_tree_id) {
82  if (left_deep_tree_infos_.empty() || !left_deep_join_tree_id) {
83  return nullptr;
84  }
85  CHECK(left_deep_join_tree_id.has_value());
86  auto it = left_deep_tree_infos_.find(left_deep_join_tree_id.value());
87  if (it == left_deep_tree_infos_.end()) {
88  return nullptr;
89  }
90  return &it->second;
91  }
92 
94 
95  std::string getExtractedQueryPlanDagStr(size_t start_pos = 0);
96 
97  std::vector<InnerOuterOrLoopQual> normalizeColumnsPair(
98  const Analyzer::BinOper* condition);
99 
100  bool isEmptyQueryPlanDag(const std::string& dag) { return dag.compare("N/A") == 0; }
101 
103 
104  void addTableIdToNodeLink(const int table_id, const RelAlgNode* node) {
105  auto it = table_id_to_node_map_.find(table_id);
106  if (it == table_id_to_node_map_.end()) {
107  table_id_to_node_map_.emplace(table_id, node);
108  }
109  }
110 
113  extracted_dag_.clear();
114  table_id_to_node_map_.clear();
115  }
116 
117  private:
118  void visit(const RelAlgNode*, const RelAlgNode*);
119  std::vector<Analyzer::ColumnVar const*> getColVar(const Analyzer::Expr* col_info);
120  void register_and_visit(const RelAlgNode* parent_node, const RelAlgNode* child_node);
123  bool validateNodeId(const RelAlgNode* node, std::optional<RelNodeId> retrieved_node_id);
124  bool registerNodeToDagCache(const RelAlgNode* parent_node,
125  const RelAlgNode* child_node,
126  std::optional<RelNodeId> retrieved_node_id);
127  static void extractQueryPlanDagImpl(const RelAlgNode* top_npde,
128  QueryPlanDagExtractor& dag_extractor);
129 
132  std::unordered_map<unsigned, JoinQualsPerNestingLevel> left_deep_tree_infos_;
133  Executor* executor_;
135  std::shared_ptr<TranslatedJoinInfo> translated_join_info_;
138  std::vector<std::string> extracted_dag_;
139 };
std::pair< const Analyzer::Expr *, const Analyzer::Expr * > inner_outer
QueryPlanDagCache & global_dag_
TableIdToNodeMap getTableIdToNodeMap()
static ExtractedQueryPlanDag extractQueryPlanDag(const RelAlgNode *top_node, Executor *executor)
static ExtractedJoinInfo extractJoinInfo(const RelAlgNode *top_node, std::optional< unsigned > left_deep_tree_id, std::unordered_map< unsigned, JoinQualsPerNestingLevel > left_deep_tree_infos, Executor *executor)
std::vector< InnerOuterOrLoopQual > normalizeColumnsPair(const Analyzer::BinOper *condition)
const QueryPlanDAG extracted_dag
TableIdToNodeMap table_id_to_node_map_
std::string QueryPlanDAG
std::vector< std::string > extracted_dag_
QueryPlanDagExtractor(QueryPlanDagCache &global_dag, std::unordered_map< unsigned, JoinQualsPerNestingLevel > left_deep_tree_infos, Executor *executor, bool analyze_join_ops)
std::vector< JoinCondition > JoinQualsPerNestingLevel
const TableIdToNodeMap table_id_to_node_map
void register_and_visit(const RelAlgNode *parent_node, const RelAlgNode *child_node)
bool validateNodeId(const RelAlgNode *node, std::optional< RelNodeId > retrieved_node_id)
std::unordered_map< size_t, HashTableBuildDag > HashTableBuildDagMap
bool isEmptyQueryPlanDag(const std::string &dag)
void handleTranslatedJoin(const RelAlgNode *, const RelTranslatedJoin *)
std::unordered_map< int, const RelAlgNode * > TableIdToNodeMap
HashTableBuildDagMap getHashTableBuildDag()
bool registerNodeToDagCache(const RelAlgNode *parent_node, const RelAlgNode *child_node, std::optional< RelNodeId > retrieved_node_id)
static void extractQueryPlanDagImpl(const RelAlgNode *top_npde, QueryPlanDagExtractor &dag_extractor)
std::unordered_map< unsigned, JoinQualsPerNestingLevel > left_deep_tree_infos_
std::vector< std::shared_ptr< RelTranslatedJoin >> TranslatedJoinInfo
HashTableBuildDagMap hash_table_query_plan_dag_
void handleLeftDeepJoinTree(const RelAlgNode *, const RelLeftDeepInnerJoin *)
void addTableIdToNodeLink(const int table_id, const RelAlgNode *node)
void visit(const RelAlgNode *, const RelAlgNode *)
std::shared_ptr< TranslatedJoinInfo > translated_join_info_
bool g_enable_watchdog false
Definition: Execute.cpp:79
#define CHECK(condition)
Definition: Logger.h:223
std::string getExtractedQueryPlanDagStr(size_t start_pos=0)
std::vector< Analyzer::ColumnVar const * > getColVar(const Analyzer::Expr *col_info)
Execution unit for relational algebra. It&#39;s a low-level description of any relational algebra operati...
const JoinQualsPerNestingLevel * getPerNestingJoinQualInfo(std::optional< unsigned > left_deep_join_tree_id)
const HashTableBuildDagMap hash_table_plan_dag