1--
2-- STRINGS
3-- Test various data entry syntaxes.
4--
5
6-- SQL string continuation syntax
7-- E021-03 character string literals
8SELECT 'first line'
9' - next line'
10	' - third line'
11	AS "Three lines to one";
12
13-- illegal string continuation syntax
14SELECT 'first line'
15' - next line' /* this comment is not allowed here */
16' - third line'
17	AS "Illegal comment within continuation";
18
19-- Unicode escapes
20SET standard_conforming_strings TO on;
21
22SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
23SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
24
25SELECT U&' \' UESCAPE '!' AS "tricky";
26SELECT 'tricky' AS U&"\" UESCAPE '!';
27
28SELECT U&'wrong: \061';
29SELECT U&'wrong: \+0061';
30SELECT U&'wrong: +0061' UESCAPE '+';
31
32SET standard_conforming_strings TO off;
33
34SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
35SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
36
37SELECT U&' \' UESCAPE '!' AS "tricky";
38SELECT 'tricky' AS U&"\" UESCAPE '!';
39
40SELECT U&'wrong: \061';
41SELECT U&'wrong: \+0061';
42SELECT U&'wrong: +0061' UESCAPE '+';
43
44RESET standard_conforming_strings;
45
46-- bytea
47SET bytea_output TO hex;
48SELECT E'\\xDeAdBeEf'::bytea;
49SELECT E'\\x De Ad Be Ef '::bytea;
50SELECT E'\\xDeAdBeE'::bytea;
51SELECT E'\\xDeAdBeEx'::bytea;
52SELECT E'\\xDe00BeEf'::bytea;
53SELECT E'DeAdBeEf'::bytea;
54SELECT E'De\\000dBeEf'::bytea;
55SELECT E'De\123dBeEf'::bytea;
56SELECT E'De\\123dBeEf'::bytea;
57SELECT E'De\\678dBeEf'::bytea;
58
59SET bytea_output TO escape;
60SELECT E'\\xDeAdBeEf'::bytea;
61SELECT E'\\x De Ad Be Ef '::bytea;
62SELECT E'\\xDe00BeEf'::bytea;
63SELECT E'DeAdBeEf'::bytea;
64SELECT E'De\\000dBeEf'::bytea;
65SELECT E'De\\123dBeEf'::bytea;
66
67--
68-- test conversions between various string types
69-- E021-10 implicit casting among the character data types
70--
71
72SELECT CAST(f1 AS text) AS "text(char)" FROM CHAR_TBL;
73
74SELECT CAST(f1 AS text) AS "text(varchar)" FROM VARCHAR_TBL;
75
76SELECT CAST(name 'namefield' AS text) AS "text(name)";
77
78-- since this is an explicit cast, it should truncate w/o error:
79SELECT CAST(f1 AS char(10)) AS "char(text)" FROM TEXT_TBL;
80-- note: implicit-cast case is tested in char.sql
81
82SELECT CAST(f1 AS char(20)) AS "char(text)" FROM TEXT_TBL;
83
84SELECT CAST(f1 AS char(10)) AS "char(varchar)" FROM VARCHAR_TBL;
85
86SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
87
88SELECT CAST(f1 AS varchar) AS "varchar(text)" FROM TEXT_TBL;
89
90SELECT CAST(f1 AS varchar) AS "varchar(char)" FROM CHAR_TBL;
91
92SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
93
94--
95-- test SQL string functions
96-- E### and T### are feature reference numbers from SQL99
97--
98
99-- E021-09 trim function
100SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
101
102SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
103
104SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
105
106SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
107
108-- E021-06 substring expression
109SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
110
111SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
112
113-- test overflow cases
114SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
115SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
116SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
117
118-- T581 regular expression substring (with SQL's bizarre regexp syntax)
119SELECT SUBSTRING('abcdefg' FROM 'a#"(b_d)#"%' FOR '#') AS "bcd";
120
121-- No match should return NULL
122SELECT SUBSTRING('abcdefg' FROM '#"(b_d)#"%' FOR '#') IS NULL AS "True";
123
124-- Null inputs should return NULL
125SELECT SUBSTRING('abcdefg' FROM '(b|c)' FOR NULL) IS NULL AS "True";
126SELECT SUBSTRING(NULL FROM '(b|c)' FOR '#') IS NULL AS "True";
127SELECT SUBSTRING('abcdefg' FROM NULL FOR '#') IS NULL AS "True";
128
129-- PostgreSQL extension to allow omitting the escape character;
130-- here the regexp is taken as Posix syntax
131SELECT SUBSTRING('abcdefg' FROM 'c.e') AS "cde";
132
133-- With a parenthesized subexpression, return only what matches the subexpr
134SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
135
136-- PostgreSQL extension to allow using back reference in replace string;
137SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
138SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
139SELECT regexp_replace('AAA', '^|$', 'Z', 'g');
140SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'gi');
141-- invalid regexp option
142SELECT regexp_replace('AAA aaa', 'A+', 'Z', 'z');
143
144-- set so we can tell NULL from empty string
145\pset null '\\N'
146
147-- return all matches from regexp
148SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
149
150-- test case insensitive
151SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
152
153-- global option - more than one match
154SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
155
156-- empty capture group (matched empty string)
157SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$);
158-- no match
159SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$);
160-- optional capture group did not match, null entry in array
161SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
162
163-- no capture groups
164SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
165
166-- start/end-of-line matches are of zero length
167SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '^', 'mg');
168SELECT regexp_matches('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || chr(10) || 'baz', '$', 'mg');
169SELECT regexp_matches('1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '^.?', 'mg');
170SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4' || chr(10), '.?$', 'mg');
171SELECT regexp_matches(chr(10) || '1' || chr(10) || '2' || chr(10) || '3' || chr(10) || '4', '.?$', 'mg');
172
173-- give me errors
174SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz');
175SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$);
176SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
177
178-- split string on regexp
179SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo;
180SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
181
182SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo;
183SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$);
184SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo;
185SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '');
186-- case insensitive
187SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo;
188SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i');
189-- no match of pattern
190SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo;
191SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch');
192-- some corner cases
193SELECT regexp_split_to_array('123456','1');
194SELECT regexp_split_to_array('123456','6');
195SELECT regexp_split_to_array('123456','.');
196SELECT regexp_split_to_array('123456','');
197SELECT regexp_split_to_array('123456','(?:)');
198SELECT regexp_split_to_array('1','');
199-- errors
200SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo;
201SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz');
202-- global option meaningless for regexp_split
203SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo;
204SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
205
206-- change NULL-display back
207\pset null ''
208
209-- E021-11 position expression
210SELECT POSITION('4' IN '1234567890') = '4' AS "4";
211
212SELECT POSITION('5' IN '1234567890') = '5' AS "5";
213
214-- T312 character overlay function
215SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
216
217SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
218
219SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
220
221SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
222
223--
224-- test LIKE
225-- Be sure to form every test as a LIKE/NOT LIKE pair.
226--
227
228-- simplest examples
229-- E061-04 like predicate
230SELECT 'hawkeye' LIKE 'h%' AS "true";
231SELECT 'hawkeye' NOT LIKE 'h%' AS "false";
232
233SELECT 'hawkeye' LIKE 'H%' AS "false";
234SELECT 'hawkeye' NOT LIKE 'H%' AS "true";
235
236SELECT 'hawkeye' LIKE 'indio%' AS "false";
237SELECT 'hawkeye' NOT LIKE 'indio%' AS "true";
238
239SELECT 'hawkeye' LIKE 'h%eye' AS "true";
240SELECT 'hawkeye' NOT LIKE 'h%eye' AS "false";
241
242SELECT 'indio' LIKE '_ndio' AS "true";
243SELECT 'indio' NOT LIKE '_ndio' AS "false";
244
245SELECT 'indio' LIKE 'in__o' AS "true";
246SELECT 'indio' NOT LIKE 'in__o' AS "false";
247
248SELECT 'indio' LIKE 'in_o' AS "false";
249SELECT 'indio' NOT LIKE 'in_o' AS "true";
250
251-- unused escape character
252SELECT 'hawkeye' LIKE 'h%' ESCAPE '#' AS "true";
253SELECT 'hawkeye' NOT LIKE 'h%' ESCAPE '#' AS "false";
254
255SELECT 'indio' LIKE 'ind_o' ESCAPE '$' AS "true";
256SELECT 'indio' NOT LIKE 'ind_o' ESCAPE '$' AS "false";
257
258-- escape character
259-- E061-05 like predicate with escape clause
260SELECT 'h%' LIKE 'h#%' ESCAPE '#' AS "true";
261SELECT 'h%' NOT LIKE 'h#%' ESCAPE '#' AS "false";
262
263SELECT 'h%wkeye' LIKE 'h#%' ESCAPE '#' AS "false";
264SELECT 'h%wkeye' NOT LIKE 'h#%' ESCAPE '#' AS "true";
265
266SELECT 'h%wkeye' LIKE 'h#%%' ESCAPE '#' AS "true";
267SELECT 'h%wkeye' NOT LIKE 'h#%%' ESCAPE '#' AS "false";
268
269SELECT 'h%awkeye' LIKE 'h#%a%k%e' ESCAPE '#' AS "true";
270SELECT 'h%awkeye' NOT LIKE 'h#%a%k%e' ESCAPE '#' AS "false";
271
272SELECT 'indio' LIKE '_ndio' ESCAPE '$' AS "true";
273SELECT 'indio' NOT LIKE '_ndio' ESCAPE '$' AS "false";
274
275SELECT 'i_dio' LIKE 'i$_d_o' ESCAPE '$' AS "true";
276SELECT 'i_dio' NOT LIKE 'i$_d_o' ESCAPE '$' AS "false";
277
278SELECT 'i_dio' LIKE 'i$_nd_o' ESCAPE '$' AS "false";
279SELECT 'i_dio' NOT LIKE 'i$_nd_o' ESCAPE '$' AS "true";
280
281SELECT 'i_dio' LIKE 'i$_d%o' ESCAPE '$' AS "true";
282SELECT 'i_dio' NOT LIKE 'i$_d%o' ESCAPE '$' AS "false";
283
284-- escape character same as pattern character
285SELECT 'maca' LIKE 'm%aca' ESCAPE '%' AS "true";
286SELECT 'maca' NOT LIKE 'm%aca' ESCAPE '%' AS "false";
287
288SELECT 'ma%a' LIKE 'm%a%%a' ESCAPE '%' AS "true";
289SELECT 'ma%a' NOT LIKE 'm%a%%a' ESCAPE '%' AS "false";
290
291SELECT 'bear' LIKE 'b_ear' ESCAPE '_' AS "true";
292SELECT 'bear' NOT LIKE 'b_ear' ESCAPE '_' AS "false";
293
294SELECT 'be_r' LIKE 'b_e__r' ESCAPE '_' AS "true";
295SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false";
296
297SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false";
298SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true";
299
300
301--
302-- test ILIKE (case-insensitive LIKE)
303-- Be sure to form every test as an ILIKE/NOT ILIKE pair.
304--
305
306SELECT 'hawkeye' ILIKE 'h%' AS "true";
307SELECT 'hawkeye' NOT ILIKE 'h%' AS "false";
308
309SELECT 'hawkeye' ILIKE 'H%' AS "true";
310SELECT 'hawkeye' NOT ILIKE 'H%' AS "false";
311
312SELECT 'hawkeye' ILIKE 'H%Eye' AS "true";
313SELECT 'hawkeye' NOT ILIKE 'H%Eye' AS "false";
314
315SELECT 'Hawkeye' ILIKE 'h%' AS "true";
316SELECT 'Hawkeye' NOT ILIKE 'h%' AS "false";
317
318--
319-- test %/_ combination cases, cf bugs #4821 and #5478
320--
321
322SELECT 'foo' LIKE '_%' as t, 'f' LIKE '_%' as t, '' LIKE '_%' as f;
323SELECT 'foo' LIKE '%_' as t, 'f' LIKE '%_' as t, '' LIKE '%_' as f;
324
325SELECT 'foo' LIKE '__%' as t, 'foo' LIKE '___%' as t, 'foo' LIKE '____%' as f;
326SELECT 'foo' LIKE '%__' as t, 'foo' LIKE '%___' as t, 'foo' LIKE '%____' as f;
327
328SELECT 'jack' LIKE '%____%' AS t;
329
330
331--
332-- test implicit type conversion
333--
334
335-- E021-07 character concatenation
336SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
337
338SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
339
340SELECT char(20) 'characters' || ' and text' AS "Concat char to unknown type";
341
342SELECT text 'text' || char(20) ' and characters' AS "Concat text to char";
343
344SELECT text 'text' || varchar ' and varchar' AS "Concat text to varchar";
345
346--
347-- test substr with toasted text values
348--
349CREATE TABLE toasttest(f1 text);
350
351insert into toasttest values(repeat('1234567890',10000));
352insert into toasttest values(repeat('1234567890',10000));
353
354--
355-- Ensure that some values are uncompressed, to test the faster substring
356-- operation used in that case
357--
358alter table toasttest alter column f1 set storage external;
359insert into toasttest values(repeat('1234567890',10000));
360insert into toasttest values(repeat('1234567890',10000));
361
362-- If the starting position is zero or less, then return from the start of the string
363-- adjusting the length to be consistent with the "negative start" per SQL.
364SELECT substr(f1, -1, 5) from toasttest;
365
366-- If the length is less than zero, an ERROR is thrown.
367SELECT substr(f1, 5, -1) from toasttest;
368
369-- If no third argument (length) is provided, the length to the end of the
370-- string is assumed.
371SELECT substr(f1, 99995) from toasttest;
372
373-- If start plus length is > string length, the result is truncated to
374-- string length
375SELECT substr(f1, 99995, 10) from toasttest;
376
377TRUNCATE TABLE toasttest;
378INSERT INTO toasttest values (repeat('1234567890',300));
379INSERT INTO toasttest values (repeat('1234567890',300));
380INSERT INTO toasttest values (repeat('1234567890',300));
381INSERT INTO toasttest values (repeat('1234567890',300));
382-- expect >0 blocks
383select 0 = pg_relation_size('pg_toast.pg_toast_'||(select oid from pg_class where relname = 'toasttest'))/current_setting('block_size')::integer as blocks;
384
385TRUNCATE TABLE toasttest;
386ALTER TABLE toasttest set (toast_tuple_target = 4080);
387INSERT INTO toasttest values (repeat('1234567890',300));
388INSERT INTO toasttest values (repeat('1234567890',300));
389INSERT INTO toasttest values (repeat('1234567890',300));
390INSERT INTO toasttest values (repeat('1234567890',300));
391-- expect 0 blocks
392select 0 = pg_relation_size('pg_toast.pg_toast_'||(select oid from pg_class where relname = 'toasttest'))/current_setting('block_size')::integer as blocks;
393
394DROP TABLE toasttest;
395
396--
397-- test substr with toasted bytea values
398--
399CREATE TABLE toasttest(f1 bytea);
400
401insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
402insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
403
404--
405-- Ensure that some values are uncompressed, to test the faster substring
406-- operation used in that case
407--
408alter table toasttest alter column f1 set storage external;
409insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
410insert into toasttest values(decode(repeat('1234567890',10000),'escape'));
411
412-- If the starting position is zero or less, then return from the start of the string
413-- adjusting the length to be consistent with the "negative start" per SQL.
414SELECT substr(f1, -1, 5) from toasttest;
415
416-- If the length is less than zero, an ERROR is thrown.
417SELECT substr(f1, 5, -1) from toasttest;
418
419-- If no third argument (length) is provided, the length to the end of the
420-- string is assumed.
421SELECT substr(f1, 99995) from toasttest;
422
423-- If start plus length is > string length, the result is truncated to
424-- string length
425SELECT substr(f1, 99995, 10) from toasttest;
426
427DROP TABLE toasttest;
428
429-- test internally compressing datums
430
431-- this tests compressing a datum to a very small size which exercises a
432-- corner case in packed-varlena handling: even though small, the compressed
433-- datum must be given a 4-byte header because there are no bits to indicate
434-- compression in a 1-byte header
435
436CREATE TABLE toasttest (c char(4096));
437INSERT INTO toasttest VALUES('x');
438SELECT length(c), c::text FROM toasttest;
439SELECT c FROM toasttest;
440DROP TABLE toasttest;
441
442--
443-- test length
444--
445
446SELECT length('abcdef') AS "length_6";
447
448--
449-- test strpos
450--
451
452SELECT strpos('abcdef', 'cd') AS "pos_3";
453
454SELECT strpos('abcdef', 'xy') AS "pos_0";
455
456--
457-- test replace
458--
459SELECT replace('abcdef', 'de', '45') AS "abc45f";
460
461SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
462
463SELECT replace('yabadoo', 'bad', '') AS "yaoo";
464
465--
466-- test split_part
467--
468select split_part('joeuser@mydatabase','@',0) AS "an error";
469
470select split_part('joeuser@mydatabase','@',1) AS "joeuser";
471
472select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
473
474select split_part('joeuser@mydatabase','@',3) AS "empty string";
475
476select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
477
478--
479-- test to_hex
480--
481select to_hex(256*256*256 - 1) AS "ffffff";
482
483select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
484
485--
486-- MD5 test suite - from IETF RFC 1321
487-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
488--
489select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
490
491select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
492
493select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
494
495select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
496
497select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
498
499select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
500
501select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
502
503select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
504
505select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
506
507select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
508
509select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
510
511select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
512
513select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
514
515select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
516
517--
518-- SHA-2
519--
520SET bytea_output TO hex;
521
522SELECT sha224('');
523SELECT sha224('The quick brown fox jumps over the lazy dog.');
524
525SELECT sha256('');
526SELECT sha256('The quick brown fox jumps over the lazy dog.');
527
528SELECT sha384('');
529SELECT sha384('The quick brown fox jumps over the lazy dog.');
530
531SELECT sha512('');
532SELECT sha512('The quick brown fox jumps over the lazy dog.');
533
534--
535-- encode/decode
536--
537SELECT encode('\x1234567890abcdef00', 'hex');
538SELECT decode('1234567890abcdef00', 'hex');
539SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
540SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
541                     'base64'), 'base64');
542SELECT encode('\x1234567890abcdef00', 'escape');
543SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
544
545--
546-- get_bit/set_bit etc
547--
548SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
549SELECT get_bit('\x1234567890abcdef00'::bytea, 99);  -- error
550SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
551SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0);  -- error
552SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
553SELECT get_byte('\x1234567890abcdef00'::bytea, 99);  -- error
554SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
555SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11);  -- error
556
557--
558-- test behavior of escape_string_warning and standard_conforming_strings options
559--
560set escape_string_warning = off;
561set standard_conforming_strings = off;
562
563show escape_string_warning;
564show standard_conforming_strings;
565
566set escape_string_warning = on;
567set standard_conforming_strings = on;
568
569show escape_string_warning;
570show standard_conforming_strings;
571
572select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
573
574set standard_conforming_strings = off;
575
576select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
577
578set escape_string_warning = off;
579set standard_conforming_strings = on;
580
581select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
582
583set standard_conforming_strings = off;
584
585select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
586
587
588--
589-- Additional string functions
590--
591SET bytea_output TO escape;
592
593SELECT initcap('hi THOMAS');
594
595SELECT lpad('hi', 5, 'xy');
596SELECT lpad('hi', 5);
597SELECT lpad('hi', -5, 'xy');
598SELECT lpad('hello', 2);
599SELECT lpad('hi', 5, '');
600
601SELECT rpad('hi', 5, 'xy');
602SELECT rpad('hi', 5);
603SELECT rpad('hi', -5, 'xy');
604SELECT rpad('hello', 2);
605SELECT rpad('hi', 5, '');
606
607SELECT ltrim('zzzytrim', 'xyz');
608
609SELECT translate('', '14', 'ax');
610SELECT translate('12345', '14', 'ax');
611
612SELECT ascii('x');
613SELECT ascii('');
614
615SELECT chr(65);
616SELECT chr(0);
617
618SELECT repeat('Pg', 4);
619SELECT repeat('Pg', -4);
620
621SELECT SUBSTRING('1234567890'::bytea FROM 3) "34567890";
622SELECT SUBSTRING('1234567890'::bytea FROM 4 FOR 3) AS "456";
623SELECT SUBSTRING('string'::bytea FROM 2 FOR 2147483646) AS "tring";
624SELECT SUBSTRING('string'::bytea FROM -10 FOR 2147483646) AS "string";
625SELECT SUBSTRING('string'::bytea FROM -10 FOR -2147483646) AS "error";
626
627SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea);
628SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea);
629SELECT btrim(''::bytea, E'\\000'::bytea);
630SELECT btrim(E'\\000trim\\000'::bytea, ''::bytea);
631SELECT encode(overlay(E'Th\\000omas'::bytea placing E'Th\\001omas'::bytea from 2),'escape');
632SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 8),'escape');
633SELECT encode(overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 5 for 3),'escape');
634