This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 623
/
Copy pathplan_executor.cpp
393 lines (327 loc) · 12.3 KB
/
plan_executor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//===----------------------------------------------------------------------===//
//
// Peloton
//
// plan_executor.cpp
//
// Identification: src/executor/plan_executor.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "executor/plan_executor.h"
#include "codegen/buffering_consumer.h"
#include "codegen/query.h"
#include "codegen/query_cache.h"
#include "codegen/query_compiler.h"
#include "common/logger.h"
#include "concurrency/transaction_manager_factory.h"
#include "executor/executor_context.h"
#include "executor/executors.h"
#include "settings/settings_manager.h"
#include "storage/tuple_iterator.h"
namespace peloton {
namespace executor {
executor::AbstractExecutor *BuildExecutorTree(
executor::AbstractExecutor *root, const planner::AbstractPlan *plan,
executor::ExecutorContext *executor_context);
void CleanExecutorTree(executor::AbstractExecutor *root);
static void CompileAndExecutePlan(
std::shared_ptr<planner::AbstractPlan> plan,
concurrency::TransactionContext *txn,
const std::vector<type::Value> ¶ms,
std::function<void(executor::ExecutionResult, std::vector<ResultValue> &&)>
on_complete) {
LOG_TRACE("Compiling and executing query ...");
// Perform binding
planner::BindingContext context;
plan->PerformBinding(context);
// Prepare output buffer
std::vector<oid_t> columns;
plan->GetOutputColumns(columns);
codegen::BufferingConsumer consumer{columns, context};
// The executor context for this execution
executor::ExecutorContext executor_context{
txn, codegen::QueryParameters(*plan, params)};
// Check if we have a cached compiled plan already
codegen::Query *query = codegen::QueryCache::Instance().Find(plan);
if (query == nullptr) {
codegen::QueryCompiler compiler;
auto compiled_query = compiler.Compile(
*plan, executor_context.GetParams().GetQueryParametersMap(), consumer);
compiled_query->Compile();
// Grab an instance to the plan
query = compiled_query.get();
// Insert the compiled plan into the cache
codegen::QueryCache::Instance().Add(plan, std::move(compiled_query));
}
// Execute the query!
query->Execute(executor_context, consumer);
// Execution complete, setup the results
executor::ExecutionResult result;
result.m_processed = executor_context.num_processed;
result.m_result = ResultType::SUCCESS;
// Iterate over results
std::vector<ResultValue> values;
for (const auto &tuple : consumer.GetOutputTuples()) {
for (uint32_t i = 0; i < tuple.tuple_.size(); i++) {
auto column_val = tuple.GetValue(i);
auto str = column_val.IsNull() ? "" : column_val.ToString();
LOG_TRACE("column content: [%s]", str.c_str());
values.push_back(std::move(str));
}
}
// Done, invoke callback
plan->ClearParameterValues();
on_complete(result, std::move(values));
}
static void InterpretPlan(
std::shared_ptr<planner::AbstractPlan> plan,
concurrency::TransactionContext *txn,
const std::vector<type::Value> ¶ms,
const std::vector<int> &result_format,
std::function<void(executor::ExecutionResult, std::vector<ResultValue> &&)>
on_complete) {
executor::ExecutionResult result;
std::vector<ResultValue> values;
std::unique_ptr<executor::ExecutorContext> executor_context(
new executor::ExecutorContext(txn, params));
bool status;
std::unique_ptr<executor::AbstractExecutor> executor_tree(
BuildExecutorTree(nullptr, plan.get(), executor_context.get()));
status = executor_tree->Init();
if (status != true) {
result.m_result = ResultType::FAILURE;
result.m_error_message = "Failed initialization of query execution tree";
CleanExecutorTree(executor_tree.get());
plan->ClearParameterValues();
on_complete(result, std::move(values));
return;
}
// Execute the tree until we get values tiles from root node
while (status == true) {
status = executor_tree->Execute();
std::unique_ptr<executor::LogicalTile> tile(executor_tree->GetOutput());
// Some executors don't return logical tiles (e.g., Update).
if (tile.get() != nullptr) {
LOG_TRACE("Final Answer: %s", tile->GetInfo().c_str());
std::vector<std::vector<std::string>> tuples;
tuples = tile->GetAllValuesAsStrings(result_format, false);
// Construct the returned results
for (auto &tuple : tuples) {
for (unsigned int i = 0; i < tile->GetColumnCount(); i++) {
LOG_TRACE("column content: %s",
tuple[i].c_str() != nullptr ? tuple[i].c_str() : "-empty-");
values.push_back(std::move(tuple[i]));
}
}
}
}
result.m_processed = executor_context->num_processed;
result.m_result = ResultType::SUCCESS;
CleanExecutorTree(executor_tree.get());
plan->ClearParameterValues();
on_complete(result, std::move(values));
}
void PlanExecutor::ExecutePlan(
std::shared_ptr<planner::AbstractPlan> plan,
concurrency::TransactionContext *txn,
const std::vector<type::Value> ¶ms,
const std::vector<int> &result_format,
std::function<void(executor::ExecutionResult, std::vector<ResultValue> &&)>
on_complete) {
PELOTON_ASSERT(plan != nullptr && txn != nullptr);
LOG_TRACE("PlanExecutor Start (Txn ID=%" PRId64 ")", txn->GetTransactionId());
bool codegen_enabled =
settings::SettingsManager::GetBool(settings::SettingId::codegen);
try {
if (codegen_enabled && codegen::QueryCompiler::IsSupported(*plan)) {
CompileAndExecutePlan(plan, txn, params, on_complete);
} else {
InterpretPlan(plan, txn, params, result_format, on_complete);
}
} catch (Exception &e) {
ExecutionResult result;
result.m_result = ResultType::FAILURE;
result.m_error_message =
StringUtil::Format("ERROR: during execution ['%s']", e.what());
LOG_ERROR("Error during execution: %s", e.what());
on_complete(result, {});
}
}
// FIXME this function is here temporarily to support PelotonService
// which should be refactorized to use ExecutePlan() above
/**
* @brief Build a executor tree and execute it.
* Use std::vector<type::Value> as params to make it more elegant for
* networking
* Before ExecutePlan, a node first receives value list, so we should pass
* value list directly rather than passing Postgres's ParamListInfo
* @return number of executed tuples and logical_tile_list
*/
int PlanExecutor::ExecutePlan(
planner::AbstractPlan *plan, const std::vector<type::Value> ¶ms,
std::vector<std::unique_ptr<executor::LogicalTile>> &logical_tile_list) {
PELOTON_ASSERT(plan != nullptr);
LOG_TRACE("PlanExecutor Start with transaction");
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
PELOTON_ASSERT(txn);
LOG_TRACE("Txn ID = %" PRId64, txn->GetTransactionId());
std::unique_ptr<executor::ExecutorContext> executor_context(
new executor::ExecutorContext(txn, params));
std::unique_ptr<executor::AbstractExecutor> executor_tree(
BuildExecutorTree(nullptr, plan, executor_context.get()));
bool init_failure = false;
bool status = executor_tree->Init();
if (status == false) {
init_failure = true;
txn->SetResult(ResultType::FAILURE);
goto cleanup;
}
LOG_TRACE("Running the executor tree");
// Execute the tree until we get result tiles from root node
while (status == true) {
status = executor_tree->Execute();
if (status == false) break;
std::unique_ptr<executor::LogicalTile> logical_tile(
executor_tree->GetOutput());
logical_tile_list.push_back(std::move(logical_tile));
}
cleanup:
LOG_TRACE("About to commit: init_failure: %d, status: %s", init_failure,
ResultTypeToString(txn->GetResult()).c_str());
// clean up executor tree
CleanExecutorTree(executor_tree.get());
// should we commit or abort ?
if (init_failure == true) {
auto status = txn->GetResult();
switch (status) {
case ResultType::SUCCESS:
return executor_context->num_processed;
case ResultType::FAILURE:
default:
return -1;
}
}
return executor_context->num_processed;
}
/**
* @brief Build the executor tree.
* @param The current executor tree
* @param The plan tree
* @param Transation context
* @return The updated executor tree.
*/
executor::AbstractExecutor *BuildExecutorTree(
executor::AbstractExecutor *root, const planner::AbstractPlan *plan,
executor::ExecutorContext *executor_context) {
// Base case
if (plan == nullptr) return root;
executor::AbstractExecutor *child_executor = nullptr;
auto plan_node_type = plan->GetPlanNodeType();
switch (plan_node_type) {
case PlanNodeType::INVALID:
LOG_ERROR("Invalid plan node type ");
break;
case PlanNodeType::SEQSCAN:
child_executor = new executor::SeqScanExecutor(plan, executor_context);
break;
case PlanNodeType::INDEXSCAN:
child_executor = new executor::IndexScanExecutor(plan, executor_context);
break;
case PlanNodeType::INSERT:
child_executor = new executor::InsertExecutor(plan, executor_context);
break;
case PlanNodeType::DELETE:
child_executor = new executor::DeleteExecutor(plan, executor_context);
break;
case PlanNodeType::UPDATE:
child_executor = new executor::UpdateExecutor(plan, executor_context);
break;
case PlanNodeType::LIMIT:
child_executor = new executor::LimitExecutor(plan, executor_context);
break;
case PlanNodeType::NESTLOOP:
child_executor =
new executor::NestedLoopJoinExecutor(plan, executor_context);
break;
case PlanNodeType::MERGEJOIN:
child_executor = new executor::MergeJoinExecutor(plan, executor_context);
break;
case PlanNodeType::HASH:
child_executor = new executor::HashExecutor(plan, executor_context);
break;
case PlanNodeType::HASHJOIN:
child_executor = new executor::HashJoinExecutor(plan, executor_context);
break;
case PlanNodeType::PROJECTION:
child_executor = new executor::ProjectionExecutor(plan, executor_context);
break;
case PlanNodeType::MATERIALIZE:
child_executor =
new executor::MaterializationExecutor(plan, executor_context);
break;
case PlanNodeType::AGGREGATE_V2:
child_executor = new executor::AggregateExecutor(plan, executor_context);
break;
case PlanNodeType::ORDERBY:
child_executor = new executor::OrderByExecutor(plan, executor_context);
break;
case PlanNodeType::DROP:
child_executor = new executor::DropExecutor(plan, executor_context);
break;
case PlanNodeType::ANALYZE:
child_executor = new executor::AnalyzeExecutor(plan, executor_context);
break;
case PlanNodeType::CREATE:
child_executor = new executor::CreateExecutor(plan, executor_context);
break;
case PlanNodeType::CREATE_FUNC:
child_executor =
new executor::CreateFunctionExecutor(plan, executor_context);
break;
case PlanNodeType::EXPORT_EXTERNAL_FILE:
child_executor = new executor::CopyExecutor(plan, executor_context);
break;
case PlanNodeType::POPULATE_INDEX:
child_executor =
new executor::PopulateIndexExecutor(plan, executor_context);
break;
default:
throw NotImplementedException{
StringUtil::Format("Unsupported plan node type : %s",
PlanNodeTypeToString(plan_node_type).c_str())};
}
LOG_TRACE("Adding %s Executor", PlanNodeTypeToString(plan_node_type).c_str());
// Base case
if (child_executor != nullptr) {
if (root != nullptr)
root->AddChild(child_executor);
else
root = child_executor;
}
// Recurse
auto &children = plan->GetChildren();
for (auto &child : children) {
child_executor =
BuildExecutorTree(child_executor, child.get(), executor_context);
}
return root;
}
/**
* @brief Clean up the executor tree.
* @param The current executor tree
* @return none.
*/
void CleanExecutorTree(executor::AbstractExecutor *root) {
if (root == nullptr) return;
// Recurse
auto children = root->GetChildren();
for (auto child : children) {
CleanExecutorTree(child);
delete child;
}
}
} // namespace executor
} // namespace peloton