| 1 |
CREATE EXTENSION pg_trgm; |
| 2 |
|
| 3 |
-- Check whether any of our opclasses fail amvalidate |
| 4 |
SELECT amname, opcname |
| 5 |
FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod |
| 6 |
WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid); |
| 7 |
|
| 8 |
--backslash is used in tests below, installcheck will fail if |
| 9 |
--standard_conforming_string is off |
| 10 |
set standard_conforming_strings=on; |
| 11 |
|
| 12 |
-- reduce noise |
| 13 |
set extra_float_digits = 0; |
| 14 |
|
| 15 |
select show_trgm(''); |
| 16 |
select show_trgm('(*&^$@%@'); |
| 17 |
select show_trgm('a b c'); |
| 18 |
select show_trgm(' a b c '); |
| 19 |
select show_trgm('aA bB cC'); |
| 20 |
select show_trgm(' aA bB cC '); |
| 21 |
select show_trgm('a b C0*%^'); |
| 22 |
|
| 23 |
select similarity('wow','WOWa '); |
| 24 |
select similarity('wow',' WOW '); |
| 25 |
|
| 26 |
select similarity('---', '####---'); |
| 27 |
|
| 28 |
CREATE TABLE test_trgm(t text COLLATE "C"); |
| 29 |
|
| 30 |
\copy test_trgm from 'data/trgm.data' |
| 31 |
|
| 32 |
select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t; |
| 33 |
select t,similarity(t,'gwertyu0988') as sml from test_trgm where t % 'gwertyu0988' order by sml desc, t; |
| 34 |
select t,similarity(t,'gwertyu1988') as sml from test_trgm where t % 'gwertyu1988' order by sml desc, t; |
| 35 |
select t <-> 'q0987wertyu0988', t from test_trgm order by t <-> 'q0987wertyu0988' limit 2; |
| 36 |
select count(*) from test_trgm where t ~ '[qwerty]{2}-?[qwerty]{2}'; |
| 37 |
|
| 38 |
create index trgm_idx on test_trgm using gist (t gist_trgm_ops); |
| 39 |
set enable_seqscan=off; |
| 40 |
|
| 41 |
select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t; |
| 42 |
select t,similarity(t,'gwertyu0988') as sml from test_trgm where t % 'gwertyu0988' order by sml desc, t; |
| 43 |
select t,similarity(t,'gwertyu1988') as sml from test_trgm where t % 'gwertyu1988' order by sml desc, t; |
| 44 |
explain (costs off) |
| 45 |
select t <-> 'q0987wertyu0988', t from test_trgm order by t <-> 'q0987wertyu0988' limit 2; |
| 46 |
select t <-> 'q0987wertyu0988', t from test_trgm order by t <-> 'q0987wertyu0988' limit 2; |
| 47 |
select count(*) from test_trgm where t ~ '[qwerty]{2}-?[qwerty]{2}'; |
| 48 |
|
| 49 |
drop index trgm_idx; |
| 50 |
create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=0)); |
| 51 |
create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=2025)); |
| 52 |
create index trgm_idx on test_trgm using gist (t gist_trgm_ops(siglen=2024)); |
| 53 |
set enable_seqscan=off; |
| 54 |
|
| 55 |
-- check index compatibility handling when opclass option is specified |
| 56 |
alter table test_trgm alter column t type varchar(768); |
| 57 |
alter table test_trgm alter column t type text; |
| 58 |
|
| 59 |
select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t; |
| 60 |
select t,similarity(t,'gwertyu0988') as sml from test_trgm where t % 'gwertyu0988' order by sml desc, t; |
| 61 |
select t,similarity(t,'gwertyu1988') as sml from test_trgm where t % 'gwertyu1988' order by sml desc, t; |
| 62 |
explain (costs off) |
| 63 |
select t <-> 'q0987wertyu0988', t from test_trgm order by t <-> 'q0987wertyu0988' limit 2; |
| 64 |
select t <-> 'q0987wertyu0988', t from test_trgm order by t <-> 'q0987wertyu0988' limit 2; |
| 65 |
select count(*) from test_trgm where t ~ '[qwerty]{2}-?[qwerty]{2}'; |
| 66 |
|
| 67 |
drop index trgm_idx; |
| 68 |
create index trgm_idx on test_trgm using gin (t gin_trgm_ops); |
| 69 |
set enable_seqscan=off; |
| 70 |
|
| 71 |
select t,similarity(t,'qwertyu0988') as sml from test_trgm where t % 'qwertyu0988' order by sml desc, t; |
| 72 |
select t,similarity(t,'gwertyu0988') as sml from test_trgm where t % 'gwertyu0988' order by sml desc, t; |
| 73 |
select t,similarity(t,'gwertyu1988') as sml from test_trgm where t % 'gwertyu1988' order by sml desc, t; |
| 74 |
select count(*) from test_trgm where t ~ '[qwerty]{2}-?[qwerty]{2}'; |
| 75 |
|
| 76 |
-- check handling of indexquals that generate no searchable conditions |
| 77 |
explain (costs off) |
| 78 |
select count(*) from test_trgm where t like '%99%' and t like '%qwerty%'; |
| 79 |
select count(*) from test_trgm where t like '%99%' and t like '%qwerty%'; |
| 80 |
explain (costs off) |
| 81 |
select count(*) from test_trgm where t like '%99%' and t like '%qw%'; |
| 82 |
select count(*) from test_trgm where t like '%99%' and t like '%qw%'; |
| 83 |
explain (costs off) |
| 84 |
select count(*) from test_trgm where t %> '' and t %> '%qwerty%'; |
| 85 |
select count(*) from test_trgm where t %> '' and t %> '%qwerty%'; |
| 86 |
-- ensure that pending-list items are handled correctly, too |
| 87 |
create temp table t_test_trgm(t text COLLATE "C"); |
| 88 |
create index t_trgm_idx on t_test_trgm using gin (t gin_trgm_ops); |
| 89 |
insert into t_test_trgm values ('qwerty99'), ('qwerty01'); |
| 90 |
explain (costs off) |
| 91 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qwerty%'; |
| 92 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qwerty%'; |
| 93 |
explain (costs off) |
| 94 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qw%'; |
| 95 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qw%'; |
| 96 |
explain (costs off) |
| 97 |
select count(*) from t_test_trgm where t %> '' and t %> '%qwerty%'; |
| 98 |
select count(*) from t_test_trgm where t %> '' and t %> '%qwerty%'; |
| 99 |
|
| 100 |
-- run the same queries with sequential scan to check the results |
| 101 |
set enable_bitmapscan=off; |
| 102 |
set enable_seqscan=on; |
| 103 |
select count(*) from test_trgm where t like '%99%' and t like '%qwerty%'; |
| 104 |
select count(*) from test_trgm where t like '%99%' and t like '%qw%'; |
| 105 |
select count(*) from test_trgm where t %> '' and t %> '%qwerty%'; |
| 106 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qwerty%'; |
| 107 |
select count(*) from t_test_trgm where t like '%99%' and t like '%qw%'; |
| 108 |
select count(*) from t_test_trgm where t %> '' and t %> '%qwerty%'; |
| 109 |
reset enable_bitmapscan; |
| 110 |
|
| 111 |
create table test2(t text COLLATE "C"); |
| 112 |
insert into test2 values ('abcdef'); |
| 113 |
insert into test2 values ('quark'); |
| 114 |
insert into test2 values (' z foo bar'); |
| 115 |
insert into test2 values ('/123/-45/'); |
| 116 |
insert into test2 values ('line 1'); |
| 117 |
insert into test2 values ('%line 2'); |
| 118 |
insert into test2 values ('line 3%'); |
| 119 |
insert into test2 values ('%line 4%'); |
| 120 |
insert into test2 values ('%li%ne 5%'); |
| 121 |
insert into test2 values ('li_e 6'); |
| 122 |
create index test2_idx_gin on test2 using gin (t gin_trgm_ops); |
| 123 |
set enable_seqscan=off; |
| 124 |
explain (costs off) |
| 125 |
select * from test2 where t like '%BCD%'; |
| 126 |
explain (costs off) |
| 127 |
select * from test2 where t ilike '%BCD%'; |
| 128 |
select * from test2 where t like '%BCD%'; |
| 129 |
select * from test2 where t like '%bcd%'; |
| 130 |
select * from test2 where t like E'%\\bcd%'; |
| 131 |
select * from test2 where t ilike '%BCD%'; |
| 132 |
select * from test2 where t ilike 'qua%'; |
| 133 |
select * from test2 where t like '%z foo bar%'; |
| 134 |
select * from test2 where t like ' z foo%'; |
| 135 |
explain (costs off) |
| 136 |
select * from test2 where t ~ '[abc]{3}'; |
| 137 |
explain (costs off) |
| 138 |
select * from test2 where t ~* 'DEF'; |
| 139 |
select * from test2 where t ~ '[abc]{3}'; |
| 140 |
select * from test2 where t ~ 'a[bc]+d'; |
| 141 |
select * from test2 where t ~ '(abc)*$'; |
| 142 |
select * from test2 where t ~* 'DEF'; |
| 143 |
select * from test2 where t ~ 'dEf'; |
| 144 |
select * from test2 where t ~* '^q'; |
| 145 |
select * from test2 where t ~* '[abc]{3}[def]{3}'; |
| 146 |
select * from test2 where t ~* 'ab[a-z]{3}'; |
| 147 |
select * from test2 where t ~* '(^| )qua'; |
| 148 |
select * from test2 where t ~ 'q.*rk$'; |
| 149 |
select * from test2 where t ~ 'q'; |
| 150 |
select * from test2 where t ~ '[a-z]{3}'; |
| 151 |
select * from test2 where t ~* '(a{10}|b{10}|c{10}){10}'; |
| 152 |
select * from test2 where t ~ 'z foo bar'; |
| 153 |
select * from test2 where t ~ ' z foo bar'; |
| 154 |
select * from test2 where t ~ ' z foo bar'; |
| 155 |
select * from test2 where t ~ ' z foo'; |
| 156 |
select * from test2 where t ~ 'qua(?!foo)'; |
| 157 |
select * from test2 where t ~ '/\d+/-\d'; |
| 158 |
-- test = operator |
| 159 |
explain (costs off) |
| 160 |
select * from test2 where t = 'abcdef'; |
| 161 |
select * from test2 where t = 'abcdef'; |
| 162 |
explain (costs off) |
| 163 |
select * from test2 where t = '%line%'; |
| 164 |
select * from test2 where t = '%line%'; |
| 165 |
select * from test2 where t = 'li_e 1'; |
| 166 |
select * from test2 where t = '%line 2'; |
| 167 |
select * from test2 where t = 'line 3%'; |
| 168 |
select * from test2 where t = '%line 3%'; |
| 169 |
select * from test2 where t = '%line 4%'; |
| 170 |
select * from test2 where t = '%line 5%'; |
| 171 |
select * from test2 where t = '%li_ne 5%'; |
| 172 |
select * from test2 where t = '%li%ne 5%'; |
| 173 |
select * from test2 where t = 'line 6'; |
| 174 |
select * from test2 where t = 'li_e 6'; |
| 175 |
drop index test2_idx_gin; |
| 176 |
|
| 177 |
create index test2_idx_gist on test2 using gist (t gist_trgm_ops); |
| 178 |
set enable_seqscan=off; |
| 179 |
explain (costs off) |
| 180 |
select * from test2 where t like '%BCD%'; |
| 181 |
explain (costs off) |
| 182 |
select * from test2 where t ilike '%BCD%'; |
| 183 |
select * from test2 where t like '%BCD%'; |
| 184 |
select * from test2 where t like '%bcd%'; |
| 185 |
select * from test2 where t like E'%\\bcd%'; |
| 186 |
select * from test2 where t ilike '%BCD%'; |
| 187 |
select * from test2 where t ilike 'qua%'; |
| 188 |
select * from test2 where t like '%z foo bar%'; |
| 189 |
select * from test2 where t like ' z foo%'; |
| 190 |
explain (costs off) |
| 191 |
select * from test2 where t ~ '[abc]{3}'; |
| 192 |
explain (costs off) |
| 193 |
select * from test2 where t ~* 'DEF'; |
| 194 |
select * from test2 where t ~ '[abc]{3}'; |
| 195 |
select * from test2 where t ~ 'a[bc]+d'; |
| 196 |
select * from test2 where t ~ '(abc)*$'; |
| 197 |
select * from test2 where t ~* 'DEF'; |
| 198 |
select * from test2 where t ~ 'dEf'; |
| 199 |
select * from test2 where t ~* '^q'; |
| 200 |
select * from test2 where t ~* '[abc]{3}[def]{3}'; |
| 201 |
select * from test2 where t ~* 'ab[a-z]{3}'; |
| 202 |
select * from test2 where t ~* '(^| )qua'; |
| 203 |
select * from test2 where t ~ 'q.*rk$'; |
| 204 |
select * from test2 where t ~ 'q'; |
| 205 |
select * from test2 where t ~ '[a-z]{3}'; |
| 206 |
select * from test2 where t ~* '(a{10}|b{10}|c{10}){10}'; |
| 207 |
select * from test2 where t ~ 'z foo bar'; |
| 208 |
select * from test2 where t ~ ' z foo bar'; |
| 209 |
select * from test2 where t ~ ' z foo bar'; |
| 210 |
select * from test2 where t ~ ' z foo'; |
| 211 |
select * from test2 where t ~ 'qua(?!foo)'; |
| 212 |
select * from test2 where t ~ '/\d+/-\d'; |
| 213 |
-- test = operator |
| 214 |
explain (costs off) |
| 215 |
select * from test2 where t = 'abcdef'; |
| 216 |
select * from test2 where t = 'abcdef'; |
| 217 |
explain (costs off) |
| 218 |
select * from test2 where t = '%line%'; |
| 219 |
select * from test2 where t = '%line%'; |
| 220 |
select * from test2 where t = 'li_e 1'; |
| 221 |
select * from test2 where t = '%line 2'; |
| 222 |
select * from test2 where t = 'line 3%'; |
| 223 |
select * from test2 where t = '%line 3%'; |
| 224 |
select * from test2 where t = '%line 4%'; |
| 225 |
select * from test2 where t = '%line 5%'; |
| 226 |
select * from test2 where t = '%li_ne 5%'; |
| 227 |
select * from test2 where t = '%li%ne 5%'; |
| 228 |
select * from test2 where t = 'line 6'; |
| 229 |
select * from test2 where t = 'li_e 6'; |
| 230 |
|
| 231 |
-- Check similarity threshold (bug #14202) |
| 232 |
|
| 233 |
CREATE TEMP TABLE restaurants (city text); |
| 234 |
INSERT INTO restaurants SELECT 'Warsaw' FROM generate_series(1, 10000); |
| 235 |
INSERT INTO restaurants SELECT 'Szczecin' FROM generate_series(1, 10000); |
| 236 |
CREATE INDEX ON restaurants USING gist(city gist_trgm_ops); |
| 237 |
|
| 238 |
-- Similarity of the two names (for reference). |
| 239 |
SELECT similarity('Szczecin', 'Warsaw'); |
| 240 |
|
| 241 |
-- Should get only 'Warsaw' for either setting of set_limit. |
| 242 |
EXPLAIN (COSTS OFF) |
| 243 |
SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() |
| 244 |
FROM restaurants WHERE city % 'Warsaw'; |
| 245 |
SELECT set_limit(0.3); |
| 246 |
SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() |
| 247 |
FROM restaurants WHERE city % 'Warsaw'; |
| 248 |
SELECT set_limit(0.5); |
| 249 |
SELECT DISTINCT city, similarity(city, 'Warsaw'), show_limit() |
| 250 |
FROM restaurants WHERE city % 'Warsaw'; |