PostgreSQL Source Code git master
Loading...
Searching...
No Matches
spgkdtreeproc.c
Go to the documentation of this file.
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"
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
29{
30#ifdef NOT_USED
32#endif
34
35 cfg->prefixType = FLOAT8OID;
36 cfg->labelType = VOIDOID; /* we don't need node labels */
37 cfg->canReturnData = true;
38 cfg->longValuesOK = false;
40}
41
42static int
43getSide(double coord, bool isX, Point *tst)
44{
45 double tstcoord = (isX) ? tst->x : tst->y;
46
47 if (coord == tstcoord)
48 return 0;
49 else if (coord > tstcoord)
50 return 1;
51 else
52 return -1;
53}
54
57{
61 double coord;
62
63 if (in->allTheSame)
64 elog(ERROR, "allTheSame should not occur for k-d trees");
65
66 Assert(in->hasPrefix);
68
69 Assert(in->nNodes == 2);
70
73 (getSide(coord, in->level % 2, inPoint) > 0) ? 0 : 1;
74 out->result.matchNode.levelAdd = 1;
76
78}
79
80typedef struct SortedPoint
81{
83 int i;
85
86static int
87x_cmp(const void *a, const void *b)
88{
89 const SortedPoint *pa = a;
90 const SortedPoint *pb = b;
91
92 if (pa->p->x == pb->p->x)
93 return 0;
94 return (pa->p->x > pb->p->x) ? 1 : -1;
95}
96
97static int
98y_cmp(const void *a, const void *b)
99{
100 const SortedPoint *pa = a;
101 const SortedPoint *pb = b;
102
103 if (pa->p->y == pb->p->y)
104 return 0;
105 return (pa->p->y > pb->p->y) ? 1 : -1;
106}
107
108
109Datum
111{
114 int i;
115 int middle;
116 SortedPoint *sorted;
117 double coord;
118
119 sorted = palloc_array(SortedPoint, in->nTuples);
120 for (i = 0; i < in->nTuples; i++)
121 {
122 sorted[i].p = DatumGetPointP(in->datums[i]);
123 sorted[i].i = i;
124 }
125
126 qsort(sorted, in->nTuples, sizeof(*sorted),
127 (in->level % 2) ? x_cmp : y_cmp);
128 middle = in->nTuples >> 1;
129 coord = (in->level % 2) ? sorted[middle].p->x : sorted[middle].p->y;
130
131 out->hasPrefix = true;
133
134 out->nNodes = 2;
135 out->nodeLabels = NULL; /* we don't need node labels */
136
137 out->mapTuplesToNodes = palloc_array(int, 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 for (i = 0; i < in->nTuples; i++)
150 {
151 Point *p = sorted[i].p;
152 int n = sorted[i].i;
153
154 out->mapTuplesToNodes[n] = (i < middle) ? 0 : 1;
155 out->leafTupleDatums[n] = PointPGetDatum(p);
156 }
157
159}
160
161Datum
163{
166 double coord;
167 int which;
168 int i;
169 BOX bboxes[2];
170
171 Assert(in->hasPrefix);
173
174 if (in->allTheSame)
175 elog(ERROR, "allTheSame should not occur for k-d trees");
176
177 Assert(in->nNodes == 2);
178
179 /* "which" is a bitmask of children that satisfy all constraints */
180 which = (1 << 1) | (1 << 2);
181
182 for (i = 0; i < in->nkeys; i++)
183 {
185 BOX *boxQuery;
186
187 switch (in->scankeys[i].sk_strategy)
188 {
190 if ((in->level % 2) != 0 && FPlt(query->x, coord))
191 which &= (1 << 1);
192 break;
194 if ((in->level % 2) != 0 && FPgt(query->x, coord))
195 which &= (1 << 2);
196 break;
198 if ((in->level % 2) != 0)
199 {
200 if (FPlt(query->x, coord))
201 which &= (1 << 1);
202 else if (FPgt(query->x, coord))
203 which &= (1 << 2);
204 }
205 else
206 {
207 if (FPlt(query->y, coord))
208 which &= (1 << 1);
209 else if (FPgt(query->y, coord))
210 which &= (1 << 2);
211 }
212 break;
215 if ((in->level % 2) == 0 && FPlt(query->y, coord))
216 which &= (1 << 1);
217 break;
220 if ((in->level % 2) == 0 && FPgt(query->y, coord))
221 which &= (1 << 2);
222 break;
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 */
231
232 if ((in->level % 2) != 0)
233 {
234 if (FPlt(boxQuery->high.x, coord))
235 which &= (1 << 1);
236 else if (FPgt(boxQuery->low.x, coord))
237 which &= (1 << 2);
238 }
239 else
240 {
241 if (FPlt(boxQuery->high.y, coord))
242 which &= (1 << 1);
243 else if (FPgt(boxQuery->low.y, coord))
244 which &= (1 << 2);
245 }
246 break;
247 default:
248 elog(ERROR, "unrecognized strategy number: %d",
249 in->scankeys[i].sk_strategy);
250 break;
251 }
252
253 if (which == 0)
254 break; /* no need to consider remaining conditions */
255 }
256
257 /* We must descend into the children identified by which */
258 out->nNodes = 0;
259
260 /* Fast-path for no matching children */
261 if (!which)
263
264 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 if (in->norderbys > 0)
274 {
275 BOX infArea;
276 BOX *area;
277
278 out->distances = palloc_array(double *, in->nNodes);
279 out->traversalValues = palloc_array(void *, in->nNodes);
280
281 if (in->level == 0)
282 {
284
285 infArea.high.x = inf;
286 infArea.high.y = inf;
287 infArea.low.x = -inf;
288 infArea.low.y = -inf;
289 area = &infArea;
290 }
291 else
292 {
293 area = (BOX *) in->traversalValue;
294 Assert(area);
295 }
296
297 bboxes[0].low = area->low;
298 bboxes[1].high = area->high;
299
300 if (in->level % 2)
301 {
302 /* split box by x */
303 bboxes[0].high.x = bboxes[1].low.x = coord;
304 bboxes[0].high.y = area->high.y;
305 bboxes[1].low.y = area->low.y;
306 }
307 else
308 {
309 /* split box by y */
310 bboxes[0].high.y = bboxes[1].low.y = coord;
311 bboxes[0].high.x = area->high.x;
312 bboxes[1].low.x = area->low.x;
313 }
314 }
315
316 for (i = 1; i <= 2; i++)
317 {
318 if (which & (1 << i))
319 {
320 out->nodeNumbers[out->nNodes] = i - 1;
321
322 if (in->norderbys > 0)
323 {
325 BOX *box = box_copy(&bboxes[i - 1]);
326
328
329 out->traversalValues[out->nNodes] = box;
330
332 in->orderbys, in->norderbys);
333 }
334
335 out->nNodes++;
336 }
337 }
338
339 /* Set up level increments, too */
340 out->levelAdds = palloc_array(int, 2);
341 out->levelAdds[0] = 1;
342 out->levelAdds[1] = 1;
343
345}
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 */
#define Assert(condition)
Definition c.h:873
double float8
Definition c.h:644
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define palloc_array(type, count)
Definition fe_memutils.h:76
static float8 get_float8_infinity(void)
Definition float.h:65
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
static bool FPlt(double A, double B)
Definition geo_decls.h:59
static Point * DatumGetPointP(Datum X)
Definition geo_decls.h:175
static Datum PointPGetDatum(const Point *X)
Definition geo_decls.h:180
static BOX * DatumGetBoxP(Datum X)
Definition geo_decls.h:233
static bool FPgt(double A, double B)
Definition geo_decls.h:71
static Datum BoxPGetDatum(const BOX *X)
Definition geo_decls.h:238
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define qsort(a, b, c, d)
Definition port.h:495
static float8 DatumGetFloat8(Datum X)
Definition postgres.h:495
uint64_t Datum
Definition postgres.h:70
static Datum Float8GetDatum(float8 X)
Definition postgres.h:512
static int fb(int x)
@ spgMatchNode
Definition spgist.h:69
Datum spg_kd_inner_consistent(PG_FUNCTION_ARGS)
static int x_cmp(const void *a, const void *b)
Datum spg_kd_config(PG_FUNCTION_ARGS)
Datum spg_kd_choose(PG_FUNCTION_ARGS)
static int y_cmp(const void *a, const void *b)
static int getSide(double coord, bool isX, Point *tst)
Datum spg_kd_picksplit(PG_FUNCTION_ARGS)
BOX * box_copy(BOX *orig)
Definition spgproc.c:82
double * spg_key_orderbys_distances(Datum key, bool isLeaf, ScanKey orderbys, int norderbys)
Definition spgproc.c:63
#define RTLeftStrategyNumber
Definition stratnum.h:51
#define RTRightStrategyNumber
Definition stratnum.h:55
#define RTSameStrategyNumber
Definition stratnum.h:56
#define RTOldBelowStrategyNumber
Definition stratnum.h:79
#define RTBelowStrategyNumber
Definition stratnum.h:60
#define RTOldAboveStrategyNumber
Definition stratnum.h:80
#define RTAboveStrategyNumber
Definition stratnum.h:61
#define RTContainedByStrategyNumber
Definition stratnum.h:58
Point low
Definition geo_decls.h:142
Point high
Definition geo_decls.h:141
float8 y
Definition geo_decls.h:98
float8 x
Definition geo_decls.h:97
Datum sk_argument
Definition skey.h:72
StrategyNumber sk_strategy
Definition skey.h:68
bool hasPrefix
Definition spgist.h:61
Datum prefixDatum
Definition spgist.h:62
int nNodes
Definition spgist.h:63
Datum datum
Definition spgist.h:55
int level
Definition spgist.h:57
bool allTheSame
Definition spgist.h:60
spgChooseResultType resultType
Definition spgist.h:76
int levelAdd
Definition spgist.h:82
Datum restDatum
Definition spgist.h:83
int nodeN
Definition spgist.h:81
union spgChooseOut::@54 result
struct spgChooseOut::@54::@55 matchNode
bool longValuesOK
Definition spgist.h:47
bool canReturnData
Definition spgist.h:46
Oid labelType
Definition spgist.h:44
Oid prefixType
Definition spgist.h:43
void * traversalValue
Definition spgist.h:141
MemoryContext traversalMemoryContext
Definition spgist.h:142
void ** traversalValues
Definition spgist.h:160
double ** distances
Definition spgist.h:161
Datum * datums
Definition spgist.h:113
int * mapTuplesToNodes
Definition spgist.h:125
Datum * nodeLabels
Definition spgist.h:123
Datum * leafTupleDatums
Definition spgist.h:126
Datum prefixDatum
Definition spgist.h:120