Age Owner Branch data TLA 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 */
1400 tomas.vondra@postgre 18 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_bool_compress);
19 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_fetch);
20 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_union);
21 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_picksplit);
22 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_consistent);
23 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_penalty);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_same);
156 heikki.linnakangas@i 25 : 4 : PG_FUNCTION_INFO_V1(gbt_bool_sortsupport);
26 : :
27 : : static bool
1400 tomas.vondra@postgre 28 : 2 : gbt_boolgt(const void *a, const void *b, FmgrInfo *flinfo)
29 : : {
30 : 2 : return (*((const bool *) a) > *((const bool *) b));
31 : : }
32 : : static bool
33 : 2 : gbt_boolge(const void *a, const void *b, FmgrInfo *flinfo)
34 : : {
35 : 2 : return (*((const bool *) a) >= *((const bool *) b));
36 : : }
37 : : static bool
38 : 6 : gbt_booleq(const void *a, const void *b, FmgrInfo *flinfo)
39 : : {
40 : 6 : return (*((const bool *) a) == *((const bool *) b));
41 : : }
42 : : static bool
43 : 2 : gbt_boolle(const void *a, const void *b, FmgrInfo *flinfo)
44 : : {
45 : 2 : return (*((const bool *) a) <= *((const bool *) b));
46 : : }
47 : : static bool
48 : 2 : gbt_boollt(const void *a, const void *b, FmgrInfo *flinfo)
49 : : {
50 : 2 : return (*((const bool *) a) < *((const bool *) b));
51 : : }
52 : :
53 : : static int
1400 tomas.vondra@postgre 54 :UBC 0 : gbt_boolkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
55 : : {
1213 tgl@sss.pgh.pa.us 56 : 0 : boolKEY *ia = (boolKEY *) (((const Nsrt *) a)->t);
57 : 0 : boolKEY *ib = (boolKEY *) (((const Nsrt *) b)->t);
58 : :
1400 tomas.vondra@postgre 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
1400 tomas.vondra@postgre 90 :CBC 2 : gbt_bool_compress(PG_FUNCTION_ARGS)
91 : : {
92 : 2 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
93 : :
94 : 2 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
95 : : }
96 : :
97 : : Datum
98 : 7 : gbt_bool_fetch(PG_FUNCTION_ARGS)
99 : : {
100 : 7 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
101 : :
102 : 7 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
103 : : }
104 : :
105 : : Datum
106 : 14 : gbt_bool_consistent(PG_FUNCTION_ARGS)
107 : : {
108 : 14 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
109 : 14 : bool query = PG_GETARG_INT16(1);
110 : 14 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
111 : :
112 : : /* Oid subtype = PG_GETARG_OID(3); */
113 : 14 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
114 : 14 : boolKEY *kkk = (boolKEY *) DatumGetPointer(entry->key);
115 : : GBT_NUMKEY_R key;
116 : :
117 : : /* All cases served by this function are exact */
118 : 14 : *recheck = false;
119 : :
120 : 14 : key.lower = (GBT_NUMKEY *) &kkk->lower;
121 : 14 : key.upper = (GBT_NUMKEY *) &kkk->upper;
122 : :
282 peter@eisentraut.org 123 : 14 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
124 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
125 : : }
126 : :
127 : : Datum
1400 tomas.vondra@postgre 128 :UBC 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);
282 peter@eisentraut.org 134 : 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
135 : : }
136 : :
137 : : Datum
1400 tomas.vondra@postgre 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
156 heikki.linnakangas@i 169 :CBC 1 : gbt_bool_ssup_cmp(Datum x, Datum y, SortSupport ssup)
170 : : {
171 : 1 : boolKEY *arg1 = (boolKEY *) DatumGetPointer(x);
172 : 1 : boolKEY *arg2 = (boolKEY *) DatumGetPointer(y);
173 : :
174 : : /* for leaf items we expect lower == upper, so only compare lower */
175 : 1 : return (int32) arg1->lower - (int32) arg2->lower;
176 : : }
177 : :
178 : : Datum
179 : 1 : gbt_bool_sortsupport(PG_FUNCTION_ARGS)
180 : : {
181 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
182 : :
183 : 1 : ssup->comparator = gbt_bool_ssup_cmp;
184 : 1 : ssup->ssup_extra = NULL;
185 : :
186 : 1 : PG_RETURN_VOID();
187 : : }
|