New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plan clone refactor #1278
Plan clone refactor #1278
Changes from all commits
8c41351
6ce1889
8f7b6a7
bad7c64
91439fd
c6f65c4
1ac1230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,95 +5,73 @@ | |
*/ | ||
|
||
#include "execution_plan_clone.h" | ||
#include "../RG.h" | ||
#include "../query_ctx.h" | ||
#include "../util/rax_extensions.h" | ||
|
||
// Clones an execution plan operations, with respect to the original execution plan segment. | ||
static OpBase *_ExecutionPlan_CloneOperations(const ExecutionPlan *orig_plan, | ||
ExecutionPlan *clone_plan, const OpBase *op) { | ||
// If there is no op, or the op is a part of a different segment, return NULL. | ||
if(!op || op->plan != orig_plan) return NULL; | ||
static ExecutionPlan *_ClonePlanInternals(const ExecutionPlan *template) { | ||
ExecutionPlan *clone = ExecutionPlan_NewEmptyExecutionPlan(); | ||
|
||
// Clone the op. | ||
OpBase *clone = OpBase_Clone(clone_plan, op); | ||
// Clone the op's children and add them the cloned children array. | ||
for(uint i = 0; i < op->childCount; i++) { | ||
OpBase *cloned_child = _ExecutionPlan_CloneOperations(orig_plan, clone_plan, | ||
op->children[i]); | ||
// Assumption: all the children are either in the same segment or on a different segment. | ||
if(!cloned_child) break; | ||
ExecutionPlan_AddOp(clone, cloned_child); | ||
clone->record_map = raxClone(template->record_map); | ||
if(template->ast_segment) clone->ast_segment = AST_ShallowCopy(template->ast_segment); | ||
if(template->query_graph) clone->query_graph = QueryGraph_Clone(template->query_graph); | ||
// TODO improve QueryGraph logic so that we do not need to store or clone connected_components. | ||
if(template->connected_components) { | ||
array_clone_with_cb(clone->connected_components, template->connected_components, QueryGraph_Clone); | ||
} | ||
|
||
// Temporarily set the thread-local AST to be the one referenced by this ExecutionPlan segment. | ||
QueryCtx_SetAST(clone->ast_segment); | ||
|
||
return clone; | ||
} | ||
|
||
// Merge cloned execution plan segments, with respect to the plan type (union or not). | ||
static void _ExecutionPlan_MergeSegments(ExecutionPlan *plan) { | ||
// No need to merge segments if there aren't any. | ||
uint segment_count = array_len(plan->segments); | ||
if(segment_count == 0) return; | ||
|
||
if(plan->is_union) { | ||
// Locate the join operation. | ||
OpBase *join_op = ExecutionPlan_LocateOp(plan->root, OPType_JOIN); | ||
assert(join_op); | ||
// Each segment is a sub execution plan that needs to be joined. | ||
for(int i = 0; i < segment_count; i++) { | ||
ExecutionPlan *sub_plan = plan->segments[i]; | ||
ExecutionPlan_AddOp(join_op, sub_plan->root); | ||
} | ||
static OpBase *_CloneOpTree(OpBase *template_parent, OpBase *template_current, | ||
OpBase *clone_parent) { | ||
const ExecutionPlan *plan_segment; | ||
if(!template_parent || (template_current->plan != template_parent->plan)) { | ||
/* If this is the first operation or it was built using a different ExecutionPlan | ||
* segment than its parent, clone the ExecutionPlan segment. */ | ||
plan_segment = _ClonePlanInternals(template_current->plan); | ||
} else { | ||
array_append(plan->segments, plan); | ||
segment_count = array_len(plan->segments); | ||
// Plan is not union, concatenate the segments. | ||
OpBase *connecting_op; | ||
// segments[0] is the first segment of the execution. | ||
OpBase *prev_root = plan->segments[0]->root; | ||
for(uint i = 1; i < segment_count; i++) { | ||
ExecutionPlan *current_segment = plan->segments[i]; | ||
connecting_op = ExecutionPlan_LocateOpMatchingType(current_segment->root, PROJECT_OPS, | ||
PROJECT_OP_COUNT); | ||
assert(connecting_op->childCount == 0); | ||
ExecutionPlan_AddOp(connecting_op, prev_root); | ||
prev_root = current_segment->root; | ||
} | ||
array_pop(plan->segments); | ||
// This op was built as part of the same segment as its parent, don't change ExecutionPlans. | ||
plan_segment = clone_parent->plan; | ||
} | ||
} | ||
|
||
/* This function clones execution plan by cloning each segment in the execution plan as a unit. | ||
* Each segment has its own filter tree, record mapping, query graphs and ast segment, that compose | ||
* a single logical execution unit, together with the segment operations. | ||
* The ast segment is shallow copied while all the other objects are deep cloned. | ||
*/ | ||
ExecutionPlan *ExecutionPlan_Clone(const ExecutionPlan *template) { | ||
if(template == NULL) return NULL; | ||
// Verify that the execution plan template is not prepared yet. | ||
assert(template->prepared == false && "Execution plan cloning should be only on templates"); | ||
// Allocate an empty execution plan. | ||
ExecutionPlan *clone = ExecutionPlan_NewEmptyExecutionPlan(); | ||
// Clone the current operation. | ||
OpBase *clone_current = OpBase_Clone(plan_segment, template_current); | ||
|
||
clone->is_union = template->is_union; | ||
clone->record_map = raxClone(template->record_map); | ||
if(template->ast_segment) clone->ast_segment = AST_ShallowCopy(template->ast_segment); | ||
if(template->query_graph) clone->query_graph = QueryGraph_Clone(template->query_graph); | ||
if(template->connected_components) { | ||
array_clone_with_cb(clone->connected_components, template->connected_components, QueryGraph_Clone); | ||
for(uint i = 0; i < template_current->childCount; i++) { | ||
// Recursively visit and clone the op's children. | ||
OpBase *child_op = _CloneOpTree(template_current, template_current->children[i], clone_current); | ||
ExecutionPlan_AddOp(clone_current, child_op); | ||
} | ||
|
||
// The execution plan segment clone requires the specific AST segment for referenced entities. | ||
return clone_current; | ||
} | ||
|
||
static ExecutionPlan *_ExecutionPlan_Clone(const ExecutionPlan *template) { | ||
OpBase *clone_root = _CloneOpTree(NULL, template->root, NULL); | ||
// The "master" execution plan is the one constructed with the root op. | ||
ExecutionPlan *clone = (ExecutionPlan *)clone_root->plan; | ||
// The root op is currently NULL; set it now. | ||
clone->root = clone_root; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This turns out to not be redundant, the |
||
|
||
return clone; | ||
} | ||
|
||
/* This function clones the input ExecutionPlan by recursively visiting its tree of ops. | ||
* When an op is encountered that was constructed as part of a different ExecutionPlan segment, that segment | ||
* and its internal members (FilterTree, record mapping, query graphs, and AST segment) are also cloned. */ | ||
ExecutionPlan *ExecutionPlan_Clone(const ExecutionPlan *template) { | ||
ASSERT(template != NULL); | ||
// Store the original AST pointer. | ||
AST *master_ast = QueryCtx_GetAST(); | ||
QueryCtx_SetAST(clone->ast_segment); | ||
// Clone each operation in the template relevant segment. | ||
clone->root = _ExecutionPlan_CloneOperations(template, clone, template->root); | ||
// After clone, restore master ast. | ||
// Verify that the execution plan template is not prepared yet. | ||
ASSERT(template->prepared == false && "Execution plan cloning should be only on templates"); | ||
ExecutionPlan *clone = _ExecutionPlan_Clone(template); | ||
// Restore the original AST pointer. | ||
QueryCtx_SetAST(master_ast); | ||
|
||
if(template->segments) { | ||
array_clone_with_cb(clone->segments, template->segments, ExecutionPlan_Clone); | ||
} | ||
// Merge the segments | ||
_ExecutionPlan_MergeSegments(clone); | ||
return clone; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid both
query_graph
andconnected_components
if we introduce ref count to the QueryGraph object.