Skip to content

Commit e33dac2

Browse files
committed
prepare to alternative ordering. No-op yet
1 parent 993994b commit e33dac2

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

rum.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ typedef struct RumOptions
287287
{
288288
int32 vl_len_; /* varlena header (do not touch directly!) */
289289
bool useFastUpdate; /* use fast updates? */
290+
bool useAlternativeOrder;
290291
int orderByColumn;
291292
int addToColumn;
292293
} RumOptions;
@@ -302,6 +303,11 @@ typedef struct RumOptions
302303
#define RUM_SHARE BUFFER_LOCK_SHARE
303304
#define RUM_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE
304305

306+
typedef struct RumKey {
307+
ItemPointerData ipd;
308+
bool isNull;
309+
Datum addToCompare;
310+
} RumKey;
305311

306312
/*
307313
* RumState: working data structure describing the index being worked on
@@ -310,6 +316,7 @@ typedef struct RumState
310316
{
311317
Relation index;
312318
bool oneCol; /* true if single-column index */
319+
bool useAlternativeOrder;
313320
AttrNumber attrnOrderByColumn;
314321
AttrNumber attrnAddToColumn;
315322

@@ -486,6 +493,7 @@ extern void checkLeafDataPage(RumState *rumstate, AttrNumber attrnum, Page page)
486493

487494
/* rumdatapage.c */
488495
extern int rumCompareItemPointers(ItemPointer a, ItemPointer b);
496+
extern int compareRumKey(RumState *state, RumKey *a, RumKey *b);
489497
extern char *rumDataPageLeafWriteItemPointer(char *ptr, ItemPointer iptr, ItemPointer prev, bool addInfoIsNull);
490498
extern Pointer rumPlaceToDataPageLeaf(Pointer ptr, OffsetNumber attnum,
491499
ItemPointer iptr, Datum addInfo, bool addInfoIsNull, ItemPointer prev,

rumdatapage.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,42 @@ rumCompareItemPointers(ItemPointer a, ItemPointer b)
267267
return (ba > bb) ? 1 : -1;
268268
}
269269

270+
int
271+
compareRumKey(RumState *state, RumKey *a, RumKey *b)
272+
{
273+
274+
/* assume NULL is greate than any real value */
275+
if (state->useAlternativeOrder)
276+
{
277+
if (a->isNull == false && b->isNull == false)
278+
{
279+
int res;
280+
AttrNumber attnum = state->attrnOrderByColumn;
281+
282+
res = DatumGetInt32(FunctionCall2Coll(
283+
&state->compareFn[attnum - 1],
284+
state->supportCollation[attnum - 1],
285+
a->addToCompare, b->addToCompare));
286+
if (res != 0)
287+
return res;
288+
/* fallback to ItemPointerCompare */
289+
}
290+
else if (a->isNull == true)
291+
{
292+
if (b->isNull == false)
293+
return 1;
294+
/* fallback to ItemPointerCompare */
295+
}
296+
else
297+
{
298+
Assert(b->isNull == true);
299+
return -1;
300+
}
301+
}
302+
303+
return rumCompareItemPointers(&a->ipd, &b->ipd);
304+
}
305+
270306
/*
271307
* Merge two ordered arrays of itempointers, eliminating any duplicates.
272308
* Returns the number of items in the result.

rumutil.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ _PG_init(void)
5555
add_string_reloption(rum_relopt_kind, "addto",
5656
"Column name to add a order by column",
5757
NULL, NULL);
58+
add_bool_reloption(rum_relopt_kind, "use_alternative_order",
59+
"Use (addinfo, itempointer) order instead of just itempointer",
60+
false);
5861
}
5962

6063
/*
@@ -158,7 +161,16 @@ initRumState(RumState *state, Relation index)
158161

159162
if (!(AttributeNumberIsValid(state->attrnOrderByColumn) &&
160163
AttributeNumberIsValid(state->attrnAddToColumn)))
161-
elog(ERROR, "AddTo and OrderBy colums should be defined both");
164+
elog(ERROR, "AddTo and OrderBy columns should be defined both");
165+
166+
if (options->useAlternativeOrder)
167+
{
168+
if (!(AttributeNumberIsValid(state->attrnOrderByColumn) &&
169+
AttributeNumberIsValid(state->attrnAddToColumn)))
170+
elog(ERROR, "to use alternative ordering AddTo and OrderBy should be defined");
171+
172+
state->useAlternativeOrder = true;
173+
}
162174
}
163175

164176
for (i = 0; i < origTupdesc->natts; i++)
@@ -766,7 +778,8 @@ rumoptions(Datum reloptions, bool validate)
766778
static const relopt_parse_elt tab[] = {
767779
{"fastupdate", RELOPT_TYPE_BOOL, offsetof(RumOptions, useFastUpdate)},
768780
{"orderby", RELOPT_TYPE_STRING, offsetof(RumOptions, orderByColumn)},
769-
{"addto", RELOPT_TYPE_STRING, offsetof(RumOptions, addToColumn)}
781+
{"addto", RELOPT_TYPE_STRING, offsetof(RumOptions, addToColumn)},
782+
{"use_alternative_order", RELOPT_TYPE_BOOL, offsetof(RumOptions, useAlternativeOrder)}
770783
};
771784

772785
options = parseRelOptions(reloptions, validate, rum_relopt_kind,

0 commit comments

Comments
 (0)