Convert a few more datatype input functions to report errors softly.
[pgsql.git] / src / test / regress / sql / strings.sql
blob932f71cbca467788e10316895e00dd8ee88a453b
1 --
2 -- STRINGS
3 -- Test various data entry syntaxes.
4 --
6 -- SQL 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";
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";
19 -- Unicode escapes
20 SET standard_conforming_strings TO on;
22 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
23 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
24 SELECT U&'a\\b' AS "a\b";
26 SELECT U&' \' UESCAPE '!' AS "tricky";
27 SELECT 'tricky' AS U&"\" UESCAPE '!';
29 SELECT U&'wrong: \061';
30 SELECT U&'wrong: \+0061';
31 SELECT U&'wrong: +0061' UESCAPE +;
32 SELECT U&'wrong: +0061' UESCAPE '+';
34 SELECT U&'wrong: \db99';
35 SELECT U&'wrong: \db99xy';
36 SELECT U&'wrong: \db99\\';
37 SELECT U&'wrong: \db99\0061';
38 SELECT U&'wrong: \+00db99\+000061';
39 SELECT U&'wrong: \+2FFFFF';
41 -- while we're here, check the same cases in E-style literals
42 SELECT E'd\u0061t\U00000061' AS "data";
43 SELECT E'a\\b' AS "a\b";
44 SELECT E'wrong: \u061';
45 SELECT E'wrong: \U0061';
46 SELECT E'wrong: \udb99';
47 SELECT E'wrong: \udb99xy';
48 SELECT E'wrong: \udb99\\';
49 SELECT E'wrong: \udb99\u0061';
50 SELECT E'wrong: \U0000db99\U00000061';
51 SELECT E'wrong: \U002FFFFF';
53 SET standard_conforming_strings TO off;
55 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
56 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
58 SELECT U&' \' UESCAPE '!' AS "tricky";
59 SELECT 'tricky' AS U&"\" UESCAPE '!';
61 SELECT U&'wrong: \061';
62 SELECT U&'wrong: \+0061';
63 SELECT U&'wrong: +0061' UESCAPE '+';
65 RESET standard_conforming_strings;
67 -- bytea
68 SET bytea_output TO hex;
69 SELECT E'\\xDeAdBeEf'::bytea;
70 SELECT E'\\x De Ad Be Ef '::bytea;
71 SELECT E'\\xDeAdBeE'::bytea;
72 SELECT E'\\xDeAdBeEx'::bytea;
73 SELECT E'\\xDe00BeEf'::bytea;
74 SELECT E'DeAdBeEf'::bytea;
75 SELECT E'De\\000dBeEf'::bytea;
76 SELECT E'De\123dBeEf'::bytea;
77 SELECT E'De\\123dBeEf'::bytea;
78 SELECT E'De\\678dBeEf'::bytea;
80 SET bytea_output TO escape;
81 SELECT E'\\xDeAdBeEf'::bytea;
82 SELECT E'\\x De Ad Be Ef '::bytea;
83 SELECT E'\\xDe00BeEf'::bytea;
84 SELECT E'DeAdBeEf'::bytea;
85 SELECT E'De\\000dBeEf'::bytea;
86 SELECT E'De\\123dBeEf'::bytea;
88 -- Test non-error-throwing API too
89 SELECT pg_input_is_valid(E'\\xDeAdBeE', 'bytea');
90 SELECT pg_input_error_message(E'\\xDeAdBeE', 'bytea');
91 SELECT pg_input_error_message(E'\\xDeAdBeEx', 'bytea');
92 SELECT pg_input_error_message(E'foo\\99bar', 'bytea');
95 -- test conversions between various string types
96 -- E021-10 implicit casting among the character data types
99 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
101 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
103 SELECT CAST(name 'namefield' AS text) AS "text(name)";
105 -- since this is an explicit cast, it should truncate w/o error:
106 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
107 -- note: implicit-cast case is tested in char.sql
109 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
111 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
113 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
115 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
117 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
119 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
122 -- test SQL string functions
123 -- E### and T### are feature reference numbers from SQL99
126 -- E021-09 trim function
127 SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
129 SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
131 SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
133 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
135 -- E021-06 substring expression
136 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
138 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
140 -- test overflow cases
141 SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
142 SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
143 SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
145 -- T581 regular expression substring (with SQL's bizarre regexp syntax)
146 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"(b_d)#"%' ESCAPE '#') AS "bcd";
147 -- obsolete SQL99 syntax
148 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
150 -- No match should return NULL
151 SELECT SUBSTRING('abcdefg' SIMILAR '#"(b_d)#"%' ESCAPE '#') IS NULL AS "True";
153 -- Null inputs should return NULL
154 SELECT SUBSTRING('abcdefg' SIMILAR '%' ESCAPE NULL) IS NULL AS "True";
155 SELECT SUBSTRING(NULL SIMILAR '%' ESCAPE '#') IS NULL AS "True";
156 SELECT SUBSTRING('abcdefg' SIMILAR NULL ESCAPE '#') IS NULL AS "True";
158 -- The first and last parts should act non-greedy
159 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"g' ESCAPE '#') AS "bcdef";
160 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*' ESCAPE '#') AS "abcdefg";
162 -- Vertical bar in any part affects only that part
163 SELECT SUBSTRING('abcdefg' SIMILAR 'a|b#"%#"g' ESCAPE '#') AS "bcdef";
164 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"x|g' ESCAPE '#') AS "bcdef";
165 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%|ab#"g' ESCAPE '#') AS "bcdef";
167 -- Can't have more than two part separators
168 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*#"x' ESCAPE '#') AS "error";
170 -- Postgres extension: with 0 or 1 separator, assume parts 1 and 3 are empty
171 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%g' ESCAPE '#') AS "bcdefg";
172 SELECT SUBSTRING('abcdefg' SIMILAR 'a%g' ESCAPE '#') AS "abcdefg";
174 -- substring() with just two arguments is not allowed by SQL spec;
175 -- we accept it, but we interpret the pattern as a POSIX regexp not SQL
176 SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
178 -- With a parenthesized subexpression, return only what matches the subexpr
179 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
180 -- Check case where we have a match, but not a subexpression match
181 SELECT SUBSTRING('foo' FROM 'foo(bar)?') IS NULL AS t;
183 -- Check behavior of SIMILAR TO, which uses largely the same regexp variant
184 SELECT 'abcdefg' SIMILAR TO '_bcd%' AS true;
185 SELECT 'abcdefg' SIMILAR TO 'bcd%' AS false;
186 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '#' AS false;
187 SELECT 'abcd%' SIMILAR TO '_bcd#%' ESCAPE '#' AS true;
188 -- Postgres uses '\' as the default escape character, which is not per spec
189 SELECT 'abcdefg' SIMILAR TO '_bcd\%' AS false;
190 -- and an empty string to mean "no escape", which is also not per spec
191 SELECT 'abcd\efg' SIMILAR TO '_bcd\%' ESCAPE '' AS true;
192 -- these behaviors are per spec, though:
193 SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
194 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
196 -- Test backslash escapes in regexp_replace's replacement string
197 SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
198 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\&Y', 'g');
199 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\\\Y', 'g');
200 -- not an error, though perhaps it should be:
201 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\Y\\1Z\\');
203 SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
204 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
205 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
206 -- invalid regexp option
207 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
209 -- extended regexp_replace tests
210 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1);
211 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 2);
212 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, 'i');
213 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'i');
214 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 2, 'i');
215 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, 'i');
216 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 9, 'i');
217 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 7, 0, 'i');
218 -- 'g' flag should be ignored when N is specified
219 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'g');
220 -- errors
221 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', -1, 0, 'i');
222 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, -1, 'i');
223 -- erroneous invocation of non-extended form
224 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', '1');
226 --  regexp_count tests
227 SELECT regexp_count('123123123123123', '(12)3');
228 SELECT regexp_count('123123123123', '123', 1);
229 SELECT regexp_count('123123123123', '123', 3);
230 SELECT regexp_count('123123123123', '123', 33);
231 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, '');
232 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, 'i');
233 -- errors
234 SELECT regexp_count('123123123123', '123', 0);
235 SELECT regexp_count('123123123123', '123', -3);
237 -- regexp_like tests
238 SELECT regexp_like('Steven', '^Ste(v|ph)en$');
239 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'n');
240 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 's');
241 SELECT regexp_like('abc', ' a . c ', 'x');
242 SELECT regexp_like('abc', 'a.c', 'g');  -- error
244 -- regexp_instr tests
245 SELECT regexp_instr('abcdefghi', 'd.f');
246 SELECT regexp_instr('abcdefghi', 'd.q');
247 SELECT regexp_instr('abcabcabc', 'a.c');
248 SELECT regexp_instr('abcabcabc', 'a.c', 2);
249 SELECT regexp_instr('abcabcabc', 'a.c', 1, 3);
250 SELECT regexp_instr('abcabcabc', 'a.c', 1, 4);
251 SELECT regexp_instr('abcabcabc', 'A.C', 1, 2, 0, 'i');
252 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0);
253 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1);
254 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2);
255 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3);
256 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4);
257 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 5);
258 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0);
259 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1);
260 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2);
261 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3);
262 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4);
263 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5);
264 -- Check case where we have a match, but not a subexpression match
265 SELECT regexp_instr('foo', 'foo(bar)?', 1, 1, 0, '', 1);
266 -- errors
267 SELECT regexp_instr('abcabcabc', 'a.c', 0, 1);
268 SELECT regexp_instr('abcabcabc', 'a.c', 1, 0);
269 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, -1);
270 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 2);
271 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, 'g');
272 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, '', -1);
274 -- regexp_substr tests
275 SELECT regexp_substr('abcdefghi', 'd.f');
276 SELECT regexp_substr('abcdefghi', 'd.q') IS NULL AS t;
277 SELECT regexp_substr('abcabcabc', 'a.c');
278 SELECT regexp_substr('abcabcabc', 'a.c', 2);
279 SELECT regexp_substr('abcabcabc', 'a.c', 1, 3);
280 SELECT regexp_substr('abcabcabc', 'a.c', 1, 4) IS NULL AS t;
281 SELECT regexp_substr('abcabcabc', 'A.C', 1, 2, 'i');
282 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0);
283 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 1);
284 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 2);
285 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 3);
286 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4);
287 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 5) IS NULL AS t;
288 -- Check case where we have a match, but not a subexpression match
289 SELECT regexp_substr('foo', 'foo(bar)?', 1, 1, '', 1) IS NULL AS t;
290 -- errors
291 SELECT regexp_substr('abcabcabc', 'a.c', 0, 1);
292 SELECT regexp_substr('abcabcabc', 'a.c', 1, 0);
293 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, 'g');
294 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, '', -1);
296 -- set so we can tell NULL from empty string
297 \pset null '\\N'
299 -- return all matches from regexp
300 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
302 -- test case insensitive
303 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
305 -- global option - more than one match
306 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
308 -- empty capture group (matched empty string)
309 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
310 -- no match
311 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
312 -- optional capture group did not match, null entry in array
313 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
315 -- no capture groups
316 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
318 -- start/end-of-line matches are of zero length
319 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg');
320 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg');
321 SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg');
322 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg');
323 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg');
325 -- give me errors
326 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
327 SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
328 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
330 -- split string on regexp
331 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo;
332 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
334 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo;
335 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$);
336 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo;
337 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '');
338 -- case insensitive
339 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo;
340 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i');
341 -- no match of pattern
342 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo;
343 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch');
344 -- some corner cases
345 SELECT regexp_split_to_array('123456','1');
346 SELECT regexp_split_to_array('123456','6');
347 SELECT regexp_split_to_array('123456','.');
348 SELECT regexp_split_to_array('123456','');
349 SELECT regexp_split_to_array('123456','(?:)');
350 SELECT regexp_split_to_array('1','');
351 -- errors
352 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo;
353 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz');
354 -- global option meaningless for regexp_split
355 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo;
356 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
358 -- change NULL-display back
359 \pset null ''
361 -- E021-11 position expression
362 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
364 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
366 -- T312 character overlay function
367 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
369 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
371 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
373 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
376 -- test LIKE
377 -- Be sure to form every test as a LIKE/NOT LIKE pair.
380 -- simplest examples
381 -- E061-04 like predicate
382 SELECT 'hawkeye' LIKE 'h%' AS "true";
383 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
385 SELECT 'hawkeye' LIKE 'H%' AS "false";
386 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
388 SELECT 'hawkeye' LIKE 'indio%' AS "false";
389 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
391 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
392 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
394 SELECT 'indio' LIKE '_ndio' AS "true";
395 SELECT 'indio' NOT LIKE '_ndio' AS "false";
397 SELECT 'indio' LIKE 'in__o' AS "true";
398 SELECT 'indio' NOT LIKE 'in__o' AS "false";
400 SELECT 'indio' LIKE 'in_o' AS "false";
401 SELECT 'indio' NOT LIKE 'in_o' AS "true";
403 SELECT 'abc'::name LIKE '_b_' AS "true";
404 SELECT 'abc'::name NOT LIKE '_b_' AS "false";
406 SELECT 'abc'::bytea LIKE '_b_'::bytea AS "true";
407 SELECT 'abc'::bytea NOT LIKE '_b_'::bytea AS "false";
409 -- unused escape character
410 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
411 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
413 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
414 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
416 -- escape character
417 -- E061-05 like predicate with escape clause
418 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
419 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
421 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
422 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
424 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
425 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
427 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
428 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
430 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
431 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
433 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
434 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
436 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
437 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
439 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
440 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
442 SELECT 'a_c'::bytea LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "true";
443 SELECT 'a_c'::bytea NOT LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "false";
445 -- escape character same as pattern character
446 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
447 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
449 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
450 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
452 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
453 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
455 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
456 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
458 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
459 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
463 -- test ILIKE (case-insensitive LIKE)
464 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
467 SELECT 'hawkeye' ILIKE 'h%' AS "true";
468 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
470 SELECT 'hawkeye' ILIKE 'H%' AS "true";
471 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
473 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
474 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
476 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
477 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
479 SELECT 'ABC'::name ILIKE '_b_' AS "true";
480 SELECT 'ABC'::name NOT ILIKE '_b_' AS "false";
483 -- test %/_ combination cases, cf bugs #4821 and #5478
486 SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f;
487 SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f;
489 SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f;
490 SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
492 SELECT 'jack' LIKE '%____%' AS t;
496 -- basic tests of LIKE with indexes
499 CREATE TABLE texttest (a text PRIMARY KEY, b int);
500 SELECT * FROM texttest WHERE a LIKE '%1%';
502 CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
503 SELECT * FROM byteatest WHERE a LIKE '%1%';
505 DROP TABLE texttest, byteatest;
509 -- test implicit type conversion
512 -- E021-07 character concatenation
513 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
515 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
517 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
519 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
521 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
524 -- test substr with toasted text values
526 CREATE TABLE toasttest(f1 text);
528 insert into toasttest values(repeat('1234567890',10000));
529 insert into toasttest values(repeat('1234567890',10000));
532 -- Ensure that some values are uncompressed, to test the faster substring
533 -- operation used in that case
535 alter table toasttest alter column f1 set storage external;
536 insert into toasttest values(repeat('1234567890',10000));
537 insert into toasttest values(repeat('1234567890',10000));
539 -- If the starting position is zero or less, then return from the start of the string
540 -- adjusting the length to be consistent with the "negative start" per SQL.
541 SELECT substr(f1, -1, 5) from toasttest;
543 -- If the length is less than zero, an ERROR is thrown.
544 SELECT substr(f1, 5, -1) from toasttest;
546 -- If no third argument (length) is provided, the length to the end of the
547 -- string is assumed.
548 SELECT substr(f1, 99995) from toasttest;
550 -- If start plus length is > string length, the result is truncated to
551 -- string length
552 SELECT substr(f1, 99995, 10) from toasttest;
554 TRUNCATE TABLE toasttest;
555 INSERT INTO toasttest values (repeat('1234567890',300));
556 INSERT INTO toasttest values (repeat('1234567890',300));
557 INSERT INTO toasttest values (repeat('1234567890',300));
558 INSERT INTO toasttest values (repeat('1234567890',300));
559 -- expect >0 blocks
560 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
561   FROM pg_class where relname = 'toasttest';
563 TRUNCATE TABLE toasttest;
564 ALTER TABLE toasttest set (toast_tuple_target = 4080);
565 INSERT INTO toasttest values (repeat('1234567890',300));
566 INSERT INTO toasttest values (repeat('1234567890',300));
567 INSERT INTO toasttest values (repeat('1234567890',300));
568 INSERT INTO toasttest values (repeat('1234567890',300));
569 -- expect 0 blocks
570 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
571   FROM pg_class where relname = 'toasttest';
573 DROP TABLE toasttest;
576 -- test substr with toasted bytea values
578 CREATE TABLE toasttest(f1 bytea);
580 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
581 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
584 -- Ensure that some values are uncompressed, to test the faster substring
585 -- operation used in that case
587 alter table toasttest alter column f1 set storage external;
588 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
589 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
591 -- If the starting position is zero or less, then return from the start of the string
592 -- adjusting the length to be consistent with the "negative start" per SQL.
593 SELECT substr(f1, -1, 5) from toasttest;
595 -- If the length is less than zero, an ERROR is thrown.
596 SELECT substr(f1, 5, -1) from toasttest;
598 -- If no third argument (length) is provided, the length to the end of the
599 -- string is assumed.
600 SELECT substr(f1, 99995) from toasttest;
602 -- If start plus length is > string length, the result is truncated to
603 -- string length
604 SELECT substr(f1, 99995, 10) from toasttest;
606 DROP TABLE toasttest;
608 -- test internally compressing datums
610 -- this tests compressing a datum to a very small size which exercises a
611 -- corner case in packed-varlena handling: even though small, the compressed
612 -- datum must be given a 4-byte header because there are no bits to indicate
613 -- compression in a 1-byte header
615 CREATE TABLE toasttest (c char(4096));
616 INSERT INTO toasttest VALUES('x');
617 SELECT length(c), c::text FROM toasttest;
618 SELECT c FROM toasttest;
619 DROP TABLE toasttest;
622 -- test length
625 SELECT length('abcdef') AS "length_6";
628 -- test strpos
631 SELECT strpos('abcdef', 'cd') AS "pos_3";
633 SELECT strpos('abcdef', 'xy') AS "pos_0";
635 SELECT strpos('abcdef', '') AS "pos_1";
637 SELECT strpos('', 'xy') AS "pos_0";
639 SELECT strpos('', '') AS "pos_1";
642 -- test replace
644 SELECT replace('abcdef', 'de', '45') AS "abc45f";
646 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
648 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
651 -- test split_part
653 select split_part('','@',1) AS "empty string";
655 select split_part('','@',-1) AS "empty string";
657 select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
659 select split_part('joeuser@mydatabase','',2) AS "empty string";
661 select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
663 select split_part('joeuser@mydatabase','',-2) AS "empty string";
665 select split_part('joeuser@mydatabase','@',0) AS "an error";
667 select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
669 select split_part('joeuser@mydatabase','@@',2) AS "empty string";
671 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
673 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
675 select split_part('joeuser@mydatabase','@',3) AS "empty string";
677 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
679 select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
681 select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
683 select split_part('joeuser@mydatabase','@',-3) AS "empty string";
685 select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
688 -- test to_hex
690 select to_hex(256*256*256 - 1) AS "ffffff";
692 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
695 -- SHA-2
697 SET bytea_output TO hex;
699 SELECT sha224('');
700 SELECT sha224('The quick brown fox jumps over the lazy dog.');
702 SELECT sha256('');
703 SELECT sha256('The quick brown fox jumps over the lazy dog.');
705 SELECT sha384('');
706 SELECT sha384('The quick brown fox jumps over the lazy dog.');
708 SELECT sha512('');
709 SELECT sha512('The quick brown fox jumps over the lazy dog.');
712 -- encode/decode
714 SELECT encode('\x1234567890abcdef00', 'hex');
715 SELECT decode('1234567890abcdef00', 'hex');
716 SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
717 SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
718                      'base64'), 'base64');
719 SELECT encode('\x1234567890abcdef00', 'escape');
720 SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
723 -- get_bit/set_bit etc
725 SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
726 SELECT get_bit('\x1234567890abcdef00'::bytea, 99);  -- error
727 SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
728 SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0);  -- error
729 SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
730 SELECT get_byte('\x1234567890abcdef00'::bytea, 99);  -- error
731 SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
732 SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11);  -- error
735 -- test behavior of escape_string_warning and standard_conforming_strings options
737 set escape_string_warning = off;
738 set standard_conforming_strings = off;
740 show escape_string_warning;
741 show standard_conforming_strings;
743 set escape_string_warning = on;
744 set standard_conforming_strings = on;
746 show escape_string_warning;
747 show standard_conforming_strings;
749 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
751 set standard_conforming_strings = off;
753 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
755 set escape_string_warning = off;
756 set standard_conforming_strings = on;
758 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
760 set standard_conforming_strings = off;
762 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
764 reset standard_conforming_strings;
768 -- Additional string functions
770 SET bytea_output TO escape;
772 SELECT initcap('hi THOMAS');
774 SELECT lpad('hi', 5, 'xy');
775 SELECT lpad('hi', 5);
776 SELECT lpad('hi', -5, 'xy');
777 SELECT lpad('hello', 2);
778 SELECT lpad('hi', 5, '');
780 SELECT rpad('hi', 5, 'xy');
781 SELECT rpad('hi', 5);
782 SELECT rpad('hi', -5, 'xy');
783 SELECT rpad('hello', 2);
784 SELECT rpad('hi', 5, '');
786 SELECT ltrim('zzzytrim', 'xyz');
788 SELECT translate('', '14', 'ax');
789 SELECT translate('12345', '14', 'ax');
791 SELECT ascii('x');
792 SELECT ascii('');
794 SELECT chr(65);
795 SELECT chr(0);
797 SELECT repeat('Pg', 4);
798 SELECT repeat('Pg', -4);
800 SELECT SUBSTRING('1234567890'::bytea FROM 3) "34567890";
801 SELECT SUBSTRING('1234567890'::bytea FROM 4 FOR 3) AS "456";
802 SELECT SUBSTRING('string'::bytea FROM 2 FOR 2147483646) AS "tring";
803 SELECT SUBSTRING('string'::bytea FROM -10 FOR 2147483646) AS "string";
804 SELECT SUBSTRING('string'::bytea FROM -10 FOR -2147483646) AS "error";
806 SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
807 SELECT trim(leading E'\\000'::bytea from E'\\000Tom\\000'::bytea);
808 SELECT trim(trailing E'\\000'::bytea from E'\\000Tom\\000'::bytea);
809 SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
810 SELECT btrim(''::bytea, E'\\000'::bytea);
811 SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
812 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape');
813 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape');
814 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape');
816 SELECT bit_count('\x1234567890'::bytea);
818 SELECT unistr('\0064at\+0000610');
819 SELECT unistr('d\u0061t\U000000610');
820 SELECT unistr('a\\b');
821 -- errors:
822 SELECT unistr('wrong: \db99');
823 SELECT unistr('wrong: \db99\0061');
824 SELECT unistr('wrong: \+00db99\+000061');
825 SELECT unistr('wrong: \+2FFFFF');
826 SELECT unistr('wrong: \udb99\u0061');
827 SELECT unistr('wrong: \U0000db99\U00000061');
828 SELECT unistr('wrong: \U002FFFFF');
829 SELECT unistr('wrong: \xyz');