1 /*-------------------------------------------------------------------------
4 * Comparison functions for btree access method.
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/access/nbtree/nbtcompare.c
15 * These functions are stored in pg_amproc. For each operator class
16 * defined on btrees, they compute
23 * The result is always an int32 regardless of the input datatype.
25 * Although any negative int32 (except INT_MIN) is acceptable for reporting
26 * "<", and any positive int32 is acceptable for reporting ">", routines
27 * that work on 32-bit or wider datatypes can't just return "a - b".
28 * That could overflow and give the wrong answer. Also, one must not
29 * return INT_MIN to report "<", since some callers will negate the result.
31 * NOTE: it is critical that the comparison function impose a total order
32 * on all non-NULL values of the data type, and that the datatype's
33 * boolean comparison operators (= < >= etc) yield results consistent
34 * with the comparison routine. Otherwise bad behavior may ensue.
35 * (For example, the comparison operators must NOT punt when faced with
36 * NAN or other funny values; you must devise some collation sequence for
37 * all such values.) If the datatype is not trivial, this is most
38 * reliably done by having the boolean operators invoke the same
39 * three-way comparison code that the btree function does. Therefore,
40 * this file contains only btree support for "trivial" datatypes ---
41 * all others are in the /utils/adt/ files that implement their datatypes.
43 * NOTE: these routines must not leak memory, since memory allocated
44 * during an index access won't be recovered till end of query. This
45 * primarily affects comparison routines for toastable datatypes;
46 * they have to be careful to free any detoasted copy of an input datum.
47 *-------------------------------------------------------------------------
51 #include "utils/builtins.h"
52 #include "utils/sortsupport.h"
56 btboolcmp(PG_FUNCTION_ARGS)
58 bool a = PG_GETARG_BOOL(0);
59 bool b = PG_GETARG_BOOL(1);
61 PG_RETURN_INT32((int32) a - (int32) b);
65 btint2cmp(PG_FUNCTION_ARGS)
67 int16 a = PG_GETARG_INT16(0);
68 int16 b = PG_GETARG_INT16(1);
70 PG_RETURN_INT32((int32) a - (int32) b);
74 btint2fastcmp(Datum x, Datum y, SortSupport ssup)
76 int16 a = DatumGetInt16(x);
77 int16 b = DatumGetInt16(y);
79 return (int) a - (int) b;
83 btint2sortsupport(PG_FUNCTION_ARGS)
85 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
87 ssup->comparator = btint2fastcmp;
92 btint4cmp(PG_FUNCTION_ARGS)
94 int32 a = PG_GETARG_INT32(0);
95 int32 b = PG_GETARG_INT32(1);
106 btint4fastcmp(Datum x, Datum y, SortSupport ssup)
108 int32 a = DatumGetInt32(x);
109 int32 b = DatumGetInt32(y);
120 btint4sortsupport(PG_FUNCTION_ARGS)
122 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
124 ssup->comparator = btint4fastcmp;
129 btint8cmp(PG_FUNCTION_ARGS)
131 int64 a = PG_GETARG_INT64(0);
132 int64 b = PG_GETARG_INT64(1);
143 btint8fastcmp(Datum x, Datum y, SortSupport ssup)
145 int64 a = DatumGetInt64(x);
146 int64 b = DatumGetInt64(y);
157 btint8sortsupport(PG_FUNCTION_ARGS)
159 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
161 ssup->comparator = btint8fastcmp;
166 btint48cmp(PG_FUNCTION_ARGS)
168 int32 a = PG_GETARG_INT32(0);
169 int64 b = PG_GETARG_INT64(1);
180 btint84cmp(PG_FUNCTION_ARGS)
182 int64 a = PG_GETARG_INT64(0);
183 int32 b = PG_GETARG_INT32(1);
194 btint24cmp(PG_FUNCTION_ARGS)
196 int16 a = PG_GETARG_INT16(0);
197 int32 b = PG_GETARG_INT32(1);
208 btint42cmp(PG_FUNCTION_ARGS)
210 int32 a = PG_GETARG_INT32(0);
211 int16 b = PG_GETARG_INT16(1);
222 btint28cmp(PG_FUNCTION_ARGS)
224 int16 a = PG_GETARG_INT16(0);
225 int64 b = PG_GETARG_INT64(1);
236 btint82cmp(PG_FUNCTION_ARGS)
238 int64 a = PG_GETARG_INT64(0);
239 int16 b = PG_GETARG_INT16(1);
250 btoidcmp(PG_FUNCTION_ARGS)
252 Oid a = PG_GETARG_OID(0);
253 Oid b = PG_GETARG_OID(1);
264 btoidfastcmp(Datum x, Datum y, SortSupport ssup)
266 Oid a = DatumGetObjectId(x);
267 Oid b = DatumGetObjectId(y);
278 btoidsortsupport(PG_FUNCTION_ARGS)
280 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
282 ssup->comparator = btoidfastcmp;
287 btoidvectorcmp(PG_FUNCTION_ARGS)
289 oidvector *a = (oidvector *) PG_GETARG_POINTER(0);
290 oidvector *b = (oidvector *) PG_GETARG_POINTER(1);
293 /* We arbitrarily choose to sort first by vector length */
294 if (a->dim1 != b->dim1)
295 PG_RETURN_INT32(a->dim1 - b->dim1);
297 for (i = 0; i < a->dim1; i++)
299 if (a->values[i] != b->values[i])
301 if (a->values[i] > b->values[i])
311 btcharcmp(PG_FUNCTION_ARGS)
313 char a = PG_GETARG_CHAR(0);
314 char b = PG_GETARG_CHAR(1);
316 /* Be careful to compare chars as unsigned */
317 PG_RETURN_INT32((int32) ((uint8) a) - (int32) ((uint8) b));
321 btnamecmp(PG_FUNCTION_ARGS)
323 Name a = PG_GETARG_NAME(0);
324 Name b = PG_GETARG_NAME(1);
326 PG_RETURN_INT32(strncmp(NameStr(*a), NameStr(*b), NAMEDATALEN));
330 btnamefastcmp(Datum x, Datum y, SortSupport ssup)
332 Name a = DatumGetName(x);
333 Name b = DatumGetName(y);
335 return strncmp(NameStr(*a), NameStr(*b), NAMEDATALEN);
339 btnamesortsupport(PG_FUNCTION_ARGS)
341 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
343 ssup->comparator = btnamefastcmp;