]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/strings.sql
9ab6c218728ac6b222edacdb14836184a5efd58d
[postgresql] / src / test / regress / sql / strings.sql
1 --
2 -- STRINGS
3 -- Test various data entry syntaxes.
4 --
5
6 -- SQL92 string continuation syntax
7 -- E021-03 character string literals
8 SELECT 'first line'
9 ' - next line'
10         ' - third line'
11         AS "Three lines to one";
12
13 -- illegal string continuation syntax
14 SELECT 'first line'
15 ' - next line' /* this comment is not allowed here */
16 ' - third line'
17         AS "Illegal comment within continuation";
18
19 --
20 -- test conversions between various string types
21 -- E021-10 implicit casting among the character data types
22 --
23
24 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
25
26 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
27
28 SELECT CAST(name 'namefield' AS text) AS "text(name)";
29
30 -- since this is an explicit cast, it should truncate w/o error:
31 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
32 -- note: implicit-cast case is tested in char.sql
33
34 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
35
36 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
37
38 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
39
40 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
41
42 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
43
44 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
45
46 --
47 -- test SQL92 string functions
48 -- E### and T### are feature reference numbers from SQL99
49 --
50
51 -- E021-09 trim function
52 SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
53
54 SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
55
56 SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
57
58 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
59
60 -- E021-06 substring expression
61 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
62
63 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
64
65 -- T581 regular expression substring (with SQL99's bizarre regexp syntax)
66 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
67
68 -- No match should return NULL
69 SELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True";
70
71 -- Null inputs should return NULL
72 SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True";
73 SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True";
74 SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True";
75
76 -- PostgreSQL extension to allow omitting the escape character;
77 -- here the regexp is taken as Posix syntax
78 SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
79
80 -- With a parenthesized subexpression, return only what matches the subexpr
81 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
82
83 -- PostgreSQL extension to allow using back reference in replace string;
84 SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
85 SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
86 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
87 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
88 -- invalid regexp option
89 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
90
91 -- set so we can tell NULL from empty string
92 \pset null '\\N'
93
94 -- return all matches from regexp
95 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
96
97 -- test case insensitive
98 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
99
100 -- global option - more than one match
101 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
102
103 -- empty capture group (matched empty string)
104 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
105 -- no match
106 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
107 -- optional capture group did not match, null entry in array
108 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
109
110 -- no capture groups
111 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
112
113 -- give me errors
114 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
115 SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
116 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
117
118 -- split string on regexp
119 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', $re$\s+$re$) AS foo;
120 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', $re$\s+$re$);
121
122 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', $re$\s*$re$) AS foo;
123 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', $re$\s*$re$);
124 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', '') AS foo;
125 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', '');
126 -- case insensitive
127 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i') AS foo;
128 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i');
129 -- no match of pattern
130 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', 'nomatch') AS foo;
131 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', 'nomatch');
132 -- some corner cases
133 SELECT regexp_split_to_array('123456','1');
134 SELECT regexp_split_to_array('123456','6');
135 SELECT regexp_split_to_array('123456','.');
136 -- errors
137 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'zippy') AS foo;
138 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'iz');
139 -- global option meaningless for regexp_split
140 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'g') AS foo;
141 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'g');
142
143 -- change NULL-display back
144 \pset null ''
145
146 -- E021-11 position expression
147 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
148
149 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
150
151 -- T312 character overlay function
152 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
153
154 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
155
156 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
157
158 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
159
160 --
161 -- test LIKE
162 -- Be sure to form every test as a LIKE/NOT LIKE pair.
163 --
164
165 -- simplest examples
166 -- E061-04 like predicate
167 SELECT 'hawkeye' LIKE 'h%' AS "true";
168 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
169
170 SELECT 'hawkeye' LIKE 'H%' AS "false";
171 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
172
173 SELECT 'hawkeye' LIKE 'indio%' AS "false";
174 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
175
176 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
177 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
178
179 SELECT 'indio' LIKE '_ndio' AS "true";
180 SELECT 'indio' NOT LIKE '_ndio' AS "false";
181
182 SELECT 'indio' LIKE 'in__o' AS "true";
183 SELECT 'indio' NOT LIKE 'in__o' AS "false";
184
185 SELECT 'indio' LIKE 'in_o' AS "false";
186 SELECT 'indio' NOT LIKE 'in_o' AS "true";
187
188 -- unused escape character
189 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
190 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
191
192 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
193 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
194
195 -- escape character
196 -- E061-05 like predicate with escape clause
197 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
198 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
199
200 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
201 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
202
203 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
204 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
205
206 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
207 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
208
209 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
210 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
211
212 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
213 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
214
215 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
216 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
217
218 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
219 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
220
221 -- escape character same as pattern character
222 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
223 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
224
225 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
226 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
227
228 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
229 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
230
231 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
232 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
233
234 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
235 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
236
237
238 --
239 -- test ILIKE (case-insensitive LIKE)
240 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
241 --
242
243 SELECT 'hawkeye' ILIKE 'h%' AS "true";
244 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
245
246 SELECT 'hawkeye' ILIKE 'H%' AS "true";
247 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
248
249 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
250 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
251
252 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
253 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
254
255 --
256 -- test implicit type conversion
257 --
258
259 -- E021-07 character concatenation
260 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
261
262 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
263
264 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
265
266 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
267
268 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
269
270 --
271 -- test substr with toasted text values
272 --
273 CREATE TABLE toasttest(f1 text);
274
275 insert into toasttest values(repeat('1234567890',10000));
276 insert into toasttest values(repeat('1234567890',10000));
277
278 --
279 -- Ensure that some values are uncompressed, to test the faster substring
280 -- operation used in that case
281 --
282 alter table toasttest alter column f1 set storage external;
283 insert into toasttest values(repeat('1234567890',10000));
284 insert into toasttest values(repeat('1234567890',10000));
285
286 -- If the starting position is zero or less, then return from the start of the string
287 -- adjusting the length to be consistent with the "negative start" per SQL92.
288 SELECT substr(f1, -1, 5) from toasttest;
289
290 -- If the length is less than zero, an ERROR is thrown.
291 SELECT substr(f1, 5, -1) from toasttest;
292
293 -- If no third argument (length) is provided, the length to the end of the
294 -- string is assumed.
295 SELECT substr(f1, 99995) from toasttest;
296
297 -- If start plus length is > string length, the result is truncated to
298 -- string length
299 SELECT substr(f1, 99995, 10) from toasttest;
300
301 DROP TABLE toasttest;
302
303 --
304 -- test substr with toasted bytea values
305 --
306 CREATE TABLE toasttest(f1 bytea);
307
308 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
309 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
310
311 --
312 -- Ensure that some values are uncompressed, to test the faster substring
313 -- operation used in that case
314 --
315 alter table toasttest alter column f1 set storage external;
316 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
317 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
318
319 -- If the starting position is zero or less, then return from the start of the string
320 -- adjusting the length to be consistent with the "negative start" per SQL92.
321 SELECT substr(f1, -1, 5) from toasttest;
322
323 -- If the length is less than zero, an ERROR is thrown.
324 SELECT substr(f1, 5, -1) from toasttest;
325
326 -- If no third argument (length) is provided, the length to the end of the
327 -- string is assumed.
328 SELECT substr(f1, 99995) from toasttest;
329
330 -- If start plus length is > string length, the result is truncated to
331 -- string length
332 SELECT substr(f1, 99995, 10) from toasttest;
333
334 DROP TABLE toasttest;
335
336 -- test internally compressing datums
337
338 -- this tests compressing a datum to a very small size which exercises a
339 -- corner case in packed-varlena handling: even though small, the compressed
340 -- datum must be given a 4-byte header because there are no bits to indicate
341 -- compression in a 1-byte header
342
343 CREATE TABLE toasttest (c char(4096));
344 INSERT INTO toasttest VALUES('x');
345 SELECT length(c), c::text FROM toasttest;
346 SELECT c FROM toasttest;
347 DROP TABLE toasttest;
348
349 --
350 -- test length
351 --
352
353 SELECT length('abcdef') AS "length_6";
354
355 --
356 -- test strpos
357 --
358
359 SELECT strpos('abcdef', 'cd') AS "pos_3";
360
361 SELECT strpos('abcdef', 'xy') AS "pos_0";
362
363 --
364 -- test replace
365 --
366 SELECT replace('abcdef', 'de', '45') AS "abc45f";
367
368 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
369
370 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
371
372 --
373 -- test split_part
374 --
375 select split_part('joeuser@mydatabase','@',0) AS "an error";
376
377 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
378
379 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
380
381 select split_part('joeuser@mydatabase','@',3) AS "empty string";
382
383 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
384
385 --
386 -- test to_hex
387 --
388 select to_hex(256*256*256 - 1) AS "ffffff";
389
390 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
391
392 --
393 -- MD5 test suite - from IETF RFC 1321
394 -- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
395 --
396 select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
397
398 select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
399
400 select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
401
402 select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
403
404 select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
405
406 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
407
408 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
409
410 select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
411
412 select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
413
414 select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
415
416 select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
417
418 select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
419
420 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
421
422 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
423
424 --
425 -- test behavior of escape_string_warning and standard_conforming_strings options
426 --
427 set escape_string_warning = off;
428 set standard_conforming_strings = off;
429
430 show escape_string_warning;
431 show standard_conforming_strings;
432
433 set escape_string_warning = on;
434 set standard_conforming_strings = on;
435
436 show escape_string_warning;
437 show standard_conforming_strings;
438
439 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
440
441 set standard_conforming_strings = off;
442
443 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
444
445 set escape_string_warning = off;
446 set standard_conforming_strings = on;
447
448 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
449
450 set standard_conforming_strings = off;
451
452 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;