8000 Fix contrib/pg_trgm's similarity() function for trigram-free strings. · commandprompt/postgres@04d7fa5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04d7fa5

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 ae1525f commit 04d7fa5

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
@@ -59,6 +59,12 @@ select similarity('wow',' WOW ');
5959
1
6060
(1 row)
6161

62+
select similarity('---', '####---');
63+
similarity
64+
------------
65+
0
66+
(1 row)
67+
6268
CREATE TABLE test_trgm(t text);
6369
\copy test_trgm from 'data/trgm.data
6470
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
@@ -19,6 +19,8 @@ select show_trgm('a b C0*%^');
1919
select similarity('wow','WOWa ');
2020
select similarity('wow',' WOW ');
2121

22+
select similarity('---', '####---');
23+
2224
CREATE TABLE test_trgm(t text);
2325

2426
\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
@@ -311,6 +311,10 @@ cnt_sml(TRGM *trg1, TRGM *trg2)
311311
len1 = ARRNELEM(trg1);
312312
len2 = ARRNELEM(trg2);
313313

314+
/* explicit test is needed to avoid 0/0 division when both lengths are 0 */
315+
if (len1 <= 0 || len2 <= 0)
316+
return (float4) 0.0;
317+
314318
while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
315319
{
316320
int res = CMPTRGM(ptr1, ptr2);
@@ -328,9 +332,9 @@ cnt_sml(TRGM *trg1, TRGM *trg2)
328332
}
329333

330334
#ifdef DIVUNION
331-
return ((((float4) count) / ((float4) (len1 + len2 - count))));
335+
return ((float4) count) / ((float4) (len1 + len2 - count));
332336
#else
333-
return (((float) count) / ((float) ((len1 > len2) ? len1 : len2)));
337+
return ((float4) count) / ((float4) ((len1 > len2) ? len1 : len2));
334338
#endif
335339

336340
}

0 commit comments

Comments
 (0)
0