Fix the query used for \d against 8.2 and 8.3 servers.
[PostgreSQL.git] / contrib / btree_gist / btree_bit.c
blob2c16463e003c556f3b98a1432fbd72d55dfefdc0
1 /*
2 * $PostgreSQL:$
3 */
4 #include "btree_gist.h"
5 #include "btree_utils_var.h"
6 #include "utils/builtins.h"
7 #include "utils/varbit.h"
11 ** Bit ops
13 PG_FUNCTION_INFO_V1(gbt_bit_compress);
14 PG_FUNCTION_INFO_V1(gbt_bit_union);
15 PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
16 PG_FUNCTION_INFO_V1(gbt_bit_consistent);
17 PG_FUNCTION_INFO_V1(gbt_bit_penalty);
18 PG_FUNCTION_INFO_V1(gbt_bit_same);
20 Datum gbt_bit_compress(PG_FUNCTION_ARGS);
21 Datum gbt_bit_union(PG_FUNCTION_ARGS);
22 Datum gbt_bit_picksplit(PG_FUNCTION_ARGS);
23 Datum gbt_bit_consistent(PG_FUNCTION_ARGS);
24 Datum gbt_bit_penalty(PG_FUNCTION_ARGS);
25 Datum gbt_bit_same(PG_FUNCTION_ARGS);
29 /* define for comparison */
31 static bool
32 gbt_bitgt(const void *a, const void *b)
34 return (DatumGetBool(DirectFunctionCall2(bitgt, PointerGetDatum(a), PointerGetDatum(b))));
37 static bool
38 gbt_bitge(const void *a, const void *b)
40 return (DatumGetBool(DirectFunctionCall2(bitge, PointerGetDatum(a), PointerGetDatum(b))));
43 static bool
44 gbt_biteq(const void *a, const void *b)
46 return (DatumGetBool(DirectFunctionCall2(biteq, PointerGetDatum(a), PointerGetDatum(b))));
49 static bool
50 gbt_bitle(const void *a, const void *b)
52 return (DatumGetBool(DirectFunctionCall2(bitle, PointerGetDatum(a), PointerGetDatum(b))));
55 static bool
56 gbt_bitlt(const void *a, const void *b)
58 return (DatumGetBool(DirectFunctionCall2(bitlt, PointerGetDatum(a), PointerGetDatum(b))));
61 static int32
62 gbt_bitcmp(const bytea *a, const bytea *b)
64 return
65 (DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
69 static bytea *
70 gbt_bit_xfrm(bytea *leaf)
72 bytea *out = leaf;
73 int s = INTALIGN(VARBITBYTES(leaf) + VARHDRSZ);
75 out = palloc(s);
76 SET_VARSIZE(out, s);
77 memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
78 return out;
84 static GBT_VARKEY *
85 gbt_bit_l2n(GBT_VARKEY * leaf)
88 GBT_VARKEY *out = leaf;
89 GBT_VARKEY_R r = gbt_var_key_readable(leaf);
90 bytea *o;
92 o = gbt_bit_xfrm(r.lower);
93 r.upper = r.lower = o;
94 out = gbt_var_key_copy(&r, TRUE);
95 pfree(o);
97 return out;
101 static const gbtree_vinfo tinfo =
103 gbt_t_bit,
105 TRUE,
106 gbt_bitgt,
107 gbt_bitge,
108 gbt_biteq,
109 gbt_bitle,
110 gbt_bitlt,
111 gbt_bitcmp,
112 gbt_bit_l2n
116 /**************************************************
117 * Bit ops
118 **************************************************/
120 Datum
121 gbt_bit_compress(PG_FUNCTION_ARGS)
123 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
125 PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
128 Datum
129 gbt_bit_consistent(PG_FUNCTION_ARGS)
131 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
133 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
134 /* Oid subtype = PG_GETARG_OID(3); */
135 bool *recheck = (bool *) PG_GETARG_POINTER(4);
136 bool retval = FALSE;
137 GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
138 GBT_VARKEY_R r = gbt_var_key_readable(key);
140 /* All cases served by this function are exact */
141 *recheck = false;
143 if (GIST_LEAF(entry))
144 retval = gbt_var_consistent(&r, query, &strategy, TRUE, &tinfo);
145 else
147 bytea *q = gbt_bit_xfrm((bytea *) query);
149 retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
151 PG_RETURN_BOOL(retval);
156 Datum
157 gbt_bit_union(PG_FUNCTION_ARGS)
159 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
160 int32 *size = (int *) PG_GETARG_POINTER(1);
162 PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
166 Datum
167 gbt_bit_picksplit(PG_FUNCTION_ARGS)
169 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
170 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
172 gbt_var_picksplit(entryvec, v, &tinfo);
173 PG_RETURN_POINTER(v);
176 Datum
177 gbt_bit_same(PG_FUNCTION_ARGS)
179 Datum d1 = PG_GETARG_DATUM(0);
180 Datum d2 = PG_GETARG_DATUM(1);
181 bool *result = (bool *) PG_GETARG_POINTER(2);
183 PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
187 Datum
188 gbt_bit_penalty(PG_FUNCTION_ARGS)
190 GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
191 GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
192 float *result = (float *) PG_GETARG_POINTER(2);
194 PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));