void (*readtup) (Tuplesortstate *state, SortTuple *stup,
LogicalTape *tape, unsigned int len);
+ /*
+ * Whether SortTuple's datum1 and isnull1 members are maintained by the
+ * above routines. If not, some sort specializations are disabled.
+ */
+ bool haveDatum1;
+
/*
* This array holds the tuples now in sort memory. If we are in state
* INITIAL, the tuples are in no particular order; if we are in state
state->copytup = copytup_heap;
state->writetup = writetup_heap;
state->readtup = readtup_heap;
+ state->haveDatum1 = true;
state->tupDesc = tupDesc; /* assume we need not copy tupDesc */
state->abbrevNext = 10;
state->indexInfo = BuildIndexInfo(indexRel);
+ /*
+ * If we don't have a simple leading attribute, we don't currently
+ * initialize datum1, so disable optimizations that require it.
+ */
+ if (state->indexInfo->ii_IndexAttrNumbers[0] == 0)
+ state->haveDatum1 = false;
+ else
+ state->haveDatum1 = true;
+
state->tupDesc = tupDesc; /* assume we need not copy tupDesc */
indexScanKey = _bt_mkscankey(indexRel, NULL);
state->writetup = writetup_index;
state->readtup = readtup_index;
state->abbrevNext = 10;
+ state->haveDatum1 = true;
state->heapRel = heapRel;
state->indexRel = indexRel;
state->copytup = copytup_index;
state->writetup = writetup_index;
state->readtup = readtup_index;
+ state->haveDatum1 = true;
state->heapRel = heapRel;
state->indexRel = indexRel;
state->copytup = copytup_index;
state->writetup = writetup_index;
state->readtup = readtup_index;
+ state->haveDatum1 = true;
state->heapRel = heapRel;
state->indexRel = indexRel;
state->writetup = writetup_datum;
state->readtup = readtup_datum;
state->abbrevNext = 10;
+ state->haveDatum1 = true;
state->datumType = datumType;
if (state->memtupcount > 1)
{
- /* Do we have a specialization for the leading column's comparator? */
- if (state->sortKeys &&
- state->sortKeys[0].comparator == ssup_datum_unsigned_cmp)
- {
- elog(DEBUG1, "qsort_tuple_unsigned");
- qsort_tuple_unsigned(state->memtuples, state->memtupcount, state);
- }
- else if (state->sortKeys &&
- state->sortKeys[0].comparator == ssup_datum_signed_cmp)
- {
- elog(DEBUG1, "qsort_tuple_signed");
- qsort_tuple_signed(state->memtuples, state->memtupcount, state);
- }
- else if (state->sortKeys &&
- state->sortKeys[0].comparator == ssup_datum_int32_cmp)
+ /*
+ * Do we have the leading column's value or abbreviation in datum1,
+ * and is there a specialization for its comparator?
+ */
+ if (state->haveDatum1 && state->sortKeys)
{
- elog(DEBUG1, "qsort_tuple_int32");
- qsort_tuple_int32(state->memtuples, state->memtupcount, state);
+ if (state->sortKeys[0].comparator == ssup_datum_unsigned_cmp)
+ {
+ elog(DEBUG1, "qsort_tuple_unsigned");
+ qsort_tuple_unsigned(state->memtuples,
+ state->memtupcount,
+ state);
+ return;
+ }
+ else if (state->sortKeys[0].comparator == ssup_datum_signed_cmp)
+ {
+ elog(DEBUG1, "qsort_tuple_signed");
+ qsort_tuple_signed(state->memtuples,
+ state->memtupcount,
+ state);
+ return;
+ }
+ else if (state->sortKeys[0].comparator == ssup_datum_int32_cmp)
+ {
+ elog(DEBUG1, "qsort_tuple_int32");
+ qsort_tuple_int32(state->memtuples,
+ state->memtupcount,
+ state);
+ return;
+ }
}
+
/* Can we use the single-key sort function? */
- else if (state->onlyKey != NULL)
+ if (state->onlyKey != NULL)
{
elog(DEBUG1, "qsort_ssup");
qsort_ssup(state->memtuples, state->memtupcount,
datum2;
bool isnull1,
isnull2;
- AttrNumber leading = state->indexInfo->ii_IndexAttrNumbers[0];
/* Be prepared to compare additional sort keys */
ltup = (HeapTuple) a->tuple;
tupDesc = state->tupDesc;
/* Compare the leading sort key, if it's simple */
- if (leading != 0)
+ if (state->haveDatum1)
{
compare = ApplySortComparator(a->datum1, a->isnull1,
b->datum1, b->isnull1,
if (sortKey->abbrev_converter)
{
+ AttrNumber leading = state->indexInfo->ii_IndexAttrNumbers[0];
+
datum1 = heap_getattr(ltup, leading, tupDesc, &isnull1);
datum2 = heap_getattr(rtup, leading, tupDesc, &isnull2);
* set up first-column key value, and potentially abbreviate, if it's a
* simple column
*/
- if (state->indexInfo->ii_IndexAttrNumbers[0] == 0)
+ if (!state->haveDatum1)
return;
original = heap_getattr(tuple,
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen));
stup->tuple = (void *) tuple;
/* set up first-column key value, if it's a simple column */
- if (state->indexInfo->ii_IndexAttrNumbers[0] != 0)
+ if (state->haveDatum1)
stup->datum1 = heap_getattr(tuple,
state->indexInfo->ii_IndexAttrNumbers[0],
state->tupDesc,