private Optional < PlanNode > recurseToPartial ( PlanNode node , Lookup lookup , PlanNodeIdAllocator idAllocator ) { if ( node instanceof AggregationNode && ( ( AggregationNode ) node ) . getStep ( ) == PARTIAL ) { return Optional . of ( addGatheringIntermediate ( ( AggregationNode ) node , idAllocator ) ) ; } if ( ! ( node instanceof ExchangeNode ) && ! ( node instanceof ProjectNode ) ) { return Optional . empty ( ) ; } ImmutableList . Builder < PlanNode > builder = ImmutableList . builder ( ) ; for ( PlanNode source : node . getSources ( ) ) { Optional < PlanNode > planNode = recurseToPartial ( lookup . resolve ( source ) , lookup , idAllocator ) ; if ( ! planNode . isPresent ( ) ) { return Optional . empty ( ) ; } builder . add ( planNode . get ( ) ) ; } return Optional . of ( node . replaceChildren ( builder . build ( ) ) ) ; }
Recurse through a series of preceding ExchangeNodes and ProjectNodes to find the preceding PARTIAL aggregation