onlyKey
authorRobert Haas <rhaas@postgresql.org>
Tue, 14 Feb 2012 21:40:18 +0000 (16:40 -0500)
committerRobert Haas <rhaas@postgresql.org>
Tue, 14 Feb 2012 21:40:18 +0000 (16:40 -0500)
src/backend/utils/sort/tuplesort.c

index 6c7fa062aef72132a8414646c1b1dd8269ef0d8c..10b19c0b21f526effe6d2cad73b25f90046f9cc2 100644 (file)
@@ -365,12 +365,14 @@ struct Tuplesortstate
        /* These are specific to the index_hash subcase: */
        uint32          hash_mask;              /* mask for sortable part of hash code */
 
+       /* This is initialized when, and only when, there's just one key. */
+       SortSupport     onlyKey;
+
        /*
         * These variables are specific to the Datum case; they are set by
         * tuplesort_begin_datum and used only by the DatumTuple routines.
         */
        Oid                     datumType;
-       SortSupport     datumKey;
        /* we need typelen and byval in order to know how to copy the Datums. */
        int                     datumTypeLen;
        bool            datumTypeByVal;
@@ -638,6 +640,9 @@ tuplesort_begin_heap(TupleDesc tupDesc,
                PrepareSortSupportFromOrderingOp(sortOperators[i], sortKey);
        }
 
+       if (nkeys == 1)
+               state->onlyKey = state->sortKeys;
+
        MemoryContextSwitchTo(oldcontext);
 
        return state;
@@ -816,13 +821,13 @@ tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation,
        state->datumType = datumType;
 
        /* Prepare SortSupport data */
-       state->datumKey = (SortSupport) palloc0(sizeof(SortSupportData));
+       state->onlyKey = (SortSupport) palloc0(sizeof(SortSupportData));
 
-       state->datumKey->ssup_cxt = CurrentMemoryContext;
-       state->datumKey->ssup_collation = sortCollation;
-       state->datumKey->ssup_nulls_first = nullsFirstFlag;
+       state->onlyKey->ssup_cxt = CurrentMemoryContext;
+       state->onlyKey->ssup_collation = sortCollation;
+       state->onlyKey->ssup_nulls_first = nullsFirstFlag;
 
-       PrepareSortSupportFromOrderingOp(sortOperator, state->datumKey);
+       PrepareSortSupportFromOrderingOp(sortOperator, state->onlyKey);
 
        /* lookup necessary attributes of the datum type */
        get_typlenbyval(datumType, &typlen, &typbyval);
@@ -1230,9 +1235,9 @@ tuplesort_performsort(Tuplesortstate *state)
                         */
                        if (state->memtupcount > 1)
                        {
-                               if (state->comparetup == comparetup_heap && state->nKeys == 1)
+                               if (state->onlyKey != NULL)
                                        qsort_ssup(state->memtuples, state->memtupcount,
-                                                          state->sortKeys);
+                                                          state->onlyKey);
                                else
                                        qsort_tuple(state->memtuples,
                                                                state->memtupcount,
@@ -3231,9 +3236,9 @@ reversedirection_index_hash(Tuplesortstate *state)
 static int
 comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
 {
-       return ApplySortComparator(a->datum1, a->isnull1,
-                                                          b->datum1, b->isnull1,
-                                                          state->datumKey);
+       /* Not currently needed */
+       elog(ERROR, "comparetup_datum() should not be called");
+       return 0;
 }
 
 static void
@@ -3325,8 +3330,8 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup,
 static void
 reversedirection_datum(Tuplesortstate *state)
 {
-       state->datumKey->ssup_reverse = !state->datumKey->ssup_reverse;
-       state->datumKey->ssup_nulls_first = !state->datumKey->ssup_nulls_first;
+       state->onlyKey->ssup_reverse = !state->onlyKey->ssup_reverse;
+       state->onlyKey->ssup_nulls_first = !state->onlyKey->ssup_nulls_first;
 }
 
 /*