Skip to content

Commit bc67f41

Browse files
committed
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
1 parent b6d72dd commit bc67f41

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/backend/access/gist/gistscan.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "access/gist_private.h"
1818
#include "access/gistscan.h"
1919
#include "access/relscan.h"
20+
#include "utils/float.h"
2021
#include "utils/lsyscache.h"
2122
#include "utils/memutils.h"
2223
#include "utils/rel.h"
@@ -36,8 +37,10 @@ pairingheap_GISTSearchItem_cmp(const pairingheap_node *a, const pairingheap_node
3637
/* Order according to distance comparison */
3738
for (i = 0; i < scan->numberOfOrderBys; i++)
3839
{
39-
if (sa->distances[i] != sb->distances[i])
40-
return (sa->distances[i] < sb->distances[i]) ? 1 : -1;
40+
int cmp = -float8_cmp_internal(sa->distances[i], sb->distances[i]);
41+
42+
if (cmp != 0)
43+
return cmp;
4144
}
4245

4346
/* Heap items go before inner pages, to ensure a depth-first search */

src/test/regress/expected/create_index.out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,16 @@ SELECT * FROM point_tbl ORDER BY f1 <-> '0,1';
523523
SELECT * FROM point_tbl ORDER BY f1 <-> '0,1';
524524
f1
525525
-------------------
526-
(10,10)
527-
(NaN,NaN)
528526
(0,0)
529527
(1e-300,-1e-300)
530528
(-3,4)
531529
(-10,0)
530+
(10,10)
532531
(-5,-12)
533532
(5.1,34.5)
534-
535533
(1e+300,Infinity)
534+
535+
(NaN,NaN)
536536
(10 rows)
537537

538538
EXPLAIN (COSTS OFF)
@@ -561,15 +561,15 @@ SELECT * FROM point_tbl WHERE f1 IS NOT NULL ORDER BY f1 <-> '0,1';
561561
SELECT * FROM point_tbl WHERE f1 IS NOT NULL ORDER BY f1 <-> '0,1';
562562
f1
563563
-------------------
564-
(10,10)
565-
(NaN,NaN)
566564
(0,0)
567565
(1e-300,-1e-300)
568566
(-3,4)
569567
(-10,0)
568+
(10,10)
570569
(-5,-12)
571570
(5.1,34.5)
572571
(1e+300,Infinity)
572+
(NaN,NaN)
573573
(9 rows)
574574

575575
EXPLAIN (COSTS OFF)

0 commit comments

Comments
 (0)