--
-- 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";
         Three lines to one          
-------------------------------------
 first line - next line - third line
(1 row)

-- illegal string continuation syntax
SELECT 'first line'
' - next line' /* this comment is not allowed here */
' - third line'
	AS "Illegal comment within continuation";
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:17: Status: GENERIC_ERROR
Issues: 
<main>:3:1: Error: ERROR:  syntax error at or near "' - third line'"


SELECT U&'d\0061t\+000061' AS U&"d\0061t\+000061";
 data 
------
 data
(1 row)

SELECT U&'d!0061t\+000061' UESCAPE '!' AS U&"d*0061t\+000061" UESCAPE '*';
 dat\+000061 
-------------
 dat\+000061
(1 row)

SELECT U&'a\\b' AS "a\b";
 a\b 
-----
 a\b
(1 row)

SELECT U&' \' UESCAPE '!' AS "tricky";
 tricky 
--------
  \
(1 row)

SELECT 'tricky' AS U&"\" UESCAPE '!';
   \    
--------
 tricky
(1 row)

SELECT U&'wrong: \061';
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:26: Status: GENERIC_ERROR
Issues: 
<main>:1:18: Error: ERROR:  invalid Unicode escape


SELECT U&'wrong: \+0061';
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:27: Status: GENERIC_ERROR
Issues: 
<main>:1:18: Error: ERROR:  invalid Unicode escape


SELECT U&'wrong: +0061' UESCAPE +;
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:28: Status: GENERIC_ERROR
Issues: 
<main>:1:33: Error: ERROR:  UESCAPE must be followed by a simple string literal at or near "+"


SELECT U&'wrong: +0061' UESCAPE '+';
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:29: Status: GENERIC_ERROR
Issues: 
<main>:1:33: Error: ERROR:  invalid Unicode escape character at or near "'+'"


SELECT U&'wrong: \db99';
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:31: Status: GENERIC_ERROR
Issues: 
<main>:1:23: Error: ERROR:  invalid Unicode surrogate pair


SELECT U&'wrong: \db99xy';
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:32: Status: GENERIC_ERROR
Issues: 
<main>:1:23: Error: ERROR:  invalid Unicode surrogate pair


--
-- test conversions between various string types
-- E021-10 implicit casting among the character data types
--
SELECT CAST(name 'namefield' AS text) AS "text(name)";
 text(name) 
------------
 namefield
(1 row)

SELECT CAST(name 'namefield' AS char(10)) AS "char(name)";
 char(name) 
------------
 namefield 
(1 row)

SELECT CAST(name 'namefield' AS varchar) AS "varchar(name)";
 varchar(name) 
---------------
 namefield
(1 row)

--
-- test SQL string functions
-- E### and T### are feature reference numbers from SQL99
--
-- E021-09 trim function
SELECT TRIM(BOTH FROM '  bunch o blanks  ') = 'bunch o blanks' AS "bunch o blanks";
 bunch o blanks 
----------------
 t
(1 row)

SELECT TRIM(LEADING FROM '  bunch o blanks  ') = 'bunch o blanks  ' AS "bunch o blanks  ";
 bunch o blanks   
------------------
 t
(1 row)

SELECT TRIM(TRAILING FROM '  bunch o blanks  ') = '  bunch o blanks' AS "  bunch o blanks";
   bunch o blanks 
------------------
 t
(1 row)

SELECT TRIM(BOTH 'x' FROM 'xxxxxsome Xsxxxxx') = 'some Xs' AS "some Xs";
 some Xs 
---------
 t
(1 row)

-- E021-06 substring expression
SELECT SUBSTRING('1234567890' FROM 3) = '34567890' AS "34567890";
 34567890 
----------
 t
(1 row)

SELECT SUBSTRING('1234567890' FROM 4 FOR 3) = '456' AS "456";
 456 
-----
 t
(1 row)

-- test overflow cases
SELECT SUBSTRING('string' FROM 2 FOR 2147483646) AS "tring";
 tring 
-------
 tring
(1 row)

SELECT SUBSTRING('string' FROM -10 FOR 2147483646) AS "string";
 string 
--------
 string
(1 row)

SELECT SUBSTRING('string' FROM -10 FOR -2147483646) AS "error";
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:66: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(46): ERROR:  negative substring length not allowed



-- 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";
 cde 
-----
 cde
(1 row)

-- With a parenthesized subexpression, return only what matches the subexpr
SELECT SUBSTRING('abcdefg' FROM 'b(.*)f') AS "cde";
 cde 
-----
 cde
(1 row)

-- T312 character overlay function
SELECT OVERLAY('abcdef' PLACING '45' FROM 4) AS "abc45f";
 abc45f 
--------
 abc45f
(1 row)

SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5) AS "yabadaba";
 yabadaba 
----------
 yabadaba
(1 row)

SELECT OVERLAY('yabadoo' PLACING 'daba' FROM 5 FOR 0) AS "yabadabadoo";
 yabadabadoo 
-------------
 yabadabadoo
(1 row)

SELECT OVERLAY('babosa' PLACING 'ubb' FROM 2 FOR 4) AS "bubba";
 bubba 
-------
 bubba
(1 row)

--
-- test implicit type conversion
--
-- E021-07 character concatenation
SELECT 'unknown' || ' and unknown' AS "Concat unknown types";
 Concat unknown types 
----------------------
 unknown and unknown
(1 row)

SELECT text 'text' || ' and unknown' AS "Concat text to unknown type";
 Concat text to unknown type 
-----------------------------
 text and unknown
(1 row)

--
-- test length
--
SELECT length('abcdef') AS "length_6";
 length_6 
----------
        6
(1 row)

--
-- test strpos
--
SELECT strpos('abcdef', 'cd') AS "pos_3";
 pos_3 
-------
     3
(1 row)

SELECT strpos('abcdef', 'xy') AS "pos_0";
 pos_0 
-------
     0
(1 row)

SELECT strpos('abcdef', '') AS "pos_1";
 pos_1 
-------
     1
(1 row)

SELECT strpos('', 'xy') AS "pos_0";
 pos_0 
-------
     0
(1 row)

SELECT strpos('', '') AS "pos_1";
 pos_1 
-------
     1
(1 row)

--
-- test replace
--
SELECT replace('abcdef', 'de', '45') AS "abc45f";
 abc45f 
--------
 abc45f
(1 row)

SELECT replace('yabadabadoo', 'ba', '123') AS "ya123da123doo";
 ya123da123doo 
---------------
 ya123da123doo
(1 row)

SELECT replace('yabadoo', 'bad', '') AS "yaoo";
 yaoo 
------
 yaoo
(1 row)

--
-- test split_part
--
select split_part('','@',1) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('','@',-1) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
 joeuser@mydatabase 
--------------------
 joeuser@mydatabase
(1 row)

select split_part('joeuser@mydatabase','',2) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
 joeuser@mydatabase 
--------------------
 joeuser@mydatabase
(1 row)

select split_part('joeuser@mydatabase','',-2) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('joeuser@mydatabase','@',0) AS "an error";
psql:/home/runner/.ya/build/build_root/vnm9/001c5e/environment/arcadia/ydb/tests/functional/postgresql/cases/strings.sql:137: Status: PRECONDITION_FAILED
Issues: 
<main>: Error: Terminate was called, reason(40): ERROR:  field position must not be zero



select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
 joeuser@mydatabase 
--------------------
 joeuser@mydatabase
(1 row)

select split_part('joeuser@mydatabase','@@',2) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('joeuser@mydatabase','@',1) AS "joeuser";
 joeuser 
---------
 joeuser
(1 row)

select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
 mydatabase 
------------
 mydatabase
(1 row)

select split_part('joeuser@mydatabase','@',3) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
 joeuser 
---------
 joeuser
(1 row)

select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
 mydatabase 
------------
 mydatabase
(1 row)

select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
 joeuser 
---------
 joeuser
(1 row)

select split_part('joeuser@mydatabase','@',-3) AS "empty string";
 empty string 
--------------
 
(1 row)

select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
 mydatabase 
------------
 mydatabase
(1 row)

--
-- test to_hex
--
select to_hex(256*256*256 - 1) AS "ffffff";
 ffffff 
--------
 ffffff
(1 row)

select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
 ffffffff 
----------
 ffffffff
(1 row)

--
-- MD5 test suite - from IETF RFC 1321
-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
--
select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
 TRUE 
------
 t
(1 row)

select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
 TRUE 
------
 t
(1 row)

