]> granicus.if.org Git - postgresql/commitdiff
Fix handling Inf and Nan values in GiST pairing heap comparator
authorAlexander Korotkov <akorotkov@postgresql.org>
Sun, 8 Sep 2019 18:07:30 +0000 (21:07 +0300)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sun, 8 Sep 2019 18:48:44 +0000 (21:48 +0300)
Previously plain float comparison was used in GiST pairing heap.  Such
comparison doesn't provide proper ordering for value sets containing Inf and Nan
values.  This commit fixes that by usage of float8_cmp_internal().  Note, there
is remaining problem with NULL distances, which are represented as Inf in
pairing heap.  It would be fixes in subsequent commit.

Backpatch to all supported versions.

Reported-by: Andrey Borodin
Discussion: https://postgr.es/m/CAPpHfdsNvNdA0DBS%2BwMpFrgwT6C3-q50sFVGLSiuWnV3FqOJuQ%40mail.gmail.com
Author: Alexander Korotkov
Reviewed-by: Heikki Linnakangas
Backpatch-through: 9.4

src/backend/access/gist/gistscan.c

index 12c74b982ca8d699ae5804fc8d1636cb3ea90b93..2105ed604be200a3f128ea5edb972af86c29aa26 100644 (file)
@@ -17,6 +17,7 @@
 #include "access/gist_private.h"
 #include "access/gistscan.h"
 #include "access/relscan.h"
+#include "utils/builtins.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
@@ -36,8 +37,10 @@ pairingheap_GISTSearchItem_cmp(const pairingheap_node *a, const pairingheap_node
        /* Order according to distance comparison */
        for (i = 0; i < scan->numberOfOrderBys; i++)
        {
-               if (sa->distances[i] != sb->distances[i])
-                       return (sa->distances[i] < sb->distances[i]) ? 1 : -1;
+               int                     cmp = -float8_cmp_internal(sa->distances[i], sb->distances[i]);
+
+               if (cmp != 0)
+                       return cmp;
        }
 
        /* Heap items go before inner pages, to ensure a depth-first search */