summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Korotkov2019-09-08 18:07:30 +0000
committerAlexander Korotkov2019-09-08 19:08:12 +0000
commite5d8f3596100da0d38a38513c69e803b7fe7041a (patch)
tree398f1d0a3e6018db49c6964b36a89e3e30f7fe94
parent862ef372d6b23629f93d4afc123ddd7d172501ac (diff)
Fix handling Inf and Nan values in GiST pairing heap comparator
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
-rw-r--r--src/backend/access/gist/gistscan.c7
-rw-r--r--src/test/regress/expected/create_index.out10
2 files changed, 10 insertions, 7 deletions
diff --git a/src/backend/access/gist/gistscan.c b/src/backend/access/gist/gistscan.c
index 893d7765b6c..636aa62b680 100644
--- a/src/backend/access/gist/gistscan.c
+++ b/src/backend/access/gist/gistscan.c
@@ -17,6 +17,7 @@
#include "access/gist_private.h"
#include "access/gistscan.h"
#include "access/relscan.h"
+#include "utils/float.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 */
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index c6d575a2f99..117b11a1a63 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -523,16 +523,16 @@ SELECT * FROM point_tbl ORDER BY f1 <-> '0,1';
SELECT * FROM point_tbl ORDER BY f1 <-> '0,1';
f1
-------------------
- (10,10)
- (NaN,NaN)
(0,0)
(1e-300,-1e-300)
(-3,4)
(-10,0)
+ (10,10)
(-5,-12)
(5.1,34.5)
-
(1e+300,Infinity)
+
+ (NaN,NaN)
(10 rows)
EXPLAIN (COSTS OFF)
@@ -561,15 +561,15 @@ SELECT * FROM point_tbl WHERE f1 IS NOT NULL ORDER BY f1 <-> '0,1';
SELECT * FROM point_tbl WHERE f1 IS NOT NULL ORDER BY f1 <-> '0,1';
f1
-------------------
- (10,10)
- (NaN,NaN)
(0,0)
(1e-300,-1e-300)
(-3,4)
(-10,0)
+ (10,10)
(-5,-12)
(5.1,34.5)
(1e+300,Infinity)
+ (NaN,NaN)
(9 rows)
EXPLAIN (COSTS OFF)