1 /*-------------------------------------------------------------------------
4 * SP-GiST implementation of 4-dimensional quad tree over boxes
6 * This module provides SP-GiST implementation for boxes using quad tree
7 * analogy in 4-dimensional space. SP-GiST doesn't allow indexing of
8 * overlapping objects. We are making 2D objects never-overlapping in
9 * 4D space. This technique has some benefits compared to traditional
10 * R-Tree which is implemented as GiST. The performance tests reveal
11 * that this technique especially beneficial with too much overlapping
12 * objects, so called "spaghetti data".
14 * Unlike the original quad tree, we are splitting the tree into 16
15 * quadrants in 4D space. It is easier to imagine it as splitting space
23 * -------------+-------------
30 * We are using box datatype as the prefix, but we are treating them
31 * as points in 4-dimensional space, because 2D boxes are not enough
32 * to represent the quadrant boundaries in 4D space. They however are
33 * sufficient to point out the additional boundaries of the next
36 * We are using traversal values provided by SP-GiST to calculate and
37 * to store the bounds of the quadrants, while traversing into the tree.
38 * Traversal value has all the boundaries in the 4D space, and is capable
39 * of transferring the required boundaries to the following traversal
40 * values. In conclusion, three things are necessary to calculate the
41 * next traversal value:
43 * (1) the traversal value of the parent
44 * (2) the quadrant of the current node
45 * (3) the prefix of the current node
47 * If we visualize them on our simplified drawing (see the drawing above);
48 * transferred boundaries of (1) would be the outer axis, relevant part
49 * of (2) would be the up right part of the other axis, and (3) would be
52 * For example, consider the case of overlapping. When recursion
53 * descends deeper and deeper down the tree, all quadrants in
54 * the current node will be checked for overlapping. The boundaries
55 * will be re-calculated for all quadrants. Overlap check answers
56 * the question: can any box from this quadrant overlap with the given
57 * box? If yes, then this quadrant will be walked. If no, then this
58 * quadrant will be skipped.
60 * This method provides restrictions for minimum and maximum values of
61 * every dimension of every corner of the box on every level of the tree
62 * except the root. For the root node, we are setting the boundaries
63 * that we don't yet have as infinity.
65 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
66 * Portions Copyright (c) 1994, Regents of the University of California
69 * src/backend/utils/adt/geo_spgist.c
71 *-------------------------------------------------------------------------
76 #include "access/spgist.h"
77 #include "access/stratnum.h"
78 #include "catalog/pg_type.h"
79 #include "utils/float.h"
80 #include "utils/fmgrprotos.h"
81 #include "utils/geo_decls.h"
84 * Comparator for qsort
86 * We don't need to use the floating point macros in here, because this
87 * is only going to be used in a place to effect the performance
88 * of the index, not the correctness.
91 compareDoubles(const void *a, const void *b)
93 float8 x = *(float8 *) a;
94 float8 y = *(float8 *) b;
98 return (x > y) ? 1 : -1;
115 RangeBox range_box_x;
116 RangeBox range_box_y;
120 * Calculate the quadrant
122 * The quadrant is 8 bit unsigned integer with 4 least bits in use.
123 * This function accepts BOXes as input. They are not casted to
124 * RangeBoxes, yet. All 4 bits are set by comparing a corner of the box.
125 * This makes 16 quadrants in total.
128 getQuadrant(BOX *centroid, BOX *inBox)
132 if (inBox->low.x > centroid->low.x)
135 if (inBox->high.x > centroid->high.x)
138 if (inBox->low.y > centroid->low.y)
141 if (inBox->high.y > centroid->high.y)
148 * Get RangeBox using BOX
150 * We are turning the BOX to our structures to emphasize their function
151 * of representing points in 4D space. It also is more convenient to
152 * access the values with this structure.
155 getRangeBox(BOX *box)
157 RangeBox *range_box = (RangeBox *) palloc(sizeof(RangeBox));
159 range_box->left.low = box->low.x;
160 range_box->left.high = box->high.x;
162 range_box->right.low = box->low.y;
163 range_box->right.high = box->high.y;
169 * Initialize the traversal value
171 * In the beginning, we don't have any restrictions. We have to
172 * initialize the struct to cover the whole 4D space.
177 RectBox *rect_box = (RectBox *) palloc(sizeof(RectBox));
178 float8 infinity = get_float8_infinity();
180 rect_box->range_box_x.left.low = -infinity;
181 rect_box->range_box_x.left.high = infinity;
183 rect_box->range_box_x.right.low = -infinity;
184 rect_box->range_box_x.right.high = infinity;
186 rect_box->range_box_y.left.low = -infinity;
187 rect_box->range_box_y.left.high = infinity;
189 rect_box->range_box_y.right.low = -infinity;
190 rect_box->range_box_y.right.high = infinity;
196 * Calculate the next traversal value
198 * All centroids are bounded by RectBox, but SP-GiST only keeps
199 * boxes. When we are traversing the tree, we must calculate RectBox,
200 * using centroid and quadrant.
203 nextRectBox(RectBox *rect_box, RangeBox *centroid, uint8 quadrant)
205 RectBox *next_rect_box = (RectBox *) palloc(sizeof(RectBox));
207 memcpy(next_rect_box, rect_box, sizeof(RectBox));
210 next_rect_box->range_box_x.left.low = centroid->left.low;
212 next_rect_box->range_box_x.left.high = centroid->left.low;
215 next_rect_box->range_box_x.right.low = centroid->left.high;
217 next_rect_box->range_box_x.right.high = centroid->left.high;
220 next_rect_box->range_box_y.left.low = centroid->right.low;
222 next_rect_box->range_box_y.left.high = centroid->right.low;
225 next_rect_box->range_box_y.right.low = centroid->right.high;
227 next_rect_box->range_box_y.right.high = centroid->right.high;
229 return next_rect_box;
232 /* Can any range from range_box overlap with this argument? */
234 overlap2D(RangeBox *range_box, Range *query)
236 return FPge(range_box->right.high, query->low) &&
237 FPle(range_box->left.low, query->high);
240 /* Can any rectangle from rect_box overlap with this argument? */
242 overlap4D(RectBox *rect_box, RangeBox *query)
244 return overlap2D(&rect_box->range_box_x, &query->left) &&
245 overlap2D(&rect_box->range_box_y, &query->right);
248 /* Can any range from range_box contain this argument? */
250 contain2D(RangeBox *range_box, Range *query)
252 return FPge(range_box->right.high, query->high) &&
253 FPle(range_box->left.low, query->low);
256 /* Can any rectangle from rect_box contain this argument? */
258 contain4D(RectBox *rect_box, RangeBox *query)
260 return contain2D(&rect_box->range_box_x, &query->left) &&
261 contain2D(&rect_box->range_box_y, &query->right);
264 /* Can any range from range_box be contained by this argument? */
266 contained2D(RangeBox *range_box, Range *query)
268 return FPle(range_box->left.low, query->high) &&
269 FPge(range_box->left.high, query->low) &&
270 FPle(range_box->right.low, query->high) &&
271 FPge(range_box->right.high, query->low);
274 /* Can any rectangle from rect_box be contained by this argument? */
276 contained4D(RectBox *rect_box, RangeBox *query)
278 return contained2D(&rect_box->range_box_x, &query->left) &&
279 contained2D(&rect_box->range_box_y, &query->right);
282 /* Can any range from range_box to be lower than this argument? */
284 lower2D(RangeBox *range_box, Range *query)
286 return FPlt(range_box->left.low, query->low) &&
287 FPlt(range_box->right.low, query->low);
290 /* Can any range from range_box not extend to the right side of the query? */
292 overLower2D(RangeBox *range_box, Range *query)
294 return FPle(range_box->left.low, query->high) &&
295 FPle(range_box->right.low, query->high);
298 /* Can any range from range_box to be higher than this argument? */
300 higher2D(RangeBox *range_box, Range *query)
302 return FPgt(range_box->left.high, query->high) &&
303 FPgt(range_box->right.high, query->high);
306 /* Can any range from range_box not extend to the left side of the query? */
308 overHigher2D(RangeBox *range_box, Range *query)
310 return FPge(range_box->left.high, query->low) &&
311 FPge(range_box->right.high, query->low);
314 /* Can any rectangle from rect_box be left of this argument? */
316 left4D(RectBox *rect_box, RangeBox *query)
318 return lower2D(&rect_box->range_box_x, &query->left);
321 /* Can any rectangle from rect_box does not extend the right of this argument? */
323 overLeft4D(RectBox *rect_box, RangeBox *query)
325 return overLower2D(&rect_box->range_box_x, &query->left);
328 /* Can any rectangle from rect_box be right of this argument? */
330 right4D(RectBox *rect_box, RangeBox *query)
332 return higher2D(&rect_box->range_box_x, &query->left);
335 /* Can any rectangle from rect_box does not extend the left of this argument? */
337 overRight4D(RectBox *rect_box, RangeBox *query)
339 return overHigher2D(&rect_box->range_box_x, &query->left);
342 /* Can any rectangle from rect_box be below of this argument? */
344 below4D(RectBox *rect_box, RangeBox *query)
346 return lower2D(&rect_box->range_box_y, &query->right);
349 /* Can any rectangle from rect_box does not extend above this argument? */
351 overBelow4D(RectBox *rect_box, RangeBox *query)
353 return overLower2D(&rect_box->range_box_y, &query->right);
356 /* Can any rectangle from rect_box be above of this argument? */
358 above4D(RectBox *rect_box, RangeBox *query)
360 return higher2D(&rect_box->range_box_y, &query->right);
363 /* Can any rectangle from rect_box does not extend below of this argument? */
365 overAbove4D(RectBox *rect_box, RangeBox *query)
367 return overHigher2D(&rect_box->range_box_y, &query->right);
371 * SP-GiST config function
374 spg_box_quad_config(PG_FUNCTION_ARGS)
376 spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
378 cfg->prefixType = BOXOID;
379 cfg->labelType = VOIDOID; /* We don't need node labels. */
380 cfg->canReturnData = true;
381 cfg->longValuesOK = false;
387 * SP-GiST choose function
390 spg_box_quad_choose(PG_FUNCTION_ARGS)
392 spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0);
393 spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1);
394 BOX *centroid = DatumGetBoxP(in->prefixDatum),
395 *box = DatumGetBoxP(in->leafDatum);
397 out->resultType = spgMatchNode;
398 out->result.matchNode.restDatum = BoxPGetDatum(box);
400 /* nodeN will be set by core, when allTheSame. */
402 out->result.matchNode.nodeN = getQuadrant(centroid, box);
408 * SP-GiST pick-split function
410 * It splits a list of boxes into quadrants by choosing a central 4D
411 * point as the median of the coordinates of the boxes.
414 spg_box_quad_picksplit(PG_FUNCTION_ARGS)
416 spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0);
417 spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1);
421 float8 *lowXs = palloc(sizeof(float8) * in->nTuples);
422 float8 *highXs = palloc(sizeof(float8) * in->nTuples);
423 float8 *lowYs = palloc(sizeof(float8) * in->nTuples);
424 float8 *highYs = palloc(sizeof(float8) * in->nTuples);
426 /* Calculate median of all 4D coordinates */
427 for (i = 0; i < in->nTuples; i++)
429 BOX *box = DatumGetBoxP(in->datums[i]);
431 lowXs[i] = box->low.x;
432 highXs[i] = box->high.x;
433 lowYs[i] = box->low.y;
434 highYs[i] = box->high.y;
437 qsort(lowXs, in->nTuples, sizeof(float8), compareDoubles);
438 qsort(highXs, in->nTuples, sizeof(float8), compareDoubles);
439 qsort(lowYs, in->nTuples, sizeof(float8), compareDoubles);
440 qsort(highYs, in->nTuples, sizeof(float8), compareDoubles);
442 median = in->nTuples / 2;
444 centroid = palloc(sizeof(BOX));
446 centroid->low.x = lowXs[median];
447 centroid->high.x = highXs[median];
448 centroid->low.y = lowYs[median];
449 centroid->high.y = highYs[median];
451 /* Fill the output */
452 out->hasPrefix = true;
453 out->prefixDatum = BoxPGetDatum(centroid);
456 out->nodeLabels = NULL; /* We don't need node labels. */
458 out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples);
459 out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples);
462 * Assign ranges to corresponding nodes according to quadrants relative to
463 * the "centroid" range
465 for (i = 0; i < in->nTuples; i++)
467 BOX *box = DatumGetBoxP(in->datums[i]);
468 uint8 quadrant = getQuadrant(centroid, box);
470 out->leafTupleDatums[i] = BoxPGetDatum(box);
471 out->mapTuplesToNodes[i] = quadrant;
478 * Check if result of consistent method based on bounding box is exact.
481 is_bounding_box_test_exact(StrategyNumber strategy)
485 case RTLeftStrategyNumber:
486 case RTOverLeftStrategyNumber:
487 case RTOverRightStrategyNumber:
488 case RTRightStrategyNumber:
489 case RTOverBelowStrategyNumber:
490 case RTBelowStrategyNumber:
491 case RTAboveStrategyNumber:
492 case RTOverAboveStrategyNumber:
501 * Get bounding box for ScanKey.
504 spg_box_quad_get_scankey_bbox(ScanKey sk, bool *recheck)
506 switch (sk->sk_subtype)
509 return DatumGetBoxP(sk->sk_argument);
512 if (recheck && !is_bounding_box_test_exact(sk->sk_strategy))
514 return &DatumGetPolygonP(sk->sk_argument)->boundbox;
517 elog(ERROR, "unrecognized scankey subtype: %d", sk->sk_subtype);
523 * SP-GiST inner consistent function
526 spg_box_quad_inner_consistent(PG_FUNCTION_ARGS)
528 spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0);
529 spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1);
531 MemoryContext old_ctx;
539 /* Report that all nodes should be visited */
540 out->nNodes = in->nNodes;
541 out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
542 for (i = 0; i < in->nNodes; i++)
543 out->nodeNumbers[i] = i;
549 * We are saving the traversal value or initialize it an unbounded one, if
550 * we have just begun to walk the tree.
552 if (in->traversalValue)
553 rect_box = in->traversalValue;
555 rect_box = initRectBox();
558 * We are casting the prefix and queries to RangeBoxes for ease of the
559 * following operations.
561 centroid = getRangeBox(DatumGetBoxP(in->prefixDatum));
562 queries = (RangeBox **) palloc(in->nkeys * sizeof(RangeBox *));
563 for (i = 0; i < in->nkeys; i++)
565 BOX *box = spg_box_quad_get_scankey_bbox(&in->scankeys[i], NULL);
567 queries[i] = getRangeBox(box);
570 /* Allocate enough memory for nodes */
572 out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
573 out->traversalValues = (void **) palloc(sizeof(void *) * in->nNodes);
576 * We switch memory context, because we want to allocate memory for new
577 * traversal values (next_rect_box) and pass these pieces of memory to
578 * further call of this function.
580 old_ctx = MemoryContextSwitchTo(in->traversalMemoryContext);
582 for (quadrant = 0; quadrant < in->nNodes; quadrant++)
584 RectBox *next_rect_box = nextRectBox(rect_box, centroid, quadrant);
587 for (i = 0; i < in->nkeys; i++)
589 StrategyNumber strategy = in->scankeys[i].sk_strategy;
593 case RTOverlapStrategyNumber:
594 flag = overlap4D(next_rect_box, queries[i]);
597 case RTContainsStrategyNumber:
598 flag = contain4D(next_rect_box, queries[i]);
601 case RTSameStrategyNumber:
602 case RTContainedByStrategyNumber:
603 flag = contained4D(next_rect_box, queries[i]);
606 case RTLeftStrategyNumber:
607 flag = left4D(next_rect_box, queries[i]);
610 case RTOverLeftStrategyNumber:
611 flag = overLeft4D(next_rect_box, queries[i]);
614 case RTRightStrategyNumber:
615 flag = right4D(next_rect_box, queries[i]);
618 case RTOverRightStrategyNumber:
619 flag = overRight4D(next_rect_box, queries[i]);
622 case RTAboveStrategyNumber:
623 flag = above4D(next_rect_box, queries[i]);
626 case RTOverAboveStrategyNumber:
627 flag = overAbove4D(next_rect_box, queries[i]);
630 case RTBelowStrategyNumber:
631 flag = below4D(next_rect_box, queries[i]);
634 case RTOverBelowStrategyNumber:
635 flag = overBelow4D(next_rect_box, queries[i]);
639 elog(ERROR, "unrecognized strategy: %d", strategy);
642 /* If any check is failed, we have found our answer. */
649 out->traversalValues[out->nNodes] = next_rect_box;
650 out->nodeNumbers[out->nNodes] = quadrant;
656 * If this node is not selected, we don't need to keep the next
657 * traversal value in the memory context.
659 pfree(next_rect_box);
664 MemoryContextSwitchTo(old_ctx);
670 * SP-GiST inner consistent function
673 spg_box_quad_leaf_consistent(PG_FUNCTION_ARGS)
675 spgLeafConsistentIn *in = (spgLeafConsistentIn *) PG_GETARG_POINTER(0);
676 spgLeafConsistentOut *out = (spgLeafConsistentOut *) PG_GETARG_POINTER(1);
677 Datum leaf = in->leafDatum;
681 /* All tests are exact. */
682 out->recheck = false;
684 /* leafDatum is what it is... */
685 out->leafValue = in->leafDatum;
687 /* Perform the required comparison(s) */
688 for (i = 0; i < in->nkeys; i++)
690 StrategyNumber strategy = in->scankeys[i].sk_strategy;
691 BOX *box = spg_box_quad_get_scankey_bbox(&in->scankeys[i],
693 Datum query = BoxPGetDatum(box);
697 case RTOverlapStrategyNumber:
698 flag = DatumGetBool(DirectFunctionCall2(box_overlap, leaf,
702 case RTContainsStrategyNumber:
703 flag = DatumGetBool(DirectFunctionCall2(box_contain, leaf,
707 case RTContainedByStrategyNumber:
708 flag = DatumGetBool(DirectFunctionCall2(box_contained, leaf,
712 case RTSameStrategyNumber:
713 flag = DatumGetBool(DirectFunctionCall2(box_same, leaf,
717 case RTLeftStrategyNumber:
718 flag = DatumGetBool(DirectFunctionCall2(box_left, leaf,
722 case RTOverLeftStrategyNumber:
723 flag = DatumGetBool(DirectFunctionCall2(box_overleft, leaf,
727 case RTRightStrategyNumber:
728 flag = DatumGetBool(DirectFunctionCall2(box_right, leaf,
732 case RTOverRightStrategyNumber:
733 flag = DatumGetBool(DirectFunctionCall2(box_overright, leaf,
737 case RTAboveStrategyNumber:
738 flag = DatumGetBool(DirectFunctionCall2(box_above, leaf,
742 case RTOverAboveStrategyNumber:
743 flag = DatumGetBool(DirectFunctionCall2(box_overabove, leaf,
747 case RTBelowStrategyNumber:
748 flag = DatumGetBool(DirectFunctionCall2(box_below, leaf,
752 case RTOverBelowStrategyNumber:
753 flag = DatumGetBool(DirectFunctionCall2(box_overbelow, leaf,
758 elog(ERROR, "unrecognized strategy: %d", strategy);
761 /* If any check is failed, we have found our answer. */
766 PG_RETURN_BOOL(flag);
771 * SP-GiST config function for 2-D types that are lossy represented by their
775 spg_bbox_quad_config(PG_FUNCTION_ARGS)
777 spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
779 cfg->prefixType = BOXOID; /* A type represented by its bounding box */
780 cfg->labelType = VOIDOID; /* We don't need node labels. */
781 cfg->leafType = BOXOID;
782 cfg->canReturnData = false;
783 cfg->longValuesOK = false;
789 * SP-GiST compress function for polygons
792 spg_poly_quad_compress(PG_FUNCTION_ARGS)
794 POLYGON *polygon = PG_GETARG_POLYGON_P(0);
797 box = (BOX *) palloc(sizeof(BOX));
798 *box = polygon->boundbox;
800 PG_RETURN_BOX_P(box);