OmniSciDB  471d68cefb
 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  // a root node of the query plan
33  // extracted DAG starting from the root_node
35  // join qual's info collected while converting calcite AST to logical query plan tree
36  const std::shared_ptr<TranslatedJoinInfo> translated_join_info;
37  // join condition info collected during converting calcite AST to logical query plan
38  // tree
40  // a map btw. join qual to an access path (as a query plan DAG) to corresponding
41  // hashtable
43  // a map btw. inner join col's input table id to corresponding rel node ptr
45  // show whether this query plan is not available to extract a DAG
47 };
48 
49 // set the bool flag be true when the InnerOuter qual is for loop join
51  std::pair<const Analyzer::Expr*, const Analyzer::Expr*> inner_outer;
52  bool loop_join_qual{false};
53 };
54 
56  public:
58  QueryPlanDagCache& global_dag,
59  const Catalog_Namespace::Catalog& catalog,
60  std::unordered_map<unsigned, JoinQualsPerNestingLevel>& left_deep_tree_infos,
61  const TemporaryTables& temporary_tables,
62  Executor* executor)
63  : global_dag_(global_dag)
64  , catalog_(catalog)
66  , left_deep_tree_infos_(left_deep_tree_infos)
67  , temporary_tables_(temporary_tables)
68  , executor_(executor) {
69  translated_join_info_ = std::make_shared<TranslatedJoinInfo>();
70  }
71 
72  // a function that try to extract query plan DAG
74  const RelAlgNode* node,
75  const Catalog_Namespace::Catalog& catalog,
76  std::optional<unsigned> left_deep_tree_id,
77  std::unordered_map<unsigned, JoinQualsPerNestingLevel>& left_deep_tree_infos,
78  const TemporaryTables& temporary_tables,
79  Executor* executor,
80  const RelAlgTranslator& rel_alg_translator);
81 
83 
84  std::shared_ptr<TranslatedJoinInfo> getTranslatedJoinInfo() {
85  return translated_join_info_;
86  }
87 
89  std::optional<unsigned> left_deep_join_tree_id) {
90  if (left_deep_tree_infos_.empty() || !left_deep_join_tree_id) {
91  return nullptr;
92  }
93  CHECK(left_deep_join_tree_id.has_value());
94  auto it = left_deep_tree_infos_.find(left_deep_join_tree_id.value());
95  if (it == left_deep_tree_infos_.end()) {
96  return nullptr;
97  }
98  return &it->second;
99  }
100 
102 
103  std::string getExtractedQueryPlanDagStr();
104 
105  std::vector<InnerOuterOrLoopQual> normalizeColumnsPair(
106  const Analyzer::BinOper* condition,
108 
109  bool isEmptyQueryPlanDag(const std::string& dag) { return dag.compare("N/A") == 0; }
110 
112 
113  void addTableIdToNodeLink(const int table_id, const RelAlgNode* node) {
114  auto it = table_id_to_node_map_.find(table_id);
115  if (it == table_id_to_node_map_.end()) {
116  table_id_to_node_map_.emplace(table_id, node);
117  }
118  }
119 
122  extracted_dag_.clear();
123  table_id_to_node_map_.clear();
124  }
125 
126  private:
127  void visit(const RelAlgNode*, const RelAlgNode*);
128  Analyzer::ColumnVar const* getColVar(const Analyzer::Expr* col_info);
131  bool validateNodeId(const RelAlgNode* node, std::optional<RelNodeId> retrieved_node_id);
132  bool registerNodeToDagCache(const RelAlgNode* parent_node,
133  const RelAlgNode* child_node,
134  std::optional<RelNodeId> retrieved_node_id);
136  const RelAlgNode* node,
137  const Catalog_Namespace::Catalog& catalog,
138  std::optional<unsigned> left_deep_tree_id,
139  std::unordered_map<unsigned, JoinQualsPerNestingLevel>& left_deep_tree_infos,
140  const TemporaryTables& temporary_tables,
141  Executor* executor);
142 
146  std::unordered_map<unsigned, JoinQualsPerNestingLevel>& left_deep_tree_infos_;
148  Executor* executor_;
149  std::shared_ptr<TranslatedJoinInfo> translated_join_info_;
152  std::vector<size_t> extracted_dag_;
153 };
std::pair< const Analyzer::Expr *, const Analyzer::Expr * > inner_outer
QueryPlanDagCache & global_dag_
std::string cat(Ts &&...args)
std::vector< InnerOuterOrLoopQual > normalizeColumnsPair(const Analyzer::BinOper *condition, const Catalog_Namespace::Catalog &cat)
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:113
const std::shared_ptr< TranslatedJoinInfo > translated_join_info
const Catalog_Namespace::Catalog & catalog_
TableIdToNodeMap table_id_to_node_map_
static ExtractedPlanDag extractQueryPlanDag(const RelAlgNode *node, const Catalog_Namespace::Catalog &catalog, std::optional< unsigned > left_deep_tree_id, std::unordered_map< unsigned, JoinQualsPerNestingLevel > &left_deep_tree_infos, const TemporaryTables &temporary_tables, Executor *executor, const RelAlgTranslator &rel_alg_translator)
const TemporaryTables & temporary_tables_
const QueryPlan extracted_dag
TableIdToNodeMap & getTableIdToNodeMap()
std::vector< JoinCondition > JoinQualsPerNestingLevel
const TableIdToNodeMap table_id_to_node_map
HashTableBuildDagMap & getHashTableBuildDag()
std::unordered_map< int, const ResultSetPtr & > TemporaryTables
Definition: InputMetadata.h:31
bool validateNodeId(const RelAlgNode *node, std::optional< RelNodeId > retrieved_node_id)
std::string getExtractedQueryPlanDagStr()
bool isEmptyQueryPlanDag(const std::string &dag)
QueryPlanDagExtractor(QueryPlanDagCache &global_dag, const Catalog_Namespace::Catalog &catalog, std::unordered_map< unsigned, JoinQualsPerNestingLevel > &left_deep_tree_infos, const TemporaryTables &temporary_tables, Executor *executor)
void handleTranslatedJoin(const RelAlgNode *, const RelTranslatedJoin *)
std::unordered_map< int, const RelAlgNode * > TableIdToNodeMap
std::vector< size_t > extracted_dag_
bool registerNodeToDagCache(const RelAlgNode *parent_node, const RelAlgNode *child_node, std::optional< RelNodeId > retrieved_node_id)
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:76
#define CHECK(condition)
Definition: Logger.h:209
std::unordered_map< unsigned, JoinQualsPerNestingLevel > & left_deep_tree_infos_
const RelAlgNode * root_node
static ExtractedPlanDag extractQueryPlanDagImpl(const RelAlgNode *node, const Catalog_Namespace::Catalog &catalog, std::optional< unsigned > left_deep_tree_id, std::unordered_map< unsigned, JoinQualsPerNestingLevel > &left_deep_tree_infos, const TemporaryTables &temporary_tables, Executor *executor)
Execution unit for relational algebra. It&#39;s a low-level description of any relational algebra operati...
const HashTableBuildDagMap hash_table_plan_dag
std::string QueryPlan
const JoinQualsPerNestingLevel * per_nesting_level_join_quals_info
const JoinQualsPerNestingLevel * getPerNestingJoinQualInfo(std::optional< unsigned > left_deep_join_tree_id)
std::shared_ptr< TranslatedJoinInfo > getTranslatedJoinInfo()
std::unordered_map< JoinColumnsInfo, HashTableBuildDag > HashTableBuildDagMap
Analyzer::ColumnVar const * getColVar(const Analyzer::Expr *col_info)