OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  using ExecutorType = Executor;
52 };
53 
54 class RelAlgExecutor : private StorageIOFacility<RelAlgExecutorTraits> {
55  public:
56  using TargetInfoList = std::vector<TargetInfo>;
57 
58  RelAlgExecutor(Executor* executor,
60  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
61  : StorageIOFacility(executor, cat)
62  , executor_(executor)
63  , cat_(cat)
64  , query_state_(std::move(query_state))
65  , now_(0)
66  , queue_time_ms_(0) {}
67 
68  RelAlgExecutor(Executor* executor,
70  const std::string& query_ra,
71  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
72  : StorageIOFacility(executor, cat)
73  , executor_(executor)
74  , cat_(cat)
75  , query_dag_(std::make_unique<RelAlgDagBuilder>(query_ra, cat_, nullptr))
76  , query_state_(std::move(query_state))
77  , now_(0)
78  , queue_time_ms_(0) {}
79 
80  RelAlgExecutor(Executor* executor,
82  std::unique_ptr<RelAlgDagBuilder> query_dag,
83  std::shared_ptr<const query_state::QueryState> query_state = nullptr)
84  : StorageIOFacility(executor, cat)
85  , executor_(executor)
86  , cat_(cat)
87  , query_dag_(std::move(query_dag))
88  , query_state_(std::move(query_state))
89  , now_(0)
90  , queue_time_ms_(0) {}
91 
92  size_t getOuterFragmentCount(const CompilationOptions& co, const ExecutionOptions& eo);
93 
95  const ExecutionOptions& eo,
96  const bool just_explain_plan,
97  RenderInfo* render_info);
98 
100  const CompilationOptions& co,
101  const ExecutionOptions& eo,
102  RenderInfo* render_info,
103  const int64_t queue_time_ms);
104 
106  const AggregatedColRange& agg_col_range,
107  const StringDictionaryGenerations& string_dictionary_generations,
108  const TableGenerations& table_generations);
109 
111  const CompilationOptions& co,
112  const ExecutionOptions& eo,
113  RenderInfo* render_info,
114  const int64_t queue_time_ms,
115  const bool with_existing_temp_tables = false);
116 
118  const std::pair<size_t, size_t> interval,
119  const CompilationOptions& co,
120  const ExecutionOptions& eo,
121  RenderInfo* render_info,
122  const int64_t queue_time_ms);
123 
125  const size_t step_idx,
126  const CompilationOptions& co,
127  const ExecutionOptions& eo,
128  RenderInfo* render_info);
129 
130  void addLeafResult(const unsigned id, const AggregatedResult& result) {
131  const auto it_ok = leaf_results_.emplace(id, result);
132  CHECK(it_ok.second);
133  }
134 
135  const RelAlgNode& getRootRelAlgNode() const {
136  CHECK(query_dag_);
137  return query_dag_->getRootNode();
138  }
139  const std::vector<std::shared_ptr<RexSubQuery>>& getSubqueries() const noexcept {
140  CHECK(query_dag_);
141  return query_dag_->getSubqueries();
142  };
144  CHECK(query_dag_);
145  return query_dag_->getQueryHints();
146  }
147 
149 
153 
154  Executor* getExecutor() const;
155 
156  void cleanupPostExecution();
157 
158  static std::string getErrorMessageFromCode(const int32_t error_code);
159 
160  private:
162  const ExecutionOptions& eo,
163  const bool just_explain_plan,
164  RenderInfo* render_info);
165 
166  void executeRelAlgStep(const RaExecutionSequence& seq,
167  const size_t step_idx,
168  const CompilationOptions&,
169  const ExecutionOptions&,
170  RenderInfo*,
171  const int64_t queue_time_ms);
172 
173  void executeUpdate(const RelAlgNode* node,
174  const CompilationOptions& co,
175  const ExecutionOptions& eo,
176  const int64_t queue_time_ms);
177 
178  void executeDelete(const RelAlgNode* node,
179  const CompilationOptions& co,
180  const ExecutionOptions& eo_in,
181  const int64_t queue_time_ms);
182 
184  const CompilationOptions&,
185  const ExecutionOptions&,
186  RenderInfo*,
187  const int64_t queue_time_ms);
188 
190  const CompilationOptions& co,
191  const ExecutionOptions& eo,
192  RenderInfo* render_info,
193  const int64_t queue_time_ms);
194 
196  const CompilationOptions&,
197  const ExecutionOptions&,
198  RenderInfo*,
199  const int64_t queue_time_ms,
200  const ssize_t previous_count);
201 
203  const CompilationOptions&,
204  const ExecutionOptions&,
205  const int64_t queue_time_ms);
206 
207  // Computes the window function results to be used by the query.
208  void computeWindow(const RelAlgExecutionUnit& ra_exe_unit,
209  const CompilationOptions& co,
210  const ExecutionOptions& eo,
211  ColumnCacheMap& column_cache_map,
212  const int64_t queue_time_ms);
213 
214  // Creates the window context for the given window function.
215  std::unique_ptr<WindowFunctionContext> createWindowFunctionContext(
216  const Analyzer::WindowFunction* window_func,
217  const std::shared_ptr<Analyzer::BinOper>& partition_key_cond,
218  const RelAlgExecutionUnit& ra_exe_unit,
219  const std::vector<InputTableInfo>& query_infos,
220  const CompilationOptions& co,
221  ColumnCacheMap& column_cache_map,
222  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner);
223 
225  const CompilationOptions&,
226  const ExecutionOptions&,
227  RenderInfo*,
228  const int64_t queue_time_ms);
229 
231  const CompilationOptions&,
232  const ExecutionOptions&,
233  RenderInfo*,
234  const int64_t queue_time_ms);
235 
237 
238  ExecutionResult executeModify(const RelModify* modify, const ExecutionOptions& eo);
239 
241  const RaExecutionSequence&,
242  const CompilationOptions&,
243  const ExecutionOptions&,
244  RenderInfo*,
245  const int64_t queue_time_ms);
246 
247  // TODO(alex): just move max_groups_buffer_entry_guess to RelAlgExecutionUnit once
248  // we deprecate the plan-based executor paths and remove WorkUnit
249  struct WorkUnit {
251  const RelAlgNode* body;
253  std::unique_ptr<QueryRewriter> query_rewriter;
254  const std::vector<size_t> input_permutation;
255  const std::vector<size_t> left_deep_join_input_sizes;
256  };
257 
260  const RelAlgNode* body;
261  };
262 
264 
265  ExecutionResult executeWorkUnit(const WorkUnit& work_unit,
266  const std::vector<TargetMetaInfo>& targets_meta,
267  const bool is_agg,
268  const CompilationOptions& co_in,
269  const ExecutionOptions& eo,
270  RenderInfo*,
271  const int64_t queue_time_ms,
272  const ssize_t previous_count = -1);
273 
274  size_t getNDVEstimation(const WorkUnit& work_unit,
275  const bool is_agg,
276  const CompilationOptions& co,
277  const ExecutionOptions& eo);
278 
279  ssize_t getFilteredCountAll(const WorkUnit& work_unit,
280  const bool is_agg,
281  const CompilationOptions& co,
282  const ExecutionOptions& eo);
283 
285  const std::vector<std::shared_ptr<Analyzer::Expr>>& filter_expressions,
286  const CompilationOptions& co,
287  const ExecutionOptions& eo);
288 
289  std::vector<PushedDownFilterInfo> selectFiltersToBePushedDown(
290  const RelAlgExecutor::WorkUnit& work_unit,
291  const CompilationOptions& co,
292  const ExecutionOptions& eo);
293 
294  bool isRowidLookup(const WorkUnit& work_unit);
295 
297  const std::vector<TargetMetaInfo>& targets_meta,
298  const bool is_agg,
299  const CompilationOptions& co,
300  const ExecutionOptions& eo,
301  RenderInfo* render_info,
302  const bool was_multifrag_kernel_launch,
303  const int64_t queue_time_ms);
304 
305  // Allows an out of memory error through if CPU retry is enabled. Otherwise, throws an
306  // appropriate exception corresponding to the query error code.
307  static void handlePersistentError(const int32_t error_code);
308 
309  WorkUnit createWorkUnit(const RelAlgNode*, const SortInfo&, const ExecutionOptions& eo);
310 
312  const SortInfo&,
313  const ExecutionOptions& eo);
314 
316  const SortInfo&,
317  const bool just_explain);
318 
320  const SortInfo&,
321  const ExecutionOptions& eo);
322 
324  const SortInfo&,
325  const bool just_explain);
326 
327  WorkUnit createJoinWorkUnit(const RelJoin*, const SortInfo&, const bool just_explain);
328 
330  const SortInfo&,
331  const ExecutionOptions& eo);
332 
334  const bool just_explain);
335 
336  void addTemporaryTable(const int table_id, const ResultSetPtr& result) {
337  CHECK_LT(size_t(0), result->colCount());
338  CHECK_LT(table_id, 0);
339  const auto it_ok = temporary_tables_.emplace(table_id, result);
340  CHECK(it_ok.second);
341  }
342 
343  void eraseFromTemporaryTables(const int table_id) { temporary_tables_.erase(table_id); }
344 
345  void handleNop(RaExecutionDesc& ed);
346 
348  const RelLeftDeepInnerJoin* join,
349  const std::vector<InputDescriptor>& input_descs,
350  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
351  const bool just_explain);
352 
353  // Transform the provided `join_condition` to conjunctive form, find composite
354  // key opportunities and finally translate it to an Analyzer expression.
355  std::list<std::shared_ptr<Analyzer::Expr>> makeJoinQuals(
356  const RexScalar* join_condition,
357  const std::vector<JoinType>& join_types,
358  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
359  const bool just_explain) const;
360 
361  Executor* executor_;
363  std::unique_ptr<RelAlgDagBuilder> query_dag_;
364  std::shared_ptr<const query_state::QueryState> query_state_;
366  time_t now_;
367  std::vector<std::shared_ptr<Analyzer::Expr>> target_exprs_owned_; // TODO(alex): remove
368  std::unordered_map<unsigned, AggregatedResult> leaf_results_;
369  int64_t queue_time_ms_;
371  static const size_t max_groups_buffer_entry_default_guess{16384};
372 
374 };
375 
376 #endif // QUERYENGINE_RELALGEXECUTOR_H
bool is_agg(const Analyzer::Expr *expr)
int64_t queue_time_ms_
ExecutionResult executeAggregate(const RelAggregate *aggregate, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info, const int64_t queue_time_ms)
ExecutionResult executeProject(const RelProject *, const CompilationOptions &, const ExecutionOptions &, RenderInfo *, const int64_t queue_time_ms, const ssize_t previous_count)
const std::vector< size_t > left_deep_join_input_sizes
TableFunctionWorkUnit createTableFunctionWorkUnit(const RelTableFunction *table_func, const bool just_explain)
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:86
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)
WorkUnit createAggregateWorkUnit(const RelAggregate *, const SortInfo &, const bool just_explain)
const RelAlgNode * body
bool g_skip_intermediate_count
Streaming Top N algorithm.
TableGenerations computeTableGenerations()
static SpeculativeTopNBlacklist speculative_topn_blacklist_
RelAlgExecutionUnit exe_unit
TemporaryTables temporary_tables_
static const size_t max_groups_buffer_entry_default_guess
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::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
size_t getNDVEstimation(const WorkUnit &work_unit, const bool is_agg, const CompilationOptions &co, const ExecutionOptions &eo)
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::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_
const QueryHint getParsedQueryHints() const
size_t getOuterFragmentCount(const CompilationOptions &co, const ExecutionOptions &eo)
CHECK(cgen_state)
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
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)
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)
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)
#define CHECK_LT(x, y)
Definition: Logger.h:207
ssize_t getFilteredCountAll(const WorkUnit &work_unit, const bool is_agg, const CompilationOptions &co, const ExecutionOptions &eo)
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.
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)
TableFunctionExecutionUnit exe_unit
ExecutionResult result
static void handlePersistentError(const int32_t error_code)
const size_t max_groups_buffer_entry_guess
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 ssize_t previous_count=-1)
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)
const MergeType merge_type
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
specifies the content in-memory of a row in the table metadata table
FirstStepExecutionResult executeRelAlgQuerySingleStep(const RaExecutionSequence &seq, const size_t step_idx, const CompilationOptions &co, const ExecutionOptions &eo, RenderInfo *render_info)
void executeDelete(const RelAlgNode *node, const CompilationOptions &co, const ExecutionOptions &eo_in, const int64_t queue_time_ms)
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)