Skip to content

Commit 9728eda

Browse files
committed
Fix contrib/pg_trgm's similarity() function for trigram-free strings.
Cases such as similarity('', '') produced a NaN result due to computing 0/0. Per discussion, make it return zero instead. This appears to be the basic cause of bug #7867 from Michele Baravalle, although it remains unclear why her installation doesn't think Cyrillic letters are letters. Back-patch to all active branches.
1 parent cd89965 commit 9728eda

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

contrib/pg_trgm/expected/pg_trgm.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ select similarity('wow',' WOW ');
5353
1
5454
(1 row)
5555

56+
select similarity('---', '####---');
57+
similarity
58+
------------
59+
0
60+
(1 row)
61+
5662
CREATE TABLE test_trgm(t text);
5763
\copy test_trgm from 'data/trgm.data
5864
select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t;

contrib/pg_trgm/sql/pg_trgm.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ select show_trgm('a b C0*%^');
1111
select similarity('wow','WOWa ');
1212
select similarity('wow',' WOW ');
1313

14+
select similarity('---', '####---');
15+
1416
CREATE TABLE test_trgm(t text);
1517

1618
\copy test_trgm from 'data/trgm.data

contrib/pg_trgm/trgm_op.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ cnt_sml(TRGM *trg1, TRGM *trg2)
553553
len1 = ARRNELEM(trg1);
554554
len2 = ARRNELEM(trg2);
555555

556+
/* explicit test is needed to avoid 0/0 division when both lengths are 0 */
557+
if (len1 <= 0 || len2 <= 0)
558+
return (float4) 0.0;
559+
556560
while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
557561
{
558562
int res = CMPTRGM(ptr1, ptr2);
@@ -570,9 +574,9 @@ cnt_sml(TRGM *trg1, TRGM *trg2)
570574
}
571575

572576
#ifdef DIVUNION
573-
return ((((float4) count) / ((float4) (len1 + len2 - count))));
577+
return ((float4) count) / ((float4) (len1 + len2 - count));
574578
#else
575-
return (((float) count) / ((float) ((len1 > len2) ? len1 : len2)));
579+
return ((float4) count) / ((float4) ((len1 > len2) ? len1 : len2));
576580
#endif
577581

578582
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy