Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * spgkdtreeproc.c
4 : : * implementation of k-d tree over points for SP-GiST
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/spgist/spgkdtreeproc.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include "access/spgist.h"
19 : : #include "access/spgist_private.h"
20 : : #include "access/stratnum.h"
21 : : #include "catalog/pg_type.h"
22 : : #include "utils/float.h"
23 : : #include "utils/fmgrprotos.h"
24 : : #include "utils/geo_decls.h"
25 : :
26 : :
27 : : Datum
28 : 4 : spg_kd_config(PG_FUNCTION_ARGS)
29 : : {
30 : : #ifdef NOT_USED
31 : : spgConfigIn *cfgin = (spgConfigIn *) PG_GETARG_POINTER(0);
32 : : #endif
33 : 4 : spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
34 : :
35 : 4 : cfg->prefixType = FLOAT8OID;
36 : 4 : cfg->labelType = VOIDOID; /* we don't need node labels */
37 : 4 : cfg->canReturnData = true;
38 : 4 : cfg->longValuesOK = false;
39 : 4 : PG_RETURN_VOID();
40 : 4 : }
41 : :
42 : : static int
43 : 0 : getSide(double coord, bool isX, Point *tst)
44 : : {
45 [ # # ]: 0 : double tstcoord = (isX) ? tst->x : tst->y;
46 : :
47 [ # # ]: 0 : if (coord == tstcoord)
48 : 0 : return 0;
49 [ # # ]: 0 : else if (coord > tstcoord)
50 : 0 : return 1;
51 : : else
52 : 0 : return -1;
53 : 0 : }
54 : :
55 : : Datum
56 : 0 : spg_kd_choose(PG_FUNCTION_ARGS)
57 : : {
58 : 0 : spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0);
59 : 0 : spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1);
60 : 0 : Point *inPoint = DatumGetPointP(in->datum);
61 : 0 : double coord;
62 : :
63 [ # # ]: 0 : if (in->allTheSame)
64 [ # # # # ]: 0 : elog(ERROR, "allTheSame should not occur for k-d trees");
65 : :
66 [ # # ]: 0 : Assert(in->hasPrefix);
67 : 0 : coord = DatumGetFloat8(in->prefixDatum);
68 : :
69 [ # # ]: 0 : Assert(in->nNodes == 2);
70 : :
71 : 0 : out->resultType = spgMatchNode;
72 : 0 : out->result.matchNode.nodeN =
73 : 0 : (getSide(coord, in->level % 2, inPoint) > 0) ? 0 : 1;
74 : 0 : out->result.matchNode.levelAdd = 1;
75 : 0 : out->result.matchNode.restDatum = PointPGetDatum(inPoint);
76 : :
77 : 0 : PG_RETURN_VOID();
78 : 0 : }
79 : :
80 : : typedef struct SortedPoint
81 : : {
82 : : Point *p;
83 : : int i;
84 : : } SortedPoint;
85 : :
86 : : static int
87 : 0 : x_cmp(const void *a, const void *b)
88 : : {
89 : 0 : const SortedPoint *pa = a;
90 : 0 : const SortedPoint *pb = b;
91 : :
92 [ # # ]: 0 : if (pa->p->x == pb->p->x)
93 : 0 : return 0;
94 : 0 : return (pa->p->x > pb->p->x) ? 1 : -1;
95 : 0 : }
96 : :
97 : : static int
98 : 0 : y_cmp(const void *a, const void *b)
99 : : {
100 : 0 : const SortedPoint *pa = a;
101 : 0 : const SortedPoint *pb = b;
102 : :
103 [ # # ]: 0 : if (pa->p->y == pb->p->y)
104 : 0 : return 0;
105 : 0 : return (pa->p->y > pb->p->y) ? 1 : -1;
106 : 0 : }
107 : :
108 : :
109 : : Datum
110 : 0 : spg_kd_picksplit(PG_FUNCTION_ARGS)
111 : : {
112 : 0 : spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0);
113 : 0 : spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1);
114 : 0 : int i;
115 : 0 : int middle;
116 : 0 : SortedPoint *sorted;
117 : 0 : double coord;
118 : :
119 : 0 : sorted = palloc_array(SortedPoint, in->nTuples);
120 [ # # ]: 0 : for (i = 0; i < in->nTuples; i++)
121 : : {
122 : 0 : sorted[i].p = DatumGetPointP(in->datums[i]);
123 : 0 : sorted[i].i = i;
124 : 0 : }
125 : :
126 : 0 : qsort(sorted, in->nTuples, sizeof(*sorted),
127 : : (in->level % 2) ? x_cmp : y_cmp);
128 : 0 : middle = in->nTuples >> 1;
129 [ # # ]: 0 : coord = (in->level % 2) ? sorted[middle].p->x : sorted[middle].p->y;
130 : :
131 : 0 : out->hasPrefix = true;
132 : 0 : out->prefixDatum = Float8GetDatum(coord);
133 : :
134 : 0 : out->nNodes = 2;
135 : 0 : out->nodeLabels = NULL; /* we don't need node labels */
136 : :
137 : 0 : out->mapTuplesToNodes = palloc_array(int, in->nTuples);
138 : 0 : out->leafTupleDatums = palloc_array(Datum, in->nTuples);
139 : :
140 : : /*
141 : : * Note: points that have coordinates exactly equal to coord may get
142 : : * classified into either node, depending on where they happen to fall in
143 : : * the sorted list. This is okay as long as the inner_consistent function
144 : : * descends into both sides for such cases. This is better than the
145 : : * alternative of trying to have an exact boundary, because it keeps the
146 : : * tree balanced even when we have many instances of the same point value.
147 : : * So we should never trigger the allTheSame logic.
148 : : */
149 [ # # ]: 0 : for (i = 0; i < in->nTuples; i++)
150 : : {
151 : 0 : Point *p = sorted[i].p;
152 : 0 : int n = sorted[i].i;
153 : :
154 : 0 : out->mapTuplesToNodes[n] = (i < middle) ? 0 : 1;
155 : 0 : out->leafTupleDatums[n] = PointPGetDatum(p);
156 : 0 : }
157 : :
158 : 0 : PG_RETURN_VOID();
159 : 0 : }
160 : :
161 : : Datum
162 : 0 : spg_kd_inner_consistent(PG_FUNCTION_ARGS)
163 : : {
164 : 0 : spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0);
165 : 0 : spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1);
166 : 0 : double coord;
167 : 0 : int which;
168 : 0 : int i;
169 : 0 : BOX bboxes[2];
170 : :
171 [ # # ]: 0 : Assert(in->hasPrefix);
172 : 0 : coord = DatumGetFloat8(in->prefixDatum);
173 : :
174 [ # # ]: 0 : if (in->allTheSame)
175 [ # # # # ]: 0 : elog(ERROR, "allTheSame should not occur for k-d trees");
176 : :
177 [ # # ]: 0 : Assert(in->nNodes == 2);
178 : :
179 : : /* "which" is a bitmask of children that satisfy all constraints */
180 : 0 : which = (1 << 1) | (1 << 2);
181 : :
182 [ # # ]: 0 : for (i = 0; i < in->nkeys; i++)
183 : : {
184 : 0 : Point *query = DatumGetPointP(in->scankeys[i].sk_argument);
185 : 0 : BOX *boxQuery;
186 : :
187 [ # # # # : 0 : switch (in->scankeys[i].sk_strategy)
# # # ]
188 : : {
189 : : case RTLeftStrategyNumber:
190 [ # # # # ]: 0 : if ((in->level % 2) != 0 && FPlt(query->x, coord))
191 : 0 : which &= (1 << 1);
192 : 0 : break;
193 : : case RTRightStrategyNumber:
194 [ # # # # ]: 0 : if ((in->level % 2) != 0 && FPgt(query->x, coord))
195 : 0 : which &= (1 << 2);
196 : 0 : break;
197 : : case RTSameStrategyNumber:
198 [ # # ]: 0 : if ((in->level % 2) != 0)
199 : : {
200 [ # # ]: 0 : if (FPlt(query->x, coord))
201 : 0 : which &= (1 << 1);
202 [ # # ]: 0 : else if (FPgt(query->x, coord))
203 : 0 : which &= (1 << 2);
204 : 0 : }
205 : : else
206 : : {
207 [ # # ]: 0 : if (FPlt(query->y, coord))
208 : 0 : which &= (1 << 1);
209 [ # # ]: 0 : else if (FPgt(query->y, coord))
210 : 0 : which &= (1 << 2);
211 : : }
212 : 0 : break;
213 : : case RTBelowStrategyNumber:
214 : : case RTOldBelowStrategyNumber:
215 [ # # # # ]: 0 : if ((in->level % 2) == 0 && FPlt(query->y, coord))
216 : 0 : which &= (1 << 1);
217 : 0 : break;
218 : : case RTAboveStrategyNumber:
219 : : case RTOldAboveStrategyNumber:
220 [ # # # # ]: 0 : if ((in->level % 2) == 0 && FPgt(query->y, coord))
221 : 0 : which &= (1 << 2);
222 : 0 : break;
223 : : case RTContainedByStrategyNumber:
224 : :
225 : : /*
226 : : * For this operator, the query is a box not a point. We
227 : : * cheat to the extent of assuming that DatumGetPointP won't
228 : : * do anything that would be bad for a pointer-to-box.
229 : : */
230 : 0 : boxQuery = DatumGetBoxP(in->scankeys[i].sk_argument);
231 : :
232 [ # # ]: 0 : if ((in->level % 2) != 0)
233 : : {
234 [ # # ]: 0 : if (FPlt(boxQuery->high.x, coord))
235 : 0 : which &= (1 << 1);
236 [ # # ]: 0 : else if (FPgt(boxQuery->low.x, coord))
237 : 0 : which &= (1 << 2);
238 : 0 : }
239 : : else
240 : : {
241 [ # # ]: 0 : if (FPlt(boxQuery->high.y, coord))
242 : 0 : which &= (1 << 1);
243 [ # # ]: 0 : else if (FPgt(boxQuery->low.y, coord))
244 : 0 : which &= (1 << 2);
245 : : }
246 : 0 : break;
247 : : default:
248 [ # # # # ]: 0 : elog(ERROR, "unrecognized strategy number: %d",
249 : : in->scankeys[i].sk_strategy);
250 : 0 : break;
251 : : }
252 : :
253 [ # # ]: 0 : if (which == 0)
254 : 0 : break; /* no need to consider remaining conditions */
255 [ # # # ]: 0 : }
256 : :
257 : : /* We must descend into the children identified by which */
258 : 0 : out->nNodes = 0;
259 : :
260 : : /* Fast-path for no matching children */
261 [ # # ]: 0 : if (!which)
262 : 0 : PG_RETURN_VOID();
263 : :
264 : 0 : out->nodeNumbers = palloc_array(int, 2);
265 : :
266 : : /*
267 : : * When ordering scan keys are specified, we've to calculate distance for
268 : : * them. In order to do that, we need calculate bounding boxes for both
269 : : * children nodes. Calculation of those bounding boxes on non-zero level
270 : : * require knowledge of bounding box of upper node. So, we save bounding
271 : : * boxes to traversalValues.
272 : : */
273 [ # # ]: 0 : if (in->norderbys > 0)
274 : : {
275 : 0 : BOX infArea;
276 : 0 : BOX *area;
277 : :
278 : 0 : out->distances = palloc_array(double *, in->nNodes);
279 : 0 : out->traversalValues = palloc_array(void *, in->nNodes);
280 : :
281 [ # # ]: 0 : if (in->level == 0)
282 : : {
283 : 0 : float8 inf = get_float8_infinity();
284 : :
285 : 0 : infArea.high.x = inf;
286 : 0 : infArea.high.y = inf;
287 : 0 : infArea.low.x = -inf;
288 : 0 : infArea.low.y = -inf;
289 : 0 : area = &infArea;
290 : 0 : }
291 : : else
292 : : {
293 : 0 : area = (BOX *) in->traversalValue;
294 [ # # ]: 0 : Assert(area);
295 : : }
296 : :
297 : 0 : bboxes[0].low = area->low;
298 : 0 : bboxes[1].high = area->high;
299 : :
300 [ # # ]: 0 : if (in->level % 2)
301 : : {
302 : : /* split box by x */
303 : 0 : bboxes[0].high.x = bboxes[1].low.x = coord;
304 : 0 : bboxes[0].high.y = area->high.y;
305 : 0 : bboxes[1].low.y = area->low.y;
306 : 0 : }
307 : : else
308 : : {
309 : : /* split box by y */
310 : 0 : bboxes[0].high.y = bboxes[1].low.y = coord;
311 : 0 : bboxes[0].high.x = area->high.x;
312 : 0 : bboxes[1].low.x = area->low.x;
313 : : }
314 : 0 : }
315 : :
316 [ # # ]: 0 : for (i = 1; i <= 2; i++)
317 : : {
318 [ # # ]: 0 : if (which & (1 << i))
319 : : {
320 : 0 : out->nodeNumbers[out->nNodes] = i - 1;
321 : :
322 [ # # ]: 0 : if (in->norderbys > 0)
323 : : {
324 : 0 : MemoryContext oldCtx = MemoryContextSwitchTo(in->traversalMemoryContext);
325 : 0 : BOX *box = box_copy(&bboxes[i - 1]);
326 : :
327 : 0 : MemoryContextSwitchTo(oldCtx);
328 : :
329 : 0 : out->traversalValues[out->nNodes] = box;
330 : :
331 : 0 : out->distances[out->nNodes] = spg_key_orderbys_distances(BoxPGetDatum(box), false,
332 : 0 : in->orderbys, in->norderbys);
333 : 0 : }
334 : :
335 : 0 : out->nNodes++;
336 : 0 : }
337 : 0 : }
338 : :
339 : : /* Set up level increments, too */
340 : 0 : out->levelAdds = palloc_array(int, 2);
341 : 0 : out->levelAdds[0] = 1;
342 : 0 : out->levelAdds[1] = 1;
343 : :
344 : 0 : PG_RETURN_VOID();
345 : 0 : }
346 : :
347 : : /*
348 : : * spg_kd_leaf_consistent() is the same as spg_quad_leaf_consistent(),
349 : : * since we support the same operators and the same leaf data type.
350 : : * So we just borrow that function.
351 : : */
|