PolarDB-for-PostgreSQL
221 строка · 4.7 Кб
1/*
2* contrib/btree_gist/btree_oid.c
3*/
4#include "postgres.h"
5
6#include "btree_gist.h"
7#include "btree_utils_num.h"
8
9typedef struct
10{
11Oid lower;
12Oid upper;
13} oidKEY;
14
15/*
16** OID ops
17*/
18PG_FUNCTION_INFO_V1(gbt_oid_compress);
19PG_FUNCTION_INFO_V1(gbt_oid_fetch);
20PG_FUNCTION_INFO_V1(gbt_oid_union);
21PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
22PG_FUNCTION_INFO_V1(gbt_oid_consistent);
23PG_FUNCTION_INFO_V1(gbt_oid_distance);
24PG_FUNCTION_INFO_V1(gbt_oid_penalty);
25PG_FUNCTION_INFO_V1(gbt_oid_same);
26
27
28static bool
29gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
30{
31return (*((const Oid *) a) > *((const Oid *) b));
32}
33static bool
34gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
35{
36return (*((const Oid *) a) >= *((const Oid *) b));
37}
38static bool
39gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
40{
41return (*((const Oid *) a) == *((const Oid *) b));
42}
43static bool
44gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
45{
46return (*((const Oid *) a) <= *((const Oid *) b));
47}
48static bool
49gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
50{
51return (*((const Oid *) a) < *((const Oid *) b));
52}
53
54static int
55gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56{
57oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
58oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
59
60if (ia->lower == ib->lower)
61{
62if (ia->upper == ib->upper)
63return 0;
64
65return (ia->upper > ib->upper) ? 1 : -1;
66}
67
68return (ia->lower > ib->lower) ? 1 : -1;
69}
70
71static float8
72gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
73{
74Oid aa = *(const Oid *) a;
75Oid bb = *(const Oid *) b;
76
77if (aa < bb)
78return (float8) (bb - aa);
79else
80return (float8) (aa - bb);
81}
82
83
84static const gbtree_ninfo tinfo =
85{
86gbt_t_oid,
87sizeof(Oid),
888, /* sizeof(gbtreekey8) */
89gbt_oidgt,
90gbt_oidge,
91gbt_oideq,
92gbt_oidle,
93gbt_oidlt,
94gbt_oidkey_cmp,
95gbt_oid_dist
96};
97
98
99PG_FUNCTION_INFO_V1(oid_dist);
100Datum
101oid_dist(PG_FUNCTION_ARGS)
102{
103Oid a = PG_GETARG_OID(0);
104Oid b = PG_GETARG_OID(1);
105Oid res;
106
107if (a < b)
108res = b - a;
109else
110res = a - b;
111PG_RETURN_OID(res);
112}
113
114
115/**************************************************
116* Oid ops
117**************************************************/
118
119
120Datum
121gbt_oid_compress(PG_FUNCTION_ARGS)
122{
123GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124
125PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126}
127
128Datum
129gbt_oid_fetch(PG_FUNCTION_ARGS)
130{
131GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132
133PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
134}
135
136Datum
137gbt_oid_consistent(PG_FUNCTION_ARGS)
138{
139GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140Oid query = PG_GETARG_OID(1);
141StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142
143/* Oid subtype = PG_GETARG_OID(3); */
144bool *recheck = (bool *) PG_GETARG_POINTER(4);
145oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
146GBT_NUMKEY_R key;
147
148/* All cases served by this function are exact */
149*recheck = false;
150
151key.lower = (GBT_NUMKEY *) &kkk->lower;
152key.upper = (GBT_NUMKEY *) &kkk->upper;
153
154PG_RETURN_BOOL(
155gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
156);
157}
158
159
160Datum
161gbt_oid_distance(PG_FUNCTION_ARGS)
162{
163GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
164Oid query = PG_GETARG_OID(1);
165
166/* Oid subtype = PG_GETARG_OID(3); */
167oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
168GBT_NUMKEY_R key;
169
170key.lower = (GBT_NUMKEY *) &kkk->lower;
171key.upper = (GBT_NUMKEY *) &kkk->upper;
172
173PG_RETURN_FLOAT8(
174gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
175);
176}
177
178
179Datum
180gbt_oid_union(PG_FUNCTION_ARGS)
181{
182GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
183void *out = palloc(sizeof(oidKEY));
184
185*(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
186PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
187}
188
189
190Datum
191gbt_oid_penalty(PG_FUNCTION_ARGS)
192{
193oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
194oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
195float *result = (float *) PG_GETARG_POINTER(2);
196
197penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
198
199PG_RETURN_POINTER(result);
200}
201
202Datum
203gbt_oid_picksplit(PG_FUNCTION_ARGS)
204{
205PG_RETURN_POINTER(gbt_num_picksplit(
206(GistEntryVector *) PG_GETARG_POINTER(0),
207(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
208&tinfo, fcinfo->flinfo
209));
210}
211
212Datum
213gbt_oid_same(PG_FUNCTION_ARGS)
214{
215oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
216oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
217bool *result = (bool *) PG_GETARG_POINTER(2);
218
219*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
220PG_RETURN_POINTER(result);
221}
222