private void connectChildrenBestPlans ( AbstractPlanNode parentPlan ) { if ( parentPlan instanceof AbstractScanPlanNode ) { AbstractScanPlanNode scanNode = ( AbstractScanPlanNode ) parentPlan ; StmtTableScan tableScan = scanNode . getTableScan ( ) ; if ( tableScan instanceof StmtSubqueryScan ) { CompiledPlan bestCostPlan = ( ( StmtSubqueryScan ) tableScan ) . getBestCostPlan ( ) ; assert ( bestCostPlan != null ) ; AbstractPlanNode subQueryRoot = bestCostPlan . rootPlanGraph ; subQueryRoot . disconnectParents ( ) ; scanNode . clearChildren ( ) ; scanNode . addAndLinkChild ( subQueryRoot ) ; } else if ( tableScan instanceof StmtCommonTableScan ) { assert ( parentPlan instanceof SeqScanPlanNode ) ; SeqScanPlanNode scanPlanNode = ( SeqScanPlanNode ) parentPlan ; StmtCommonTableScan cteScan = ( StmtCommonTableScan ) tableScan ; CompiledPlan bestCostBasePlan = cteScan . getBestCostBasePlan ( ) ; CompiledPlan bestCostRecursivePlan = cteScan . getBestCostRecursivePlan ( ) ; assert ( bestCostBasePlan != null ) ; AbstractPlanNode basePlanRoot = bestCostBasePlan . rootPlanGraph ; scanPlanNode . setCTEBaseNode ( basePlanRoot ) ; if ( bestCostRecursivePlan != null ) { // Either the CTE is not recursive, or this is a recursive CTE but we // got here during the planning of the recurse query when the recurse // query plan is still being worked on. AbstractPlanNode recursePlanRoot = bestCostRecursivePlan . rootPlanGraph ; assert ( basePlanRoot instanceof CommonTablePlanNode ) ; CommonTablePlanNode ctePlanNode = ( CommonTablePlanNode ) basePlanRoot ; ctePlanNode . setRecursiveNode ( recursePlanRoot ) ; } } } else { for ( int i = 0 ; i < parentPlan . getChildCount ( ) ; ++ i ) { connectChildrenBestPlans ( parentPlan . getChild ( i ) ) ; } } }
For each sub - query or CTE node in the plan tree attach the corresponding plans to the parent node .