3 -- Test various data entry syntaxes.
6 -- SQL92 string continuation syntax
7 -- E021-03 character string literals
11 AS "Three lines to one";
13 -- illegal string continuation syntax
15 ' - next line' /* this comment is not allowed here */
17 AS "Illegal comment within continuation";
20 -- test conversions between various string types
21 -- E021-10 implicit casting among the character data types
24 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
26 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
28 SELECT CAST(name 'namefield' AS text) AS "text(name)";
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
34 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
36 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
38 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
40 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
42 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
44 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
47 -- test SQL92 string functions
48 -- E### and T### are feature reference numbers from SQL99
51 -- E021-09 trim function
52 SELECT TRIM(BOTH FROM ' bunch o blanks ') = 'bunch o blanks' AS "bunch o blanks";
54 SELECT TRIM(LEADING FROM ' bunch o blanks ') = 'bunch o blanks ' AS "bunch o blanks ";
56 SELECT TRIM(TRAILING FROM ' bunch o blanks ') = ' bunch o blanks' AS " bunch o blanks";
58 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
60 -- E021-06 substring expression
61 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
63 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
65 -- T581 regular expression substring (with SQL99's bizarre regexp syntax)
66 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
68 -- No match should return NULL
69 SELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True";
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";
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";
80 -- With a parenthesized subexpression, return only what matches the subexpr
81 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
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');
91 -- set so we can tell NULL from empty string
94 -- return all matches from regexp
95 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
97 -- test case insensitive
98 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
100 -- global option - more than one match
101 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
103 -- empty capture group (matched empty string)
104 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
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$);
111 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
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$);
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$);
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', '');
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');
133 SELECT regexp_split_to_array('123456','1');
134 SELECT regexp_split_to_array('123456','6');
135 SELECT regexp_split_to_array('123456','.');
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');
143 -- change NULL-display back
146 -- E021-11 position expression
147 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
149 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
151 -- T312 character overlay function
152 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
154 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
156 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
158 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
162 -- Be sure to form every test as a LIKE/NOT LIKE pair.
166 -- E061-04 like predicate
167 SELECT 'hawkeye' LIKE 'h%' AS "true";
168 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
170 SELECT 'hawkeye' LIKE 'H%' AS "false";
171 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
173 SELECT 'hawkeye' LIKE 'indio%' AS "false";
174 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
176 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
177 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
179 SELECT 'indio' LIKE '_ndio' AS "true";
180 SELECT 'indio' NOT LIKE '_ndio' AS "false";
182 SELECT 'indio' LIKE 'in__o' AS "true";
183 SELECT 'indio' NOT LIKE 'in__o' AS "false";
185 SELECT 'indio' LIKE 'in_o' AS "false";
186 SELECT 'indio' NOT LIKE 'in_o' AS "true";
188 -- unused escape character
189 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
190 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
192 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
193 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
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";
200 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
201 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
203 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
204 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
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";
209 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
210 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
212 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
213 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
215 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
216 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
218 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
219 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
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";
225 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
226 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
228 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
229 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
231 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
232 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
234 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
235 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
239 -- test ILIKE (case-insensitive LIKE)
240 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
243 SELECT 'hawkeye' ILIKE 'h%' AS "true";
244 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
246 SELECT 'hawkeye' ILIKE 'H%' AS "true";
247 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
249 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
250 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
252 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
253 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
256 -- test implicit type conversion
259 -- E021-07 character concatenation
260 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
262 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
264 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
266 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
268 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
271 -- test substr with toasted text values
273 CREATE TABLE toasttest(f1 text);
275 insert into toasttest values(repeat('1234567890',10000));
276 insert into toasttest values(repeat('1234567890',10000));
279 -- Ensure that some values are uncompressed, to test the faster substring
280 -- operation used in that case
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));
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;
290 -- If the length is less than zero, an ERROR is thrown.
291 SELECT substr(f1, 5, -1) from toasttest;
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;
297 -- If start plus length is > string length, the result is truncated to
299 SELECT substr(f1, 99995, 10) from toasttest;
301 DROP TABLE toasttest;
304 -- test substr with toasted bytea values
306 CREATE TABLE toasttest(f1 bytea);
308 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
309 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
312 -- Ensure that some values are uncompressed, to test the faster substring
313 -- operation used in that case
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'));
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;
323 -- If the length is less than zero, an ERROR is thrown.
324 SELECT substr(f1, 5, -1) from toasttest;
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;
330 -- If start plus length is > string length, the result is truncated to
332 SELECT substr(f1, 99995, 10) from toasttest;
334 DROP TABLE toasttest;
336 -- test internally compressing datums
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
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;
353 SELECT length('abcdef') AS "length_6";
359 SELECT strpos('abcdef', 'cd') AS "pos_3";
361 SELECT strpos('abcdef', 'xy') AS "pos_0";
366 SELECT replace('abcdef', 'de', '45') AS "abc45f";
368 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
370 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
375 select split_part('joeuser@mydatabase','@',0) AS "an error";
377 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
379 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
381 select split_part('joeuser@mydatabase','@',3) AS "empty string";
383 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
388 select to_hex(256*256*256 - 1) AS "ffffff";
390 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
393 -- MD5 test suite - from IETF RFC 1321
394 -- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
396 select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
398 select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
400 select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
402 select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
404 select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
406 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
408 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
410 select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
412 select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
414 select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
416 select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
418 select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
420 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
422 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
425 -- test behavior of escape_string_warning and standard_conforming_strings options
427 set escape_string_warning = off;
428 set standard_conforming_strings = off;
430 show escape_string_warning;
431 show standard_conforming_strings;
433 set escape_string_warning = on;
434 set standard_conforming_strings = on;
436 show escape_string_warning;
437 show standard_conforming_strings;
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;
441 set standard_conforming_strings = off;
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;
445 set escape_string_warning = off;
446 set standard_conforming_strings = on;
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;
450 set standard_conforming_strings = off;
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;