Skip to content

Commit cadfdd1

Browse files
committed
Add new Tuplesortstate.removeabbrev function
This commit is the preparation to move abbreviation logic into puttuple_common(). The new removeabbrev function turns datum1 representation of SortTuple's from the abbreviated key to the first column value. Therefore, it encapsulates the differential part of abbreviation handling code in tuplesort_put*() functions, making these functions similar. Discussion: https://postgr.es/m/CAPpHfdvjix0Ahx-H3Jp1M2R%2B_74P-zKnGGygx4OWr%3DbUQ8BNdw%40mail.gmail.com Author: Alexander Korotkov Reviewed-by: Pavel Borisov, Maxim Orlov, Matthias van de Meent Reviewed-by: Andres Freund, John Naylor
1 parent d47da31 commit cadfdd1

File tree

1 file changed

+89
-48
lines changed

1 file changed

+89
-48
lines changed

src/backend/utils/sort/tuplesort.c

Lines changed: 89 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ struct Tuplesortstate
279279
*/
280280
SortTupleComparator comparetup;
281281

282+
/*
283+
* Alter datum1 representation in the SortTuple's array back from the
284+
* abbreviated key to the first column value.
285+
*/
286+
void (*removeabbrev) (Tuplesortstate *state, SortTuple *stups,
287+
int count);
288+
282289
/*
283290
* Function to write a stored tuple onto tape. The representation of the
284291
* tuple on tape need not be the same as it is in memory; requirements on
@@ -540,6 +547,7 @@ struct Sharedsort
540547
pfree(buf); \
541548
} while(0)
542549

550+
#define REMOVEABBREV(state,stup,count) ((*(state)->removeabbrev) (state, stup, count))
543551
#define COMPARETUP(state,a,b) ((*(state)->comparetup) (a, b, state))
544552
#define WRITETUP(state,tape,stup) ((*(state)->writetup) (state, tape, stup))
545553
#define READTUP(state,stup,tape,len) ((*(state)->readtup) (state, stup, tape, len))
@@ -629,6 +637,14 @@ static void reversedirection(Tuplesortstate *state);
629637
static unsigned int getlen(LogicalTape *tape, bool eofOK);
630638
static void markrunend(LogicalTape *tape);
631639
static void *readtup_alloc(Tuplesortstate *state, Size tuplen);
640+
static void removeabbrev_heap(Tuplesortstate *state, SortTuple *stups,
641+
int count);
642+
static void removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups,
643+
int count);
644+
static void removeabbrev_index(Tuplesortstate *state, SortTuple *stups,
645+
int count);
646+
static void removeabbrev_datum(Tuplesortstate *state, SortTuple *stups,
647+
int count);
632648
static int comparetup_heap(const SortTuple *a, const SortTuple *b,
633649
Tuplesortstate *state);
634650
static void writetup_heap(Tuplesortstate *state, LogicalTape *tape,
@@ -1042,6 +1058,7 @@ tuplesort_begin_heap(TupleDesc tupDesc,
10421058
sortopt & TUPLESORT_RANDOMACCESS,
10431059
PARALLEL_SORT(state));
10441060

1061+
state->removeabbrev = removeabbrev_heap;
10451062
state->comparetup = comparetup_heap;
10461063
state->writetup = writetup_heap;
10471064
state->readtup = readtup_heap;
@@ -1117,6 +1134,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc,
11171134
sortopt & TUPLESORT_RANDOMACCESS,
11181135
PARALLEL_SORT(state));
11191136

1137+
state->removeabbrev = removeabbrev_cluster;
11201138
state->comparetup = comparetup_cluster;
11211139
state->writetup = writetup_cluster;
11221140
state->readtup = readtup_cluster;
@@ -1221,6 +1239,7 @@ tuplesort_begin_index_btree(Relation heapRel,
12211239
sortopt & TUPLESORT_RANDOMACCESS,
12221240
PARALLEL_SORT(state));
12231241

1242+
state->removeabbrev = removeabbrev_index;
12241243
state->comparetup = comparetup_index_btree;
12251244
state->writetup = writetup_index;
12261245
state->readtup = readtup_index;
@@ -1297,6 +1316,7 @@ tuplesort_begin_index_hash(Relation heapRel,
12971316

12981317
state->nKeys = 1; /* Only one sort column, the hash code */
12991318

1319+
state->removeabbrev = removeabbrev_index;
13001320
state->comparetup = comparetup_index_hash;
13011321
state->writetup = writetup_index;
13021322
state->readtup = readtup_index;
@@ -1337,6 +1357,7 @@ tuplesort_begin_index_gist(Relation heapRel,
13371357

13381358
state->nKeys = IndexRelationGetNumberOfKeyAttributes(indexRel);
13391359

1360+
state->removeabbrev = removeabbrev_index;
13401361
state->comparetup = comparetup_index_btree;
13411362
state->writetup = writetup_index;
13421363
state->readtup = readtup_index;
@@ -1400,6 +1421,7 @@ tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation,
14001421
sortopt & TUPLESORT_RANDOMACCESS,
14011422
PARALLEL_SORT(state));
14021423

1424+
state->removeabbrev = removeabbrev_datum;
14031425
state->comparetup = comparetup_datum;
14041426
state->writetup = writetup_datum;
14051427
state->readtup = readtup_datum;
@@ -1858,8 +1880,6 @@ tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
18581880
else
18591881
{
18601882
/* Abort abbreviation */
1861-
int i;
1862-
18631883
stup.datum1 = original;
18641884

18651885
/*
@@ -1871,20 +1891,7 @@ tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
18711891
* sorted on tape, since serialized tuples lack abbreviated keys
18721892
* (TSS_BUILDRUNS state prevents control reaching here in any case).
18731893
*/
1874-
for (i = 0; i < state->memtupcount; i++)
1875-
{
1876-
SortTuple *mtup = &state->memtuples[i];
1877-
1878-
htup.t_len = ((MinimalTuple) mtup->tuple)->t_len +
1879-
MINIMAL_TUPLE_OFFSET;
1880-
htup.t_data = (HeapTupleHeader) ((char *) mtup->tuple -
1881-
MINIMAL_TUPLE_OFFSET);
1882-
1883-
mtup->datum1 = heap_getattr(&htup,
1884-
state->sortKeys[0].ssup_attno,
1885-
state->tupDesc,
1886-
&mtup->isnull1);
1887-
}
1894+
REMOVEABBREV(state, state->memtuples, state->memtupcount);
18881895
}
18891896

18901897
puttuple_common(state, &stup);
@@ -1943,8 +1950,6 @@ tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
19431950
else
19441951
{
19451952
/* Abort abbreviation */
1946-
int i;
1947-
19481953
stup.datum1 = original;
19491954

19501955
/*
@@ -1957,16 +1962,7 @@ tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
19571962
* (TSS_BUILDRUNS state prevents control reaching here in any
19581963
* case).
19591964
*/
1960-
for (i = 0; i < state->memtupcount; i++)
1961-
{
1962-
SortTuple *mtup = &state->memtuples[i];
1963-
1964-
tup = (HeapTuple) mtup->tuple;
1965-
mtup->datum1 = heap_getattr(tup,
1966-
state->indexInfo->ii_IndexAttrNumbers[0],
1967-
state->tupDesc,
1968-
&mtup->isnull1);
1969-
}
1965+
REMOVEABBREV(state, state->memtuples, state->memtupcount);
19701966
}
19711967
}
19721968

@@ -2023,8 +2019,6 @@ tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel,
20232019
else
20242020
{
20252021
/* Abort abbreviation */
2026-
int i;
2027-
20282022
stup.datum1 = original;
20292023

20302024
/*
@@ -2036,16 +2030,7 @@ tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel,
20362030
* sorted on tape, since serialized tuples lack abbreviated keys
20372031
* (TSS_BUILDRUNS state prevents control reaching here in any case).
20382032
*/
2039-
for (i = 0; i < state->memtupcount; i++)
2040-
{
2041-
SortTuple *mtup = &state->memtuples[i];
2042-
2043-
tuple = mtup->tuple;
2044-
mtup->datum1 = index_getattr(tuple,
2045-
1,
2046-
RelationGetDescr(state->indexRel),
2047-
&mtup->isnull1);
2048-
}
2033+
REMOVEABBREV(state, state->memtuples, state->memtupcount);
20492034
}
20502035

20512036
puttuple_common(state, &stup);
@@ -2109,8 +2094,6 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
21092094
else
21102095
{
21112096
/* Abort abbreviation */
2112-
int i;
2113-
21142097
stup.datum1 = original;
21152098

21162099
/*
@@ -2123,12 +2106,7 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
21232106
* (TSS_BUILDRUNS state prevents control reaching here in any
21242107
* case).
21252108
*/
2126-
for (i = 0; i < state->memtupcount; i++)
2127-
{
2128-
SortTuple *mtup = &state->memtuples[i];
2129-
2130-
mtup->datum1 = PointerGetDatum(mtup->tuple);
2131-
}
2109+
REMOVEABBREV(state, state->memtuples, state->memtupcount);
21322110
}
21332111
}
21342112

@@ -3985,6 +3963,26 @@ readtup_alloc(Tuplesortstate *state, Size tuplen)
39853963
* Routines specialized for HeapTuple (actually MinimalTuple) case
39863964
*/
39873965

3966+
static void
3967+
removeabbrev_heap(Tuplesortstate *state, SortTuple *stups, int count)
3968+
{
3969+
int i;
3970+
3971+
for (i = 0; i < count; i++)
3972+
{
3973+
HeapTupleData htup;
3974+
3975+
htup.t_len = ((MinimalTuple) stups[i].tuple)->t_len +
3976+
MINIMAL_TUPLE_OFFSET;
3977+
htup.t_data = (HeapTupleHeader) ((char *) stups[i].tuple -
3978+
MINIMAL_TUPLE_OFFSET);
3979+
stups[i].datum1 = heap_getattr(&htup,
3980+
state->sortKeys[0].ssup_attno,
3981+
state->tupDesc,
3982+
&stups[i].isnull1);
3983+
}
3984+
}
3985+
39883986
static int
39893987
comparetup_heap(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
39903988
{
@@ -4103,6 +4101,23 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup,
41034101
* comparisons per a btree index definition)
41044102
*/
41054103

4104+
static void
4105+
removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups, int count)
4106+
{
4107+
int i;
4108+
4109+
for (i = 0; i < count; i++)
4110+
{
4111+
HeapTuple tup;
4112+
4113+
tup = (HeapTuple) stups[i].tuple;
4114+
stups[i].datum1 = heap_getattr(tup,
4115+
state->indexInfo->ii_IndexAttrNumbers[0],
4116+
state->tupDesc,
4117+
&stups[i].isnull1);
4118+
}
4119+
}
4120+
41064121
static int
41074122
comparetup_cluster(const SortTuple *a, const SortTuple *b,
41084123
Tuplesortstate *state)
@@ -4272,6 +4287,23 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup,
42724287
* functions can be shared.
42734288
*/
42744289

4290+
static void
4291+
removeabbrev_index(Tuplesortstate *state, SortTuple *stups, int count)
4292+
{
4293+
int i;
4294+
4295+
for (i = 0; i < count; i++)
4296+
{
4297+
IndexTuple tuple;
4298+
4299+
tuple = stups[i].tuple;
4300+
stups[i].datum1 = index_getattr(tuple,
4301+
1,
4302+
RelationGetDescr(state->indexRel),
4303+
&stups[i].isnull1);
4304+
}
4305+
}
4306+
42754307
static int
42764308
comparetup_index_btree(const SortTuple *a, const SortTuple *b,
42774309
Tuplesortstate *state)
@@ -4504,6 +4536,15 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
45044536
* Routines specialized for DatumTuple case
45054537
*/
45064538

4539+
static void
4540+
removeabbrev_datum(Tuplesortstate *state, SortTuple *stups, int count)
4541+
{
4542+
int i;
4543+
4544+
for (i = 0; i < count; i++)
4545+
stups[i].datum1 = PointerGetDatum(stups[i].tuple);
4546+
}
4547+
45074548
static int
45084549
comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
45094550
{

0 commit comments

Comments
 (0)