@@ -288,9 +288,7 @@ switchToPresortedPrefixMode(PlanState *pstate)
288
288
{
289
289
IncrementalSortState * node = castNode (IncrementalSortState , pstate );
290
290
ScanDirection dir ;
291
- int64 nTuples = 0 ;
292
- bool lastTuple = false;
293
- bool firstTuple = true;
291
+ int64 nTuples ;
294
292
TupleDesc tupDesc ;
295
293
PlanState * outerNode ;
296
294
IncrementalSort * plannode = castNode (IncrementalSort , node -> ss .ps .plan );
@@ -343,20 +341,16 @@ switchToPresortedPrefixMode(PlanState *pstate)
343
341
* Copy as many tuples as we can (i.e., in the same prefix key group) from
344
342
* the full sort state to the prefix sort state.
345
343
*/
346
- for (;; )
344
+ for (nTuples = 0 ; nTuples < node -> n_fullsort_remaining ; nTuples ++ )
347
345
{
348
- lastTuple = node -> n_fullsort_remaining - nTuples == 1 ;
349
-
350
346
/*
351
347
* When we encounter multiple prefix key groups inside the full sort
352
348
* tuplesort we have to carry over the last read tuple into the next
353
349
* batch.
354
350
*/
355
- if (firstTuple && !TupIsNull (node -> transfer_tuple ))
351
+ if (nTuples == 0 && !TupIsNull (node -> transfer_tuple ))
356
352
{
357
353
tuplesort_puttupleslot (node -> prefixsort_state , node -> transfer_tuple );
358
- nTuples ++ ;
359
-
360
354
/* The carried over tuple is our new group pivot tuple. */
361
355
ExecCopySlot (node -> group_pivot , node -> transfer_tuple );
362
356
}
@@ -376,7 +370,6 @@ switchToPresortedPrefixMode(PlanState *pstate)
376
370
if (isCurrentGroup (node , node -> group_pivot , node -> transfer_tuple ))
377
371
{
378
372
tuplesort_puttupleslot (node -> prefixsort_state , node -> transfer_tuple );
379
- nTuples ++ ;
380
373
}
381
374
else
382
375
{
@@ -395,27 +388,10 @@ switchToPresortedPrefixMode(PlanState *pstate)
395
388
*/
396
389
ExecClearTuple (node -> group_pivot );
397
390
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 */
404
392
break ;
405
393
}
406
394
}
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 ;
419
395
}
420
396
421
397
/*
@@ -428,14 +404,15 @@ switchToPresortedPrefixMode(PlanState *pstate)
428
404
node -> n_fullsort_remaining -= nTuples ;
429
405
SO1_printf ("Setting n_fullsort_remaining to " INT64_FORMAT "\n" , node -> n_fullsort_remaining );
430
406
431
- if (lastTuple )
407
+ if (node -> n_fullsort_remaining == 0 )
432
408
{
433
409
/*
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.
439
416
*/
440
417
ExecCopySlot (node -> group_pivot , node -> transfer_tuple );
441
418
SO_printf ("Setting execution_status to INCSORT_LOADPREFIXSORT (switchToPresortedPrefixMode)\n" );
@@ -1104,7 +1081,7 @@ ExecEndIncrementalSort(IncrementalSortState *node)
1104
1081
ExecClearTuple (node -> ss .ss_ScanTupleSlot );
1105
1082
/* must drop pointer to sort result tuple */
1106
1083
ExecClearTuple (node -> ss .ps .ps_ResultTupleSlot );
1107
- /* must drop stanalone tuple slots from outer node */
1084
+ /* must drop standalone tuple slots from outer node */
1108
1085
ExecDropSingleTupleTableSlot (node -> group_pivot );
1109
1086
ExecDropSingleTupleTableSlot (node -> transfer_tuple );
1110
1087
0 commit comments