# LogicTest: default parallel-stmts distsql

query error unknown function: foo.bar
SELECT foo.bar()

query error unknown function: defaults
SELECT defaults()

query II colnames
SELECT LENGTH('roach7'), LENGTH(b'roach77')
----
length('roach7')  length(b'roach77')
6                 7

query II
SELECT LENGTH('Hello, 世界'), LENGTH(b'Hello, 世界')
----
9 13

statement error unknown signature: length\(int\)
SELECT LENGTH(23)

query II
SELECT octet_length('Hello'), octet_length(b'世界')
----
5 6

query T colnames
SELECT UPPER('roacH7')
----
upper('roacH7')
ROACH7

statement error unknown signature: upper\(decimal\)
SELECT UPPER(2.2)

query T colnames
SELECT LOWER('RoacH7')
----
lower('RoacH7')
roach7

statement error unknown signature: lower\(int\)
SELECT LOWER(32)

# Multiplying by zero so the result is deterministic.
query R
SELECT RANDOM() * 0.0
----
0

# Concatenating 'empty' because the empty string doesn't work in these tests.
query T
SELECT CONCAT() || 'empty'
----
empty

query T
SELECT CONCAT('RoacH', NULL)
----
RoacH

statement error unknown signature: concat\(string, bool, decimal, bool\)
SELECT CONCAT('RoacH', false, 64.532, TRUE)
----

query T
SELECT SUBSTR('RoacH', 2, 3)
----
oac

query T
SELECT SUBSTRING('RoacH', 2, 3)
----
oac

query T
SELECT SUBSTRING('💩oacH', 2, 3)
----
oac

query T
SELECT SUBSTRING('RoacH' from 2 for 3)
----
oac

query T
SELECT SUBSTRING('RoacH' for 3 from 2)
----
oac

query T
SELECT SUBSTR('RoacH', 2)
----
oacH

query T
SELECT SUBSTR('💩oacH', 2)
----
oacH

query T
SELECT SUBSTRING('RoacH' from 2)
----
oacH

query T
SELECT SUBSTR('RoacH', -2)
----
RoacH

query T
SELECT SUBSTR('RoacH', -2, 4)
----
R

# See #6601 and #8210. We use three arguments to guarantee a very high
# probability that at least one of the UUIDs is invalid UTF8.
query error invalid utf8
select cast(uuid_v4() as string), cast(uuid_v4() as string), cast(uuid_v4() as string)

query T
SELECT SUBSTR('12345', 2, 77)
----
2345

query T
SELECT SUBSTR('12345', -2, 77)
----
12345

statement error substr\(\): negative substring length -1 not allowed
SELECT SUBSTR('12345', 2, -1)

query T
SELECT SUBSTR('string', 4827075662841736053, 5123273972570225659) || 'empty'
----
empty

query T
SELECT SUBSTRING('12345' for 3)
----
123

query T
SELECT SUBSTRING('foobar' from 'o.b')
----
oob

query T
SELECT SUBSTRING('f(oabaroob' from '\(o(.)b')
----
a

query T
SELECT SUBSTRING('f(oabaroob' from '+(o(.)b' for '+')
----
a

query error substring\(\): error parsing regexp: missing closing \): `\\\\\(o\(.\)b`
SELECT SUBSTRING('f(oabaroob' from '\(o(.)b' for '+')

query error unknown signature: substring\(\)
SELECT SUBSTRING()

query error unknown signature: concat_ws\(\)
SELECT CONCAT_WS()

query T
SELECT CONCAT_WS(NULL::STRING, 'a', 'b')
----
NULL

query T
SELECT CONCAT_WS(',', 'abcde', NULL)
----
abcde

query T
SELECT CONCAT_WS(',', 'abcde', '2')
----
abcde,2

statement error unknown signature: concat_ws\(string, string, int, NULL, int\)
SELECT CONCAT_WS(',', 'abcde', 2, NULL, 22)
----

query T
SELECT split_part('abc~@~def~@~ghi', '~@~', 2)
----
def

query T
SELECT repeat('Pg', 4)
----
PgPgPgPg

query T
SELECT repeat('Pg', -1) || 'empty'
----
empty

statement error pq: repeat\(\): .* memory budget exceeded
SELECT repeat('s', 9223372036854775807)

query I
SELECT ascii('x')
----
120

query I
select ascii('禅')
----
31109

query error ascii\(\): the input string must not be empty
select ascii('')

query T
SELECT md5('abc')
----
900150983cd24fb0d6963f7d28e17f72

query T
SELECT sha1('abc')
----
a9993e364706816aba3e25717850c26c9cd0d89d

query T
SELECT sha256('abc')
----
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

query IIII
SELECT fnv32('abc'), fnv32a('abc'), fnv64('abc'), fnv64a('abc')
----
1134309195  440920331  -2820157060406071861  -1792535898324117685

query II
SELECT crc32ieee('abc'), crc32c('abc')
----
891568578  910901175

query T
SELECT to_hex(2147483647)
----
7fffffff

query I
SELECT strpos('high', 'a')
----
0

query I
SELECT strpos('high', 'ig')
----
2

query I
SELECT strpos('💩high', 'ig')
----
3

query I
SELECT position('ig' in 'high')
----
2

query I
SELECT position('a' in 'high')
----
0

query error unknown signature: strpos\(\)
SELECT position()

query T
SELECT overlay('123456789' placing 'xxxx' from 3)
----
12xxxx789

query T
SELECT overlay('123456789' placing 'xxxx' from 3 for 2)
----
12xxxx56789

query T
SELECT overlay('123456789' placing 'xxxx' from 3 for 6)
----
12xxxx9

query T
SELECT overlay('123456789' placing 'xxxx' from 15 for 6)
----
123456789xxxx

query T
SELECT overlay('123456789' placing 'xxxx' from 3 for 10)
----
12xxxx

query T
SELECT overlay('123456789' placing 'xxxx' from 3 for -1)
----
12xxxx23456789

query T
SELECT overlay('123456789' placing 'xxxx' from 3 for -8)
----
12xxxx123456789

query T
SELECT overlay('💩123456789' placing 'xxxxÂ' from 3 for 3)
----
💩1xxxxÂ56789

query error non-positive substring length not allowed: -1
SELECT overlay('123456789' placing 'xxxx' from -1 for 6)

query T
SELECT btrim('xyxtrimyyx', 'xy')
----
trim

query T
SELECT trim('xy' from 'xyxtrimyyx')
----
trim

query T
SELECT trim(both 'xy' from 'xyxtrimyyx')
----
trim

query T
SELECT 'a' || btrim('    postgres    ') || 'b'
----
apostgresb

query T
SELECT ltrim('zzzytrimxyz', 'xyz')
----
trimxyz

query T
SELECT trim(leading 'xyz' from 'zzzytrimxyz')
----
trimxyz

query T
SELECT ltrim('   trimxyz')
----
trimxyz

query T
SELECT trim(leading '   trimxyz')
----
trimxyz

query T
SELECT trim(leading from '   trimxyz')
----
trimxyz


query T
SELECT rtrim('xyzzzzytrimxyz', 'xyz')
----
xyzzzzytrim

query T
SELECT trim(trailing 'xyz' from 'xyzzzzytrimxyz')
----
xyzzzzytrim

query T
SELECT 'a' || rtrim(' zzzytrimxyz   ')
----
a zzzytrimxyz

query T
SELECT reverse('abcde')
----
edcba

query T
SELECT reverse('世界')
----
界世

query T
SELECT replace('abcdefabcdef', 'cd', 'XX')
----
abXXefabXXef

query T
SELECT replace(initcap('hi THOMAS'), ' ', '')
----
HiThomas

query T
SELECT initcap('THOMAS')
----
Thomas

query T
SELECT left('💩abcde'::bytes, 2)
----
[240 159]

query T
SELECT right('abcde💩'::bytes, 2)
----
[146 169]

query T
SELECT left('💩abcde', 2)
----
💩a

query T
SELECT right('abcde💩', 2)
----
e💩

query RRRIIR
SELECT abs(-1.2::float), abs(1.2::float), abs(-0.0::float), abs(0), abs(1), abs(-1.2121::decimal)
----
1.2 1.2 0 0 1 1.2121

query error abs\(\): abs of min integer value \(-9223372036854775808\) not defined
SELECT abs(-9223372036854775808)

query I
SELECT abs(-9223372036854775807)
----
9223372036854775807

query B
SELECT abs(sin(pi())) < 1e-12
----
true

query RR
SELECT acos(-0.5), acos(0.5)
----
2.0943951023931957 1.0471975511965976

query RR
SELECT cot(-0.5), cot(0.5)
----
-1.830487721712452 1.830487721712452

query RRR
SELECT asin(-0.5), asin(0.5), asin(1.5)
----
-0.5235987755982989 0.5235987755982989 NaN

query RR
SELECT atan(-0.5), atan(0.5)
----
-0.4636476090008061 0.4636476090008061

query RR
SELECT atan2(-10.0, 5.0), atan2(10.0, 5.0)
----
-1.1071487177940904 1.1071487177940904

query RRR
SELECT cbrt(-1.0::float), cbrt(27.0::float), cbrt(19.3::decimal)
----
-1 3 2.6823725926296729544

query RRRRR
SELECT ceil(-0.5::float), ceil(0.5::float), ceiling(0.5::float), ceil(0.1::decimal), ceiling(-0.9::decimal)
----
-0  1  1  1  -0

query RR
SELECT cos(-0.5), cos(0.5)
----
0.8775825618903728 0.8775825618903728

query IIII
SELECT div(-1::int, 2::int), div(1::int, 2::int), div(9::int, 4::int), div(-9::int, 4::int)
----
0 0 2 -2

query RRRRRR
SELECT div(-1.0::float, 2.0), div(1.0::float, 2.0), div(9.0::float, 4.0), div(-9.0::float, 4.0), div(1.0::float, 0.0), div(1111.0::decimal, 9.44)
----
-0 0 2 -2 +Inf 117

query RR
SELECT degrees(-0.5), degrees(0.5)
----
-28.64788975654116 28.64788975654116

query error div\(\): division by zero
SELECT div(1.0::decimal, 0.0::decimal)

query error div\(\): division by zero
SELECT div(1::int, 0::int)

# math.Exp(1.0) returns different results on amd64 vs arm64.
# Round to make this test consistent across archs.
# See https://github.com/golang/go/issues/20319.
query RRR
SELECT exp(-1.0::float), round(exp(1.0::float), 13), exp(2.0::decimal)
----
0.36787944117144233 2.718281828459 7.3890560989306502272

query error exp\(\): overflow
SELECT exp(1e2000::decimal)

query RRR
SELECT floor(-1.5::float), floor(1.5::float), floor(9.123456789::decimal)
----
-2 1 9

query BBBBBB
SELECT 1::FLOAT IS NAN, 1::FLOAT IS NOT NAN, isnan(1::FLOAT), 'NaN'::FLOAT IS NAN, 'NaN'::FLOAT IS NOT NAN, isnan('NaN'::FLOAT)
----
false true false true false true

query RRR
SELECT ln(-2.0::float), ln(2.0::float), ln(2.5::decimal)
----
NaN 0.6931471805599453 0.91629073187415506518

query error cannot take logarithm of a negative number
SELECT ln(-100.000::decimal)

query error cannot take logarithm of zero
SELECT ln(0::decimal)

query RR
SELECT log(10.0::float), log(100.000::decimal)
----
1 2.0000000000000000000

query error cannot take logarithm of a negative number
SELECT log(-100.000::decimal)

query error cannot take logarithm of zero
SELECT log(0::decimal)

query RRIR
SELECT mod(5.0::float, 2.0), mod(1.0::float, 0.0), mod(5, 2), mod(19.3::decimal, 2)
----
1 NaN 1 1.3

# mod returns the same results as PostgreSQL 9.4.4
# in tests below (except for the error message).

query error mod\(\): zero modulus
SELECT mod(5, 0)

query error mod\(\): zero modulus
SELECT mod(5::decimal, 0::decimal)

query II
SELECT mod(-100, -8), mod(-100, 8)
----
-4 -4

query I
SELECT mod(-9223372036854775808, 3)
----
-2

query I
SELECT mod(-9223372036854775808, -1)
----
0

query I
SELECT mod(9223372036854775807, -1)
----
0

query I
SELECT mod(9223372036854775807, -2)
----
1

query I
SELECT mod(9223372036854775807, 1)
----
0

query I
SELECT mod(9223372036854775807, 2)
----
1

query I
SELECT mod(9223372036854775807, 4)
----
3

# div and mod are a logical pair

query R
SELECT div(9.0::float, 2.0) * 2.0 + mod(9.0::float, 2.0)
----
9

query R
SELECT div(9.0::float, -2.0) * -2.0 + mod(9.0::float, -2.0)
----
9

query R
SELECT div(-9.0::float, 2.0) * 2.0 + mod(-9.0::float, 2.0)
----
-9

query R
SELECT div(-9.0::float, -2.0) * -2.0 + mod(-9.0::float, -2.0)
----
-9

query R
SELECT pi()
----
3.141592653589793

query II
SELECT pow(-2::int, 3::int), pow(2::int, 3::int)
----
-8 8

statement error integer out of range
SELECT pow(2::int, -3::int)

query III
SELECT pow(0::int, 3::int), pow(3::int, 0::int), pow(-3::int, 0::int)
----
0 1 1

statement error integer out of range
SELECT pow(0::int, -3::int)

statement error invalid operation
SELECT pow(0::int, 0::int)

query RRR
SELECT pow(-3.0::float, 2.0), power(3.0::float, 2.0), pow(5.0::decimal, 2.0)
----
9 9 25.00

query R
SELECT pow(CAST (pi() AS DECIMAL), DECIMAL '2.0')
----
9.8696044010893571205

query R
SELECT power(0::decimal, -1)
----
Infinity

statement error invalid operation
SELECT power(-1, -.1)

query RR
SELECT radians(-45.0), radians(45.0)
----
-0.7853981633974483 0.7853981633974483

query error invalid operation
SELECT round(123.456::float, -2438602134409251682)

query RRR
SELECT round(4.2::float, 0), round(4.2::float, 10), round(4.22222222::decimal, 3)
----
4 4.2 4.222

query R
SELECT round(1e-308::float, 324)
----
1e-308

# round to nearest even
query RRRR
SELECT round(-2.5::float, 0), round(-1.5::float, 0), round(1.5::float, 0), round(2.5::float, 0)
----
-2 -2 2 2

query RRRRRR
SELECT round(-2.5::float), round(-1.5::float), round(-0.0::float), round(0.0::float), round(1.5::float), round(2.5::float)
----
-2 -2 -0 0 2 2

# round up for decimals
# These results are indeed different than floats. Compare with postgres.
# Float rounding uses banker, decimal rounding uses half away from zero.
query RRRR
SELECT round(-2.5::decimal, 0), round(-1.5::decimal, 0), round(1.5::decimal, 0), round(2.5::decimal, 0)
----
-3 -2 2 3

query RRRRR
SELECT round(-2.5::decimal, 3), round(-1.5::decimal, 3), round(0.0::decimal, 3), round(1.5::decimal, 3), round(2.5::decimal, 3)
----
-2.500 -1.500 0.000 1.500 2.500

query RRRRR
SELECT round(-2.5::decimal), round(-1.5::decimal), round(0.0::decimal), round(1.5::decimal), round(2.5::decimal)
----
-3 -2 0 2 3

# Test rounding to 14 digits, because the logic test itself
# formats floats rounded to 15 digits behind the decimal point.

query RRR
SELECT round(-2.123456789, 5), round(2.123456789, 5), round(2.12345678901234567890, 14)
----
-2.12346 2.12346 2.12345678901235

query RR
SELECT round(-1.7976931348623157e+308::float, 1), round(1.7976931348623157e+308::float, 1)
----
-1.7976931348623157e+308 1.7976931348623157e+308

query RR
SELECT round(-1.7976931348623157e+308::float, -303), round(1.7976931348623157e+308::float, -303)
----
-1.79769e+308 1.79769e+308

query RR
SELECT round(-1.23456789e+308::float, -308), round(1.23456789e+308::float, -308)
----
-1e+308 1e+308

query RR
SELECT round(-1.7976931348623157e-308::float, 1), round(1.7976931348623157e-308::float, 1)
----
-0 0

query RRRR
SELECT 1.234567890123456789::float,  round(1.234567890123456789::float,15), round(1.234567890123456789::float,16), round(1.234567890123456789::float,17)
----
1.2345678901234567 1.234567890123457 1.2345678901234567 1.2345678901234567

query RRR
SELECT round(123.456::float, -1), round(123.456::float, -2), round(123.456::float, -3)
----
120 100 0

query RRRR
SELECT round(123.456::decimal, -1), round(123.456::decimal, -2), round(123.456::decimal, -3), round(123.456::decimal, -200)
----
120  100  0000  000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

query error invalid operation
SELECT round(1::decimal, 3000)

query III
SELECT sign(-2), sign(0), sign(2)
----
-1 0 1

query RRRR
SELECT sign(-2.0), sign(-0.0), sign(0.0), sign(2.0)
----
-1 0 0 1

query RRR
SELECT sin(-1.0), sin(0.0), sin(1.0)
----
-0.8414709848078965 0 0.8414709848078965

query RR
SELECT sqrt(4.0::float), sqrt(9.0::decimal)
----
2 3

query error cannot take square root of a negative number
SELECT sqrt(-1.0::float)

query error cannot take square root of a negative number
SELECT sqrt(-1.0::decimal)

query RRR
SELECT tan(-5.0), tan(0.0), tan(5.0)
----
3.3805150062465854 0 -3.3805150062465854

query RRRR
SELECT trunc(-0.0), trunc(0.0), trunc(1.9), trunc(19.5678::decimal)
----
0 0 1 19

query T
SELECT translate('Techonthenet.com', 'e.to', '456')
----
T4chn6h4n465cm

query T
SELECT translate('12345', '143', 'ax')
----
a2x5

query T
SELECT translate('12345', 'abc', 'ax')
----
12345

query T
SELECT translate('a‰ÒÁ', 'aÒ', '∏p')
----
∏‰pÁ

query T
SELECT regexp_extract('foobar', 'o.b')
----
oob

query T
SELECT regexp_extract('foobar', 'o(.)b')
----
o

query T
SELECT regexp_extract('foobar', '(o(.)b)')
----
oob

query T
SELECT regexp_extract('foabaroob', 'o(.)b')
----
a

query T
SELECT regexp_extract('foobar', 'o.x')
----
NULL

query T
SELECT regexp_replace('foobarbaz', 'b..', 'X')
----
fooXbaz

query T
SELECT regexp_replace('foobarbaz', 'b..', 'X', 'g')
----
fooXX

query T
SELECT regexp_replace('foobarbaz', 'b(..)', E'X\\1Y', 'g')
----
fooXarYXazY

query T
SELECT regexp_replace('foobarbaz', 'b(.)(.)', E'X\\2\\1\\3Y', 'g')
----
fooXraYXzaY

query T
SELECT regexp_replace(E'fooBa\nrbaz', 'b(..)', E'X\\&Y', 'gi')
----
fooXBa
YrXbazY

query T
SELECT regexp_replace(E'fooBa\nrbaz', 'b(..)', E'X\\&Y', 'gmi')
----
fooBa
rXbazY

query T
SELECT regexp_replace(E'fooBar\nbaz', 'b(..)$', E'X\\&Y', 'gpi')
----
fooBar
XbazY

query T
SELECT regexp_replace(E'fooBar\nbaz', 'b(..)$', E'X\\&Y', 'gwi')
----
fooXBarY
XbazY

query T
SELECT regexp_replace('foobarbaz', 'nope', 'NO')
----
foobarbaz

query error regexp_replace\(\): invalid regexp flag: 'z'
SELECT regexp_replace(E'fooBar\nbaz', 'b(..)$', E'X\\&Y', 'z')

query T
SELECT regexp_replace(E'Foo\nFoo', '^(foo)', 'BAR', 'i')
----
BAR
Foo

query T
SELECT regexp_replace(e'DOGGIE\ndog \nDOG', '^d.+', 'CAT', 's')
----
DOGGIE
dog
DOG

query T
SELECT regexp_replace(e'DOGGIE\ndog \nDOG', '^d.+', 'CAT', 'n');
----
DOGGIE
CAT
DOG

query T
SELECT regexp_replace(e'DOGGIE\ndog \nDOG', '^D.+', 'CAT', 'p')
----
CAT
dog
DOG

query T
SELECT regexp_replace(e'DOGGIE\ndog \nDOG', '^d.+', 'CAT', 'w')
----
DOGGIE
CAT

query T
SELECT (timestamp '2016-02-10 19:46:33.306157519')::string
----
2016-02-10 19:46:33.306158+00:00

query T
SELECT (timestamptz '2016-02-10 19:46:33.306157519')::string
----
2016-02-10 19:46:33.306158+00:00

query I
SELECT extract(milliseconds FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306

query I
SELECT extract(millisecond FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306

query I
SELECT extract(microseconds FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306158

query I
SELECT extract(microsecond FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
306158

query I
SELECT extract(second FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
33

query I
SELECT extract(seconds FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
33

query I
SELECT extract(minute FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
46

query I
SELECT extract(minutes FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
46

query I
SELECT extract(hour FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
19

query I
SELECT extract(hours FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
19

query I
SELECT extract(day FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
10

query I
SELECT extract(days FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
10

query I
SELECT extract(month FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
2

query I
SELECT extract(months FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
2

query I
SELECT extract(year FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
2016

query I
SELECT extract(years FROM '2016-02-10 19:46:33.306157519'::timestamp)
----
2016

query I
SELECT extract(epoch FROM '1970-01-02 00:00:01.000001'::timestamp)
----
86401

query B
SELECT unique_rowid() < unique_rowid()
----
true

query BI
SELECT uuid_v4() != uuid_v4(), length(uuid_v4())
----
true 16

query error syntax error at or near.*
SELECT GREATEST()

query error syntax error at or near.*
SELECT LEAST()

query I
SELECT GREATEST(4, 5, 7, 1, 2)
----
7

query I
SELECT LEAST(4, 5, 7, 1, 2)
----
1

query I
SELECT GREATEST(4, NULL, 7, 1, 2)
----
7

query I
SELECT GREATEST(NULL, NULL, 7, NULL, 2)
----
7

query I
SELECT GREATEST(NULL, NULL, NULL, NULL, 2)
----
2

query I
SELECT GREATEST(2, NULL, NULL, NULL, NULL)
----
2

query I
SELECT LEAST(4, NULL, 7, 1, 2)
----
1

query I
SELECT GREATEST(NULL, NULL, NULL)
----
NULL

query I
SELECT LEAST(NULL, NULL, NULL)
----
NULL

query error greatest\(\): expected '4' to be of type int, found type string
SELECT GREATEST(2, '4')

query error least\(\): expected '4' to be of type int, found type string
SELECT LEAST(2, '4')

query T
SELECT GREATEST('foo', 'bar', 'foobar')
----
foobar

query T
SELECT LEAST('foo', 'bar', 'foobar')
----
bar

query R
SELECT GREATEST(1, 1.2)
----
1.2

# Test float and int comparison.

query BBBB
select 1 = 1.0::float, 1.0::float = 1, 1 = 2.0::float, 2.0::float = 1
----
true true false false

query BBBB
select 1 < 2.0::float, 1.0::float < 2, 2.0::float < 1, 2 < 1.0::float
----
true true false false

query BBBB
select 1 <= 1.0::float, 1.0::float <= 1, 2.0::float <= 1, 2 <= 1.0::float
----
true true false false

query BBBB
select 2 > 1.0::float, 2.0::float > 1, 1 > 2.0::float, 1.0::float > 2
----
true true false false

query BBBB
select 1 >= 1.0::float, 1.0::float >= 1, 1.0::float >= 2, 1 >= 2.0::float
----
true true false false

# Test decimal and int comparison.

query BBBB
select 1 = 1.0::decimal, 1.0::decimal = 1, 1 = 2.0::decimal, 2.0::decimal = 1
----
true true false false

query BBBB
select 1 < 2.0::decimal, 1.0::decimal < 2, 2.0::decimal < 1, 2 < 1.0::decimal
----
true true false false

query BBBB
select 1 <= 1.0::decimal, 1.0::decimal <= 1, 2.0::decimal <= 1, 2 <= 1.0::decimal
----
true true false false

query BBBB
select 2 > 1.0::decimal, 2.0::decimal > 1, 1 > 2.0::decimal, 1.0::decimal > 2
----
true true false false

query BBBB
select 1 >= 1.0::decimal, 1.0::decimal >= 1, 1.0::decimal >= 2, 1 >= 2.0::decimal
----
true true false false

# Test float and decimal comparison.

query BBBB
select 1::decimal = 1.0, 1.0 = 1::decimal, 1::decimal = 2.0, 2.0 = 1::decimal
----
true true false false

query BBBB
select 1::decimal < 2.0, 1.0 < 2::decimal, 2.0 < 1::decimal, 2::decimal < 1.0
----
true true false false

query BBBB
select 1::decimal <= 1.0, 1.0 <= 1::decimal, 2.0 <= 1::decimal, 2::decimal <= 1.0
----
true true false false

query BBBB
select 2::decimal > 1.0, 2.0 > 1::decimal, 1::decimal > 2.0, 1.0 > 2::decimal
----
true true false false

query BBBB
select 1::decimal >= 1.0, 1.0 >= 1::decimal, 1.0 >= 2::decimal, 1::decimal >= 2.0
----
true true false false

query I
SELECT strpos(version(), 'CockroachDB')
----
1

# Don't panic during incorrect use of * (#7727)
query error pq: cos\(\): cannot use "\*" in this context
SELECT COS(*) FROM system.namespace

# Don't panic with invalid names (#8045)
query error rtrim\(\): cannot use "nonexistent.\*" in this context
SELECT TRIM(TRAILING nonexistent.*[1])

query error rtrim\(\): cannot use "foo.\*" in this context
SELECT TRIM(TRAILING foo.*[1]) FROM (VALUES (1)) AS foo(x)

# Don't panic with invalid names (#8044)
query error cannot use "nonexistent.\*" in this context
SELECT OVERLAY(nonexistent.* PLACING 'string' FROM 'string')

query error cannot use "foo.\*" in this context
SELECT OVERLAY(foo.* PLACING 'string' FROM 'string') FROM (VALUES (1)) AS foo(x)

# Don't panic with invalid names (#8023)
query error cannot use "nonexistent.\*" in this context
SELECT nonexistent.* IS NOT TRUE

query error cannot use "foo.\*" in this context
SELECT foo.* IS NOT TRUE FROM (VALUES (1)) AS foo(x)

query T
SELECT CURRENT_SCHEMAS(true)
----
{test,pg_catalog}

query T
SELECT CURRENT_SCHEMAS(false)
----
{test}

query error pq: unknown signature: current_schemas()
SELECT CURRENT_SCHEMAS()

query T
SELECT CURRENT_SCHEMAS(NULL::bool)
----
NULL

query B
SELECT 'test' = ANY (CURRENT_SCHEMAS(true))
----
true

query B
SELECT 'not test' = ANY (CURRENT_SCHEMAS(true))
----
false

statement ok
SET DATABASE = ''

query T
SELECT current_schema()
----
NULL

statement ok
SET DATABASE = 'test'

query T
SELECT current_schema()
----
test

statement ok
SET DATABASE = ''

query T
SELECT current_schema()
----
NULL

query I
SELECT array_length(ARRAY['a', 'b'], 1)
----
2

query I
SELECT array_length(ARRAY['a'], 1)
----
1

query I
SELECT array_length(ARRAY['a'], 0)
----
NULL

query I
SELECT array_length(ARRAY['a'], 2)
----
NULL

query I
SELECT array_lower(ARRAY['a', 'b'], 1)
----
1

query I
SELECT array_lower(ARRAY['a'], 1)
----
1

query I
SELECT array_lower(ARRAY['a'], 0)
----
NULL

query I
SELECT array_lower(ARRAY['a'], 2)
----
NULL

query I
SELECT array_upper(ARRAY['a', 'b'], 1)
----
2

query I
SELECT array_upper(ARRAY['a'], 1)
----
1

query I
SELECT array_upper(ARRAY['a'], 0)
----
NULL

query I
SELECT array_upper(ARRAY['a'], 2)
----
NULL

query I
SELECT array_length(ARRAY[]:::int[], 1)
----
NULL

query I
SELECT array_lower(ARRAY[]:::int[], 1)
----
NULL

query I
SELECT array_upper(ARRAY[]:::int[], 1)
----
NULL

query I
SELECT array_length(ARRAY[ARRAY[1, 2]], 2)
----
2

query I
SELECT array_lower(ARRAY[ARRAY[1, 2]], 2)
----
1

query I
SELECT array_upper(ARRAY[ARRAY[1, 2]], 2)
----
2

query T
SELECT FROM_IP(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x01\x02\x03\x04')
----
1.2.3.4

query T
SELECT FROM_IP(TO_IP('1.2.3.4'))
----
1.2.3.4

# net.IP.String() always gives us the most succinct form of ipv6
query T
select FROM_IP(TO_IP('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))
----
2001:db8:85a3::8a2e:370:7334

query error pq: unknown signature: to_ip()
SELECT TO_IP()

query error pq: from_ip\(\): zero length IP
SELECT FROM_IP(b'')

query error pq: to_ip\(\): invalid IP format: ''
SELECT TO_IP('')

query error pq: to_ip\(\): invalid IP format: 'asdf'
select TO_IP('asdf')

query R
select ln(4.0786335175292462e+34::decimal)
----
79.693655171940461633

query TTTTTT
SELECT TO_UUID('63616665-6630-3064-6465-616462656566'),
       TO_UUID('{63616665-6630-3064-6465-616462656566}'),
       TO_UUID('urn:uuid:63616665-6630-3064-6465-616462656566'),
       FROM_UUID(b'cafef00ddeadbeef'),
       TO_UUID(FROM_UUID(b'cafef00ddeadbeef')),
       FROM_UUID(TO_UUID('63616665-6630-3064-6465-616462656566'))
----
cafef00ddeadbeef
cafef00ddeadbeef
cafef00ddeadbeef
63616665-6630-3064-6465-616462656566
cafef00ddeadbeef
63616665-6630-3064-6465-616462656566

query error uuid: UUID string too short
SELECT TO_UUID('63616665-6630-3064-6465')

query error uuid: UUID string too long
SELECT TO_UUID('63616665-6630-3064-6465-616462656566-123')

query error uuid: invalid string format
SELECT TO_UUID('6361666512-6630-3064-6465-616462656566')

query error uuid: UUID must be exactly 16 bytes long, got 4 bytes
SELECT FROM_UUID(b'f00d')

query T
SELECT pg_catalog.pg_typeof(sign(1:::decimal))
----
decimal

query T
VALUES (pg_typeof(1:::int)),
       (pg_typeof('a':::string)),
       (pg_typeof(true)),
       (pg_typeof(null)),
       (pg_typeof('3m':::interval)),
       (pg_typeof('2016-11-12':::date)),
       (pg_typeof(now():::timestamptz)),
       (pg_typeof(b'a':::bytes)),
       (pg_typeof(array[1,2,3]))
----
int
string
bool
NULL
interval
date
timestamptz
bytes
int[]

query T
VALUES (format_type('anyelement'::regtype, -1)),
       (format_type('bool'::regtype, -1)),
       (format_type('bytea'::regtype, -1)),
       (format_type('date'::regtype, -1)),
       (format_type('numeric'::regtype, -1)),
       (format_type('interval'::regtype, -1)),
       (format_type('timestamp'::regtype, -1)),
       (format_type('timestamptz'::regtype, -1)),
       (format_type('record'::regtype, -1))
----
anyelement
boolean
bytea
date
numeric
interval
timestamp without time zone
timestamp with time zone
record

query T
SELECT format_type(oid, -1) FROM pg_type WHERE typname='text' LIMIT 1
----
text

query T
SELECT format_type(oid, -1) FROM pg_type WHERE typname='int8' LIMIT 1
----
bigint

query T
SELECT format_type(oid, -1) FROM pg_type WHERE typname='float8' LIMIT 1
----
double precision

query T
SELECT format_type(oid, -1) FROM pg_type WHERE typname='_int8' LIMIT 1
----
bigint[]

query T
SELECT format_type(oid, -1) FROM pg_type WHERE typname='_text' LIMIT 1
----
text[]

query T
SELECT pg_catalog.pg_get_userbyid((SELECT oid FROM pg_roles WHERE rolname='root'))
----
root

query T
SELECT pg_catalog.pg_get_userbyid(20)
----
unknown (OID=20)

query error unknown index \(OID=0\)
SELECT pg_catalog.pg_get_indexdef(0)

statement ok
CREATE TABLE test.pg_indexdef_test (a INT, INDEX pg_indexdef_idx (a ASC), INDEX other (a DESC))

query T
SELECT pg_catalog.pg_get_indexdef((SELECT oid from pg_class WHERE relname='pg_indexdef_idx'))
----
CREATE INDEX pg_indexdef_idx ON test.pg_indexdef_test (a ASC)

query error pq: unknown signature: pg_get_indexdef\(int, int, bool\)
SELECT pg_catalog.pg_get_indexdef(0, 0, true)

# These functions always return NULL since we don't support comments.
query TTTT
SELECT col_description('pg_class'::regclass::oid, 2),
       obj_description('pg_class'::regclass::oid, 'pg_class'),
       obj_description('pg_class'::regclass::oid),
       shobj_description('pg_class'::regclass::oid, 'pg_class')
----
NULL  NULL  NULL  NULL

# Check that base function names are also visible in namespace pg_catalog.
query I
SELECT pg_catalog.length('hello')
----
5

query OOO
SELECT oid(3), oid(0), oid(12023948723)
----
3  0  12023948723

query T
SELECT to_english(i) FROM (VALUES (1), (13), (617), (-2)) AS a(i)
----
one
one-three
six-one-seven
minus-two

# Do some basic sanity checking of the variadic hash functions.
query BBBBBBBBB
SELECT
  sha512('1') = sha512('1'),
  sha512('1') = sha512('2'),
  sha512('1', '2') = sha512('1', '2'),
  sha512('1', '2') = sha512('2', '1'),
  sha512('1', '2') = sha512('12'),
  sha512('1', '2') = sha512('21'),
  sha512('bar') = sha512(b'bar':::bytes),
  sha512(b'bar'::bytes) = sha512(b'bar':::bytes),
  sha512(b'bar'::bytes) = sha512('bar')
----
true false true false true false true true true

# The hash functions should be stable, so verify that the following hashes
# don't change.
query T
SELECT i FROM (VALUES
  (sha512(NULL::string)),
  (sha512(true::string)),
  (sha512(false::string)),
  (sha512(1::int::string)),
  (sha512(1.1::float::string)),
  (sha512('foo'::string)),
  (sha512('3m'::interval::string)),
  (sha512('2016-11-12'::date::string)),
  (sha512('2015-08-24 23:45:45.53453'::timestamptz::string)),
  (sha512(b'bar':::bytes::string)),
  (sha512(b'bar'::bytes))
) AS a(i)
----
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
9120cd5faef07a08e971ff024a3fcbea1e3a6b44142a6d82ca28c6c42e4f852595bcf53d81d776f10541045abdb7c37950629415d0dc66c8d86c64a5606d32de
719fa67eef49c4b2a2b83f0c62bddd88c106aaadb7e21ae057c8802b700e36f81fe3f144812d8b05d66dc663d908b25645e153262cf6d457aa34e684af9e328d
4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
be09b235155bae6cb96b94ce4645260937e856ac3907d710850256e6351f50b428f948a7af33937445604f41cf3a3121b2dd069a057708ed1f047e133e09151e
f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7
6ca65db2a861fa0ecd0c1e29ef9566425e952d2a205706a6e955a6b1bbd721fdc2d28efdba515f9d29f34aa84223382f6ca6d11bdef23984b8ff9339b59e80f5
b2d173023893f71caadf7cb2f9557355462570de2c9c971b9cfa5494936e28df8e13d0db4d550aab66d5e7a002f678ddb02def092c069ce473cf5fb293953986
960b0fed9378be1e9adefd91e1be6ac9c1de7208008dfec438ff845135727bebea0f7458a5181079f61288176e0168cfea501b900c3e495b3ab9bbe4d372486d
d82c4eb5261cb9c8aa9855edd67d1bd10482f41529858d925094d173fa662aa91ff39bc5b188615273484021dfb16fd8284cf684ccf0fc795be3aa2fc1e6c181
d82c4eb5261cb9c8aa9855edd67d1bd10482f41529858d925094d173fa662aa91ff39bc5b188615273484021dfb16fd8284cf684ccf0fc795be3aa2fc1e6c181
