strings.sql 25 KB

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