Skip to content

Commit 2101dd1

Browse files
author
Artur Zakirov
committed
fixes for Alvaro Herrera review
1 parent 47f9783 commit 2101dd1

File tree

12 files changed

+293
-218
lines changed

12 files changed

+293
-218
lines changed

contrib/pg_trgm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE_big = pg_trgm
44
OBJS = trgm_op.o trgm_gist.o trgm_gin.o trgm_regexp.o $(WIN32RES)
55

66
EXTENSION = pg_trgm
7-
DATA = pg_trgm--1.3.sql pg_trgm--1.0--1.1.sql pg_trgm--1.1--1.2.sql pg_trgm--1.2--1.3.sql pg_trgm--unpackaged--1.0.sql
7+
DATA = pg_trgm--1.2.sql pg_trgm--1.0--1.1.sql pg_trgm--1.1--1.2.sql pg_trgm--unpackaged--1.0.sql
88
PGFILEDESC = "pg_trgm - trigram matching"
99

1010
REGRESS = pg_trgm pg_substring_trgm

contrib/pg_trgm/expected/pg_substring_trgm.out

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,7 @@ select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where t %> '
503503
Tombankala | 0.6
504504
(17 rows)
505505

506-
select set_substring_limit(0.5);
507-
set_substring_limit
508-
---------------------
509-
0.5
510-
(1 row)
511-
506+
set "pg_trgm.substring_limit" to 0.5;
512507
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t order by sml desc, t;
513508
t | sml
514509
-------------------------------------+----------
@@ -697,12 +692,7 @@ select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where t %> '
697692
Rokba Kala Sardoun | 0.5
698693
(25 rows)
699694

700-
select set_substring_limit(0.3);
701-
set_substring_limit
702-
---------------------
703-
0.3
704-
(1 row)
705-
695+
set "pg_trgm.substring_limit" to 0.3;
706696
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t order by sml desc, t;
707697
t | sml
708698
-----------------------------------------------------------+----------

contrib/pg_trgm/pg_trgm--1.1--1.2.sql

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,47 @@
33
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
44
\echo Use "ALTER EXTENSION pg_trgm UPDATE TO '1.2'" to load this file. \quit
55

6+
CREATE FUNCTION substring_similarity(text,text)
7+
RETURNS float4
8+
AS 'MODULE_PATHNAME'
9+
LANGUAGE C STRICT IMMUTABLE;
10+
11+
CREATE FUNCTION substring_similarity_op(text,text)
12+
RETURNS bool
13+
AS 'MODULE_PATHNAME'
14+
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_substring_limit
15+
16+
CREATE FUNCTION substring_similarity_commutator_op(text,text)
17+
RETURNS bool
18+
AS 'MODULE_PATHNAME'
19+
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_substring_limit
20+
21+
CREATE OPERATOR <% (
22+
LEFTARG = text,
23+
RIGHTARG = text,
24+
PROCEDURE = substring_similarity_op,
25+
COMMUTATOR = '%>',
26+
RESTRICT = contsel,
27+
JOIN = contjoinsel
28+
);
29+
30+
CREATE OPERATOR %> (
31+
LEFTARG = text,
32+
RIGHTARG = text,
33+
PROCEDURE = substring_similarity_commutator_op,
34+
COMMUTATOR = '<%',
35+
RESTRICT = contsel,
36+
JOIN = contjoinsel
37+
);
38+
639
CREATE FUNCTION gin_trgm_triconsistent(internal, int2, text, int4, internal, internal, internal)
740
RETURNS "char"
841
AS 'MODULE_PATHNAME'
942
LANGUAGE C IMMUTABLE STRICT;
1043

44+
ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD
45+
OPERATOR 7 %> (text, text);
46+
1147
ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD
12-
FUNCTION 6 (text, text) gin_trgm_triconsistent (internal, int2, text, int4, internal, internal, internal);
48+
OPERATOR 7 %> (text, text),
49+
FUNCTION 6 (text, text) gin_trgm_triconsistent (internal, int2, text, int4, internal, internal, internal);

contrib/pg_trgm/pg_trgm--1.2--1.3.sql

Lines changed: 0 additions & 53 deletions
This file was deleted.

contrib/pg_trgm/pg_trgm--1.3.sql renamed to contrib/pg_trgm/pg_trgm--1.2.sql

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
/* contrib/pg_trgm/pg_trgm--1.3.sql */
1+
/* contrib/pg_trgm/pg_trgm--1.2.sql */
22

33
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
44
\echo Use "CREATE EXTENSION pg_trgm" to load this file. \quit
55

6+
-- Deprecated function
67
CREATE FUNCTION set_limit(float4)
78
RETURNS float4
89
AS 'MODULE_PATHNAME'
910
LANGUAGE C STRICT VOLATILE;
1011

12+
-- Deprecated function
1113
CREATE FUNCTION show_limit()
1214
RETURNS float4
1315
AS 'MODULE_PATHNAME'
@@ -26,7 +28,7 @@ LANGUAGE C STRICT IMMUTABLE;
2628
CREATE FUNCTION similarity_op(text,text)
2729
RETURNS bool
2830
AS 'MODULE_PATHNAME'
29-
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_limit
31+
LANGUAGE C STRICT STABLE; -- stable because depends on pg_trgm.limit
3032

3133
CREATE OPERATOR % (
3234
LEFTARG = text,
@@ -37,16 +39,6 @@ CREATE OPERATOR % (
3739
JOIN = contjoinsel
3840
);
3941

40-
CREATE FUNCTION set_substring_limit(float4)
41-
RETURNS float4
42-
AS 'MODULE_PATHNAME'
43-
LANGUAGE C STRICT VOLATILE;
44-
45-
CREATE FUNCTION show_substring_limit()
46-
RETURNS float4
47-
AS 'MODULE_PATHNAME'
48-
LANGUAGE C STRICT STABLE;
49-
5042
CREATE FUNCTION substring_similarity(text,text)
5143
RETURNS float4
5244
AS 'MODULE_PATHNAME'
@@ -55,12 +47,12 @@ LANGUAGE C STRICT IMMUTABLE;
5547
CREATE FUNCTION substring_similarity_op(text,text)
5648
RETURNS bool
5749
AS 'MODULE_PATHNAME'
58-
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_substring_limit
50+
LANGUAGE C STRICT STABLE; -- stable because depends on pg_trgm.substring_limit
5951

6052
CREATE FUNCTION substring_similarity_commutator_op(text,text)
6153
RETURNS bool
6254
AS 'MODULE_PATHNAME'
63-
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_substring_limit
55+
LANGUAGE C STRICT STABLE; -- stable because depends on pg_trgm.substring_limit
6456

6557
CREATE OPERATOR <% (
6658
LEFTARG = text,
@@ -179,11 +171,7 @@ ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD
179171

180172
ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD
181173
OPERATOR 5 pg_catalog.~ (text, text),
182-
OPERATOR 6 pg_catalog.~* (text, text);
183-
184-
-- Add operators that are new in 9.6 (pg_trgm 1.3).
185-
186-
ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD
174+
OPERATOR 6 pg_catalog.~* (text, text),
187175
OPERATOR 7 %> (text, text);
188176

189177
-- support functions for gin
@@ -233,9 +221,5 @@ AS 'MODULE_PATHNAME'
233221
LANGUAGE C IMMUTABLE STRICT;
234222

235223
ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD
236-
FUNCTION 6 (text,text) gin_trgm_triconsistent (internal, int2, text, int4, internal, internal, internal);
237-
238-
-- Add operators that are new in 9.6 (pg_trgm 1.3).
239-
240-
ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD
241-
OPERATOR 7 %> (text, text);
224+
OPERATOR 7 %> (text, text),
225+
FUNCTION 6 (text,text) gin_trgm_triconsistent (internal, int2, text, int4, internal, internal, internal);

contrib/pg_trgm/pg_trgm.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_trgm extension
22
comment = 'text similarity measurement and index searching based on trigrams'
3-
default_version = '1.3'
3+
default_version = '1.2'
44
module_pathname = '$libdir/pg_trgm'
55
relocatable = true

contrib/pg_trgm/sql/pg_substring_trgm.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where 'Kaban
2424
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where t %> 'Baykal' order by sml desc, t;
2525
select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where t %> 'Kabankala' order by sml desc, t;
2626

27-
select set_substring_limit(0.5);
27+
set "pg_trgm.substring_limit" to 0.5;
2828
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t order by sml desc, t;
2929
select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where 'Kabankala' <% t order by sml desc, t;
3030
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where t %> 'Baykal' order by sml desc, t;
3131
select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where t %> 'Kabankala' order by sml desc, t;
3232

33-
select set_substring_limit(0.3);
33+
set "pg_trgm.substring_limit" to 0.3;
3434
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t order by sml desc, t;
3535
select t,substring_similarity('Kabankala',t) as sml from test_trgm2 where 'Kabankala' <% t order by sml desc, t;
3636
select t,substring_similarity('Baykal',t) as sml from test_trgm2 where t %> 'Baykal' order by sml desc, t;

contrib/pg_trgm/trgm.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ typedef char *BITVECP;
104104
#define GETARR(x) ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
105105
#define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
106106

107+
/*
108+
* If DIVUNION is defined then similarity formula is:
109+
* count / (len1 + len2 - count)
110+
* else if DIVUNION is not defined then similarity formula is:
111+
* count / max(len1, len2)
112+
*/
107113
#ifdef DIVUNION
108114
#define CALCSML(count, len1, len2) ((float4) (count)) / ((float4) ((len1) + (len2) - (count)))
109115
#else
@@ -112,8 +118,8 @@ typedef char *BITVECP;
112118

113119
typedef struct TrgmPackedGraph TrgmPackedGraph;
114120

115-
extern float4 trgm_limit;
116-
extern float4 trgm_substring_limit;
121+
extern double trgm_limit;
122+
extern double trgm_substring_limit;
117123

118124
extern uint32 trgm2int(trgm *ptr);
119125
extern void compact_trigram(trgm *tptr, char *str, int bytelen);
@@ -126,4 +132,4 @@ extern TRGM *createTrgmNFA(text *text_re, Oid collation,
126132
TrgmPackedGraph **graph, MemoryContext rcontext);
127133
extern bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check);
128134

129-
#endif /* __TRGM_H__ */
135+
#endif /* __TRGM_H__ */

contrib/pg_trgm/trgm_gin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
208208
* similarity is just c / len1.
209209
* So, independly on DIVUNION the upper bound formula is the same.
210210
*/
211-
res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false);
211+
res = (nkeys == 0) ? false : (((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit);
212212
break;
213213
case ILikeStrategyNumber:
214214
#ifndef IGNORECASE
@@ -341,4 +341,4 @@ gin_trgm_triconsistent(PG_FUNCTION_ARGS)
341341
/* All cases served by this function are inexact */
342342
Assert(res != GIN_TRUE);
343343
PG_RETURN_GIN_TERNARY_VALUE(res);
344-
}
344+
}

contrib/pg_trgm/trgm_gist.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
191191
bool res;
192192
Size querysize = VARSIZE(query);
193193
gtrgm_consistent_cache *cache;
194-
float4 nlimit;
194+
double nlimit;
195195

196196
/*
197197
* We keep the extracted trigrams in cache, because trigram extraction is
@@ -290,15 +290,15 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
290290
case SimilarityStrategyNumber:
291291
case SubstringSimilarityStrategyNumber:
292292
/* Similarity search is exact. Substring similarity search is inexact */
293-
*recheck = (strategy == SubstringSimilarityStrategyNumber) ? true : false;
293+
*recheck = (strategy == SubstringSimilarityStrategyNumber);
294294
nlimit = (strategy == SimilarityStrategyNumber) ? trgm_limit : trgm_substring_limit;
295295

296296
if (GIST_LEAF(entry))
297297
{ /* all leafs contains orig trgm */
298298
float4 tmpsml = cnt_sml(qtrg, key, *recheck);
299299

300300
/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
301-
res = (*(int *) &tmpsml == *(int *) &nlimit || tmpsml > nlimit) ? true : false;
301+
res = (*(int *) &tmpsml == *(int *) &nlimit || tmpsml > nlimit);
302302
}
303303
else if (ISALLTRUE(key))
304304
{ /* non-leaf contains signature */
@@ -312,7 +312,7 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
312312
if (len == 0)
313313
res = false;
314314
else
315-
res = (((((float8) count) / ((float8) len))) >= nlimit) ? true : false;
315+
res = (((((float8) count) / ((float8) len))) >= nlimit);
316316
}
317317
break;
318318
case ILikeStrategyNumber:
@@ -941,4 +941,4 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
941941
v->spl_rdatum = PointerGetDatum(datum_r);
942942

943943
PG_RETURN_POINTER(v);
944-
}
944+
}

0 commit comments

Comments
 (0)