Disable the use of Unicode escapes in string constants (U&'') when
[PostgreSQL.git] / src / test / regress / expected / strings.out
blob831fb9e2037cd82b95a5e1fb0582963344029671
1 --
2 -- STRINGS
3 -- Test various data entry syntaxes.
4 --
5 -- SQL92 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&' \' UESCAPE '!' AS "tricky";
39  tricky 
40 --------
41   \
42 (1 row)
44 SELECT 'tricky' AS U&"\" UESCAPE '!';
45    \    
46 --------
47  tricky
48 (1 row)
50 SELECT U&'wrong: \061';
51 ERROR:  invalid Unicode escape value at or near "\061'"
52 LINE 1: SELECT U&'wrong: \061';
53                          ^
54 SELECT U&'wrong: \+0061';
55 ERROR:  invalid Unicode escape value at or near "\+0061'"
56 LINE 1: SELECT U&'wrong: \+0061';
57                          ^
58 SELECT U&'wrong: +0061' UESCAPE '+';
59 ERROR:  invalid Unicode escape character at or near "+'"
60 LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
61                                          ^
62 SET standard_conforming_strings TO off;
63 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
64 ERROR:  unsafe use of string constant with Unicode escapes
65 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
66 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
67 ERROR:  unsafe use of string constant with Unicode escapes
68 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
69 SELECT U&' \' UESCAPE '!' AS "tricky";
70 ERROR:  unsafe use of string constant with Unicode escapes
71 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
72 SELECT 'tricky' AS U&"\" UESCAPE '!';
73    \    
74 --------
75  tricky
76 (1 row)
78 SELECT U&'wrong: \061';
79 ERROR:  unsafe use of string constant with Unicode escapes
80 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
81 SELECT U&'wrong: \+0061';
82 ERROR:  unsafe use of string constant with Unicode escapes
83 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
84 SELECT U&'wrong: +0061' UESCAPE '+';
85 ERROR:  unsafe use of string constant with Unicode escapes
86 DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
87 RESET standard_conforming_strings;
89 -- test conversions between various string types
90 -- E021-10 implicit casting among the character data types
92 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
93  text(char) 
94 ------------
95  a
96  ab
97  abcd
98  abcd
99 (4 rows)
101 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
102  text(varchar) 
103 ---------------
105  ab
106  abcd
107  abcd
108 (4 rows)
110 SELECT CAST(name 'namefield' AS text) AS "text(name)";
111  text(name) 
112 ------------
113  namefield
114 (1 row)
116 -- since this is an explicit cast, it should truncate w/o error:
117 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
118  char(text) 
119 ------------
120  doh!      
121  hi de ho n
122 (2 rows)
124 -- note: implicit-cast case is tested in char.sql
125 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
126       char(text)      
127 ----------------------
128  doh!                
129  hi de ho neighbor   
130 (2 rows)
132 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
133  char(varchar) 
134 ---------------
135  a         
136  ab        
137  abcd      
138  abcd      
139 (4 rows)
141 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
142  char(name) 
143 ------------
144  namefield 
145 (1 row)
147 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
148    varchar(text)   
149 -------------------
150  doh!
151  hi de ho neighbor
152 (2 rows)
154 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
155  varchar(char) 
156 ---------------
158  ab
159  abcd
160  abcd
161 (4 rows)
163 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
164  varchar(name) 
165 ---------------
166  namefield
167 (1 row)
170 -- test SQL92 string functions
171 -- E### and T### are feature reference numbers from SQL99
173 -- E021-09 trim function
174 SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
175  bunch o blanks 
176 ----------------
178 (1 row)
180 SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
181  bunch o blanks   
182 ------------------
184 (1 row)
186 SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
187    bunch o blanks 
188 ------------------
190 (1 row)
192 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
193  some Xs 
194 ---------
196 (1 row)
198 -- E021-06 substring expression
199 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
200  34567890 
201 ----------
203 (1 row)
205 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
206  456 
207 -----
209 (1 row)
211 -- T581 regular expression substring (with SQL99's bizarre regexp syntax)
212 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
213  bcd 
214 -----
215  bcd
216 (1 row)
218 -- No match should return NULL
219 SELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True";
220  True 
221 ------
223 (1 row)
225 -- Null inputs should return NULL
226 SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True";
227  True 
228 ------
230 (1 row)
232 SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True";
233  True 
234 ------
236 (1 row)
238 SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True";
239  True 
240 ------
242 (1 row)
244 -- PostgreSQL extension to allow omitting the escape character;
245 -- here the regexp is taken as Posix syntax
246 SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
247  cde 
248 -----
249  cde
250 (1 row)
252 -- With a parenthesized subexpression, return only what matches the subexpr
253 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
254  cde 
255 -----
256  cde
257 (1 row)
259 -- PostgreSQL extension to allow using back reference in replace string;
260 SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
261  regexp_replace 
262 ----------------
263  (111) 222-3333
264 (1 row)
266 SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
267  regexp_replace 
268 ----------------
269  AAA BBB CCC 
270 (1 row)
272 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
273  regexp_replace 
274 ----------------
275  ZAAAZ
276 (1 row)
278 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
279  regexp_replace 
280 ----------------
281  Z Z
282 (1 row)
284 -- invalid regexp option
285 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
286 ERROR:  invalid regexp option: "z"
287 -- set so we can tell NULL from empty string
288 \pset null '\\N'
289 -- return all matches from regexp
290 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
291  regexp_matches 
292 ----------------
293  {bar,beque}
294 (1 row)
296 -- test case insensitive
297 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
298  regexp_matches 
299 ----------------
300  {bAR,bEqUE}
301 (1 row)
303 -- global option - more than one match
304 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
305  regexp_matches 
306 ----------------
307  {bar,beque}
308  {bazil,barf}
309 (2 rows)
311 -- empty capture group (matched empty string)
312 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
313  regexp_matches 
314 ----------------
315  {bar,"",beque}
316 (1 row)
318 -- no match
319 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
320  regexp_matches 
321 ----------------
322 (0 rows)
324 -- optional capture group did not match, null entry in array
325 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
326   regexp_matches  
327 ------------------
328  {bar,NULL,beque}
329 (1 row)
331 -- no capture groups
332 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
333  regexp_matches 
334 ----------------
335  {barbeque}
336 (1 row)
338 -- give me errors
339 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
340 ERROR:  invalid regexp option: "z"
341 SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
342 ERROR:  invalid regular expression: parentheses () not balanced
343 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
344 ERROR:  invalid regular expression: invalid repetition count(s)
345 -- split string on regexp
346 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', $re$\s+$re$) AS foo;
347   foo   | length 
348 --------+--------
349  the    |      3
350  quick  |      5
351  brown  |      5
352  fox    |      3
353  jumped |      6
354  over   |      4
355  the    |      3
356  lazy   |      4
357  dog    |      3
358 (9 rows)
360 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', $re$\s+$re$);
361              regexp_split_to_array              
362 ------------------------------------------------
363  {the,quick,brown,fox,jumped,over,the,lazy,dog}
364 (1 row)
366 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', $re$\s*$re$) AS foo;
367  foo | length 
368 -----+--------
369  t   |      1
370  h   |      1
371  e   |      1
372  q   |      1
373  u   |      1
374  i   |      1
375  c   |      1
376  k   |      1
377  b   |      1
378  r   |      1
379  o   |      1
380  w   |      1
381  n   |      1
382  f   |      1
383  o   |      1
384  x   |      1
385  j   |      1
386  u   |      1
387  m   |      1
388  p   |      1
389  e   |      1
390  d   |      1
391  o   |      1
392  v   |      1
393  e   |      1
394  r   |      1
395  t   |      1
396  h   |      1
397  e   |      1
398  l   |      1
399  a   |      1
400  z   |      1
401  y   |      1
402  d   |      1
403  o   |      1
404  g   |      1
405 (36 rows)
407 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', $re$\s*$re$);
408                            regexp_split_to_array                           
409 ---------------------------------------------------------------------------
410  {t,h,e,q,u,i,c,k,b,r,o,w,n,f,o,x,j,u,m,p,e,d,o,v,e,r,t,h,e,l,a,z,y,d,o,g}
411 (1 row)
413 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', '') AS foo;
414  foo | length 
415 -----+--------
416  t   |      1
417  h   |      1
418  e   |      1
419      |      1
420  q   |      1
421  u   |      1
422  i   |      1
423  c   |      1
424  k   |      1
425      |      1
426  b   |      1
427  r   |      1
428  o   |      1
429  w   |      1
430  n   |      1
431      |      1
432  f   |      1
433  o   |      1
434  x   |      1
435      |      1
436  j   |      1
437  u   |      1
438  m   |      1
439  p   |      1
440  e   |      1
441  d   |      1
442      |      1
443  o   |      1
444  v   |      1
445  e   |      1
446  r   |      1
447      |      1
448  t   |      1
449  h   |      1
450  e   |      1
451      |      1
452  l   |      1
453  a   |      1
454  z   |      1
455  y   |      1
456      |      1
457  d   |      1
458  o   |      1
459  g   |      1
460 (44 rows)
462 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', '');
463                                            regexp_split_to_array                                           
464 -----------------------------------------------------------------------------------------------------------
465  {t,h,e," ",q,u,i,c,k," ",b,r,o,w,n," ",f,o,x," ",j,u,m,p,e,d," ",o,v,e,r," ",t,h,e," ",l,a,z,y," ",d,o,g}
466 (1 row)
468 -- case insensitive
469 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i') AS foo;
470           foo          | length 
471 -----------------------+--------
472  th                    |      2
473   QUick bROWn FOx jUMP |     21
474  d ov                  |      4
475  r TH                  |      4
476   lazy dOG             |      9
477 (5 rows)
479 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'i');
480                  regexp_split_to_array                  
481 --------------------------------------------------------
482  {th," QUick bROWn FOx jUMP","d ov","r TH"," lazy dOG"}
483 (1 row)
485 -- no match of pattern
486 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', 'nomatch') AS foo;
487                      foo                      | length 
488 ----------------------------------------------+--------
489  the quick brown fox jumped over the lazy dog |     44
490 (1 row)
492 SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', 'nomatch');
493               regexp_split_to_array               
494 --------------------------------------------------
495  {"the quick brown fox jumped over the lazy dog"}
496 (1 row)
498 -- some corner cases
499 SELECT regexp_split_to_array('123456','1');
500  regexp_split_to_array 
501 -----------------------
502  {"",23456}
503 (1 row)
505 SELECT regexp_split_to_array('123456','6');
506  regexp_split_to_array 
507 -----------------------
508  {12345,""}
509 (1 row)
511 SELECT regexp_split_to_array('123456','.');
512  regexp_split_to_array  
513 ------------------------
514  {"","","","","","",""}
515 (1 row)
517 -- errors
518 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'zippy') AS foo;
519 ERROR:  invalid regexp option: "z"
520 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'iz');
521 ERROR:  invalid regexp option: "z"
522 -- global option meaningless for regexp_split
523 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'g') AS foo;
524 ERROR:  regexp_split does not support the global option
525 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPed ovEr THE lazy dOG', 'e', 'g');
526 ERROR:  regexp_split does not support the global option
527 -- change NULL-display back
528 \pset null ''
529 -- E021-11 position expression
530 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
531  4 
534 (1 row)
536 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
537  5 
540 (1 row)
542 -- T312 character overlay function
543 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
544  abc45f 
545 --------
546  abc45f
547 (1 row)
549 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
550  yabadaba 
551 ----------
552  yabadaba
553 (1 row)
555 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
556  yabadabadoo 
557 -------------
558  yabadabadoo
559 (1 row)
561 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
562  bubba 
563 -------
564  bubba
565 (1 row)
568 -- test LIKE
569 -- Be sure to form every test as a LIKE/NOT LIKE pair.
571 -- simplest examples
572 -- E061-04 like predicate
573 SELECT 'hawkeye' LIKE 'h%' AS "true";
574  true 
575 ------
577 (1 row)
579 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
580  false 
581 -------
583 (1 row)
585 SELECT 'hawkeye' LIKE 'H%' AS "false";
586  false 
587 -------
589 (1 row)
591 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
592  true 
593 ------
595 (1 row)
597 SELECT 'hawkeye' LIKE 'indio%' AS "false";
598  false 
599 -------
601 (1 row)
603 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
604  true 
605 ------
607 (1 row)
609 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
610  true 
611 ------
613 (1 row)
615 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
616  false 
617 -------
619 (1 row)
621 SELECT 'indio' LIKE '_ndio' AS "true";
622  true 
623 ------
625 (1 row)
627 SELECT 'indio' NOT LIKE '_ndio' AS "false";
628  false 
629 -------
631 (1 row)
633 SELECT 'indio' LIKE 'in__o' AS "true";
634  true 
635 ------
637 (1 row)
639 SELECT 'indio' NOT LIKE 'in__o' AS "false";
640  false 
641 -------
643 (1 row)
645 SELECT 'indio' LIKE 'in_o' AS "false";
646  false 
647 -------
649 (1 row)
651 SELECT 'indio' NOT LIKE 'in_o' AS "true";
652  true 
653 ------
655 (1 row)
657 -- unused escape character
658 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
659  true 
660 ------
662 (1 row)
664 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
665  false 
666 -------
668 (1 row)
670 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
671  true 
672 ------
674 (1 row)
676 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
677  false 
678 -------
680 (1 row)
682 -- escape character
683 -- E061-05 like predicate with escape clause
684 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
685  true 
686 ------
688 (1 row)
690 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
691  false 
692 -------
694 (1 row)
696 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
697  false 
698 -------
700 (1 row)
702 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
703  true 
704 ------
706 (1 row)
708 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
709  true 
710 ------
712 (1 row)
714 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
715  false 
716 -------
718 (1 row)
720 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
721  true 
722 ------
724 (1 row)
726 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
727  false 
728 -------
730 (1 row)
732 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
733  true 
734 ------
736 (1 row)
738 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
739  false 
740 -------
742 (1 row)
744 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
745  true 
746 ------
748 (1 row)
750 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
751  false 
752 -------
754 (1 row)
756 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
757  false 
758 -------
760 (1 row)
762 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
763  true 
764 ------
766 (1 row)
768 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
769  true 
770 ------
772 (1 row)
774 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
775  false 
776 -------
778 (1 row)
780 -- escape character same as pattern character
781 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
782  true 
783 ------
785 (1 row)
787 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
788  false 
789 -------
791 (1 row)
793 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
794  true 
795 ------
797 (1 row)
799 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
800  false 
801 -------
803 (1 row)
805 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
806  true 
807 ------
809 (1 row)
811 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
812  false 
813 -------
815 (1 row)
817 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
818  true 
819 ------
821 (1 row)
823 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
824  false 
825 -------
827 (1 row)
829 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
830  false 
831 -------
833 (1 row)
835 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
836  true 
837 ------
839 (1 row)
842 -- test ILIKE (case-insensitive LIKE)
843 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
845 SELECT 'hawkeye' ILIKE 'h%' AS "true";
846  true 
847 ------
849 (1 row)
851 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
852  false 
853 -------
855 (1 row)
857 SELECT 'hawkeye' ILIKE 'H%' AS "true";
858  true 
859 ------
861 (1 row)
863 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
864  false 
865 -------
867 (1 row)
869 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
870  true 
871 ------
873 (1 row)
875 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
876  false 
877 -------
879 (1 row)
881 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
882  true 
883 ------
885 (1 row)
887 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
888  false 
889 -------
891 (1 row)
894 -- test implicit type conversion
896 -- E021-07 character concatenation
897 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
898  Concat unknown types 
899 ----------------------
900  unknown and unknown
901 (1 row)
903 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
904  Concat text to unknown type 
905 -----------------------------
906  text and unknown
907 (1 row)
909 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
910  Concat char to unknown type 
911 -----------------------------
912  characters and text
913 (1 row)
915 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
916  Concat text to char 
917 ---------------------
918  text and characters
919 (1 row)
921 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
922  Concat text to varchar 
923 ------------------------
924  text and varchar
925 (1 row)
928 -- test substr with toasted text values
930 CREATE TABLE toasttest(f1 text);
931 insert into toasttest values(repeat('1234567890',10000));
932 insert into toasttest values(repeat('1234567890',10000));
934 -- Ensure that some values are uncompressed, to test the faster substring
935 -- operation used in that case
937 alter table toasttest alter column f1 set storage external;
938 insert into toasttest values(repeat('1234567890',10000));
939 insert into toasttest values(repeat('1234567890',10000));
940 -- If the starting position is zero or less, then return from the start of the string
941 -- adjusting the length to be consistent with the "negative start" per SQL92.
942 SELECT substr(f1, -1, 5) from toasttest;
943  substr 
944 --------
945  123
946  123
947  123
948  123
949 (4 rows)
951 -- If the length is less than zero, an ERROR is thrown.
952 SELECT substr(f1, 5, -1) from toasttest;
953 ERROR:  negative substring length not allowed
954 -- If no third argument (length) is provided, the length to the end of the
955 -- string is assumed.
956 SELECT substr(f1, 99995) from toasttest;
957  substr 
958 --------
959  567890
960  567890
961  567890
962  567890
963 (4 rows)
965 -- If start plus length is > string length, the result is truncated to
966 -- string length
967 SELECT substr(f1, 99995, 10) from toasttest;
968  substr 
969 --------
970  567890
971  567890
972  567890
973  567890
974 (4 rows)
976 DROP TABLE toasttest;
978 -- test substr with toasted bytea values
980 CREATE TABLE toasttest(f1 bytea);
981 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
982 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
984 -- Ensure that some values are uncompressed, to test the faster substring
985 -- operation used in that case
987 alter table toasttest alter column f1 set storage external;
988 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
989 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
990 -- If the starting position is zero or less, then return from the start of the string
991 -- adjusting the length to be consistent with the "negative start" per SQL92.
992 SELECT substr(f1, -1, 5) from toasttest;
993  substr 
994 --------
995  123
996  123
997  123
998  123
999 (4 rows)
1001 -- If the length is less than zero, an ERROR is thrown.
1002 SELECT substr(f1, 5, -1) from toasttest;
1003 ERROR:  negative substring length not allowed
1004 -- If no third argument (length) is provided, the length to the end of the
1005 -- string is assumed.
1006 SELECT substr(f1, 99995) from toasttest;
1007  substr 
1008 --------
1009  567890
1010  567890
1011  567890
1012  567890
1013 (4 rows)
1015 -- If start plus length is > string length, the result is truncated to
1016 -- string length
1017 SELECT substr(f1, 99995, 10) from toasttest;
1018  substr 
1019 --------
1020  567890
1021  567890
1022  567890
1023  567890
1024 (4 rows)
1026 DROP TABLE toasttest;
1027 -- test internally compressing datums
1028 -- this tests compressing a datum to a very small size which exercises a
1029 -- corner case in packed-varlena handling: even though small, the compressed
1030 -- datum must be given a 4-byte header because there are no bits to indicate
1031 -- compression in a 1-byte header
1032 CREATE TABLE toasttest (c char(4096));
1033 INSERT INTO toasttest VALUES('x');
1034 SELECT length(c), c::text FROM toasttest;
1035  length | c 
1036 --------+---
1037       1 | x
1038 (1 row)
1040 SELECT c FROM toasttest;
1041                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 c                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
1042 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1043  x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
1044 (1 row)
1046 DROP TABLE toasttest;
1048 -- test length
1050 SELECT length('abcdef') AS "length_6";
1051  length_6 
1052 ----------
1053         6
1054 (1 row)
1057 -- test strpos
1059 SELECT strpos('abcdef', 'cd') AS "pos_3";
1060  pos_3 
1061 -------
1062      3
1063 (1 row)
1065 SELECT strpos('abcdef', 'xy') AS "pos_0";
1066  pos_0 
1067 -------
1068      0
1069 (1 row)
1072 -- test replace
1074 SELECT replace('abcdef', 'de', '45') AS "abc45f";
1075  abc45f 
1076 --------
1077  abc45f
1078 (1 row)
1080 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
1081  ya123da123doo 
1082 ---------------
1083  ya123da123doo
1084 (1 row)
1086 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
1087  yaoo 
1088 ------
1089  yaoo
1090 (1 row)
1093 -- test split_part
1095 select split_part('joeuser@mydatabase','@',0) AS "an error";
1096 ERROR:  field position must be greater than zero
1097 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
1098  joeuser 
1099 ---------
1100  joeuser
1101 (1 row)
1103 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
1104  mydatabase 
1105 ------------
1106  mydatabase
1107 (1 row)
1109 select split_part('joeuser@mydatabase','@',3) AS "empty string";
1110  empty string 
1111 --------------
1113 (1 row)
1115 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
1116  joeuser 
1117 ---------
1118  joeuser
1119 (1 row)
1122 -- test to_hex
1124 select to_hex(256*256*256 - 1) AS "ffffff";
1125  ffffff 
1126 --------
1127  ffffff
1128 (1 row)
1130 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
1131  ffffffff 
1132 ----------
1133  ffffffff
1134 (1 row)
1137 -- MD5 test suite - from IETF RFC 1321
1138 -- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
1140 select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
1141  TRUE 
1142 ------
1144 (1 row)
1146 select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
1147  TRUE 
1148 ------
1150 (1 row)
1152 select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
1153  TRUE 
1154 ------
1156 (1 row)
1158 select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
1159  TRUE 
1160 ------
1162 (1 row)
1164 select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
1165  TRUE 
1166 ------
1168 (1 row)
1170 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
1171  TRUE 
1172 ------
1174 (1 row)
1176 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
1177  TRUE 
1178 ------
1180 (1 row)
1182 select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
1183  TRUE 
1184 ------
1186 (1 row)
1188 select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
1189  TRUE 
1190 ------
1192 (1 row)
1194 select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
1195  TRUE 
1196 ------
1198 (1 row)
1200 select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
1201  TRUE 
1202 ------
1204 (1 row)
1206 select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
1207  TRUE 
1208 ------
1210 (1 row)
1212 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
1213  TRUE 
1214 ------
1216 (1 row)
1218 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
1219  TRUE 
1220 ------
1222 (1 row)
1225 -- test behavior of escape_string_warning and standard_conforming_strings options
1227 set escape_string_warning = off;
1228 set standard_conforming_strings = off;
1229 show escape_string_warning;
1230  escape_string_warning 
1231 -----------------------
1232  off
1233 (1 row)
1235 show standard_conforming_strings;
1236  standard_conforming_strings 
1237 -----------------------------
1238  off
1239 (1 row)
1241 set escape_string_warning = on;
1242 set standard_conforming_strings = on;
1243 show escape_string_warning;
1244  escape_string_warning 
1245 -----------------------
1246  on
1247 (1 row)
1249 show standard_conforming_strings;
1250  standard_conforming_strings 
1251 -----------------------------
1252  on
1253 (1 row)
1255 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
1256   f1   |   f2   |   f3    |  f4   |   f5   | f6 
1257 -------+--------+---------+-------+--------+----
1258  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
1259 (1 row)
1261 set standard_conforming_strings = off;
1262 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
1263 WARNING:  nonstandard use of \\ in a string literal
1264 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
1265                ^
1266 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1267 WARNING:  nonstandard use of \\ in a string literal
1268 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
1269                                ^
1270 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1271 WARNING:  nonstandard use of \\ in a string literal
1272 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
1273                                                  ^
1274 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1275 WARNING:  nonstandard use of \\ in a string literal
1276 LINE 1: ...bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'  ...
1277                                                              ^
1278 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1279 WARNING:  nonstandard use of \\ in a string literal
1280 LINE 1: ...'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd'...
1281                                                              ^
1282 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1283 WARNING:  nonstandard use of \\ in a string literal
1284 LINE 1: ...'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as ...
1285                                                              ^
1286 HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
1287   f1   |   f2   |   f3    |  f4   |   f5   | f6 
1288 -------+--------+---------+-------+--------+----
1289  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
1290 (1 row)
1292 set escape_string_warning = off;
1293 set standard_conforming_strings = on;
1294 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
1295   f1   |   f2   |   f3    |  f4   |   f5   | f6 
1296 -------+--------+---------+-------+--------+----
1297  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
1298 (1 row)
1300 set standard_conforming_strings = off;
1301 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
1302   f1   |   f2   |   f3    |  f4   |   f5   | f6 
1303 -------+--------+---------+-------+--------+----
1304  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
1305 (1 row)
1308 -- Additional string functions
1310 SELECT initcap('hi THOMAS');
1311   initcap  
1312 -----------
1313  Hi Thomas
1314 (1 row)
1316 SELECT lpad('hi', 5, 'xy');
1317  lpad  
1318 -------
1319  xyxhi
1320 (1 row)
1322 SELECT lpad('hi', 5);
1323  lpad  
1324 -------
1325     hi
1326 (1 row)
1328 SELECT lpad('hi', -5, 'xy');
1329  lpad 
1330 ------
1332 (1 row)
1334 SELECT lpad('hello', 2);
1335  lpad 
1336 ------
1337  he
1338 (1 row)
1340 SELECT lpad('hi', 5, '');
1341  lpad 
1342 ------
1343  hi
1344 (1 row)
1346 SELECT rpad('hi', 5, 'xy');
1347  rpad  
1348 -------
1349  hixyx
1350 (1 row)
1352 SELECT rpad('hi', 5);
1353  rpad  
1354 -------
1355  hi   
1356 (1 row)
1358 SELECT rpad('hi', -5, 'xy');
1359  rpad 
1360 ------
1362 (1 row)
1364 SELECT rpad('hello', 2);
1365  rpad 
1366 ------
1367  he
1368 (1 row)
1370 SELECT rpad('hi', 5, '');
1371  rpad 
1372 ------
1373  hi
1374 (1 row)
1376 SELECT ltrim('zzzytrim', 'xyz');
1377  ltrim 
1378 -------
1379  trim
1380 (1 row)
1382 SELECT translate('', '14', 'ax');
1383  translate 
1384 -----------
1386 (1 row)
1388 SELECT translate('12345', '14', 'ax');
1389  translate 
1390 -----------
1391  a23x5
1392 (1 row)
1394 SELECT ascii('x');
1395  ascii 
1396 -------
1397    120
1398 (1 row)
1400 SELECT ascii('');
1401  ascii 
1402 -------
1403      0
1404 (1 row)
1406 SELECT chr(65);
1407  chr 
1408 -----
1410 (1 row)
1412 SELECT chr(0);
1413 ERROR:  null character not permitted
1414 SELECT repeat('Pg', 4);
1415   repeat  
1416 ----------
1417  PgPgPgPg
1418 (1 row)
1420 SELECT repeat('Pg', -4);
1421  repeat 
1422 --------
1424 (1 row)
1426 SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
1427  btrim 
1428 -------
1429  Tom
1430 (1 row)
1432 SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
1433  btrim 
1434 -------
1435  trim
1436 (1 row)
1438 SELECT btrim(''::bytea, E'\\000'::bytea);
1439  btrim 
1440 -------
1442 (1 row)
1444 SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
1445     btrim     
1446 --------------
1447  \000trim\000
1448 (1 row)