@@ -288,9 +288,7 @@ switchToPresortedPrefixMode(PlanState *pstate)
288288{
289289 IncrementalSortState * node = castNode (IncrementalSortState , pstate );
290290 ScanDirection dir ;
291- int64 nTuples = 0 ;
292- bool lastTuple = false;
293- bool firstTuple = true;
291+ int64 nTuples ;
294292 TupleDesc tupDesc ;
295293 PlanState * outerNode ;
296294 IncrementalSort * plannode = castNode (IncrementalSort , node -> ss .ps .plan );
@@ -343,20 +341,16 @@ switchToPresortedPrefixMode(PlanState *pstate)
343341 * Copy as many tuples as we can (i.e., in the same prefix key group) from
344342 * the full sort state to the prefix sort state.
345343 */
346- for (;; )
344+ for (nTuples = 0 ; nTuples < node -> n_fullsort_remaining ; nTuples ++ )
347345 {
348- lastTuple = node -> n_fullsort_remaining - nTuples == 1 ;
349-
350346 /*
351347 * When we encounter multiple prefix key groups inside the full sort
352348 * tuplesort we have to carry over the last read tuple into the next
353349 * batch.
354350 */
355- if (firstTuple && !TupIsNull (node -> transfer_tuple ))
351+ if (nTuples == 0 && !TupIsNull (node -> transfer_tuple ))
356352 {
357353 tuplesort_puttupleslot (node -> prefixsort_state , node -> transfer_tuple );
358- nTuples ++ ;
359-
360354 /* The carried over tuple is our new group pivot tuple. */
361355 ExecCopySlot (node -> group_pivot , node -> transfer_tuple );
362356 }
@@ -376,7 +370,6 @@ switchToPresortedPrefixMode(PlanState *pstate)
376370 if (isCurrentGroup (node , node -> group_pivot , node -> transfer_tuple ))
377371 {
378372 tuplesort_puttupleslot (node -> prefixsort_state , node -> transfer_tuple );
379- nTuples ++ ;
380373 }
381374 else
382375 {
@@ -395,27 +388,10 @@ switchToPresortedPrefixMode(PlanState *pstate)
395388 */
396389 ExecClearTuple (node -> group_pivot );
397390
398- /*
399- * Also make sure we take the didn't-consume-all-the-tuples
400- * path below, even if this happened to be the last tuple of
401- * the batch.
402- */
403- lastTuple = false;
391+ /* Break out of for-loop early */
404392 break ;
405393 }
406394 }
407-
408- firstTuple = false;
409-
410- /*
411- * If we've copied all of the tuples from the full sort state into the
412- * prefix sort state, then we don't actually know that we've yet found
413- * the last tuple in that prefix key group until we check the next
414- * tuple from the outer plan node, so we retain the current group
415- * pivot tuple prefix key group comparison.
416- */
417- if (lastTuple )
418- break ;
419395 }
420396
421397 /*
@@ -428,14 +404,15 @@ switchToPresortedPrefixMode(PlanState *pstate)
428404 node -> n_fullsort_remaining -= nTuples ;
429405 SO1_printf ("Setting n_fullsort_remaining to " INT64_FORMAT "\n" , node -> n_fullsort_remaining );
430406
431- if (lastTuple )
407+ if (node -> n_fullsort_remaining == 0 )
432408 {
433409 /*
434- * We've confirmed that all tuples remaining in the full sort batch is
435- * in the same prefix key group and moved all of those tuples into the
436- * presorted prefix tuplesort. Now we can save our pivot comparison
437- * tuple and continue fetching tuples from the outer execution node to
438- * load into the presorted prefix tuplesort.
410+ * We've found that all tuples remaining in the full sort batch are in
411+ * the same prefix key group and moved all of those tuples into the
412+ * presorted prefix tuplesort. We don't know that we've yet found the
413+ * last tuple in the current prefix key group, so save our pivot
414+ * comparison tuple and continue fetching tuples from the outer
415+ * execution node to load into the presorted prefix tuplesort.
439416 */
440417 ExecCopySlot (node -> group_pivot , node -> transfer_tuple );
441418 SO_printf ("Setting execution_status to INCSORT_LOADPREFIXSORT (switchToPresortedPrefixMode)\n" );
@@ -1104,7 +1081,7 @@ ExecEndIncrementalSort(IncrementalSortState *node)
11041081 ExecClearTuple (node -> ss .ss_ScanTupleSlot );
11051082 /* must drop pointer to sort result tuple */
11061083 ExecClearTuple (node -> ss .ps .ps_ResultTupleSlot );
1107- /* must drop stanalone tuple slots from outer node */
1084+ /* must drop standalone tuple slots from outer node */
11081085 ExecDropSingleTupleTableSlot (node -> group_pivot );
11091086 ExecDropSingleTupleTableSlot (node -> transfer_tuple );
11101087
0 commit comments