summaryrefslogtreecommitdiff
path: root/src/backend/access/gist/gistscan.c
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2019-09-08 21:13:40 +0300
committerAlexander Korotkov <akorotkov@postgresql.org>2019-09-08 21:48:45 +0300
commita5431b7d5f6bcccd1fc69c4e4ae6e4d5f39b83f8 (patch)
treeaa030cf8ffe87178d65735c1403bfd401c80a033 /src/backend/access/gist/gistscan.c
parentb2037afec14725e4521427edbee8071d04057a2d (diff)
Fix handling of NULL distances in KNN-GiST
In order to implement NULL LAST semantic GiST previously assumed distance to the NULL value to be Inf. However, our distance functions can return Inf and NaN for non-null values. In such cases, NULL LAST semantic appears to be broken. This commit fixes that by introducing separate array of null flags for distances. Backpatch to all supported versions. Discussion: https://postgr.es/m/CAPpHfdsNvNdA0DBS%2BwMpFrgwT6C3-q50sFVGLSiuWnV3FqOJuQ%40mail.gmail.com Author: Alexander Korotkov Backpatch-through: 9.4
Diffstat (limited to 'src/backend/access/gist/gistscan.c')
-rw-r--r--src/backend/access/gist/gistscan.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/backend/access/gist/gistscan.c b/src/backend/access/gist/gistscan.c
index 2105ed604be..2fc1d2463f9 100644
--- a/src/backend/access/gist/gistscan.c
+++ b/src/backend/access/gist/gistscan.c
@@ -33,14 +33,30 @@ pairingheap_GISTSearchItem_cmp(const pairingheap_node *a, const pairingheap_node
const GISTSearchItem *sb = (const GISTSearchItem *) b;
IndexScanDesc scan = (IndexScanDesc) arg;
int i;
+ double *da = GISTSearchItemDistanceValues(sa, scan->numberOfOrderBys),
+ *db = GISTSearchItemDistanceValues(sb, scan->numberOfOrderBys);
+ bool *na = GISTSearchItemDistanceNulls(sa, scan->numberOfOrderBys),
+ *nb = GISTSearchItemDistanceNulls(sb, scan->numberOfOrderBys);
/* Order according to distance comparison */
for (i = 0; i < scan->numberOfOrderBys; i++)
{
- int cmp = -float8_cmp_internal(sa->distances[i], sb->distances[i]);
+ if (na[i])
+ {
+ if (!nb[i])
+ return -1;
+ }
+ else if (nb[i])
+ {
+ return 1;
+ }
+ else
+ {
+ int cmp = -float8_cmp_internal(da[i], db[i]);
- if (cmp != 0)
- return cmp;
+ if (cmp != 0)
+ return cmp;
+ }
}
/* Heap items go before inner pages, to ensure a depth-first search */
@@ -84,7 +100,8 @@ gistbeginscan(Relation r, int nkeys, int norderbys)
so->queueCxt = giststate->scanCxt; /* see gistrescan */
/* workspaces with size dependent on numberOfOrderBys: */
- so->distances = palloc(sizeof(double) * scan->numberOfOrderBys);
+ so->distanceValues = palloc(sizeof(double) * scan->numberOfOrderBys);
+ so->distanceNulls = palloc(sizeof(bool) * scan->numberOfOrderBys);
so->qual_ok = true; /* in case there are zero keys */
if (scan->numberOfOrderBys > 0)
{