Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit04d7fa5

Browse files
committed
Fix contrib/pg_trgm's similarity() function for trigram-free strings.
Cases such as similarity('', '') produced a NaN result due to computing0/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 Cyrillicletters are letters.Back-patch to all active branches.
1 parentae1525f commit04d7fa5

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
CREATETABLEtest_trgm(ttext);
2325

2426
\copy test_trgmfrom'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
intres=CMPTRGM(ptr1,ptr2);
@@ -328,9 +332,9 @@ cnt_sml(TRGM *trg1, TRGM *trg2)
328332
}
329333

330334
#ifdefDIVUNION
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)

[8]ページ先頭

©2009-2025 Movatter.jp