3 -- Test various data entry syntaxes.
5 -- SQL string continuation syntax
6 -- E021-03 character string literals
10 AS "Three lines to one";
12 -------------------------------------
13 first line - next line - third line
16 -- illegal string continuation syntax
18 ' - next line' /* this comment is not allowed here */
20 AS "Illegal comment within continuation";
21 ERROR: syntax error at or near "' - third line'"
22 LINE 3: ' - third line'
25 SET standard_conforming_strings TO on;
26 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
32 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
38 SELECT U&'a\\b' AS "a\b";
44 SELECT U&' \' UESCAPE '!' AS "tricky";
50 SELECT 'tricky' AS U&"\" UESCAPE '!';
56 SELECT U&'wrong: \061';
57 ERROR: invalid Unicode escape
58 LINE 1: SELECT U&'wrong: \061';
60 HINT: Unicode escapes must be \XXXX or \+XXXXXX.
61 SELECT U&'wrong: \+0061';
62 ERROR: invalid Unicode escape
63 LINE 1: SELECT U&'wrong: \+0061';
65 HINT: Unicode escapes must be \XXXX or \+XXXXXX.
66 SELECT U&'wrong: +0061' UESCAPE +;
67 ERROR: UESCAPE must be followed by a simple string literal at or near "+"
68 LINE 1: SELECT U&'wrong: +0061' UESCAPE +;
70 SELECT U&'wrong: +0061' UESCAPE '+';
71 ERROR: invalid Unicode escape character at or near "'+'"
72 LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
74 SELECT U&'wrong: \db99';
75 ERROR: invalid Unicode surrogate pair
76 LINE 1: SELECT U&'wrong: \db99';
78 SELECT U&'wrong: \db99xy';
79 ERROR: invalid Unicode surrogate pair
80 LINE 1: SELECT U&'wrong: \db99xy';
82 SELECT U&'wrong: \db99\\';
83 ERROR: invalid Unicode surrogate pair
84 LINE 1: SELECT U&'wrong: \db99\\';
86 SELECT U&'wrong: \db99\0061';
87 ERROR: invalid Unicode surrogate pair
88 LINE 1: SELECT U&'wrong: \db99\0061';
90 SELECT U&'wrong: \+00db99\+000061';
91 ERROR: invalid Unicode surrogate pair
92 LINE 1: SELECT U&'wrong: \+00db99\+000061';
94 SELECT U&'wrong: \+2FFFFF';
95 ERROR: invalid Unicode escape value
96 LINE 1: SELECT U&'wrong: \+2FFFFF';
98 -- while we're here, check the same cases in E-style literals
99 SELECT E'd\u0061t\U00000061' AS "data";
105 SELECT E'a\\b' AS "a\b";
111 SELECT E'wrong: \u061';
112 ERROR: invalid Unicode escape
113 LINE 1: SELECT E'wrong: \u061';
115 HINT: Unicode escapes must be \uXXXX or \UXXXXXXXX.
116 SELECT E'wrong: \U0061';
117 ERROR: invalid Unicode escape
118 LINE 1: SELECT E'wrong: \U0061';
120 HINT: Unicode escapes must be \uXXXX or \UXXXXXXXX.
121 SELECT E'wrong: \udb99';
122 ERROR: invalid Unicode surrogate pair at or near "'"
123 LINE 1: SELECT E'wrong: \udb99';
125 SELECT E'wrong: \udb99xy';
126 ERROR: invalid Unicode surrogate pair at or near "x"
127 LINE 1: SELECT E'wrong: \udb99xy';
129 SELECT E'wrong: \udb99\\';
130 ERROR: invalid Unicode surrogate pair at or near "\"
131 LINE 1: SELECT E'wrong: \udb99\\';
133 SELECT E'wrong: \udb99\u0061';
134 ERROR: invalid Unicode surrogate pair at or near "\u0061"
135 LINE 1: SELECT E'wrong: \udb99\u0061';
137 SELECT E'wrong: \U0000db99\U00000061';
138 ERROR: invalid Unicode surrogate pair at or near "\U00000061"
139 LINE 1: SELECT E'wrong: \U0000db99\U00000061';
141 SELECT E'wrong: \U002FFFFF';
142 ERROR: invalid Unicode escape value at or near "\U002FFFFF"
143 LINE 1: SELECT E'wrong: \U002FFFFF';
145 SET standard_conforming_strings TO off;
146 SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
147 ERROR: unsafe use of string constant with Unicode escapes
148 LINE 1: SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
150 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
151 SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
152 ERROR: unsafe use of string constant with Unicode escapes
153 LINE 1: SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061...
155 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
156 SELECT U&' \' UESCAPE '!' AS "tricky";
157 ERROR: unsafe use of string constant with Unicode escapes
158 LINE 1: SELECT U&' \' UESCAPE '!' AS "tricky";
160 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
161 SELECT 'tricky' AS U&"\" UESCAPE '!';
167 SELECT U&'wrong: \061';
168 ERROR: unsafe use of string constant with Unicode escapes
169 LINE 1: SELECT U&'wrong: \061';
171 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
172 SELECT U&'wrong: \+0061';
173 ERROR: unsafe use of string constant with Unicode escapes
174 LINE 1: SELECT U&'wrong: \+0061';
176 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
177 SELECT U&'wrong: +0061' UESCAPE '+';
178 ERROR: unsafe use of string constant with Unicode escapes
179 LINE 1: SELECT U&'wrong: +0061' UESCAPE '+';
181 DETAIL: String constants with Unicode escapes cannot be used when "standard_conforming_strings" is off.
182 RESET standard_conforming_strings;
184 SET bytea_output TO hex;
185 SELECT E'\\xDeAdBeEf'::bytea;
191 SELECT E'\\x De Ad Be Ef '::bytea;
197 SELECT E'\\xDeAdBeE'::bytea;
198 ERROR: invalid hexadecimal data: odd number of digits
199 LINE 1: SELECT E'\\xDeAdBeE'::bytea;
201 SELECT E'\\xDeAdBeEx'::bytea;
202 ERROR: invalid hexadecimal digit: "x"
203 LINE 1: SELECT E'\\xDeAdBeEx'::bytea;
205 SELECT E'\\xDe00BeEf'::bytea;
211 SELECT E'DeAdBeEf'::bytea;
217 SELECT E'De\\000dBeEf'::bytea;
223 SELECT E'De\123dBeEf'::bytea;
229 SELECT E'De\\123dBeEf'::bytea;
235 SELECT E'De\\678dBeEf'::bytea;
236 ERROR: invalid input syntax for type bytea
237 LINE 1: SELECT E'De\\678dBeEf'::bytea;
239 SET bytea_output TO escape;
240 SELECT E'\\xDeAdBeEf'::bytea;
246 SELECT E'\\x De Ad Be Ef '::bytea;
252 SELECT E'\\xDe00BeEf'::bytea;
258 SELECT E'DeAdBeEf'::bytea;
264 SELECT E'De\\000dBeEf'::bytea;
270 SELECT E'De\\123dBeEf'::bytea;
276 -- Test non-error-throwing API too
277 SELECT pg_input_is_valid(E'\\xDeAdBeE', 'bytea');
283 SELECT * FROM pg_input_error_info(E'\\xDeAdBeE', 'bytea');
284 message | detail | hint | sql_error_code
285 ------------------------------------------------+--------+------+----------------
286 invalid hexadecimal data: odd number of digits | | | 22023
289 SELECT * FROM pg_input_error_info(E'\\xDeAdBeEx', 'bytea');
290 message | detail | hint | sql_error_code
291 --------------------------------+--------+------+----------------
292 invalid hexadecimal digit: "x" | | | 22023
295 SELECT * FROM pg_input_error_info(E'foo\\99bar', 'bytea');
296 message | detail | hint | sql_error_code
297 -------------------------------------+--------+------+----------------
298 invalid input syntax for type bytea | | | 22P02
302 -- test conversions between various string types
303 -- E021-10 implicit casting among the character data types
305 SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
314 SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
323 SELECT CAST(name 'namefield' AS text) AS "text(name)";
329 -- since this is an explicit cast, it should truncate w/o error:
330 SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
337 -- note: implicit-cast case is tested in char.sql
338 SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
340 ----------------------
345 SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
354 SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
360 SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
367 SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
376 SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
383 -- test SQL string functions
384 -- E### and T### are feature reference numbers from SQL99
386 -- E021-09 trim function
387 SELECT TRIM(BOTH FROM ' bunch o blanks ') = 'bunch o blanks' AS "bunch o blanks";
393 SELECT TRIM(LEADING FROM ' bunch o blanks ') = 'bunch o blanks ' AS "bunch o blanks ";
399 SELECT TRIM(TRAILING FROM ' bunch o blanks ') = ' bunch o blanks' AS " bunch o blanks";
405 SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
411 -- E021-06 substring expression
412 SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
418 SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
424 -- test overflow cases
425 SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
431 SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
437 SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
438 ERROR: negative substring length not allowed
439 -- T581 regular expression substring (with SQL's bizarre regexp syntax)
440 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"(b_d)#"%' ESCAPE '#') AS "bcd";
446 -- obsolete SQL99 syntax
447 SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
453 -- No match should return NULL
454 SELECT SUBSTRING('abcdefg' SIMILAR '#"(b_d)#"%' ESCAPE '#') IS NULL AS "True";
460 -- Null inputs should return NULL
461 SELECT SUBSTRING('abcdefg' SIMILAR '%' ESCAPE NULL) IS NULL AS "True";
467 SELECT SUBSTRING(NULL SIMILAR '%' ESCAPE '#') IS NULL AS "True";
473 SELECT SUBSTRING('abcdefg' SIMILAR NULL ESCAPE '#') IS NULL AS "True";
479 -- The first and last parts should act non-greedy
480 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"g' ESCAPE '#') AS "bcdef";
486 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*' ESCAPE '#') AS "abcdefg";
492 -- Vertical bar in any part affects only that part
493 SELECT SUBSTRING('abcdefg' SIMILAR 'a|b#"%#"g' ESCAPE '#') AS "bcdef";
499 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"x|g' ESCAPE '#') AS "bcdef";
505 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%|ab#"g' ESCAPE '#') AS "bcdef";
511 -- Can't have more than two part separators
512 SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*#"x' ESCAPE '#') AS "error";
513 ERROR: SQL regular expression may not contain more than two escape-double-quote separators
514 CONTEXT: SQL function "substring" statement 1
515 -- Postgres extension: with 0 or 1 separator, assume parts 1 and 3 are empty
516 SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%g' ESCAPE '#') AS "bcdefg";
522 SELECT SUBSTRING('abcdefg' SIMILAR 'a%g' ESCAPE '#') AS "abcdefg";
528 -- substring() with just two arguments is not allowed by SQL spec;
529 -- we accept it, but we interpret the pattern as a POSIX regexp not SQL
530 SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
536 -- With a parenthesized subexpression, return only what matches the subexpr
537 SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
543 -- Check case where we have a match, but not a subexpression match
544 SELECT SUBSTRING('foo' FROM 'foo(bar)?') IS NULL AS t;
550 -- Check behavior of SIMILAR TO, which uses largely the same regexp variant
551 SELECT 'abcdefg' SIMILAR TO '_bcd%' AS true;
557 SELECT 'abcdefg' SIMILAR TO 'bcd%' AS false;
563 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '#' AS false;
569 SELECT 'abcd%' SIMILAR TO '_bcd#%' ESCAPE '#' AS true;
575 -- Postgres uses '\' as the default escape character, which is not per spec
576 SELECT 'abcdefg' SIMILAR TO '_bcd\%' AS false;
582 -- and an empty string to mean "no escape", which is also not per spec
583 SELECT 'abcd\efg' SIMILAR TO '_bcd\%' ESCAPE '' AS true;
589 -- these behaviors are per spec, though:
590 SELECT 'abcdefg' SIMILAR TO '_bcd%' ESCAPE NULL AS null;
596 SELECT 'abcdefg' SIMILAR TO '_bcd#%' ESCAPE '##' AS error;
597 ERROR: invalid escape string
598 HINT: Escape string must be empty or one character.
599 -- Test backslash escapes in regexp_replace's replacement string
600 SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
606 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\&Y', 'g');
612 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\\\Y', 'g');
618 -- not an error, though perhaps it should be:
619 SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\Y\\1Z\\');
625 SELECT regexp_replace('AAA BBB CCC ', E'\\s+', ' ', 'g');
631 SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
637 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
643 -- invalid regexp option
644 SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
645 ERROR: invalid regular expression option: "z"
646 -- extended regexp_replace tests
647 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1);
649 -----------------------
650 X PostgreSQL function
653 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 2);
655 -----------------------
656 A PXstgreSQL function
659 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, 'i');
661 -----------------------
662 X PXstgrXSQL fXnctXXn
665 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'i');
667 -----------------------
668 X PostgreSQL function
671 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 2, 'i');
673 -----------------------
674 A PXstgreSQL function
677 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, 'i');
679 -----------------------
680 A PostgrXSQL function
683 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 9, 'i');
685 -----------------------
686 A PostgreSQL function
689 SELECT regexp_replace('A PostgreSQL function', 'A|e|i|o|u', 'X', 7, 0, 'i');
691 -----------------------
692 A PostgrXSQL fXnctXXn
695 -- 'g' flag should be ignored when N is specified
696 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, 'g');
698 -----------------------
699 A PXstgreSQL function
703 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', -1, 0, 'i');
704 ERROR: invalid value for parameter "start": -1
705 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, -1, 'i');
706 ERROR: invalid value for parameter "n": -1
707 -- erroneous invocation of non-extended form
708 SELECT regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', '1');
709 ERROR: invalid regular expression option: "1"
710 HINT: If you meant to use regexp_replace() with a start parameter, cast the fourth argument to integer explicitly.
711 -- regexp_count tests
712 SELECT regexp_count('123123123123123', '(12)3');
718 SELECT regexp_count('123123123123', '123', 1);
724 SELECT regexp_count('123123123123', '123', 3);
730 SELECT regexp_count('123123123123', '123', 33);
736 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, '');
742 SELECT regexp_count('ABCABCABCABC', 'Abc', 1, 'i');
749 SELECT regexp_count('123123123123', '123', 0);
750 ERROR: invalid value for parameter "start": 0
751 SELECT regexp_count('123123123123', '123', -3);
752 ERROR: invalid value for parameter "start": -3
754 SELECT regexp_like('Steven', '^Ste(v|ph)en$');
760 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'n');
766 SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 's');
772 SELECT regexp_like('abc', ' a . c ', 'x');
778 SELECT regexp_like('abc', 'a.c', 'g'); -- error
779 ERROR: regexp_like() does not support the "global" option
780 -- regexp_instr tests
781 SELECT regexp_instr('abcdefghi', 'd.f');
787 SELECT regexp_instr('abcdefghi', 'd.q');
793 SELECT regexp_instr('abcabcabc', 'a.c');
799 SELECT regexp_instr('abcabcabc', 'a.c', 2);
805 SELECT regexp_instr('abcabcabc', 'a.c', 1, 3);
811 SELECT regexp_instr('abcabcabc', 'a.c', 1, 4);
817 SELECT regexp_instr('abcabcabc', 'A.C', 1, 2, 0, 'i');
823 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0);
829 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1);
835 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2);
841 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3);
847 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4);
853 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 5);
859 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0);
865 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1);
871 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2);
877 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3);
883 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4);
889 SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5);
895 -- Check case where we have a match, but not a subexpression match
896 SELECT regexp_instr('foo', 'foo(bar)?', 1, 1, 0, '', 1);
903 SELECT regexp_instr('abcabcabc', 'a.c', 0, 1);
904 ERROR: invalid value for parameter "start": 0
905 SELECT regexp_instr('abcabcabc', 'a.c', 1, 0);
906 ERROR: invalid value for parameter "n": 0
907 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, -1);
908 ERROR: invalid value for parameter "endoption": -1
909 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 2);
910 ERROR: invalid value for parameter "endoption": 2
911 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, 'g');
912 ERROR: regexp_instr() does not support the "global" option
913 SELECT regexp_instr('abcabcabc', 'a.c', 1, 1, 0, '', -1);
914 ERROR: invalid value for parameter "subexpr": -1
915 -- regexp_substr tests
916 SELECT regexp_substr('abcdefghi', 'd.f');
922 SELECT regexp_substr('abcdefghi', 'd.q') IS NULL AS t;
928 SELECT regexp_substr('abcabcabc', 'a.c');
934 SELECT regexp_substr('abcabcabc', 'a.c', 2);
940 SELECT regexp_substr('abcabcabc', 'a.c', 1, 3);
946 SELECT regexp_substr('abcabcabc', 'a.c', 1, 4) IS NULL AS t;
952 SELECT regexp_substr('abcabcabc', 'A.C', 1, 2, 'i');
958 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0);
964 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 1);
970 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 2);
976 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 3);
982 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4);
988 SELECT regexp_substr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 5) IS NULL AS t;
994 -- Check case where we have a match, but not a subexpression match
995 SELECT regexp_substr('foo', 'foo(bar)?', 1, 1, '', 1) IS NULL AS t;
1002 SELECT regexp_substr('abcabcabc', 'a.c', 0, 1);
1003 ERROR: invalid value for parameter "start": 0
1004 SELECT regexp_substr('abcabcabc', 'a.c', 1, 0);
1005 ERROR: invalid value for parameter "n": 0
1006 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, 'g');
1007 ERROR: regexp_substr() does not support the "global" option
1008 SELECT regexp_substr('abcabcabc', 'a.c', 1, 1, '', -1);
1009 ERROR: invalid value for parameter "subexpr": -1
1010 -- set so we can tell NULL from empty string
1012 -- return all matches from regexp
1013 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
1019 -- test case insensitive
1020 SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
1026 -- global option - more than one match
1027 SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
1034 -- empty capture group (matched empty string)
1035 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
1042 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
1047 -- optional capture group did not match, null entry in array
1048 SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
1054 -- no capture groups
1055 SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
1061 -- start/end-of-line matches are of zero length
1062 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg');
1071 SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg');
1080 SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg');
1090 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg');
1105 SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg');
1120 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
1121 ERROR: invalid regular expression option: "z"
1122 SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
1123 ERROR: invalid regular expression: parentheses () not balanced
1124 SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
1125 ERROR: invalid regular expression: invalid repetition count(s)
1126 -- split string on regexp
1127 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo;
1141 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
1142 regexp_split_to_array
1143 -----------------------------------------------
1144 {the,quick,brown,fox,jumps,over,the,lazy,dog}
1147 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo;
1187 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$);
1188 regexp_split_to_array
1189 -------------------------------------------------------------------------
1190 {t,h,e,q,u,i,c,k,b,r,o,w,n,f,o,x,j,u,m,p,s,o,v,e,r,t,h,e,l,a,z,y,d,o,g}
1193 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo;
1241 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '');
1242 regexp_split_to_array
1243 ---------------------------------------------------------------------------------------------------------
1244 {t,h,e," ",q,u,i,c,k," ",b,r,o,w,n," ",f,o,x," ",j,u,m,p,s," ",o,v,e,r," ",t,h,e," ",l,a,z,y," ",d,o,g}
1248 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo;
1250 ---------------------------+--------
1252 QUick bROWn FOx jUMPs ov | 25
1257 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i');
1258 regexp_split_to_array
1259 -----------------------------------------------------
1260 {th," QUick bROWn FOx jUMPs ov","r Th"," lazy dOG"}
1263 -- no match of pattern
1264 SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo;
1266 ---------------------------------------------+--------
1267 the quick brown fox jumps over the lazy dog | 43
1270 SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch');
1271 regexp_split_to_array
1272 -------------------------------------------------
1273 {"the quick brown fox jumps over the lazy dog"}
1276 -- some corner cases
1277 SELECT regexp_split_to_array('123456','1');
1278 regexp_split_to_array
1279 -----------------------
1283 SELECT regexp_split_to_array('123456','6');
1284 regexp_split_to_array
1285 -----------------------
1289 SELECT regexp_split_to_array('123456','.');
1290 regexp_split_to_array
1291 ------------------------
1292 {"","","","","","",""}
1295 SELECT regexp_split_to_array('123456','');
1296 regexp_split_to_array
1297 -----------------------
1301 SELECT regexp_split_to_array('123456','(?:)');
1302 regexp_split_to_array
1303 -----------------------
1307 SELECT regexp_split_to_array('1','');
1308 regexp_split_to_array
1309 -----------------------
1314 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo;
1315 ERROR: invalid regular expression option: "z"
1316 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz');
1317 ERROR: invalid regular expression option: "z"
1318 -- global option meaningless for regexp_split
1319 SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo;
1320 ERROR: regexp_split_to_table() does not support the "global" option
1321 SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
1322 ERROR: regexp_split_to_array() does not support the "global" option
1323 -- change NULL-display back
1325 -- E021-11 position expression
1326 SELECT POSITION('4' IN '1234567890') = '4' AS "4";
1332 SELECT POSITION('5' IN '1234567890') = '5' AS "5";
1338 -- T312 character overlay function
1339 SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
1345 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
1351 SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
1357 SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
1365 -- Be sure to form every test as a LIKE/NOT LIKE pair.
1367 -- simplest examples
1368 -- E061-04 like predicate
1369 SELECT 'hawkeye' LIKE 'h%' AS "true";
1375 SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
1381 SELECT 'hawkeye' LIKE 'H%' AS "false";
1387 SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
1393 SELECT 'hawkeye' LIKE 'indio%' AS "false";
1399 SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
1405 SELECT 'hawkeye' LIKE 'h%eye' AS "true";
1411 SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
1417 SELECT 'indio' LIKE '_ndio' AS "true";
1423 SELECT 'indio' NOT LIKE '_ndio' AS "false";
1429 SELECT 'indio' LIKE 'in__o' AS "true";
1435 SELECT 'indio' NOT LIKE 'in__o' AS "false";
1441 SELECT 'indio' LIKE 'in_o' AS "false";
1447 SELECT 'indio' NOT LIKE 'in_o' AS "true";
1453 SELECT 'abc'::name LIKE '_b_' AS "true";
1459 SELECT 'abc'::name NOT LIKE '_b_' AS "false";
1465 SELECT 'abc'::bytea LIKE '_b_'::bytea AS "true";
1471 SELECT 'abc'::bytea NOT LIKE '_b_'::bytea AS "false";
1477 -- unused escape character
1478 SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
1484 SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
1490 SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
1496 SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
1503 -- E061-05 like predicate with escape clause
1504 SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
1510 SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
1516 SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
1522 SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
1528 SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
1534 SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
1540 SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
1546 SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
1552 SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
1558 SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
1564 SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
1570 SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
1576 SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
1582 SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
1588 SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
1594 SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
1600 SELECT 'a_c'::bytea LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "true";
1606 SELECT 'a_c'::bytea NOT LIKE 'a$__'::bytea ESCAPE '$'::bytea AS "false";
1612 -- escape character same as pattern character
1613 SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
1619 SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
1625 SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
1631 SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
1637 SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
1643 SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
1649 SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
1655 SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
1661 SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
1667 SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
1674 -- test ILIKE (case-insensitive LIKE)
1675 -- Be sure to form every test as an ILIKE/NOT ILIKE pair.
1677 SELECT 'hawkeye' ILIKE 'h%' AS "true";
1683 SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
1689 SELECT 'hawkeye' ILIKE 'H%' AS "true";
1695 SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
1701 SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
1707 SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
1713 SELECT 'Hawkeye' ILIKE 'h%' AS "true";
1719 SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
1725 SELECT 'ABC'::name ILIKE '_b_' AS "true";
1731 SELECT 'ABC'::name NOT ILIKE '_b_' AS "false";
1738 -- test %/_ combination cases, cf bugs #4821 and #5478
1740 SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f;
1746 SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f;
1752 SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f;
1758 SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
1764 SELECT 'jack' LIKE '%____%' AS t;
1771 -- basic tests of LIKE with indexes
1773 CREATE TABLE texttest (a text PRIMARY KEY, b int);
1774 SELECT * FROM texttest WHERE a LIKE '%1%';
1779 CREATE TABLE byteatest (a bytea PRIMARY KEY, b int);
1780 SELECT * FROM byteatest WHERE a LIKE '%1%';
1785 DROP TABLE texttest, byteatest;
1787 -- test implicit type conversion
1789 -- E021-07 character concatenation
1790 SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
1791 Concat unknown types
1792 ----------------------
1796 SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
1797 Concat text to unknown type
1798 -----------------------------
1802 SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
1803 Concat char to unknown type
1804 -----------------------------
1808 SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
1810 ---------------------
1814 SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
1815 Concat text to varchar
1816 ------------------------
1821 -- test substr with toasted text values
1823 CREATE TABLE toasttest(f1 text);
1824 insert into toasttest values(repeat('1234567890',10000));
1825 insert into toasttest values(repeat('1234567890',10000));
1827 -- Ensure that some values are uncompressed, to test the faster substring
1828 -- operation used in that case
1830 alter table toasttest alter column f1 set storage external;
1831 insert into toasttest values(repeat('1234567890',10000));
1832 insert into toasttest values(repeat('1234567890',10000));
1833 -- If the starting position is zero or less, then return from the start of the string
1834 -- adjusting the length to be consistent with the "negative start" per SQL.
1835 SELECT substr(f1, -1, 5) from toasttest;
1844 -- If the length is less than zero, an ERROR is thrown.
1845 SELECT substr(f1, 5, -1) from toasttest;
1846 ERROR: negative substring length not allowed
1847 -- If no third argument (length) is provided, the length to the end of the
1848 -- string is assumed.
1849 SELECT substr(f1, 99995) from toasttest;
1858 -- If start plus length is > string length, the result is truncated to
1860 SELECT substr(f1, 99995, 10) from toasttest;
1869 TRUNCATE TABLE toasttest;
1870 INSERT INTO toasttest values (repeat('1234567890',300));
1871 INSERT INTO toasttest values (repeat('1234567890',300));
1872 INSERT INTO toasttest values (repeat('1234567890',300));
1873 INSERT INTO toasttest values (repeat('1234567890',300));
1875 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
1876 FROM pg_class where relname = 'toasttest';
1882 TRUNCATE TABLE toasttest;
1883 ALTER TABLE toasttest set (toast_tuple_target = 4080);
1884 INSERT INTO toasttest values (repeat('1234567890',300));
1885 INSERT INTO toasttest values (repeat('1234567890',300));
1886 INSERT INTO toasttest values (repeat('1234567890',300));
1887 INSERT INTO toasttest values (repeat('1234567890',300));
1889 SELECT pg_relation_size(reltoastrelid) = 0 AS is_empty
1890 FROM pg_class where relname = 'toasttest';
1896 DROP TABLE toasttest;
1898 -- test substr with toasted bytea values
1900 CREATE TABLE toasttest(f1 bytea);
1901 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1902 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1904 -- Ensure that some values are uncompressed, to test the faster substring
1905 -- operation used in that case
1907 alter table toasttest alter column f1 set storage external;
1908 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1909 insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
1910 -- If the starting position is zero or less, then return from the start of the string
1911 -- adjusting the length to be consistent with the "negative start" per SQL.
1912 SELECT substr(f1, -1, 5) from toasttest;
1921 -- If the length is less than zero, an ERROR is thrown.
1922 SELECT substr(f1, 5, -1) from toasttest;
1923 ERROR: negative substring length not allowed
1924 -- If no third argument (length) is provided, the length to the end of the
1925 -- string is assumed.
1926 SELECT substr(f1, 99995) from toasttest;
1935 -- If start plus length is > string length, the result is truncated to
1937 SELECT substr(f1, 99995, 10) from toasttest;
1946 DROP TABLE toasttest;
1947 -- test internally compressing datums
1948 -- this tests compressing a datum to a very small size which exercises a
1949 -- corner case in packed-varlena handling: even though small, the compressed
1950 -- datum must be given a 4-byte header because there are no bits to indicate
1951 -- compression in a 1-byte header
1952 CREATE TABLE toasttest (c char(4096));
1953 INSERT INTO toasttest VALUES('x');
1954 SELECT length(c), c::text FROM toasttest;
1960 SELECT c FROM toasttest;
1962 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1966 DROP TABLE toasttest;
1970 SELECT length('abcdef') AS "length_6";
1979 SELECT strpos('abcdef', 'cd') AS "pos_3";
1985 SELECT strpos('abcdef', 'xy') AS "pos_0";
1991 SELECT strpos('abcdef', '') AS "pos_1";
1997 SELECT strpos('', 'xy') AS "pos_0";
2003 SELECT strpos('', '') AS "pos_1";
2012 SELECT replace('abcdef', 'de', '45') AS "abc45f";
2018 SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
2024 SELECT replace('yabadoo', 'bad', '') AS "yaoo";
2033 select split_part('','@',1) AS "empty string";
2039 select split_part('','@',-1) AS "empty string";
2045 select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
2047 --------------------
2051 select split_part('joeuser@mydatabase','',2) AS "empty string";
2057 select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
2059 --------------------
2063 select split_part('joeuser@mydatabase','',-2) AS "empty string";
2069 select split_part('joeuser@mydatabase','@',0) AS "an error";
2070 ERROR: field position must not be zero
2071 select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
2073 --------------------
2077 select split_part('joeuser@mydatabase','@@',2) AS "empty string";
2083 select split_part('joeuser@mydatabase','@',1) AS "joeuser";
2089 select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
2095 select split_part('joeuser@mydatabase','@',3) AS "empty string";
2101 select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
2107 select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
2113 select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
2119 select split_part('joeuser@mydatabase','@',-3) AS "empty string";
2125 select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
2132 -- test to_bin, to_oct, and to_hex
2134 select to_bin(-1234) AS "11111111111111111111101100101110";
2135 11111111111111111111101100101110
2136 ----------------------------------
2137 11111111111111111111101100101110
2140 select to_bin(-1234::bigint);
2142 ------------------------------------------------------------------
2143 1111111111111111111111111111111111111111111111111111101100101110
2146 select to_bin(256*256*256 - 1) AS "111111111111111111111111";
2147 111111111111111111111111
2148 --------------------------
2149 111111111111111111111111
2152 select to_bin(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111";
2153 11111111111111111111111111111111
2154 ----------------------------------
2155 11111111111111111111111111111111
2158 select to_oct(-1234) AS "37777775456";
2164 select to_oct(-1234::bigint) AS "1777777777777777775456";
2165 1777777777777777775456
2166 ------------------------
2167 1777777777777777775456
2170 select to_oct(256*256*256 - 1) AS "77777777";
2176 select to_oct(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "37777777777";
2182 select to_hex(-1234) AS "fffffb2e";
2188 select to_hex(-1234::bigint) AS "fffffffffffffb2e";
2194 select to_hex(256*256*256 - 1) AS "ffffff";
2200 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
2209 SET bytea_output TO hex;
2212 ------------------------------------------------------------
2213 \xd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
2216 SELECT sha224('The quick brown fox jumps over the lazy dog.');
2218 ------------------------------------------------------------
2219 \x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c
2224 --------------------------------------------------------------------
2225 \xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2228 SELECT sha256('The quick brown fox jumps over the lazy dog.');
2230 --------------------------------------------------------------------
2231 \xef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c
2236 ----------------------------------------------------------------------------------------------------
2237 \x38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
2240 SELECT sha384('The quick brown fox jumps over the lazy dog.');
2242 ----------------------------------------------------------------------------------------------------
2243 \xed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7
2248 ------------------------------------------------------------------------------------------------------------------------------------
2249 \xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
2252 SELECT sha512('The quick brown fox jumps over the lazy dog.');
2254 ------------------------------------------------------------------------------------------------------------------------------------
2255 \x91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
2267 SELECT crc32('The quick brown fox jumps over the lazy dog.');
2279 SELECT crc32c('The quick brown fox jumps over the lazy dog.');
2288 SELECT encode('\x1234567890abcdef00', 'hex');
2290 --------------------
2294 SELECT decode('1234567890abcdef00', 'hex');
2296 ----------------------
2297 \x1234567890abcdef00
2300 SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
2302 ------------------------------------------------------------------------------
2303 EjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN7wABEjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN+
2304 7wABEjRWeJCrze8AAQ==
2307 SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
2308 'base64'), 'base64');
2310 ------------------------------------------------------------------------------------------------------------------------------------------------
2311 \x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001
2314 SELECT encode('\x1234567890abcdef00', 'escape');
2316 -----------------------------
2317 \x124Vx\220\253\315\357\000
2320 SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
2322 ----------------------
2323 \x1234567890abcdef00
2327 -- get_bit/set_bit etc
2329 SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
2335 SELECT get_bit('\x1234567890abcdef00'::bytea, 99); -- error
2336 ERROR: index 99 out of valid range, 0..71
2337 SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
2339 ----------------------
2340 \x1234567890a3cdef00
2343 SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0); -- error
2344 ERROR: index 99 out of valid range, 0..71
2345 SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
2351 SELECT get_byte('\x1234567890abcdef00'::bytea, 99); -- error
2352 ERROR: index 99 out of valid range, 0..8
2353 SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
2355 ----------------------
2356 \x1234567890abcd0b00
2359 SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11); -- error
2360 ERROR: index 99 out of valid range, 0..8
2362 -- test behavior of escape_string_warning and standard_conforming_strings options
2364 set escape_string_warning = off;
2365 set standard_conforming_strings = off;
2366 show escape_string_warning;
2367 escape_string_warning
2368 -----------------------
2372 show standard_conforming_strings;
2373 standard_conforming_strings
2374 -----------------------------
2378 set escape_string_warning = on;
2379 set standard_conforming_strings = on;
2380 show escape_string_warning;
2381 escape_string_warning
2382 -----------------------
2386 show standard_conforming_strings;
2387 standard_conforming_strings
2388 -----------------------------
2392 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6;
2393 f1 | f2 | f3 | f4 | f5 | f6
2394 -------+--------+---------+-------+--------+----
2395 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2398 set standard_conforming_strings = off;
2399 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
2400 WARNING: nonstandard use of \\ in a string literal
2401 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2403 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2404 WARNING: nonstandard use of \\ in a string literal
2405 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2407 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2408 WARNING: nonstandard use of \\ in a string literal
2409 LINE 1: select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3,...
2411 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2412 WARNING: nonstandard use of \\ in a string literal
2413 LINE 1: ...bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' ...
2415 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2416 WARNING: nonstandard use of \\ in a string literal
2417 LINE 1: ...'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd'...
2419 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2420 WARNING: nonstandard use of \\ in a string literal
2421 LINE 1: ...'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as ...
2423 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
2424 f1 | f2 | f3 | f4 | f5 | f6
2425 -------+--------+---------+-------+--------+----
2426 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2429 set escape_string_warning = off;
2430 set standard_conforming_strings = on;
2431 select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\' as f4, 'ab\''cd' as f5, '\\' as f6;
2432 f1 | f2 | f3 | f4 | f5 | f6
2433 -------+--------+---------+-------+--------+----
2434 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2437 set standard_conforming_strings = off;
2438 select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\' as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
2439 f1 | f2 | f3 | f4 | f5 | f6
2440 -------+--------+---------+-------+--------+----
2441 a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
2444 reset standard_conforming_strings;
2446 -- Additional string functions
2448 SET bytea_output TO escape;
2449 SELECT initcap('hi THOMAS');
2455 SELECT lpad('hi', 5, 'xy');
2461 SELECT lpad('hi', 5);
2467 SELECT lpad('hi', -5, 'xy');
2473 SELECT lpad('hello', 2);
2479 SELECT lpad('hi', 5, '');
2485 SELECT rpad('hi', 5, 'xy');
2491 SELECT rpad('hi', 5);
2497 SELECT rpad('hi', -5, 'xy');
2503 SELECT rpad('hello', 2);
2509 SELECT rpad('hi', 5, '');
2515 SELECT ltrim('zzzytrim', 'xyz');
2521 SELECT translate('', '14', 'ax');
2527 SELECT translate('12345', '14', 'ax');
2533 SELECT translate('12345', '134', 'a');
2558 ERROR: null character not permitted
2559 SELECT repeat('Pg', 4);
2565 SELECT repeat('Pg', -4);
2571 SELECT SUBSTRING('1234567890'::bytea FROM 3) "34567890";
2577 SELECT SUBSTRING('1234567890'::bytea FROM 4 FOR 3) AS "456";
2583 SELECT SUBSTRING('string'::bytea FROM 2 FOR 2147483646) AS "tring";
2589 SELECT SUBSTRING('string'::bytea FROM -10 FOR 2147483646) AS "string";
2595 SELECT SUBSTRING('string'::bytea FROM -10 FOR -2147483646) AS "error";
2596 ERROR: negative substring length not allowed
2597 SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2603 SELECT trim(leading E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2609 SELECT trim(trailing E'\\000'::bytea from E'\\000Tom\\000'::bytea);
2615 SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
2621 SELECT btrim(''::bytea, E'\\000'::bytea);
2627 SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
2633 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape');
2639 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape');
2641 --------------------
2645 SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape');
2651 SELECT bit_count('\x1234567890'::bytea);
2657 SELECT unistr('\0064at\+0000610');
2663 SELECT unistr('d\u0061t\U000000610');
2669 SELECT unistr('a\\b');
2676 SELECT unistr('wrong: \db99');
2677 ERROR: invalid Unicode surrogate pair
2678 SELECT unistr('wrong: \db99\0061');
2679 ERROR: invalid Unicode surrogate pair
2680 SELECT unistr('wrong: \+00db99\+000061');
2681 ERROR: invalid Unicode surrogate pair
2682 SELECT unistr('wrong: \+2FFFFF');
2683 ERROR: invalid Unicode code point: 2FFFFF
2684 SELECT unistr('wrong: \udb99\u0061');
2685 ERROR: invalid Unicode surrogate pair
2686 SELECT unistr('wrong: \U0000db99\U00000061');
2687 ERROR: invalid Unicode surrogate pair
2688 SELECT unistr('wrong: \U002FFFFF');
2689 ERROR: invalid Unicode code point: 2FFFFF
2690 SELECT unistr('wrong: \xyz');
2691 ERROR: invalid Unicode escape
2692 HINT: Unicode escapes must be \XXXX, \+XXXXXX, \uXXXX, or \UXXXXXXXX.