Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_bool.c
3 : */
4 : #include "postgres.h"
5 :
6 : #include "btree_gist.h"
7 : #include "btree_utils_num.h"
8 : #include "utils/sortsupport.h"
9 :
10 : typedef struct boolkey
11 : {
12 : bool lower;
13 : bool upper;
14 : } boolKEY;
15 :
16 : /* GiST support functions */
17 4 : PG_FUNCTION_INFO_V1(gbt_bool_compress);
18 4 : PG_FUNCTION_INFO_V1(gbt_bool_fetch);
19 4 : PG_FUNCTION_INFO_V1(gbt_bool_union);
20 4 : PG_FUNCTION_INFO_V1(gbt_bool_picksplit);
21 4 : PG_FUNCTION_INFO_V1(gbt_bool_consistent);
22 4 : PG_FUNCTION_INFO_V1(gbt_bool_penalty);
23 4 : PG_FUNCTION_INFO_V1(gbt_bool_same);
24 4 : PG_FUNCTION_INFO_V1(gbt_bool_sortsupport);
25 :
26 : static bool
27 4 : gbt_boolgt(const void *a, const void *b, FmgrInfo *flinfo)
28 : {
29 4 : return (*((const bool *) a) > *((const bool *) b));
30 : }
31 : static bool
32 4 : gbt_boolge(const void *a, const void *b, FmgrInfo *flinfo)
33 : {
34 4 : return (*((const bool *) a) >= *((const bool *) b));
35 : }
36 : static bool
37 12 : gbt_booleq(const void *a, const void *b, FmgrInfo *flinfo)
38 : {
39 12 : return (*((const bool *) a) == *((const bool *) b));
40 : }
41 : static bool
42 4 : gbt_boolle(const void *a, const void *b, FmgrInfo *flinfo)
43 : {
44 4 : return (*((const bool *) a) <= *((const bool *) b));
45 : }
46 : static bool
47 4 : gbt_boollt(const void *a, const void *b, FmgrInfo *flinfo)
48 : {
49 4 : return (*((const bool *) a) < *((const bool *) b));
50 : }
51 :
52 : static int
53 0 : gbt_boolkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
54 : {
55 0 : boolKEY *ia = (boolKEY *) (((const Nsrt *) a)->t);
56 0 : boolKEY *ib = (boolKEY *) (((const Nsrt *) b)->t);
57 :
58 0 : if (ia->lower == ib->lower)
59 : {
60 0 : if (ia->upper == ib->upper)
61 0 : return 0;
62 :
63 0 : return (ia->upper > ib->upper) ? 1 : -1;
64 : }
65 :
66 0 : return (ia->lower > ib->lower) ? 1 : -1;
67 : }
68 :
69 :
70 : static const gbtree_ninfo tinfo =
71 : {
72 : gbt_t_bool,
73 : sizeof(bool),
74 : 2, /* sizeof(gbtreekey2) */
75 : gbt_boolgt,
76 : gbt_boolge,
77 : gbt_booleq,
78 : gbt_boolle,
79 : gbt_boollt,
80 : gbt_boolkey_cmp,
81 : };
82 :
83 :
84 : /**************************************************
85 : * GiST support functions
86 : **************************************************/
87 :
88 : Datum
89 4 : gbt_bool_compress(PG_FUNCTION_ARGS)
90 : {
91 4 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
92 :
93 4 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
94 : }
95 :
96 : Datum
97 14 : gbt_bool_fetch(PG_FUNCTION_ARGS)
98 : {
99 14 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
100 :
101 14 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
102 : }
103 :
104 : Datum
105 28 : gbt_bool_consistent(PG_FUNCTION_ARGS)
106 : {
107 28 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
108 28 : bool query = PG_GETARG_INT16(1);
109 28 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
110 :
111 : /* Oid subtype = PG_GETARG_OID(3); */
112 28 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
113 28 : boolKEY *kkk = (boolKEY *) DatumGetPointer(entry->key);
114 : GBT_NUMKEY_R key;
115 :
116 : /* All cases served by this function are exact */
117 28 : *recheck = false;
118 :
119 28 : key.lower = (GBT_NUMKEY *) &kkk->lower;
120 28 : key.upper = (GBT_NUMKEY *) &kkk->upper;
121 :
122 28 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
123 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
124 : }
125 :
126 : Datum
127 0 : gbt_bool_union(PG_FUNCTION_ARGS)
128 : {
129 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
130 0 : void *out = palloc(sizeof(boolKEY));
131 :
132 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(boolKEY);
133 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
134 : }
135 :
136 : Datum
137 0 : gbt_bool_penalty(PG_FUNCTION_ARGS)
138 : {
139 0 : boolKEY *origentry = (boolKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
140 0 : boolKEY *newentry = (boolKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
141 0 : float *result = (float *) PG_GETARG_POINTER(2);
142 :
143 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
144 :
145 0 : PG_RETURN_POINTER(result);
146 : }
147 :
148 : Datum
149 0 : gbt_bool_picksplit(PG_FUNCTION_ARGS)
150 : {
151 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
152 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
153 : &tinfo, fcinfo->flinfo));
154 : }
155 :
156 : Datum
157 0 : gbt_bool_same(PG_FUNCTION_ARGS)
158 : {
159 0 : boolKEY *b1 = (boolKEY *) PG_GETARG_POINTER(0);
160 0 : boolKEY *b2 = (boolKEY *) PG_GETARG_POINTER(1);
161 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
162 :
163 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
164 0 : PG_RETURN_POINTER(result);
165 : }
166 :
167 : static int
168 2 : gbt_bool_ssup_cmp(Datum x, Datum y, SortSupport ssup)
169 : {
170 2 : boolKEY *arg1 = (boolKEY *) DatumGetPointer(x);
171 2 : boolKEY *arg2 = (boolKEY *) DatumGetPointer(y);
172 :
173 : /* for leaf items we expect lower == upper, so only compare lower */
174 2 : return (int32) arg1->lower - (int32) arg2->lower;
175 : }
176 :
177 : Datum
178 2 : gbt_bool_sortsupport(PG_FUNCTION_ARGS)
179 : {
180 2 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
181 :
182 2 : ssup->comparator = gbt_bool_ssup_cmp;
183 2 : ssup->ssup_extra = NULL;
184 :
185 2 : PG_RETURN_VOID();
186 : }
|