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