Convert a few more datatype input functions to report errors softly.
[pgsql.git] / src / test / regress / expected / strings.out
blobf028c1f10f2009c0b57da6e4e87a5fe7dc432daa
1 --
2 -- STRINGS
3 -- Test various data entry syntaxes.
4 --
5 -- SQL string continuation syntax
6 -- E021-03 character string literals
7 SELECT 'first line'
8 ' - next line'
9         ' - third line'
10         AS "Three lines to one";
11          Three lines to one          
12 -------------------------------------
13  first line - next line - third line
14 (1 row)
16 -- illegal string continuation syntax
17 SELECT 'first line'
18 ' - next line' /* this comment is not allowed here */
19 ' - third line'
20         AS "Illegal comment within continuation";
21 ERROR:  syntax error at or near "' - third line'"
22 LINE 3: ' - third line'
23         ^
24 -- Unicode escapes
25 SET standard_conforming_strings TO on;
26 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
27  data 
28 ------
29  data
30 (1 row)
32 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
33  dat\+000061 
34 -------------
35  dat\+000061
36 (1 row)
38 SELECT U&'a\\b' AS "a\b";
39  a\b 
40 -----
41  a\b
42 (1 row)
44 SELECT U&' \' UESCAPE '!' AS "tricky";
45  tricky 
46 --------
47   \
48 (1 row)
50 SELECT 'tricky' AS U&"\" UESCAPE '!';
51    \    
52 --------
53  tricky
54 (1 row)
56 SELECT U&'wrong: \061';
57 ERROR:  invalid Unicode escape
58 LINE 1: SELECT U&'wrong: \061';
59                          ^
60 HINT:  Unicode escapes must be \XXXX or \+XXXXXX.
61 SELECT U&'wrong: \+0061';
62 ERROR:  invalid Unicode escape
63 LINE 1: SELECT U&'wrong: \+0061';
64                          ^
65 HINT:  Unicode escapes must be \XXXX or \+XXXXXX.
66 SELECT U&'wrong: +0061' UESCAPE +;
67 ERROR:  UESCAPE must be followed by a simple string literal at or near "+"
68 LINE 1: SELECT U&'wrong: +0061' UESCAPE +;
69                                         ^
70 SELECT U&'wrong: +0061' UESCAPE '+';
71 ERROR:  invalid Unicode escape character at or near "'+'"
72 LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
73                                         ^
74 SELECT U&'wrong: \db99';
75 ERROR:  invalid Unicode surrogate pair
76 LINE 1: SELECT U&'wrong: \db99';
77                               ^
78 SELECT U&'wrong: \db99xy';
79 ERROR:  invalid Unicode surrogate pair
80 LINE 1: SELECT U&'wrong: \db99xy';
81                               ^
82 SELECT U&'wrong: \db99\\';
83 ERROR:  invalid Unicode surrogate pair
84 LINE 1: SELECT U&'wrong: \db99\\';
85                               ^
86 SELECT U&'wrong: \db99\0061';
87 ERROR:  invalid Unicode surrogate pair
88 LINE 1: SELECT U&'wrong: \db99\0061';
89                               ^
90 SELECT U&'wrong: \+00db99\+000061';
91 ERROR:  invalid Unicode surrogate pair
92 LINE 1: SELECT U&'wrong: \+00db99\+000061';
93                                  ^
94 SELECT U&'wrong: \+2FFFFF';
95 ERROR:  invalid Unicode escape value
96 LINE 1: SELECT U&'wrong: \+2FFFFF';
97                          ^
98 -- while we're here, check the same cases in E-style literals
99 SELECT E'd\u0061t\U00000061' AS "data";
100  data 
101 ------
102  data
103 (1 row)
105 SELECT E'a\\b' AS "a\b";
106  a\b 
107 -----
108  a\b
109 (1 row)
111 SELECT E'wrong: \u061';
112 ERROR:  invalid Unicode escape
113 LINE 1: SELECT E'wrong: \u061';
114                         ^
115 HINT:  Unicode escapes must be \uXXXX or \UXXXXXXXX.
116 SELECT E'wrong: \U0061';
117 ERROR:  invalid Unicode escape
118 LINE 1: SELECT E'wrong: \U0061';
119                         ^
120 HINT:  Unicode escapes must be \uXXXX or \UXXXXXXXX.
121 SELECT E'wrong: \udb99';
122 ERROR:  invalid Unicode surrogate pair at or near "'"
123 LINE 1: SELECT E'wrong: \udb99';
124                               ^
125 SELECT E'wrong: \udb99xy';
126 ERROR:  invalid Unicode surrogate pair at or near "x"
127 LINE 1: SELECT E'wrong: \udb99xy';
128                               ^
129 SELECT E'wrong: \udb99\\';
130 ERROR:  invalid Unicode surrogate pair at or near "\"
131 LINE 1: SELECT E'wrong: \udb99\\';
132                               ^
133 SELECT E'wrong: \udb99\u0061';
134 ERROR:  invalid Unicode surrogate pair at or near "\u0061"
135 LINE 1: SELECT E'wrong: \udb99\u0061';
136                               ^
137 SELECT E'wrong: \U0000db99\U00000061';
138 ERROR:  invalid Unicode surrogate pair at or near "\U00000061"
139 LINE 1: SELECT E'wrong: \U0000db99\U00000061';
140                                   ^
141 SELECT E'wrong: \U002FFFFF';
142 ERROR:  invalid Unicode escape value at or near "\U002FFFFF"
143 LINE 1: SELECT E'wrong: \U002FFFFF';
144                         ^
145 SET standard_conforming_strings TO off;
146 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
147 ERROR:  unsafe use of string constant with Unicode escapes
148 LINE 1: SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
149                ^
150 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
151 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
152 ERROR:  unsafe use of string constant with Unicode escapes
153 LINE 1: SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061...
154                ^
155 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
156 SELECT U&' \' UESCAPE '!' AS "tricky";
157 ERROR:  unsafe use of string constant with Unicode escapes
158 LINE 1: SELECT U&' \' UESCAPE '!' AS "tricky";
159                ^
160 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
161 SELECT 'tricky' AS U&"\" UESCAPE '!';
162    \    
163 --------
164  tricky
165 (1 row)
167 SELECT U&'wrong: \061';
168 ERROR:  unsafe use of string constant with Unicode escapes
169 LINE 1: SELECT U&'wrong: \061';
170                ^
171 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
172 SELECT U&'wrong: \+0061';
173 ERROR:  unsafe use of string constant with Unicode escapes
174 LINE 1: SELECT U&'wrong: \+0061';
175                ^
176 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
177 SELECT U&'wrong: +0061' UESCAPE '+';
178 ERROR:  unsafe use of string constant with Unicode escapes
179 LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
180                ^
181 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
182 RESET standard_conforming_strings;
183 -- bytea
184 SET bytea_output TO hex;
185 SELECT E'\\xDeAdBeEf'::bytea;
186    bytea    
187 ------------
188  \xdeadbeef
189 (1 row)
191 SELECT E'\\x De Ad Be Ef '::bytea;
192    bytea    
193 ------------
194  \xdeadbeef
195 (1 row)
197 SELECT E'\\xDeAdBeE'::bytea;
198 ERROR:  invalid hexadecimal data: odd number of digits
199 LINE 1: SELECT E'\\xDeAdBeE'::bytea;
200                ^
201 SELECT E'\\xDeAdBeEx'::bytea;
202 ERROR:  invalid hexadecimal digit: "x"
203 LINE 1: SELECT E'\\xDeAdBeEx'::bytea;
204                ^
205 SELECT E'\\xDe00BeEf'::bytea;
206    bytea    
207 ------------
208  \xde00beef
209 (1 row)
211 SELECT E'DeAdBeEf'::bytea;
212        bytea        
213 --------------------
214  \x4465416442654566
215 (1 row)
217 SELECT E'De\\000dBeEf'::bytea;
218        bytea        
219 --------------------
220  \x4465006442654566
221 (1 row)
223 SELECT E'De\123dBeEf'::bytea;
224        bytea        
225 --------------------
226  \x4465536442654566
227 (1 row)
229 SELECT E'De\\123dBeEf'::bytea;
230        bytea        
231 --------------------
232  \x4465536442654566
233 (1 row)
235 SELECT E'De\\678dBeEf'::bytea;
236 ERROR:  invalid input syntax for type bytea
237 LINE 1: SELECT E'De\\678dBeEf'::bytea;
238                ^
239 SET bytea_output TO escape;
240 SELECT E'\\xDeAdBeEf'::bytea;
241       bytea       
242 ------------------
243  \336\255\276\357
244 (1 row)
246 SELECT E'\\x De Ad Be Ef '::bytea;
247       bytea       
248 ------------------
249  \336\255\276\357
250 (1 row)
252 SELECT E'\\xDe00BeEf'::bytea;
253       bytea       
254 ------------------
255  \336\000\276\357
256 (1 row)
258 SELECT E'DeAdBeEf'::bytea;
259   bytea   
260 ----------
261  DeAdBeEf
262 (1 row)
264 SELECT E'De\\000dBeEf'::bytea;
265     bytea    
266 -------------
267  De\000dBeEf
268 (1 row)
270 SELECT E'De\\123dBeEf'::bytea;
271   bytea   
272 ----------
273  DeSdBeEf
274 (1 row)
276 -- Test non-error-throwing API too
277 SELECT pg_input_is_valid(E'\\xDeAdBeE', 'bytea');
278  pg_input_is_valid 
279 -------------------
281 (1 row)
283 SELECT pg_input_error_message(E'\\xDeAdBeE', 'bytea');
284              pg_input_error_message             
285 ------------------------------------------------
286  invalid hexadecimal data: odd number of digits
287 (1 row)
289 SELECT pg_input_error_message(E'\\xDeAdBeEx', 'bytea');
290      pg_input_error_message     
291 --------------------------------
292  invalid hexadecimal digit: "x"
293 (1 row)
295 SELECT pg_input_error_message(E'foo\\99bar', 'bytea');
296        pg_input_error_message        
297 -------------------------------------
298  invalid input syntax for type bytea
299 (1 row)
302 -- test conversions between various string types
303 -- E021-10 implicit casting among the character data types
305 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
306  text(char) 
307 ------------
309  ab
310  abcd
311  abcd
312 (4 rows)
314 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
315  text(varchar) 
316 ---------------
318  ab
319  abcd
320  abcd
321 (4 rows)
323 SELECT CAST(name 'namefield' AS text) AS "text(name)";
324  text(name) 
325 ------------
326  namefield
327 (1 row)
329 -- since this is an explicit cast, it should truncate w/o error:
330 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
331  char(text) 
332 ------------
333  doh!      
334  hi de ho n
335 (2 rows)
337 -- note: implicit-cast case is tested in char.sql
338 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
339       char(text)      
340 ----------------------
341  doh!                
342  hi de ho neighbor   
343 (2 rows)
345 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
346  char(varchar) 
347 ---------------
348  a         
349  ab        
350  abcd      
351  abcd      
352 (4 rows)
354 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
355  char(name) 
356 ------------
357  namefield 
358 (1 row)
360 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
361    varchar(text)   
362 -------------------
363  doh!
364  hi de ho neighbor
365 (2 rows)
367 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
368  varchar(char) 
369 ---------------
371  ab
372  abcd
373  abcd
374 (4 rows)
376 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
377  varchar(name) 
378 ---------------
379  namefield
380 (1 row)
383 -- test SQL string functions
384 -- E### and T### are feature reference numbers from SQL99
386 -- E021-09 trim function
387 SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
388  bunch o blanks 
389 ----------------
391 (1 row)
393 SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
394  bunch o blanks   
395 ------------------
397 (1 row)
399 SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
400    bunch o blanks 
401 ------------------
403 (1 row)
405 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
406  some Xs 
407 ---------
409 (1 row)
411 -- E021-06 substring expression
412 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
413  34567890 
414 ----------
416 (1 row)
418 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
419  456 
420 -----
422 (1 row)
424 -- test overflow cases
425 SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
426  tring 
427 -------
428  tring
429 (1 row)
431 SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
432  string 
433 --------
434  string
435 (1 row)
437 SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
438 ERROR:  negative substring length not allowed
439 -- T581 regular expression substring (with SQL's bizarre regexp syntax)
440 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"(b_d)#"%' ESCAPE '#') AS "bcd";
441  bcd 
442 -----
443  bcd
444 (1 row)
446 -- obsolete SQL99 syntax
447 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
448  bcd 
449 -----
450  bcd
451 (1 row)
453 -- No match should return NULL
454 SELECT SUBSTRING('abcdefg' SIMILAR '#"(b_d)#"%' ESCAPE '#') IS NULL AS "True";
455  True 
456 ------
458 (1 row)
460 -- Null inputs should return NULL
461 SELECT SUBSTRING('abcdefg' SIMILAR '%' ESCAPE NULL) IS NULL AS "True";
462  True 
463 ------
465 (1 row)
467 SELECT SUBSTRING(NULL SIMILAR '%' ESCAPE '#') IS NULL AS "True";
468  True 
469 ------
471 (1 row)
473 SELECT SUBSTRING('abcdefg' SIMILAR NULL ESCAPE '#') IS NULL AS "True";
474  True 
475 ------
477 (1 row)
479 -- The first and last parts should act non-greedy
480 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"g' ESCAPE '#') AS "bcdef";
481  bcdef 
482 -------
483  bcdef
484 (1 row)
486 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*' ESCAPE '#') AS "abcdefg";
487  abcdefg 
488 ---------
489  abcdefg
490 (1 row)
492 -- Vertical bar in any part affects only that part
493 SELECT SUBSTRING('abcdefg' SIMILAR 'a|b#"%#"g' ESCAPE '#') AS "bcdef";
494  bcdef 
495 -------
496  bcdef
497 (1 row)
499 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"x|g' ESCAPE '#') AS "bcdef";
500  bcdef 
501 -------
502  bcdef
503 (1 row)
505 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%|ab#"g' ESCAPE '#') AS "bcdef";
506  bcdef 
507 -------
508  bcdef
509 (1 row)
511 -- Can't have more than two part separators
512 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*#"x' ESCAPE '#') AS "error";
513 ERROR:  SQL regular expression may not contain more than two escape-double-quote separators
514 CONTEXT:  SQL function "substring" statement 1
515 -- Postgres extension: with 0 or 1 separator, assume parts 1 and 3 are empty
516 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%g' ESCAPE '#') AS "bcdefg";
517  bcdefg 
518 --------
519  bcdefg
520 (1 row)
522 SELECT SUBSTRING('abcdefg' SIMILAR 'a%g' ESCAPE '#') AS "abcdefg";
523  abcdefg 
524 ---------
525  abcdefg
526 (1 row)
528 -- substring() with just two arguments is not allowed by SQL spec;
529 -- we accept it, but we interpret the pattern as a POSIX regexp not SQL
530 SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
531  cde 
532 -----
533  cde
534 (1 row)
536 -- With a parenthesized subexpression, return only what matches the subexpr
537 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
538  cde 
539 -----
540  cde
541 (1 row)
543 -- Check case where we have a match, but not a subexpression match
544 SELECT SUBSTRING('foo' FROM 'foo(bar)?') IS NULL AS t;
545  t 
548 (1 row)
550 -- Check behavior of SIMILAR TO, which uses largely the same regexp variant
551 SELECT 'abcdefg' SIMILAR TO '_bcd%' AS true;
552  true 
553 ------
555 (1 row)
557 SELECT 'abcdefg' SIMILAR TO 'bcd%' AS false;
558  false 
559 -------
561 (1 row)
563 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '#' AS false;
564  false 
565 -------
567 (1 row)
569 SELECT 'abcd%' SIMILAR TO '_bcd#%' ESCAPE '#' AS true;
570  true 
571 ------
573 (1 row)
575 -- Postgres uses '\' as the default escape character, which is not per spec
576 SELECT 'abcdefg' SIMILAR TO '_bcd\%' AS false;
577  false 
578 -------
580 (1 row)
582 -- and an empty string to mean "no escape", which is also not per spec
583 SELECT 'abcd\efg' SIMILAR TO '_bcd\%' ESCAPE '' AS true;
584  true 
585 ------
587 (1 row)
589 -- these behaviors are per spec, though:
590 SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
591  null 
592 ------
594 (1 row)
596 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
597 ERROR:  invalid escape string
598 HINT:  Escape string must be empty or one character.
599 -- Test backslash escapes in regexp_replace's replacement string
600 SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
601  regexp_replace 
602 ----------------
603  (111) 222-3333
604 (1 row)
606 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\&Y', 'g');
607   regexp_replace   
608 -------------------
609  fXooYbaXrrYbaXzzY
610 (1 row)
612 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\\\Y', 'g');
613  regexp_replace 
614 ----------------
615  fX\YbaX\YbaX\Y
616 (1 row)
618 -- not an error, though perhaps it should be:
619 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\Y\\1Z\\');
620  regexp_replace  
621 -----------------
622  fX\YoZ\barrbazz
623 (1 row)
625 SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
626  regexp_replace 
627 ----------------
628  AAA BBB CCC 
629 (1 row)
631 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
632  regexp_replace 
633 ----------------
634  ZAAAZ
635 (1 row)
637 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
638  regexp_replace 
639 ----------------
640  Z Z
641 (1 row)
643 -- invalid regexp option
644 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
645 ERROR:  invalid regular expression option: "z"
646 -- extended regexp_replace tests
647 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1);
648     regexp_replace     
649 -----------------------
650  X PostgreSQL function
651 (1 row)
653 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 2);
654     regexp_replace     
655 -----------------------
656  A PXstgreSQL function
657 (1 row)
659 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, 'i');
660     regexp_replace     
661 -----------------------
662  X PXstgrXSQL fXnctXXn
663 (1 row)
665 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'i');
666     regexp_replace     
667 -----------------------
668  X PostgreSQL function
669 (1 row)
671 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 2, 'i');
672     regexp_replace     
673 -----------------------
674  A PXstgreSQL function
675 (1 row)
677 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, 'i');
678     regexp_replace     
679 -----------------------
680  A PostgrXSQL function
681 (1 row)
683 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 9, 'i');
684     regexp_replace     
685 -----------------------
686  A PostgreSQL function
687 (1 row)
689 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 7, 0, 'i');
690     regexp_replace     
691 -----------------------
692  A PostgrXSQL fXnctXXn
693 (1 row)
695 -- 'g' flag should be ignored when N is specified
696 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'g');
697     regexp_replace     
698 -----------------------
699  A PXstgreSQL function
700 (1 row)
702 -- errors
703 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', -1, 0, 'i');
704 ERROR:  invalid value for parameter "start": -1
705 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, -1, 'i');
706 ERROR:  invalid value for parameter "n": -1
707 -- erroneous invocation of non-extended form
708 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', '1');
709 ERROR:  invalid regular expression option: "1"
710 HINT:  If you meant to use regexp_replace() with a start parameter, cast the fourth argument to integer explicitly.
711 --  regexp_count tests
712 SELECT regexp_count('123123123123123', '(12)3');
713  regexp_count 
714 --------------
715             5
716 (1 row)
718 SELECT regexp_count('123123123123', '123', 1);
719  regexp_count 
720 --------------
721             4
722 (1 row)
724 SELECT regexp_count('123123123123', '123', 3);
725  regexp_count 
726 --------------
727             3
728 (1 row)
730 SELECT regexp_count('123123123123', '123', 33);
731  regexp_count 
732 --------------
733             0
734 (1 row)
736 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, '');
737  regexp_count 
738 --------------
739             0
740 (1 row)
742 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, 'i');
743  regexp_count 
744 --------------
745             4
746 (1 row)
748 -- errors
749 SELECT regexp_count('123123123123', '123', 0);
750 ERROR:  invalid value for parameter "start": 0
751 SELECT regexp_count('123123123123', '123', -3);
752 ERROR:  invalid value for parameter "start": -3
753 -- regexp_like tests
754 SELECT regexp_like('Steven', '^Ste(v|ph)en$');
755  regexp_like 
756 -------------
758 (1 row)
760 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'n');
761  regexp_like 
762 -------------
764 (1 row)
766 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 's');
767  regexp_like 
768 -------------
770 (1 row)
772 SELECT regexp_like('abc', ' a . c ', 'x');
773  regexp_like 
774 -------------
776 (1 row)
778 SELECT regexp_like('abc', 'a.c', 'g');  -- error
779 ERROR:  regexp_like() does not support the "global" option
780 -- regexp_instr tests
781 SELECT regexp_instr('abcdefghi', 'd.f');
782  regexp_instr 
783 --------------
784             4
785 (1 row)
787 SELECT regexp_instr('abcdefghi', 'd.q');
788  regexp_instr 
789 --------------
790             0
791 (1 row)
793 SELECT regexp_instr('abcabcabc', 'a.c');
794  regexp_instr 
795 --------------
796             1
797 (1 row)
799 SELECT regexp_instr('abcabcabc', 'a.c', 2);
800  regexp_instr 
801 --------------
802             4
803 (1 row)
805 SELECT regexp_instr('abcabcabc', 'a.c', 1, 3);
806  regexp_instr 
807 --------------
808             7
809 (1 row)
811 SELECT regexp_instr('abcabcabc', 'a.c', 1, 4);
812  regexp_instr 
813 --------------
814             0
815 (1 row)
817 SELECT regexp_instr('abcabcabc', 'A.C', 1, 2, 0, 'i');
818  regexp_instr 
819 --------------
820             4
821 (1 row)
823 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0);
824  regexp_instr 
825 --------------
826             1
827 (1 row)
829 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1);
830  regexp_instr 
831 --------------
832             1
833 (1 row)
835 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2);
836  regexp_instr 
837 --------------
838             4
839 (1 row)
841 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3);
842  regexp_instr 
843 --------------
844             5
845 (1 row)
847 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4);
848  regexp_instr 
849 --------------
850             7
851 (1 row)
853 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 5);
854  regexp_instr 
855 --------------
856             0
857 (1 row)
859 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0);
860  regexp_instr 
861 --------------
862             9
863 (1 row)
865 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1);
866  regexp_instr 
867 --------------
868             4
869 (1 row)
871 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2);
872  regexp_instr 
873 --------------
874             9
875 (1 row)
877 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3);
878  regexp_instr 
879 --------------
880             7
881 (1 row)
883 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4);
884  regexp_instr 
885 --------------
886             9
887 (1 row)
889 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5);
890  regexp_instr 
891 --------------
892             0
893 (1 row)
895 -- Check case where we have a match, but not a subexpression match
896 SELECT regexp_instr('foo', 'foo(bar)?', 1, 1, 0, '', 1);
897  regexp_instr 
898 --------------
899             0
900 (1 row)
902 -- errors
903 SELECT regexp_instr('abcabcabc', 'a.c', 0, 1);
904 ERROR:  invalid value for parameter "start": 0
905 SELECT regexp_instr('abcabcabc', 'a.c', 1, 0);
906 ERROR:  invalid value for parameter "n": 0
907 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, -1);
908 ERROR:  invalid value for parameter "endoption": -1
909 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 2);
910 ERROR:  invalid value for parameter "endoption": 2
911 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, 'g');
912 ERROR:  regexp_instr() does not support the "global" option
913 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, '', -1);
914 ERROR:  invalid value for parameter "subexpr": -1
915 -- regexp_substr tests
916 SELECT regexp_substr('abcdefghi', 'd.f');
917  regexp_substr 
918 ---------------
919  def
920 (1 row)
922 SELECT regexp_substr('abcdefghi', 'd.q') IS NULL AS t;
923  t 
926 (1 row)
928 SELECT regexp_substr('abcabcabc', 'a.c');
929  regexp_substr 
930 ---------------
931  abc
932 (1 row)
934 SELECT regexp_substr('abcabcabc', 'a.c', 2);
935  regexp_substr 
936 ---------------
937  abc
938 (1 row)
940 SELECT regexp_substr('abcabcabc', 'a.c', 1, 3);
941  regexp_substr 
942 ---------------
943  abc
944 (1 row)
946 SELECT regexp_substr('abcabcabc', 'a.c', 1, 4) IS NULL AS t;
947  t 
950 (1 row)
952 SELECT regexp_substr('abcabcabc', 'A.C', 1, 2, 'i');
953  regexp_substr 
954 ---------------
955  abc
956 (1 row)
958 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0);
959  regexp_substr 
960 ---------------
961  12345678
962 (1 row)
964 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 1);
965  regexp_substr 
966 ---------------
967  123
968 (1 row)
970 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 2);
971  regexp_substr 
972 ---------------
973  45678
974 (1 row)
976 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 3);
977  regexp_substr 
978 ---------------
979  56
980 (1 row)
982 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4);
983  regexp_substr 
984 ---------------
985  78
986 (1 row)
988 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 5) IS NULL AS t;
989  t 
992 (1 row)
994 -- Check case where we have a match, but not a subexpression match
995 SELECT regexp_substr('foo', 'foo(bar)?', 1, 1, '', 1) IS NULL AS t;
996  t 
999 (1 row)
1001 -- errors
1002 SELECT regexp_substr('abcabcabc', 'a.c', 0, 1);
1003 ERROR:  invalid value for parameter "start": 0
1004 SELECT regexp_substr('abcabcabc', 'a.c', 1, 0);
1005 ERROR:  invalid value for parameter "n": 0
1006 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, 'g');
1007 ERROR:  regexp_substr() does not support the "global" option
1008 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, '', -1);
1009 ERROR:  invalid value for parameter "subexpr": -1
1010 -- set so we can tell NULL from empty string
1011 \pset null '\\N'
1012 -- return all matches from regexp
1013 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
1014  regexp_matches 
1015 ----------------
1016  {bar,beque}
1017 (1 row)
1019 -- test case insensitive
1020 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
1021  regexp_matches 
1022 ----------------
1023  {bAR,bEqUE}
1024 (1 row)
1026 -- global option - more than one match
1027 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
1028  regexp_matches 
1029 ----------------
1030  {bar,beque}
1031  {bazil,barf}
1032 (2 rows)
1034 -- empty capture group (matched empty string)
1035 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
1036  regexp_matches 
1037 ----------------
1038  {bar,"",beque}
1039 (1 row)
1041 -- no match
1042 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
1043  regexp_matches 
1044 ----------------
1045 (0 rows)
1047 -- optional capture group did not match, null entry in array
1048 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
1049   regexp_matches  
1050 ------------------
1051  {bar,NULL,beque}
1052 (1 row)
1054 -- no capture groups
1055 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
1056  regexp_matches 
1057 ----------------
1058  {barbeque}
1059 (1 row)
1061 -- start/end-of-line matches are of zero length
1062 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg');
1063  regexp_matches 
1064 ----------------
1065  {""}
1066  {""}
1067  {""}
1068  {""}
1069 (4 rows)
1071 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg');
1072  regexp_matches 
1073 ----------------
1074  {""}
1075  {""}
1076  {""}
1077  {""}
1078 (4 rows)
1080 SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg');
1081  regexp_matches 
1082 ----------------
1083  {1}
1084  {2}
1085  {3}
1086  {4}
1087  {""}
1088 (5 rows)
1090 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg');
1091  regexp_matches 
1092 ----------------
1093  {""}
1094  {1}
1095  {""}
1096  {2}
1097  {""}
1098  {3}
1099  {""}
1100  {4}
1101  {""}
1102  {""}
1103 (10 rows)
1105 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg');
1106  regexp_matches 
1107 ----------------
1108  {""}
1109  {1}
1110  {""}
1111  {2}
1112  {""}
1113  {3}
1114  {""}
1115  {4}
1116  {""}
1117 (9 rows)
1119 -- give me errors
1120 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
1121 ERROR:  invalid regular expression option: "z"
1122 SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
1123 ERROR:  invalid regular expression: parentheses () not balanced
1124 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
1125 ERROR:  invalid regular expression: invalid repetition count(s)
1126 -- split string on regexp
1127 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo;
1128   foo  | length 
1129 -------+--------
1130  the   |      3
1131  quick |      5
1132  brown |      5
1133  fox   |      3
1134  jumps |      5
1135  over  |      4
1136  the   |      3
1137  lazy  |      4
1138  dog   |      3
1139 (9 rows)
1141 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
1142              regexp_split_to_array             
1143 -----------------------------------------------
1144  {the,quick,brown,fox,jumps,over,the,lazy,dog}
1145 (1 row)
1147 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo;
1148  foo | length 
1149 -----+--------
1150  t   |      1
1151  h   |      1
1152  e   |      1
1153  q   |      1
1154  u   |      1
1155  i   |      1
1156  c   |      1
1157  k   |      1
1158  b   |      1
1159  r   |      1
1160  o   |      1
1161  w   |      1
1162  n   |      1
1163  f   |      1
1164  o   |      1
1165  x   |      1
1166  j   |      1
1167  u   |      1
1168  m   |      1
1169  p   |      1
1170  s   |      1
1171  o   |      1
1172  v   |      1
1173  e   |      1
1174  r   |      1
1175  t   |      1
1176  h   |      1
1177  e   |      1
1178  l   |      1
1179  a   |      1
1180  z   |      1
1181  y   |      1
1182  d   |      1
1183  o   |      1
1184  g   |      1
1185 (35 rows)
1187 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$);
1188                           regexp_split_to_array                          
1189 -------------------------------------------------------------------------
1190  {t,h,e,q,u,i,c,k,b,r,o,w,n,f,o,x,j,u,m,p,s,o,v,e,r,t,h,e,l,a,z,y,d,o,g}
1191 (1 row)
1193 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo;
1194  foo | length 
1195 -----+--------
1196  t   |      1
1197  h   |      1
1198  e   |      1
1199      |      1
1200  q   |      1
1201  u   |      1
1202  i   |      1
1203  c   |      1
1204  k   |      1
1205      |      1
1206  b   |      1
1207  r   |      1
1208  o   |      1
1209  w   |      1
1210  n   |      1
1211      |      1
1212  f   |      1
1213  o   |      1
1214  x   |      1
1215      |      1
1216  j   |      1
1217  u   |      1
1218  m   |      1
1219  p   |      1
1220  s   |      1
1221      |      1
1222  o   |      1
1223  v   |      1
1224  e   |      1
1225  r   |      1
1226      |      1
1227  t   |      1
1228  h   |      1
1229  e   |      1
1230      |      1
1231  l   |      1
1232  a   |      1
1233  z   |      1
1234  y   |      1
1235      |      1
1236  d   |      1
1237  o   |      1
1238  g   |      1
1239 (43 rows)
1241 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '');
1242                                           regexp_split_to_array                                          
1243 ---------------------------------------------------------------------------------------------------------
1244  {t,h,e," ",q,u,i,c,k," ",b,r,o,w,n," ",f,o,x," ",j,u,m,p,s," ",o,v,e,r," ",t,h,e," ",l,a,z,y," ",d,o,g}
1245 (1 row)
1247 -- case insensitive
1248 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo;
1249             foo            | length 
1250 ---------------------------+--------
1251  th                        |      2
1252   QUick bROWn FOx jUMPs ov |     25
1253  r Th                      |      4
1254   lazy dOG                 |      9
1255 (4 rows)
1257 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i');
1258                 regexp_split_to_array                
1259 -----------------------------------------------------
1260  {th," QUick bROWn FOx jUMPs ov","r Th"," lazy dOG"}
1261 (1 row)
1263 -- no match of pattern
1264 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo;
1265                      foo                     | length 
1266 ---------------------------------------------+--------
1267  the quick brown fox jumps over the lazy dog |     43
1268 (1 row)
1270 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch');
1271               regexp_split_to_array              
1272 -------------------------------------------------
1273  {"the quick brown fox jumps over the lazy dog"}
1274 (1 row)
1276 -- some corner cases
1277 SELECT regexp_split_to_array('123456','1');
1278  regexp_split_to_array 
1279 -----------------------
1280  {"",23456}
1281 (1 row)
1283 SELECT regexp_split_to_array('123456','6');
1284  regexp_split_to_array 
1285 -----------------------
1286  {12345,""}
1287 (1 row)
1289 SELECT regexp_split_to_array('123456','.');
1290  regexp_split_to_array  
1291 ------------------------
1292  {"","","","","","",""}
1293 (1 row)
1295 SELECT regexp_split_to_array('123456','');
1296  regexp_split_to_array 
1297 -----------------------
1298  {1,2,3,4,5,6}
1299 (1 row)
1301 SELECT regexp_split_to_array('123456','(?:)');
1302  regexp_split_to_array 
1303 -----------------------
1304  {1,2,3,4,5,6}
1305 (1 row)
1307 SELECT regexp_split_to_array('1','');
1308  regexp_split_to_array 
1309 -----------------------
1310  {1}
1311 (1 row)
1313 -- errors
1314 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo;
1315 ERROR:  invalid regular expression option: "z"
1316 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz');
1317 ERROR:  invalid regular expression option: "z"
1318 -- global option meaningless for regexp_split
1319 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo;
1320 ERROR:  regexp_split_to_table() does not support the "global" option
1321 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
1322 ERROR:  regexp_split_to_array() does not support the "global" option
1323 -- change NULL-display back
1324 \pset null ''
1325 -- E021-11 position expression
1326 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
1327  4 
1330 (1 row)
1332 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
1333  5 
1336 (1 row)
1338 -- T312 character overlay function
1339 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
1340  abc45f 
1341 --------
1342  abc45f
1343 (1 row)
1345 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
1346  yabadaba 
1347 ----------
1348  yabadaba
1349 (1 row)
1351 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
1352  yabadabadoo 
1353 -------------
1354  yabadabadoo
1355 (1 row)
1357 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
1358  bubba 
1359 -------
1360  bubba
1361 (1 row)
1364 -- test LIKE
1365 -- Be sure to form every test as a LIKE/NOT LIKE pair.
1367 -- simplest examples
1368 -- E061-04 like predicate
1369 SELECT 'hawkeye' LIKE 'h%' AS "true";
1370  true 
1371 ------
1373 (1 row)
1375 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
1376  false 
1377 -------
1379 (1 row)
1381 SELECT 'hawkeye' LIKE 'H%' AS "false";
1382  false 
1383 -------
1385 (1 row)
1387 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
1388  true 
1389 ------
1391 (1 row)
1393 SELECT 'hawkeye' LIKE 'indio%' AS "false";
1394  false 
1395 -------
1397 (1 row)
1399 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
1400  true 
1401 ------
1403 (1 row)
1405 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
1406  true 
1407 ------
1409 (1 row)
1411 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
1412  false 
1413 -------
1415 (1 row)
1417 SELECT 'indio' LIKE '_ndio' AS "true";
1418  true 
1419 ------
1421 (1 row)
1423 SELECT 'indio' NOT LIKE '_ndio' AS "false";
1424  false 
1425 -------
1427 (1 row)
1429 SELECT 'indio' LIKE 'in__o' AS "true";
1430  true 
1431 ------
1433 (1 row)
1435 SELECT 'indio' NOT LIKE 'in__o' AS "false";
1436  false 
1437 -------
1439 (1 row)
1441 SELECT 'indio' LIKE 'in_o' AS "false";
1442  false 
1443 -------
1445 (1 row)
1447 SELECT 'indio' NOT LIKE 'in_o' AS "true";
1448  true 
1449 ------
1451 (1 row)
1453 SELECT 'abc'::name LIKE '_b_' AS "true";
1454  true 
1455 ------
1457 (1 row)
1459 SELECT 'abc'::name NOT LIKE '_b_' AS "false";
1460  false 
1461 -------
1463 (1 row)
1465 SELECT 'abc'::bytea LIKE '_b_'::bytea AS "true";
1466  true 
1467 ------
1469 (1 row)
1471 SELECT 'abc'::bytea NOT LIKE '_b_'::bytea AS "false";
1472  false 
1473 -------
1475 (1 row)
1477 -- unused escape character
1478 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
1479  true 
1480 ------
1482 (1 row)
1484 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
1485  false 
1486 -------
1488 (1 row)
1490 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
1491  true 
1492 ------
1494 (1 row)
1496 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
1497  false 
1498 -------
1500 (1 row)
1502 -- escape character
1503 -- E061-05 like predicate with escape clause
1504 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
1505  true 
1506 ------
1508 (1 row)
1510 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
1511  false 
1512 -------
1514 (1 row)
1516 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
1517  false 
1518 -------
1520 (1 row)
1522 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
1523  true 
1524 ------
1526 (1 row)
1528 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
1529  true 
1530 ------
1532 (1 row)
1534 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
1535  false 
1536 -------
1538 (1 row)
1540 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
1541  true 
1542 ------
1544 (1 row)
1546 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
1547  false 
1548 -------
1550 (1 row)
1552 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
1553  true 
1554 ------
1556 (1 row)
1558 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
1559  false 
1560 -------
1562 (1 row)
1564 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
1565  true 
1566 ------
1568 (1 row)
1570 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
1571  false 
1572 -------
1574 (1 row)
1576 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
1577  false 
1578 -------
1580 (1 row)
1582 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
1583  true 
1584 ------
1586 (1 row)
1588 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
1589  true 
1590 ------
1592 (1 row)
1594 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
1595  false 
1596 -------
1598 (1 row)
1600 SELECT 'a_c'::bytea LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "true";
1601  true 
1602 ------
1604 (1 row)
1606 SELECT 'a_c'::bytea NOT LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "false";
1607  false 
1608 -------
1610 (1 row)
1612 -- escape character same as pattern character
1613 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
1614  true 
1615 ------
1617 (1 row)
1619 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
1620  false 
1621 -------
1623 (1 row)
1625 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
1626  true 
1627 ------
1629 (1 row)
1631 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
1632  false 
1633 -------
1635 (1 row)
1637 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
1638  true 
1639 ------
1641 (1 row)
1643 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
1644  false 
1645 -------
1647 (1 row)
1649 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
1650  true 
1651 ------
1653 (1 row)
1655 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
1656  false 
1657 -------
1659 (1 row)
1661 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
1662  false 
1663 -------
1665 (1 row)
1667 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
1668  true 
1669 ------
1671 (1 row)
1674 -- test ILIKE (case-insensitive LIKE)
1675 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
1677 SELECT 'hawkeye' ILIKE 'h%' AS "true";
1678  true 
1679 ------
1681 (1 row)
1683 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
1684  false 
1685 -------
1687 (1 row)
1689 SELECT 'hawkeye' ILIKE 'H%' AS "true";
1690  true 
1691 ------
1693 (1 row)
1695 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
1696  false 
1697 -------
1699 (1 row)
1701 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
1702  true 
1703 ------
1705 (1 row)
1707 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
1708  false 
1709 -------
1711 (1 row)
1713 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
1714  true 
1715 ------
1717 (1 row)
1719 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
1720  false 
1721 -------
1723 (1 row)
1725 SELECT 'ABC'::name ILIKE '_b_' AS "true";
1726  true 
1727 ------
1729 (1 row)
1731 SELECT 'ABC'::name NOT ILIKE '_b_' AS "false";
1732  false 
1733 -------
1735 (1 row)
1738 -- test %/_ combination cases, cf bugs #4821 and #5478
1740 SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f;
1741  t | t | f 
1742 ---+---+---
1743  t | t | f
1744 (1 row)
1746 SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f;
1747  t | t | f 
1748 ---+---+---
1749  t | t | f
1750 (1 row)
1752 SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f;
1753  t | t | f 
1754 ---+---+---
1755  t | t | f
1756 (1 row)
1758 SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
1759  t | t | f 
1760 ---+---+---
1761  t | t | f
1762 (1 row)
1764 SELECT 'jack' LIKE '%____%' AS t;
1765  t 
1768 (1 row)
1771 -- basic tests of LIKE with indexes
1773 CREATE TABLE texttest (a text PRIMARY KEY, b int);
1774 SELECT * FROM texttest WHERE a LIKE '%1%';
1775  a | b 
1776 ---+---
1777 (0 rows)
1779 CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
1780 SELECT * FROM byteatest WHERE a LIKE '%1%';
1781  a | b 
1782 ---+---
1783 (0 rows)
1785 DROP TABLE texttest, byteatest;
1787 -- test implicit type conversion
1789 -- E021-07 character concatenation
1790 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
1791  Concat unknown types 
1792 ----------------------
1793  unknown and unknown
1794 (1 row)
1796 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
1797  Concat text to unknown type 
1798 -----------------------------
1799  text and unknown
1800 (1 row)
1802 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
1803  Concat char to unknown type 
1804 -----------------------------
1805  characters and text
1806 (1 row)
1808 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
1809  Concat text to char 
1810 ---------------------
1811  text and characters
1812 (1 row)
1814 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
1815  Concat text to varchar 
1816 ------------------------
1817  text and varchar
1818 (1 row)
1821 -- test substr with toasted text values
1823 CREATE TABLE toasttest(f1 text);
1824 insert into toasttest values(repeat('1234567890',10000));
1825 insert into toasttest values(repeat('1234567890',10000));
1827 -- Ensure that some values are uncompressed, to test the faster substring
1828 -- operation used in that case
1830 alter table toasttest alter column f1 set storage external;
1831 insert into toasttest values(repeat('1234567890',10000));
1832 insert into toasttest values(repeat('1234567890',10000));
1833 -- If the starting position is zero or less, then return from the start of the string
1834 -- adjusting the length to be consistent with the "negative start" per SQL.
1835 SELECT substr(f1, -1, 5) from toasttest;
1836  substr 
1837 --------
1838  123
1839  123
1840  123
1841  123
1842 (4 rows)
1844 -- If the length is less than zero, an ERROR is thrown.
1845 SELECT substr(f1, 5, -1) from toasttest;
1846 ERROR:  negative substring length not allowed
1847 -- If no third argument (length) is provided, the length to the end of the
1848 -- string is assumed.
1849 SELECT substr(f1, 99995) from toasttest;
1850  substr 
1851 --------
1852  567890
1853  567890
1854  567890
1855  567890
1856 (4 rows)
1858 -- If start plus length is > string length, the result is truncated to
1859 -- string length
1860 SELECT substr(f1, 99995, 10) from toasttest;
1861  substr 
1862 --------
1863  567890
1864  567890
1865  567890
1866  567890
1867 (4 rows)
1869 TRUNCATE TABLE toasttest;
1870 INSERT INTO toasttest values (repeat('1234567890',300));
1871 INSERT INTO toasttest values (repeat('1234567890',300));
1872 INSERT INTO toasttest values (repeat('1234567890',300));
1873 INSERT INTO toasttest values (repeat('1234567890',300));
1874 -- expect >0 blocks
1875 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
1876   FROM pg_class where relname = 'toasttest';
1877  is_empty 
1878 ----------
1880 (1 row)
1882 TRUNCATE TABLE toasttest;
1883 ALTER TABLE toasttest set (toast_tuple_target = 4080);
1884 INSERT INTO toasttest values (repeat('1234567890',300));
1885 INSERT INTO toasttest values (repeat('1234567890',300));
1886 INSERT INTO toasttest values (repeat('1234567890',300));
1887 INSERT INTO toasttest values (repeat('1234567890',300));
1888 -- expect 0 blocks
1889 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
1890   FROM pg_class where relname = 'toasttest';
1891  is_empty 
1892 ----------
1894 (1 row)
1896 DROP TABLE toasttest;
1898 -- test substr with toasted bytea values
1900 CREATE TABLE toasttest(f1 bytea);
1901 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1902 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1904 -- Ensure that some values are uncompressed, to test the faster substring
1905 -- operation used in that case
1907 alter table toasttest alter column f1 set storage external;
1908 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1909 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1910 -- If the starting position is zero or less, then return from the start of the string
1911 -- adjusting the length to be consistent with the "negative start" per SQL.
1912 SELECT substr(f1, -1, 5) from toasttest;
1913  substr 
1914 --------
1915  123
1916  123
1917  123
1918  123
1919 (4 rows)
1921 -- If the length is less than zero, an ERROR is thrown.
1922 SELECT substr(f1, 5, -1) from toasttest;
1923 ERROR:  negative substring length not allowed
1924 -- If no third argument (length) is provided, the length to the end of the
1925 -- string is assumed.
1926 SELECT substr(f1, 99995) from toasttest;
1927  substr 
1928 --------
1929  567890
1930  567890
1931  567890
1932  567890
1933 (4 rows)
1935 -- If start plus length is > string length, the result is truncated to
1936 -- string length
1937 SELECT substr(f1, 99995, 10) from toasttest;
1938  substr 
1939 --------
1940  567890
1941  567890
1942  567890
1943  567890
1944 (4 rows)
1946 DROP TABLE toasttest;
1947 -- test internally compressing datums
1948 -- this tests compressing a datum to a very small size which exercises a
1949 -- corner case in packed-varlena handling: even though small, the compressed
1950 -- datum must be given a 4-byte header because there are no bits to indicate
1951 -- compression in a 1-byte header
1952 CREATE TABLE toasttest (c char(4096));
1953 INSERT INTO toasttest VALUES('x');
1954 SELECT length(c), c::text FROM toasttest;
1955  length | c 
1956 --------+---
1957       1 | x
1958 (1 row)
1960 SELECT c FROM toasttest;
1961                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 c                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
1962 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1963  x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
1964 (1 row)
1966 DROP TABLE toasttest;
1968 -- test length
1970 SELECT length('abcdef') AS "length_6";
1971  length_6 
1972 ----------
1973         6
1974 (1 row)
1977 -- test strpos
1979 SELECT strpos('abcdef', 'cd') AS "pos_3";
1980  pos_3 
1981 -------
1982      3
1983 (1 row)
1985 SELECT strpos('abcdef', 'xy') AS "pos_0";
1986  pos_0 
1987 -------
1988      0
1989 (1 row)
1991 SELECT strpos('abcdef', '') AS "pos_1";
1992  pos_1 
1993 -------
1994      1
1995 (1 row)
1997 SELECT strpos('', 'xy') AS "pos_0";
1998  pos_0 
1999 -------
2000      0
2001 (1 row)
2003 SELECT strpos('', '') AS "pos_1";
2004  pos_1 
2005 -------
2006      1
2007 (1 row)
2010 -- test replace
2012 SELECT replace('abcdef', 'de', '45') AS "abc45f";
2013  abc45f 
2014 --------
2015  abc45f
2016 (1 row)
2018 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
2019  ya123da123doo 
2020 ---------------
2021  ya123da123doo
2022 (1 row)
2024 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
2025  yaoo 
2026 ------
2027  yaoo
2028 (1 row)
2031 -- test split_part
2033 select split_part('','@',1) AS "empty string";
2034  empty string 
2035 --------------
2037 (1 row)
2039 select split_part('','@',-1) AS "empty string";
2040  empty string 
2041 --------------
2043 (1 row)
2045 select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
2046  joeuser@mydatabase 
2047 --------------------
2048  joeuser@mydatabase
2049 (1 row)
2051 select split_part('joeuser@mydatabase','',2) AS "empty string";
2052  empty string 
2053 --------------
2055 (1 row)
2057 select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
2058  joeuser@mydatabase 
2059 --------------------
2060  joeuser@mydatabase
2061 (1 row)
2063 select split_part('joeuser@mydatabase','',-2) AS "empty string";
2064  empty string 
2065 --------------
2067 (1 row)
2069 select split_part('joeuser@mydatabase','@',0) AS "an error";
2070 ERROR:  field position must not be zero
2071 select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
2072  joeuser@mydatabase 
2073 --------------------
2074  joeuser@mydatabase
2075 (1 row)
2077 select split_part('joeuser@mydatabase','@@',2) AS "empty string";
2078  empty string 
2079 --------------
2081 (1 row)
2083 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
2084  joeuser 
2085 ---------
2086  joeuser
2087 (1 row)
2089 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
2090  mydatabase 
2091 ------------
2092  mydatabase
2093 (1 row)
2095 select split_part('joeuser@mydatabase','@',3) AS "empty string";
2096  empty string 
2097 --------------
2099 (1 row)
2101 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
2102  joeuser 
2103 ---------
2104  joeuser
2105 (1 row)
2107 select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
2108  mydatabase 
2109 ------------
2110  mydatabase
2111 (1 row)
2113 select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
2114  joeuser 
2115 ---------
2116  joeuser
2117 (1 row)
2119 select split_part('joeuser@mydatabase','@',-3) AS "empty string";
2120  empty string 
2121 --------------
2123 (1 row)
2125 select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
2126  mydatabase 
2127 ------------
2128  mydatabase
2129 (1 row)
2132 -- test to_hex
2134 select to_hex(256*256*256 - 1) AS "ffffff";
2135  ffffff 
2136 --------
2137  ffffff
2138 (1 row)
2140 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
2141  ffffffff 
2142 ----------
2143  ffffffff
2144 (1 row)
2147 -- SHA-2
2149 SET bytea_output TO hex;
2150 SELECT sha224('');
2151                            sha224                           
2152 ------------------------------------------------------------
2153  \xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
2154 (1 row)
2156 SELECT sha224('The quick brown fox jumps over the lazy dog.');
2157                            sha224                           
2158 ------------------------------------------------------------
2159  \x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c
2160 (1 row)
2162 SELECT sha256('');
2163                                sha256                               
2164 --------------------------------------------------------------------
2165  \xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2166 (1 row)
2168 SELECT sha256('The quick brown fox jumps over the lazy dog.');
2169                                sha256                               
2170 --------------------------------------------------------------------
2171  \xef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c
2172 (1 row)
2174 SELECT sha384('');
2175                                                sha384                                               
2176 ----------------------------------------------------------------------------------------------------
2177  \x38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
2178 (1 row)
2180 SELECT sha384('The quick brown fox jumps over the lazy dog.');
2181                                                sha384                                               
2182 ----------------------------------------------------------------------------------------------------
2183  \xed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7
2184 (1 row)
2186 SELECT sha512('');
2187                                                                sha512                                                               
2188 ------------------------------------------------------------------------------------------------------------------------------------
2189  \xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
2190 (1 row)
2192 SELECT sha512('The quick brown fox jumps over the lazy dog.');
2193                                                                sha512                                                               
2194 ------------------------------------------------------------------------------------------------------------------------------------
2195  \x91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
2196 (1 row)
2199 -- encode/decode
2201 SELECT encode('\x1234567890abcdef00', 'hex');
2202        encode       
2203 --------------------
2204  1234567890abcdef00
2205 (1 row)
2207 SELECT decode('1234567890abcdef00', 'hex');
2208         decode        
2209 ----------------------
2210  \x1234567890abcdef00
2211 (1 row)
2213 SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
2214                                     encode                                    
2215 ------------------------------------------------------------------------------
2216  EjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN7wABEjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN+
2217  7wABEjRWeJCrze8AAQ==
2218 (1 row)
2220 SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
2221                      'base64'), 'base64');
2222                                                                      decode                                                                     
2223 ------------------------------------------------------------------------------------------------------------------------------------------------
2224  \x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001
2225 (1 row)
2227 SELECT encode('\x1234567890abcdef00', 'escape');
2228            encode            
2229 -----------------------------
2230  \x124Vx\220\253\315\357\000
2231 (1 row)
2233 SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
2234         decode        
2235 ----------------------
2236  \x1234567890abcdef00
2237 (1 row)
2240 -- get_bit/set_bit etc
2242 SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
2243  get_bit 
2244 ---------
2245        1
2246 (1 row)
2248 SELECT get_bit('\x1234567890abcdef00'::bytea, 99);  -- error
2249 ERROR:  index 99 out of valid range, 0..71
2250 SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
2251        set_bit        
2252 ----------------------
2253  \x1234567890a3cdef00
2254 (1 row)
2256 SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0);  -- error
2257 ERROR:  index 99 out of valid range, 0..71
2258 SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
2259  get_byte 
2260 ----------
2261       120
2262 (1 row)
2264 SELECT get_byte('\x1234567890abcdef00'::bytea, 99);  -- error
2265 ERROR:  index 99 out of valid range, 0..8
2266 SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
2267        set_byte       
2268 ----------------------
2269  \x1234567890abcd0b00
2270 (1 row)
2272 SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11);  -- error
2273 ERROR:  index 99 out of valid range, 0..8
2275 -- test behavior of escape_string_warning and standard_conforming_strings options
2277 set escape_string_warning = off;
2278 set standard_conforming_strings = off;
2279 show escape_string_warning;
2280  escape_string_warning 
2281 -----------------------
2282  off
2283 (1 row)
2285 show standard_conforming_strings;
2286  standard_conforming_strings 
2287 -----------------------------
2288  off
2289 (1 row)
2291 set escape_string_warning = on;
2292 set standard_conforming_strings = on;
2293 show escape_string_warning;
2294  escape_string_warning 
2295 -----------------------
2296  on
2297 (1 row)
2299 show standard_conforming_strings;
2300  standard_conforming_strings 
2301 -----------------------------
2302  on
2303 (1 row)
2305 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
2306   f1   |   f2   |   f3    |  f4   |   f5   | f6 
2307 -------+--------+---------+-------+--------+----
2308  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2309 (1 row)
2311 set standard_conforming_strings = off;
2312 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
2313 WARNING:  nonstandard use of \\ in a string literal
2314 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2315                ^
2316 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2317 WARNING:  nonstandard use of \\ in a string literal
2318 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2319                                ^
2320 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2321 WARNING:  nonstandard use of \\ in a string literal
2322 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2323                                                  ^
2324 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2325 WARNING:  nonstandard use of \\ in a string literal
2326 LINE 1: ...bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'  ...
2327                                                              ^
2328 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2329 WARNING:  nonstandard use of \\ in a string literal
2330 LINE 1: ...'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd'...
2331                                                              ^
2332 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2333 WARNING:  nonstandard use of \\ in a string literal
2334 LINE 1: ...'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as ...
2335                                                              ^
2336 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
2337   f1   |   f2   |   f3    |  f4   |   f5   | f6 
2338 -------+--------+---------+-------+--------+----
2339  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2340 (1 row)
2342 set escape_string_warning = off;
2343 set standard_conforming_strings = on;
2344 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
2345   f1   |   f2   |   f3    |  f4   |   f5   | f6 
2346 -------+--------+---------+-------+--------+----
2347  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2348 (1 row)
2350 set standard_conforming_strings = off;
2351 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
2352   f1   |   f2   |   f3    |  f4   |   f5   | f6 
2353 -------+--------+---------+-------+--------+----
2354  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2355 (1 row)
2357 reset standard_conforming_strings;
2359 -- Additional string functions
2361 SET bytea_output TO escape;
2362 SELECT initcap('hi THOMAS');
2363   initcap  
2364 -----------
2365  Hi Thomas
2366 (1 row)
2368 SELECT lpad('hi', 5, 'xy');
2369  lpad  
2370 -------
2371  xyxhi
2372 (1 row)
2374 SELECT lpad('hi', 5);
2375  lpad  
2376 -------
2377     hi
2378 (1 row)
2380 SELECT lpad('hi', -5, 'xy');
2381  lpad 
2382 ------
2384 (1 row)
2386 SELECT lpad('hello', 2);
2387  lpad 
2388 ------
2389  he
2390 (1 row)
2392 SELECT lpad('hi', 5, '');
2393  lpad 
2394 ------
2395  hi
2396 (1 row)
2398 SELECT rpad('hi', 5, 'xy');
2399  rpad  
2400 -------
2401  hixyx
2402 (1 row)
2404 SELECT rpad('hi', 5);
2405  rpad  
2406 -------
2407  hi   
2408 (1 row)
2410 SELECT rpad('hi', -5, 'xy');
2411  rpad 
2412 ------
2414 (1 row)
2416 SELECT rpad('hello', 2);
2417  rpad 
2418 ------
2419  he
2420 (1 row)
2422 SELECT rpad('hi', 5, '');
2423  rpad 
2424 ------
2425  hi
2426 (1 row)
2428 SELECT ltrim('zzzytrim', 'xyz');
2429  ltrim 
2430 -------
2431  trim
2432 (1 row)
2434 SELECT translate('', '14', 'ax');
2435  translate 
2436 -----------
2438 (1 row)
2440 SELECT translate('12345', '14', 'ax');
2441  translate 
2442 -----------
2443  a23x5
2444 (1 row)
2446 SELECT ascii('x');
2447  ascii 
2448 -------
2449    120
2450 (1 row)
2452 SELECT ascii('');
2453  ascii 
2454 -------
2455      0
2456 (1 row)
2458 SELECT chr(65);
2459  chr 
2460 -----
2462 (1 row)
2464 SELECT chr(0);
2465 ERROR:  null character not permitted
2466 SELECT repeat('Pg', 4);
2467   repeat  
2468 ----------
2469  PgPgPgPg
2470 (1 row)
2472 SELECT repeat('Pg', -4);
2473  repeat 
2474 --------
2476 (1 row)
2478 SELECT SUBSTRING('1234567890'::bytea FROM 3) "34567890";
2479  34567890 
2480 ----------
2481  34567890
2482 (1 row)
2484 SELECT SUBSTRING('1234567890'::bytea FROM 4 FOR 3) AS "456";
2485  456 
2486 -----
2487  456
2488 (1 row)
2490 SELECT SUBSTRING('string'::bytea FROM 2 FOR 2147483646) AS "tring";
2491  tring 
2492 -------
2493  tring
2494 (1 row)
2496 SELECT SUBSTRING('string'::bytea FROM -10 FOR 2147483646) AS "string";
2497  string 
2498 --------
2499  string
2500 (1 row)
2502 SELECT SUBSTRING('string'::bytea FROM -10 FOR -2147483646) AS "error";
2503 ERROR:  negative substring length not allowed
2504 SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2505  btrim 
2506 -------
2507  Tom
2508 (1 row)
2510 SELECT trim(leading E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2511   ltrim  
2512 ---------
2513  Tom\000
2514 (1 row)
2516 SELECT trim(trailing E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2517   rtrim  
2518 ---------
2519  \000Tom
2520 (1 row)
2522 SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
2523  btrim 
2524 -------
2525  trim
2526 (1 row)
2528 SELECT btrim(''::bytea, E'\\000'::bytea);
2529  btrim 
2530 -------
2532 (1 row)
2534 SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
2535     btrim     
2536 --------------
2537  \000trim\000
2538 (1 row)
2540 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape');
2541    encode    
2542 -------------
2543  TTh\x01omas
2544 (1 row)
2546 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape');
2547        encode       
2548 --------------------
2549  Th\000omas\x02\x03
2550 (1 row)
2552 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape');
2553      encode      
2554 -----------------
2555  Th\000o\x02\x03
2556 (1 row)
2558 SELECT bit_count('\x1234567890'::bytea);
2559  bit_count 
2560 -----------
2561         15
2562 (1 row)
2564 SELECT unistr('\0064at\+0000610');
2565  unistr 
2566 --------
2567  data0
2568 (1 row)
2570 SELECT unistr('d\u0061t\U000000610');
2571  unistr 
2572 --------
2573  data0
2574 (1 row)
2576 SELECT unistr('a\\b');
2577  unistr 
2578 --------
2579  a\b
2580 (1 row)
2582 -- errors:
2583 SELECT unistr('wrong: \db99');
2584 ERROR:  invalid Unicode surrogate pair
2585 SELECT unistr('wrong: \db99\0061');
2586 ERROR:  invalid Unicode surrogate pair
2587 SELECT unistr('wrong: \+00db99\+000061');
2588 ERROR:  invalid Unicode surrogate pair
2589 SELECT unistr('wrong: \+2FFFFF');
2590 ERROR:  invalid Unicode code point: 2FFFFF
2591 SELECT unistr('wrong: \udb99\u0061');
2592 ERROR:  invalid Unicode surrogate pair
2593 SELECT unistr('wrong: \U0000db99\U00000061');
2594 ERROR:  invalid Unicode surrogate pair
2595 SELECT unistr('wrong: \U002FFFFF');
2596 ERROR:  invalid Unicode code point: 2FFFFF
2597 SELECT unistr('wrong: \xyz');
2598 ERROR:  invalid Unicode escape
2599 HINT:  Unicode escapes must be \XXXX, \+XXXXXX, \uXXXX, or \UXXXXXXXX.