OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgExecutionDescriptor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 <boost/graph/adjacency_list.hpp>
20 
23 #include "QueryEngine/ResultSet.h"
24 #include "Shared/TargetInfo.h"
25 #include "Shared/toString.h"
26 
27 class ResultSet;
28 
30  public:
32 
33  ExecutionResult(const std::shared_ptr<ResultSet>& rows,
34  const std::vector<TargetMetaInfo>& targets_meta);
35 
36  ExecutionResult(ResultSetPtr&& result, const std::vector<TargetMetaInfo>& targets_meta);
37 
38  ExecutionResult(const ExecutionResult& that);
39 
41 
42  ExecutionResult(const std::vector<PushedDownFilterInfo>& pushed_down_filter_info,
43  bool filter_push_down_enabled);
44 
46 
47  const std::shared_ptr<ResultSet>& getRows() const { return result_; }
48 
49  bool empty() const { return !result_; }
50 
51  const ResultSetPtr& getDataPtr() const { return result_; }
52 
53  const std::vector<TargetMetaInfo>& getTargetsMeta() const { return targets_meta_; }
54 
55  const std::vector<PushedDownFilterInfo>& getPushedDownFilterInfo() const;
56 
57  const bool isFilterPushDownEnabled() const { return filter_push_down_enabled_; }
58 
59  void setQueueTime(const int64_t queue_time_ms) {
60  CHECK(result_);
61  result_->setQueueTime(queue_time_ms);
62  }
63 
64  std::string toString() const {
65  return ::typeName(this) + "(" + ::toString(result_) + ", " +
67  }
68 
70 
71  std::string getExplanation();
72  void updateResultSet(const std::string& query_ra, RType type, bool success = true);
73  RType getResultType() const { return type_; }
75  int64_t getExecutionTime() const { return execution_time_ms_; }
76  void setExecutionTime(int64_t execution_time_ms) {
77  execution_time_ms_ = execution_time_ms;
78  }
79  void addExecutionTime(int64_t execution_time_ms) {
80  execution_time_ms_ += execution_time_ms;
81  }
82 
83  private:
85  std::vector<TargetMetaInfo> targets_meta_;
86  // filters chosen to be pushed down
87  std::vector<PushedDownFilterInfo> pushed_down_filter_info_;
88  // whether or not it was allowed to look for filters to push down
90 
91  bool success_;
94 };
95 
96 class RelAlgNode;
97 
99  public:
101  : body_(body)
102  , result_(std::make_shared<ResultSet>(std::vector<TargetInfo>{},
105  nullptr,
106  0,
107  0),
108  {}) {}
109 
110  const ExecutionResult& getResult() const { return result_; }
111 
112  void setResult(const ExecutionResult& result);
113 
114  const RelAlgNode* getBody() const;
115 
116  private:
119 };
120 
121 using DAG = boost::
122  adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, const RelAlgNode*>;
123 using Vertex = DAG::vertex_descriptor;
124 
134  public:
135  RaExecutionSequence(const RelAlgNode*, Executor*, const bool build_sequence = true);
136  RaExecutionSequence(std::unique_ptr<RaExecutionDesc> exec_desc);
137 
143 
149 
155  std::optional<size_t> nextStepId(const bool after_broadcast) const;
156 
157  bool executionFinished() const;
158 
166 
171  void skipQuerySteps();
172 
173  std::vector<Vertex> mergeSortWithInput(const std::vector<Vertex>& vertices,
174  const DAG& graph);
175 
176  const std::unordered_map<int, QueryPlanHash> getSkippedQueryStepCacheKeys() const {
177  return cached_resultset_keys_;
178  }
179 
180  RaExecutionDesc* getDescriptor(size_t idx) const {
181  CHECK_LT(idx, descs_.size());
182  return descs_[idx].get();
183  }
184 
185  RaExecutionDesc* getDescriptorByBodyId(unsigned const body_id,
186  size_t const start_idx) const;
187 
188  size_t size() const { return descs_.size(); }
189  bool empty() const { return descs_.empty(); }
190 
191  size_t totalDescriptorsCount() const;
192 
193  const bool hasQueryStepForUnion() const { return has_step_for_union_; }
194 
195  private:
197  Executor* executor_;
198 
199  std::unordered_set<Vertex> joins_;
200  std::vector<Vertex> ordering_; // reverse order topological sort of graph_
201  size_t current_vertex_ = 0;
202  size_t scan_count_ = 0;
203 
204  std::unordered_map<const RelAlgNode*, int> node_ptr_to_vert_idx_;
205  std::unordered_map<int, std::unordered_set<int>> skippable_steps_;
206  // a set of query steps that their result sets are cached
207  std::unordered_set<int> cached_query_steps_;
208  std::unordered_map<int, QueryPlanHash> cached_resultset_keys_;
209  bool has_step_for_union_{false};
210  bool has_limit_clause_{false};
211 
220  size_t stepsToNextBroadcast() const;
221 
222  // The execution descriptors hold the pointers to their results. We need to push them
223  // back into this vector as they are created, so we don't lose the intermediate results
224  // later.
225  std::vector<std::unique_ptr<RaExecutionDesc>> descs_;
226 };
RType getResultType() const
RaExecutionDesc * getDescriptor(size_t idx) const
std::vector< Vertex > mergeSortWithInput(const std::vector< Vertex > &vertices, const DAG &graph)
const bool hasQueryStepForUnion() const
ExecutionResult & operator=(const ExecutionResult &that)
const std::unordered_map< int, QueryPlanHash > getSkippedQueryStepCacheKeys() const
void setQueueTime(const int64_t queue_time_ms)
const RelAlgNode * body_
const ResultSetPtr & getDataPtr() const
std::shared_ptr< ResultSet > ResultSetPtr
std::unordered_set< Vertex > joins_
void addExecutionTime(int64_t execution_time_ms)
tuple rows
Definition: report.py:114
std::unordered_set< int > cached_query_steps_
std::vector< std::unique_ptr< RaExecutionDesc > > descs_
A container for relational algebra descriptors defining the execution order for a relational algebra ...
const std::vector< TargetMetaInfo > & getTargetsMeta() const
const std::shared_ptr< ResultSet > & getRows() const
void updateResultSet(const std::string &query_ra, RType type, bool success=true)
RaExecutionDesc(const RelAlgNode *body)
std::string toString() const
std::vector< PushedDownFilterInfo > pushed_down_filter_info_
void setResultType(RType type)
#define CHECK_LT(x, y)
Definition: Logger.h:303
std::unordered_map< const RelAlgNode *, int > node_ptr_to_vert_idx_
RaExecutionSequence(const RelAlgNode *, Executor *, const bool build_sequence=true)
const std::vector< PushedDownFilterInfo > & getPushedDownFilterInfo() const
std::vector< Vertex > ordering_
Descriptor for the result set buffer layout.
std::string typeName(const T *v)
Definition: toString.h:106
const bool isFilterPushDownEnabled() const
#define CHECK(condition)
Definition: Logger.h:291
void setExecutionTime(int64_t execution_time_ms)
const RelAlgNode * getBody() const
Basic constructors and methods of the row set interface.
boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS, const RelAlgNode * > DAG
DAG::vertex_descriptor Vertex
std::vector< TargetMetaInfo > targets_meta_
int64_t getExecutionTime() const
RaExecutionDesc * getDescriptorByBodyId(unsigned const body_id, size_t const start_idx) const
ResultSet(const std::vector< TargetInfo > &targets, const ExecutorDeviceType device_type, const QueryMemoryDescriptor &query_mem_desc, const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const unsigned block_size, const unsigned grid_size)
Definition: ResultSet.cpp:64
std::unordered_map< int, std::unordered_set< int > > skippable_steps_
std::unordered_map< int, QueryPlanHash > cached_resultset_keys_
std::optional< size_t > nextStepId(const bool after_broadcast) const