Alter the xxx_pattern_ops opclasses to use the regular equality operator of
[PostgreSQL.git] / src / backend / access / nbtree / nbtcompare.c
blob3859afdeb4ada59632ffd0cabfe16c287e5c6453
1 /*-------------------------------------------------------------------------
3 * nbtcompare.c
4 * Comparison functions for btree access method.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 * NOTES
15 * These functions are stored in pg_amproc. For each operator class
16 * defined on btrees, they compute
18 * compare(a, b):
19 * < 0 if a < b,
20 * = 0 if a == b,
21 * > 0 if a > b.
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 *-------------------------------------------------------------------------
49 #include "postgres.h"
51 #include "utils/builtins.h"
54 Datum
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);
63 Datum
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);
72 Datum
73 btint4cmp(PG_FUNCTION_ARGS)
75 int32 a = PG_GETARG_INT32(0);
76 int32 b = PG_GETARG_INT32(1);
78 if (a > b)
79 PG_RETURN_INT32(1);
80 else if (a == b)
81 PG_RETURN_INT32(0);
82 else
83 PG_RETURN_INT32(-1);
86 Datum
87 btint8cmp(PG_FUNCTION_ARGS)
89 int64 a = PG_GETARG_INT64(0);
90 int64 b = PG_GETARG_INT64(1);
92 if (a > b)
93 PG_RETURN_INT32(1);
94 else if (a == b)
95 PG_RETURN_INT32(0);
96 else
97 PG_RETURN_INT32(-1);
100 Datum
101 btint48cmp(PG_FUNCTION_ARGS)
103 int32 a = PG_GETARG_INT32(0);
104 int64 b = PG_GETARG_INT64(1);
106 if (a > b)
107 PG_RETURN_INT32(1);
108 else if (a == b)
109 PG_RETURN_INT32(0);
110 else
111 PG_RETURN_INT32(-1);
114 Datum
115 btint84cmp(PG_FUNCTION_ARGS)
117 int64 a = PG_GETARG_INT64(0);
118 int32 b = PG_GETARG_INT32(1);
120 if (a > b)
121 PG_RETURN_INT32(1);
122 else if (a == b)
123 PG_RETURN_INT32(0);
124 else
125 PG_RETURN_INT32(-1);
128 Datum
129 btint24cmp(PG_FUNCTION_ARGS)
131 int16 a = PG_GETARG_INT16(0);
132 int32 b = PG_GETARG_INT32(1);
134 if (a > b)
135 PG_RETURN_INT32(1);
136 else if (a == b)
137 PG_RETURN_INT32(0);
138 else
139 PG_RETURN_INT32(-1);
142 Datum
143 btint42cmp(PG_FUNCTION_ARGS)
145 int32 a = PG_GETARG_INT32(0);
146 int16 b = PG_GETARG_INT16(1);
148 if (a > b)
149 PG_RETURN_INT32(1);
150 else if (a == b)
151 PG_RETURN_INT32(0);
152 else
153 PG_RETURN_INT32(-1);
156 Datum
157 btint28cmp(PG_FUNCTION_ARGS)
159 int16 a = PG_GETARG_INT16(0);
160 int64 b = PG_GETARG_INT64(1);
162 if (a > b)
163 PG_RETURN_INT32(1);
164 else if (a == b)
165 PG_RETURN_INT32(0);
166 else
167 PG_RETURN_INT32(-1);
170 Datum
171 btint82cmp(PG_FUNCTION_ARGS)
173 int64 a = PG_GETARG_INT64(0);
174 int16 b = PG_GETARG_INT16(1);
176 if (a > b)
177 PG_RETURN_INT32(1);
178 else if (a == b)
179 PG_RETURN_INT32(0);
180 else
181 PG_RETURN_INT32(-1);
184 Datum
185 btoidcmp(PG_FUNCTION_ARGS)
187 Oid a = PG_GETARG_OID(0);
188 Oid b = PG_GETARG_OID(1);
190 if (a > b)
191 PG_RETURN_INT32(1);
192 else if (a == b)
193 PG_RETURN_INT32(0);
194 else
195 PG_RETURN_INT32(-1);
198 Datum
199 btoidvectorcmp(PG_FUNCTION_ARGS)
201 oidvector *a = (oidvector *) PG_GETARG_POINTER(0);
202 oidvector *b = (oidvector *) PG_GETARG_POINTER(1);
203 int i;
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])
214 PG_RETURN_INT32(1);
215 else
216 PG_RETURN_INT32(-1);
219 PG_RETURN_INT32(0);
222 Datum
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));
232 Datum
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));