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