1
/*-------------------------------------------------------------------------
4
* Comparison functions for btree access method.
6
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
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"
55
btboolcmp(PG_FUNCTION_ARGS)
57
bool a = PG_GETARG_BOOL(0);
58
bool b = PG_GETARG_BOOL(1);
60
PG_RETURN_INT32((int32) a - (int32) b);
64
btint2cmp(PG_FUNCTION_ARGS)
66
int16 a = PG_GETARG_INT16(0);
67
int16 b = PG_GETARG_INT16(1);
69
PG_RETURN_INT32((int32) a - (int32) b);
73
btint4cmp(PG_FUNCTION_ARGS)
75
int32 a = PG_GETARG_INT32(0);
76
int32 b = PG_GETARG_INT32(1);
87
btint8cmp(PG_FUNCTION_ARGS)
89
int64 a = PG_GETARG_INT64(0);
90
int64 b = PG_GETARG_INT64(1);
101
btint48cmp(PG_FUNCTION_ARGS)
103
int32 a = PG_GETARG_INT32(0);
104
int64 b = PG_GETARG_INT64(1);
115
btint84cmp(PG_FUNCTION_ARGS)
117
int64 a = PG_GETARG_INT64(0);
118
int32 b = PG_GETARG_INT32(1);
129
btint24cmp(PG_FUNCTION_ARGS)
131
int16 a = PG_GETARG_INT16(0);
132
int32 b = PG_GETARG_INT32(1);
143
btint42cmp(PG_FUNCTION_ARGS)
145
int32 a = PG_GETARG_INT32(0);
146
int16 b = PG_GETARG_INT16(1);
157
btint28cmp(PG_FUNCTION_ARGS)
159
int16 a = PG_GETARG_INT16(0);
160
int64 b = PG_GETARG_INT64(1);
171
btint82cmp(PG_FUNCTION_ARGS)
173
int64 a = PG_GETARG_INT64(0);
174
int16 b = PG_GETARG_INT16(1);
185
btoidcmp(PG_FUNCTION_ARGS)
187
Oid a = PG_GETARG_OID(0);
188
Oid b = PG_GETARG_OID(1);
199
btoidvectorcmp(PG_FUNCTION_ARGS)
201
oidvector *a = (oidvector *) PG_GETARG_POINTER(0);
202
oidvector *b = (oidvector *) PG_GETARG_POINTER(1);
205
/* We arbitrarily choose to sort first by vector length */
206
if (a->dim1 != b->dim1)
207
PG_RETURN_INT32(a->dim1 - b->dim1);
209
for (i = 0; i < a->dim1; i++)
211
if (a->values[i] != b->values[i])
213
if (a->values[i] > b->values[i])
223
btcharcmp(PG_FUNCTION_ARGS)
225
char a = PG_GETARG_CHAR(0);
226
char b = PG_GETARG_CHAR(1);
228
/* Be careful to compare chars as unsigned */
229
PG_RETURN_INT32((int32) ((uint8) a) - (int32) ((uint8) b));
233
btnamecmp(PG_FUNCTION_ARGS)
235
Name a = PG_GETARG_NAME(0);
236
Name b = PG_GETARG_NAME(1);
238
PG_RETURN_INT32(strncmp(NameStr(*a), NameStr(*b), NAMEDATALEN));