* Switch to phase "newphase", which must either be 0 or 1 (to reset) or * current_phase + 1. Juggle the tuplesorts accordingly. * * Phase 0 is for hashing, which we currently handle last in the AGG_MIXED * case, so when entering phase 0, all we need to do is drop open sorts. */
| 511 | * case, so when entering phase 0, all we need to do is drop open sorts. |
| 512 | */ |
| 513 | static void |
| 514 | initialize_phase(AggState *aggstate, int newphase) |
| 515 | { |
| 516 | Assert(newphase <= 1 || newphase == aggstate->current_phase + 1); |
| 517 | |
| 518 | /* |
| 519 | * Whatever the previous state, we're now done with whatever input |
| 520 | * tuplesort was in use. |
| 521 | */ |
| 522 | if (aggstate->sort_in) |
| 523 | { |
| 524 | tuplesort_end(aggstate->sort_in); |
| 525 | aggstate->sort_in = NULL; |
| 526 | } |
| 527 | |
| 528 | if (newphase <= 1) |
| 529 | { |
| 530 | /* |
| 531 | * Discard any existing output tuplesort. |
| 532 | */ |
| 533 | if (aggstate->sort_out) |
| 534 | { |
| 535 | tuplesort_end(aggstate->sort_out); |
| 536 | aggstate->sort_out = NULL; |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | /* |
| 542 | * The old output tuplesort becomes the new input one, and this is the |
| 543 | * right time to actually sort it. |
| 544 | */ |
| 545 | aggstate->sort_in = aggstate->sort_out; |
| 546 | aggstate->sort_out = NULL; |
| 547 | Assert(aggstate->sort_in); |
| 548 | tuplesort_performsort(aggstate->sort_in); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * If this isn't the last phase, we need to sort appropriately for the |
| 553 | * next phase in sequence. |
| 554 | */ |
| 555 | if (newphase > 0 && newphase < aggstate->numphases - 1) |
| 556 | { |
| 557 | Sort *sortnode = aggstate->phases[newphase + 1].sortnode; |
| 558 | PlanState *outerNode = outerPlanState(aggstate); |
| 559 | TupleDesc tupDesc = ExecGetResultType(outerNode); |
| 560 | |
| 561 | aggstate->sort_out = tuplesort_begin_heap(tupDesc, |
| 562 | sortnode->numCols, |
| 563 | sortnode->sortColIdx, |
| 564 | sortnode->sortOperators, |
| 565 | sortnode->collations, |
| 566 | sortnode->nullsFirst, |
| 567 | PlanStateOperatorMemKB((PlanState *) aggstate), |
| 568 | NULL, false); |
| 569 | } |
| 570 |
no test coverage detected