@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_cash_consistent);
25
25
PG_FUNCTION_INFO_V1 (gbt_cash_distance );
26
26
PG_FUNCTION_INFO_V1 (gbt_cash_penalty );
27
27
PG_FUNCTION_INFO_V1 (gbt_cash_same );
28
+ PG_FUNCTION_INFO_V1 (gbt_cash_sortsupport );
28
29
29
30
static bool
30
31
gbt_cashgt (const void * a , const void * b , FmgrInfo * flinfo )
@@ -216,3 +217,82 @@ gbt_cash_same(PG_FUNCTION_ARGS)
216
217
* result = gbt_num_same ((void * ) b1 , (void * ) b2 , & tinfo , fcinfo -> flinfo );
217
218
PG_RETURN_POINTER (result );
218
219
}
220
+
221
+ static int
222
+ gbt_cash_sort_build_cmp (Datum a , Datum b , SortSupport ssup )
223
+ {
224
+ cashKEY * ia = (cashKEY * ) DatumGetPointer (a );
225
+ cashKEY * ib = (cashKEY * ) DatumGetPointer (b );
226
+
227
+ /* for leaf items we expect lower == upper */
228
+ Assert (ia -> lower == ia -> upper );
229
+ Assert (ib -> lower == ib -> upper );
230
+
231
+ if (ia -> lower == ib -> lower )
232
+ return 0 ;
233
+
234
+ return (ia -> lower > ib -> lower ) ? 1 : -1 ;
235
+ }
236
+
237
+ static Datum
238
+ gbt_cash_abbrev_convert (Datum original , SortSupport ssup )
239
+ {
240
+ cashKEY * b1 = (cashKEY * ) DatumGetPointer (original );
241
+ int64 z = b1 -> lower ;
242
+
243
+ #if SIZEOF_DATUM == 8
244
+ return Int64GetDatum (z );
245
+ #else
246
+ return Int32GetDatum (z >> 32 );
247
+ #endif
248
+ }
249
+
250
+ static int
251
+ gbt_cash_cmp_abbrev (Datum z1 , Datum z2 , SortSupport ssup )
252
+ {
253
+ #if SIZEOF_DATUM == 8
254
+ int64 a = DatumGetInt64 (z1 );
255
+ int64 b = DatumGetInt64 (z2 );
256
+ #else
257
+ int32 a = DatumGetInt32 (z1 );
258
+ int32 b = DatumGetInt32 (z2 );
259
+ #endif
260
+
261
+ if (a > b )
262
+ return 1 ;
263
+ else if (a < b )
264
+ return -1 ;
265
+ else
266
+ return 0 ;
267
+ }
268
+
269
+ /*
270
+ * We never consider aborting the abbreviation.
271
+ */
272
+ static bool
273
+ gbt_cash_abbrev_abort (int memtupcount , SortSupport ssup )
274
+ {
275
+ return false;
276
+ }
277
+
278
+ /*
279
+ * Sort support routine for fast GiST index build by sorting.
280
+ */
281
+ Datum
282
+ gbt_cash_sortsupport (PG_FUNCTION_ARGS )
283
+ {
284
+ SortSupport ssup = (SortSupport ) PG_GETARG_POINTER (0 );
285
+
286
+ if (ssup -> abbreviate )
287
+ {
288
+ ssup -> comparator = gbt_cash_cmp_abbrev ;
289
+ ssup -> abbrev_converter = gbt_cash_abbrev_convert ;
290
+ ssup -> abbrev_abort = gbt_cash_abbrev_abort ;
291
+ ssup -> abbrev_full_comparator = gbt_cash_sort_build_cmp ;
292
+ }
293
+ else
294
+ {
295
+ ssup -> comparator = gbt_cash_sort_build_cmp ;
296
+ }
297
+ PG_RETURN_VOID ();
298
+ }
0 commit comments