OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgExecutor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #ifndef QUERYENGINE_RELALGEXECUTOR_H
18 #define QUERYENGINE_RELALGEXECUTOR_H
19 
22 #include "QueryEngine/Execute.h"
29 #include "Shared/scope.h"
31 
32 #include <ctime>
33 #include <sstream>
34 
35 #include "StorageIOFacility.h"
36 
37 extern bool g_skip_intermediate_count;
38 
39 enum class MergeType { Union, Reduce };
40 
44  const unsigned node_id;
46 };
47 
49  public:
50  using TargetInfoList = std::vector<TargetInfo>;
51 
52  RelAlgExecutor(Executor* executor,
54  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
55  : StorageIOFacility(executor, cat)
56  , executor_(executor)
57  , cat_(cat)
58  , query_state_(std::move(query_state))
59  , now_(0)
60  , queue_time_ms_(0) {}
61 
62  RelAlgExecutor(Executor* executor,
64  const std::string& query_ra,
65  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
66  : StorageIOFacility(executor, cat)
67  , executor_(executor)
68  , cat_(cat)
69  , query_dag_(std::make_unique<RelAlgDagBuilder>(query_ra, cat_, nullptr))
70  , query_state_(std::move(query_state))
71  , now_(0)
72  , queue_time_ms_(0) {}
73 
74  RelAlgExecutor(Executor* executor,
76  std::unique_ptr<RelAlgDagBuilder> query_dag,
77  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
78  : StorageIOFacility(executor, cat)
79  , executor_(executor)
80  , cat_(cat)
81  , query_dag_(std::move(query_dag))
82  , query_state_(std::move(query_state))
83  , now_(0)
84  , queue_time_ms_(0) {}
85 
86  size_t getOuterFragmentCount(const CompilationOptions& co, const ExecutionOptions& eo);
87 
89  const ExecutionOptions& eo,
90  const bool just_explain_plan,
91  RenderInfo* render_info);
92 
94  const CompilationOptions& co,
95  const ExecutionOptions& eo,
96  RenderInfo* render_info,
97  const int64_t queue_time_ms);
98 
100  const AggregatedColRange& agg_col_range,
101  const StringDictionaryGenerations& string_dictionary_generations,
102  const TableGenerations& table_generations);
103 
105  const CompilationOptions& co,
106  const ExecutionOptions& eo,
107  RenderInfo* render_info,
108  const int64_t queue_time_ms,
109  const bool with_existing_temp_tables = false);
110 
112  const std::pair<size_t, size_t> interval,
113  const CompilationOptions& co,
114  const ExecutionOptions& eo,
115  RenderInfo* render_info,
116  const int64_t queue_time_ms);
117 
119  const size_t step_idx,
120  const CompilationOptions& co,
121  const ExecutionOptions& eo,
122  RenderInfo* render_info);
123 
124  void addLeafResult(const unsigned id, const AggregatedResult& result) {
125  const auto it_ok = leaf_results_.emplace(id, result);
126  CHECK(it_ok.second);
127  }
128 
129  const RelAlgNode& getRootRelAlgNode() const {
130  CHECK(query_dag_);
131  return query_dag_->getRootNode();
132  }
133 
134  std::shared_ptr<const RelAlgNode> getRootRelAlgNodeShPtr() const {
135  CHECK(query_dag_);
136  return query_dag_->getRootNodeShPtr();
137  }
138 
139  std::pair<std::vector<unsigned>, std::unordered_map<unsigned, JoinQualsPerNestingLevel>>
140  getJoinInfo(const RelAlgNode* root_node);
141 
142  std::shared_ptr<RelAlgTranslator> getRelAlgTranslator(const RelAlgNode* root_node);
143 
144  const std::vector<std::shared_ptr<RexSubQuery>>& getSubqueries() const noexcept {
145  CHECK(query_dag_);
146  return query_dag_->getSubqueries();
147  };
149  CHECK(query_dag_);
150  return query_dag_->getQueryHints();
151  }
152 
154 
158 
159  Executor* getExecutor() const;
160 
161  void cleanupPostExecution();
162 
163  static std::string getErrorMessageFromCode(const int32_t error_code);
164 
166 
167  private:
169  const ExecutionOptions& eo,
170  const bool just_explain_plan,
171  RenderInfo* render_info);
172 
173  void executeRelAlgStep(const RaExecutionSequence& seq,
174  const size_t step_idx,
175  const CompilationOptions&,
176  const ExecutionOptions&,
177  RenderInfo*,
178  const int64_t queue_time_ms);
179 
180  void executeUpdate(const RelAlgNode* node,
181  const CompilationOptions& co,
182  const ExecutionOptions& eo,
183  const int64_t queue_time_ms);
184 
185  void executeDelete(const RelAlgNode* node,
186  const CompilationOptions& co,
187  const ExecutionOptions& eo_in,
188  const int64_t queue_time_ms);
189 
191  const CompilationOptions&,
192  const ExecutionOptions&,
193  RenderInfo*,
194  const int64_t queue_time_ms);
195 
197  const CompilationOptions& co,
198  const ExecutionOptions& eo,
199  RenderInfo* render_info,
200  const int64_t queue_time_ms);
201 
203  const CompilationOptions&,
204  const ExecutionOptions&,
205  RenderInfo*,
206  const int64_t queue_time_ms,
207  const std::optional<size_t> previous_count);
208 
210  const CompilationOptions&,
211  const ExecutionOptions&,
212  const int64_t queue_time_ms);
213 
214  // Computes the window function results to be used by the query.
215  void computeWindow(const RelAlgExecutionUnit& ra_exe_unit,
216  const CompilationOptions& co,
217  const ExecutionOptions& eo,
218  ColumnCacheMap& column_cache_map,
219  const int64_t queue_time_ms);
220 
221  // Creates the window context for the given window function.
222  std::unique_ptr<WindowFunctionContext> createWindowFunctionContext(
223  const Analyzer::WindowFunction* window_func,
224  const std::shared_ptr<Analyzer::BinOper>& partition_key_cond,
225  const RelAlgExecutionUnit& ra_exe_unit,
226  const std::vector<InputTableInfo>& query_infos,
227  const CompilationOptions& co,
228  ColumnCacheMap& column_cache_map,
229  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner);
230 
232  const CompilationOptions&,
233  const ExecutionOptions&,
234  RenderInfo*,
235  const int64_t queue_time_ms);
236 
238  const CompilationOptions&,
239  const ExecutionOptions&,
240  RenderInfo*,
241  const int64_t queue_time_ms);
242 
244 
245  ExecutionResult executeModify(const RelModify* modify, const ExecutionOptions& eo);
246 
248  const RaExecutionSequence&,
249  const CompilationOptions&,
250  const ExecutionOptions&,
251  RenderInfo*,
252  const int64_t queue_time_ms);
253 
254  // TODO(alex): just move max_groups_buffer_entry_guess to RelAlgExecutionUnit once
255  // we deprecate the plan-based executor paths and remove WorkUnit
256  struct WorkUnit {
258  const RelAlgNode* body;
260  std::unique_ptr<QueryRewriter> query_rewriter;
261  const std::vector<size_t> input_permutation;
262  const std::vector<size_t> left_deep_join_input_sizes;
263  };
264 
267  const RelAlgNode* body;
268  };
269 
271 
273  const WorkUnit& work_unit,
274  const std::vector<TargetMetaInfo>& targets_meta,
275  const bool is_agg,
276  const CompilationOptions& co_in,
277  const ExecutionOptions& eo,
278  RenderInfo*,
279  const int64_t queue_time_ms,
280  const std::optional<size_t> previous_count = std::nullopt);
281 
282  size_t getNDVEstimation(const WorkUnit& work_unit,
283  const int64_t range,
284  const bool is_agg,
285  const CompilationOptions& co,
286  const ExecutionOptions& eo);
287 
288  std::optional<size_t> getFilteredCountAll(const WorkUnit& work_unit,
289  const bool is_agg,
290  const CompilationOptions& co,
291  const ExecutionOptions& eo);
292 
294  const std::vector<std::shared_ptr<Analyzer::Expr>>& filter_expressions,
295  const CompilationOptions& co,
296  const ExecutionOptions& eo);
297 
298  std::vector<PushedDownFilterInfo> selectFiltersToBePushedDown(
299  const RelAlgExecutor::WorkUnit& work_unit,
300  const CompilationOptions& co,
301  const ExecutionOptions& eo);
302 
303  bool isRowidLookup(const WorkUnit& work_unit);
304 
306  const std::vector<TargetMetaInfo>& targets_meta,
307  const bool is_agg,
308  const CompilationOptions& co,
309  const ExecutionOptions& eo,
310  RenderInfo* render_info,
311  const bool was_multifrag_kernel_launch,
312  const int64_t queue_time_ms);
313 
314  // Allows an out of memory error through if CPU retry is enabled. Otherwise, throws an
315  // appropriate exception corresponding to the query error code.
316  static void handlePersistentError(const int32_t error_code);
317 
318  WorkUnit createWorkUnit(const RelAlgNode*, const SortInfo&, const ExecutionOptions& eo);
319 
321  const SortInfo&,
322  const ExecutionOptions& eo);
323 
325  const SortInfo&,
326  const bool just_explain);
327 
329  const SortInfo&,
330  const ExecutionOptions& eo);
331 
333  const SortInfo&,
334  const bool just_explain);
335 
336  WorkUnit createJoinWorkUnit(const RelJoin*, const SortInfo&, const bool just_explain);
337 
339  const SortInfo&,
340  const ExecutionOptions& eo);
341 
343  const bool just_explain,
344  const bool is_gpu);
345 
346  void addTemporaryTable(const int table_id, const ResultSetPtr& result) {
347  CHECK_LT(size_t(0), result->colCount());
348  CHECK_LT(table_id, 0);
349  const auto it_ok = temporary_tables_.emplace(table_id, result);
350  CHECK(it_ok.second);
351  }
352 
353  void eraseFromTemporaryTables(const int table_id) { temporary_tables_.erase(table_id); }
354 
355  void handleNop(RaExecutionDesc& ed);
356 
357  std::unordered_map<unsigned, JoinQualsPerNestingLevel>& getLeftDeepJoinTreesInfo() {
358  return left_deep_join_info_;
359  }
360 
362  const RelLeftDeepInnerJoin* join,
363  const std::vector<InputDescriptor>& input_descs,
364  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
365  const bool just_explain);
366 
367  // Transform the provided `join_condition` to conjunctive form, find composite
368  // key opportunities and finally translate it to an Analyzer expression.
369  std::list<std::shared_ptr<Analyzer::Expr>> makeJoinQuals(
370  const RexScalar* join_condition,
371  const std::vector<JoinType>& join_types,
372  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
373  const bool just_explain) const;
374 
375  Executor* executor_;
377  std::unique_ptr<RelAlgDagBuilder> query_dag_;
378  std::shared_ptr<const query_state::QueryState> query_state_;
380  time_t now_;
381  std::unordered_map<unsigned, JoinQualsPerNestingLevel> left_deep_join_info_;
382  std::vector<std::shared_ptr<Analyzer::Expr>> target_exprs_owned_; // TODO(alex): remove
383  std::unordered_map<unsigned, AggregatedResult> leaf_results_;
384  int64_t queue_time_ms_;
386 
387  std::unique_ptr<TransactionParameters> dml_transaction_parameters_;
388  std::optional<std::function<void()>> post_execution_callback_;
389 
391 };
392 
393 #endif // QUERYENGINE_RELALGEXECUTOR_H
bool is_agg(const Analyzer::Expr *expr)
int64_t queue_time_ms_
std::optional< std::function< void()> > post_execution_callback_
ExecutionResult executeAggregate(const RelAggregate *aggregate, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const int64_t queue_time_ms)
const std::vector< size_t > left_deep_join_input_sizes
std::string cat(Ts &&...args)
void addLeafResult(const unsigned id, const AggregatedResult &result)
AggregatedColRange computeColRangesCache()
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:102
WorkUnit createProjectWorkUnit(const RelProject *, const SortInfo &, const ExecutionOptions &eo)
ExecutionResult executeRelAlgSeq(const RaExecutionSequence &seq, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const int64_t queue_time_ms, const bool with_existing_temp_tables=false)
TableFunctionWorkUnit createTableFunctionWorkUnit(const RelTableFunction *table_func, const bool just_explain, const bool is_gpu)
WorkUnit createAggregateWorkUnit(const RelAggregate *, const SortInfo &, const bool just_explain)
const RelAlgNode * body
bool g_skip_intermediate_count
#define const
Streaming Top N algorithm.
TableGenerations computeTableGenerations()
static SpeculativeTopNBlacklist speculative_topn_blacklist_
RelAlgExecutionUnit exe_unit
TemporaryTables temporary_tables_
ExecutionResult executeRelAlgQueryWithFilterPushDown(const RaExecutionSequence &seq, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const int64_t queue_time_ms)
FilterSelectivity getFilterSelectivity(const std::vector< std::shared_ptr< Analyzer::Expr >> &filter_expressions, const CompilationOptions &co, const ExecutionOptions &eo)
std::string join(T const &container, std::string const &delim)
std::unordered_map< unsigned, JoinQualsPerNestingLevel > & getLeftDeepJoinTreesInfo()
std::vector< PushedDownFilterInfo > selectFiltersToBePushedDown(const RelAlgExecutor::WorkUnit &work_unit, const CompilationOptions &co, const ExecutionOptions &eo)
RelAlgExecutor(Executor *executor, const Catalog_Namespace::Catalog &cat, const std::string &query_ra, std::shared_ptr< const query_state::QueryState > query_state=nullptr)
ExecutionResult executeModify(const RelModify *modify, const ExecutionOptions &eo)
void addTemporaryTable(const int table_id, const ResultSetPtr &result)
std::unique_ptr< QueryRewriter > query_rewriter
std::unordered_map< unsigned, AggregatedResult > leaf_results_
std::vector< TargetInfo > TargetInfoList
std::vector< JoinCondition > JoinQualsPerNestingLevel
std::shared_ptr< ResultSet > ResultSetPtr
ExecutionResult executeLogicalValues(const RelLogicalValues *, const ExecutionOptions &)
ExecutionResult executeFilter(const RelFilter *, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms)
const std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries() const noexcept
QueryStepExecutionResult executeRelAlgQuerySingleStep(const RaExecutionSequence &seq, const size_t step_idx, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info)
void handleNop(RaExecutionDesc &ed)
RelAlgExecutor(Executor *executor, const Catalog_Namespace::Catalog &cat, std::unique_ptr< RelAlgDagBuilder > query_dag, std::shared_ptr< const query_state::QueryState > query_state=nullptr)
void computeWindow(const RelAlgExecutionUnit &ra_exe_unit, const CompilationOptions &co, const ExecutionOptions &eo, ColumnCacheMap &column_cache_map, const int64_t queue_time_ms)
std::shared_ptr< const RelAlgNode > getRootRelAlgNodeShPtr() const
std::unordered_map< int, const ResultSetPtr & > TemporaryTables
Definition: InputMetadata.h:31
WorkUnit createCompoundWorkUnit(const RelCompound *, const SortInfo &, const ExecutionOptions &eo)
WorkUnit createWorkUnit(const RelAlgNode *, const SortInfo &, const ExecutionOptions &eo)
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs_owned_
size_t getOuterFragmentCount(const CompilationOptions &co, const ExecutionOptions &eo)
MergeType
void executeUpdate(const RelAlgNode *node, const CompilationOptions &co, const ExecutionOptions &eo, const int64_t queue_time_ms)
A container for relational algebra descriptors defining the execution order for a relational algebra ...
const Catalog_Namespace::Catalog & cat_
friend class PendingExecutionClosure
ExecutionResult handleOutOfMemoryRetry(const RelAlgExecutor::WorkUnit &work_unit, const std::vector< TargetMetaInfo > &targets_meta, const bool is_agg, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const bool was_multifrag_kernel_launch, const int64_t queue_time_ms)
void executePostExecutionCallback()
size_t getNDVEstimation(const WorkUnit &work_unit, const int64_t range, const bool is_agg, const CompilationOptions &co, const ExecutionOptions &eo)
ExecutionResult executeRelAlgSubSeq(const RaExecutionSequence &seq, const std::pair< size_t, size_t > interval, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const int64_t queue_time_ms)
const std::vector< size_t > input_permutation
WorkUnit createUnionWorkUnit(const RelLogicalUnion *, const SortInfo &, const ExecutionOptions &eo)
ExecutionResult executeRelAlgQueryNoRetry(const CompilationOptions &co, const ExecutionOptions &eo, const bool just_explain_plan, RenderInfo *render_info)
void executeRelAlgStep(const RaExecutionSequence &seq, const size_t step_idx, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms)
std::optional< size_t > getFilteredCountAll(const WorkUnit &work_unit, const bool is_agg, const CompilationOptions &co, const ExecutionOptions &eo)
Executor * getExecutor() const
std::unordered_map< int, std::unordered_map< int, std::shared_ptr< const ColumnarResults >>> ColumnCacheMap
ExecutionResult executeTableFunction(const RelTableFunction *, const CompilationOptions &, const ExecutionOptions &, const int64_t queue_time_ms)
std::unordered_map< unsigned, JoinQualsPerNestingLevel > left_deep_join_info_
#define CHECK_LT(x, y)
Definition: Logger.h:216
ExecutionResult executeSimpleInsert(const Analyzer::Query &insert_query)
WorkUnit createJoinWorkUnit(const RelJoin *, const SortInfo &, const bool just_explain)
ExecutionResult executeUnion(const RelLogicalUnion *, const RaExecutionSequence &, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms)
std::shared_ptr< const query_state::QueryState > query_state_
Speculative top N algorithm.
RegisteredQueryHint getParsedQueryHints()
const MergeType merge_type
const RelAlgNode & getRootRelAlgNode() const
void eraseFromTemporaryTables(const int table_id)
ExecutionResult executeSort(const RelSort *, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms)
bool isRowidLookup(const WorkUnit &work_unit)
ExecutionResult executeProject(const RelProject *, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms, const std::optional< size_t > previous_count)
TableFunctionExecutionUnit exe_unit
static void handlePersistentError(const int32_t error_code)
const size_t max_groups_buffer_entry_guess
StringDictionaryGenerations computeStringDictionaryGenerations()
RelAlgExecutor(Executor *executor, const Catalog_Namespace::Catalog &cat, std::shared_ptr< const query_state::QueryState > query_state=nullptr)
ExecutionResult executeRelAlgQuery(const CompilationOptions &co, const ExecutionOptions &eo, const bool just_explain_plan, RenderInfo *render_info)
#define CHECK(condition)
Definition: Logger.h:206
ExecutionResult executeCompound(const RelCompound *, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms)
std::unique_ptr< RelAlgDagBuilder > query_dag_
static std::string getErrorMessageFromCode(const int32_t error_code)
void cleanupPostExecution()
std::list< std::shared_ptr< Analyzer::Expr > > makeJoinQuals(const RexScalar *join_condition, const std::vector< JoinType > &join_types, const std::unordered_map< const RelAlgNode *, int > &input_to_nest_level, const bool just_explain) const
std::unique_ptr< TransactionParameters > dml_transaction_parameters_
ExecutionResult executeWorkUnit(const WorkUnit &work_unit, const std::vector< TargetMetaInfo > &targets_meta, const bool is_agg, const CompilationOptions &co_in, const ExecutionOptions &eo, RenderInfo *, const int64_t queue_time_ms, const std::optional< size_t > previous_count=std::nullopt)
std::pair< std::vector< unsigned >, std::unordered_map< unsigned, JoinQualsPerNestingLevel > > getJoinInfo(const RelAlgNode *root_node)
void executeDelete(const RelAlgNode *node, const CompilationOptions &co, const ExecutionOptions &eo_in, const int64_t queue_time_ms)
ExecutionResult result
std::shared_ptr< RelAlgTranslator > getRelAlgTranslator(const RelAlgNode *root_node)
Executor * executor_
std::unique_ptr< WindowFunctionContext > createWindowFunctionContext(const Analyzer::WindowFunction *window_func, const std::shared_ptr< Analyzer::BinOper > &partition_key_cond, const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, const CompilationOptions &co, ColumnCacheMap &column_cache_map, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
WorkUnit createFilterWorkUnit(const RelFilter *, const SortInfo &, const bool just_explain)
WorkUnit createSortInputWorkUnit(const RelSort *, const ExecutionOptions &eo)
JoinQualsPerNestingLevel translateLeftDeepJoinFilter(const RelLeftDeepInnerJoin *join, const std::vector< InputDescriptor > &input_descs, const std::unordered_map< const RelAlgNode *, int > &input_to_nest_level, const bool just_explain)
void prepareLeafExecution(const AggregatedColRange &agg_col_range, const StringDictionaryGenerations &string_dictionary_generations, const TableGenerations &table_generations)