-- -- STRINGS -- Test various data entry syntaxes. --
-- SQL string continuation syntax -- E021-03 character string literals SELECT'first line' ' - next line' ' - third line' AS"Three lines to one";
-- illegal string continuation syntax SELECT'first line' ' - next line'/* this comment is not allowed here */ ' - third line' AS"Illegal comment within continuation";
-- Unicode escapes SET standard_conforming_strings TOon;
SET bytea_output TO escape; SELECT E'\\xDeAdBeEf'::bytea; SELECT E'\\x De Ad Be Ef '::bytea; SELECT E'\\xDe00BeEf'::bytea; SELECT E'DeAdBeEf'::bytea; SELECT E'De\\000dBeEf'::bytea; SELECT E'De\\123dBeEf'::bytea;
-- Test non-error-throwing API too SELECT pg_input_is_valid(E'\\xDeAdBeE', 'bytea'); SELECT * FROM pg_input_error_info(E'\\xDeAdBeE', 'bytea'); SELECT * FROM pg_input_error_info(E'\\xDeAdBeEx', 'bytea'); SELECT * FROM pg_input_error_info(E'foo\\99bar', 'bytea');
-- -- test conversions between various string types -- E021-10 implicit casting among the character data types --
SELECT CAST(f1 AS text) AS"text(char)"FROM CHAR_TBL;
SELECT CAST(f1 AS text) AS"text(varchar)"FROM VARCHAR_TBL;
-- since this is an explicit cast, it should truncate w/o error: SELECT CAST(f1 ASchar(10)) AS"char(text)"FROM TEXT_TBL; -- note: implicit-cast case is tested in char.sql
-- No match should return NULL SELECT SUBSTRING('abcdefg' SIMILAR '#"(b_d)#"%' ESCAPE '#') ISNULLAS"True";
-- Null inputs should return NULL SELECT SUBSTRING('abcdefg' SIMILAR '%' ESCAPE NULL) ISNULLAS"True"; SELECT SUBSTRING(NULL SIMILAR '%' ESCAPE '#') ISNULLAS"True"; SELECT SUBSTRING('abcdefg' SIMILAR NULL ESCAPE '#') ISNULLAS"True";
-- The first and last parts should act non-greedy SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"g' ESCAPE '#') AS"bcdef"; SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*' ESCAPE '#') AS"abcdefg";
-- Vertical bar in any part affects only that part SELECT SUBSTRING('abcdefg' SIMILAR 'a|b#"%#"g' ESCAPE '#') AS"bcdef"; SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%#"x|g' ESCAPE '#') AS"bcdef"; SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%|ab#"g' ESCAPE '#') AS"bcdef";
-- Can't have more than two part separators SELECT SUBSTRING('abcdefg' SIMILAR 'a*#"%#"g*#"x' ESCAPE '#') AS"error";
-- Postgres extension: with 0 or 1 separator, assume parts 1 and 3 are empty SELECT SUBSTRING('abcdefg' SIMILAR 'a#"%g' ESCAPE '#') AS"bcdefg"; SELECT SUBSTRING('abcdefg' SIMILAR 'a%g' ESCAPE '#') AS"abcdefg";
-- substring() with just two arguments is not allowed by SQL spec; -- we accept it, but we interpret the pattern as a POSIX regexp not SQL SELECT SUBSTRING('abcdefg'FROM'c.e') AS"cde";
-- With a parenthesized subexpression, return only what matches the subexpr SELECT SUBSTRING('abcdefg'FROM'b(.*)f') AS"cde"; -- Check case where we have a match, but not a subexpression match SELECT SUBSTRING('foo'FROM'foo(bar)?') ISNULLAS t;
-- Check behavior of SIMILAR TO, which uses largely the same regexp variant SELECT'abcdefg' SIMILAR TO'_bcd%'AStrue; SELECT'abcdefg' SIMILAR TO'bcd%'ASfalse; SELECT'abcdefg' SIMILAR TO'_bcd#%' ESCAPE '#'ASfalse; SELECT'abcd%' SIMILAR TO'_bcd#%' ESCAPE '#'AStrue; -- Postgres uses '\' as the default escape character, which is not per spec SELECT'abcdefg' SIMILAR TO'_bcd\%'ASfalse; -- and an empty string to mean "no escape", which is also not per spec SELECT'abcd\efg' SIMILAR TO'_bcd\%' ESCAPE ''AStrue; -- these behaviors are per spec, though: SELECT'abcdefg' SIMILAR TO'_bcd%' ESCAPE NULLASnull; SELECT'abcdefg' SIMILAR TO'_bcd#%' ESCAPE '##'AS error;
-- Characters that should be left alone in character classes when a -- SIMILAR TO regexp pattern is converted to POSIX style. -- Underscore "_" EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'_[_[:alpha:]_]_'; -- Percentage "%" EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'%[%[:alnum:]%]%'; -- Dot "." EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'.[.[:alnum:].].'; -- Dollar "$" EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'$[$[:alnum:]$]$'; -- Opening parenthesis "(" EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'()[([:alnum:](]()'; -- Caret "^" EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'^[^[:alnum:]^[^^][[^^]][\^][[\^]]\^]^'; -- Closing square bracket "]" at the beginning of character class EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'[]%][^]%][^%]%'; -- Closing square bracket effective after two carets at the beginning -- of character class. EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'[^^]^'; -- Closing square bracket after an escape sequence at the beginning of -- a character closes the character class EXPLAIN (COSTS OFF) SELECT * FROM TEXT_TBL WHERE f1 SIMILAR TO'[|a]%' ESCAPE '|';
-- Test backslash escapes in regexp_replace's replacement string SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3'); SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\&Y', 'g'); SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\\\Y', 'g'); -- not an error, though perhaps it should be: SELECT regexp_replace('foobarrbazz', E'(.)\\1', E'X\\Y\\1Z\\');
-- set so we can tell NULL from empty string
\pset null'\\N'
-- return all matches from regexp SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$);
-- test case insensitive SELECT regexp_matches('foObARbEqUEbAz', $re$(bar)(beque)$re$, 'i');
-- global option - more than one match SELECT regexp_matches('foobarbequebazilbarfbonk', $re$(b[^b]+)(b[^b]+)$re$, 'g');
-- empty capture group (matched empty string) SELECT regexp_matches('foobarbequebaz', $re$(bar)(.*)(beque)$re$); -- no match SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)(beque)$re$); -- optional capture group did not match, null entry in array SELECT regexp_matches('foobarbequebaz', $re$(bar)(.+)?(beque)$re$);
-- no capture groups SELECT regexp_matches('foobarbequebaz', $re$barbeque$re$);
-- give me errors SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque)$re$, 'gz'); SELECT regexp_matches('foobarbequebaz', $re$(barbeque$re$); SELECT regexp_matches('foobarbequebaz', $re$(bar)(beque){2,1}$re$);
-- split string on regexp SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s+$re$) AS foo; SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s+$re$);
SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', $re$\s*$re$) AS foo; SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', $re$\s*$re$); SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '') AS foo; SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', ''); -- case insensitive SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i') AS foo; SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'i'); -- no match of pattern SELECT foo, length(foo) FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', 'nomatch') AS foo; SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', 'nomatch'); -- some corner cases SELECT regexp_split_to_array('123456','1'); SELECT regexp_split_to_array('123456','6'); SELECT regexp_split_to_array('123456','.'); SELECT regexp_split_to_array('123456',''); SELECT regexp_split_to_array('123456','(?:)'); SELECT regexp_split_to_array('1',''); -- errors SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'zippy') AS foo; SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'iz'); -- global option meaningless for regexp_split SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo; SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g');
-- change NULL-display back
\pset null''
-- E021-11 position expression SELECT POSITION('4'IN'1234567890') = '4'AS"4";
-- -- Ensure that some values are uncompressed, to test the faster substring -- operation used in that case -- altertable toasttest altercolumn f1 set storage external; insertinto toasttest values(repeat('1234567890',10000)); insertinto toasttest values(repeat('1234567890',10000));
-- If the starting position is zero or less, then return from the start of the string -- adjusting the length to be consistent with the "negative start" per SQL. SELECT substr(f1, -1, 5) from toasttest;
-- If the length is less than zero, an ERROR is thrown. SELECT substr(f1, 5, -1) from toasttest;
-- If no third argument (length) is provided, the length to the end of the -- string is assumed. SELECT substr(f1, 99995) from toasttest;
-- If start plus length is > string length, the result is truncated to -- string length SELECT substr(f1, 99995, 10) from toasttest;
-- -- Ensure that some values are uncompressed, to test the faster substring -- operation used in that case -- altertable toasttest altercolumn f1 set storage external; insertinto toasttest values(decode(repeat('1234567890',10000),'escape')); insertinto toasttest values(decode(repeat('1234567890',10000),'escape'));
-- If the starting position is zero or less, then return from the start of the string -- adjusting the length to be consistent with the "negative start" per SQL. SELECT substr(f1, -1, 5) from toasttest;
-- If the length is less than zero, an ERROR is thrown. SELECT substr(f1, 5, -1) from toasttest;
-- If no third argument (length) is provided, the length to the end of the -- string is assumed. SELECT substr(f1, 99995) from toasttest;
-- If start plus length is > string length, the result is truncated to -- string length SELECT substr(f1, 99995, 10) from toasttest;
DROPTABLE toasttest;
-- test internally compressing datums
-- this tests compressing a datum to a very small size which exercises a -- corner case in packed-varlena handling: even though small, the compressed -- datum must be given a 4-byte header because there are no bits to indicate -- compression in a 1-byte header
CREATETABLE toasttest (c char(4096)); INSERTINTO toasttest VALUES('x'); SELECT length(c), c::text FROM toasttest; SELECT c FROM toasttest; DROPTABLE toasttest;
-- -- test length --
SELECT length('abcdef') AS"length_6";
-- -- test strpos --
SELECT strpos('abcdef', 'cd') AS"pos_3";
SELECT strpos('abcdef', 'xy') AS"pos_0";
SELECT strpos('abcdef', '') AS"pos_1";
SELECT strpos('', 'xy') AS"pos_0";
SELECT strpos('', '') AS"pos_1";
-- -- test replace -- SELECTreplace('abcdef', 'de', '45') AS"abc45f";
-- -- test behavior of escape_string_warning and standard_conforming_strings options -- set escape_string_warning = off; set standard_conforming_strings = off;
show escape_string_warning; show standard_conforming_strings;
set escape_string_warning = on; set standard_conforming_strings = on;
show escape_string_warning; show standard_conforming_strings;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.