# 1074 is the OID of builtin function "string_to_array" with signature
# "string_to_array(str: string, delimiter: string) -> string[]".
query T
SELECT [FUNCTION 1074]('hello,world', ',')
----
{hello,world}

statement ok
CREATE TABLE t1(a INT PRIMARY KEY, b STRING DEFAULT ([FUNCTION 1074]('hello,world', ',')))

statement ok
INSERT INTO t1(a) VALUES (1)

query IT
SELECT * FROM t1
----
1  {hello,world}

statement ok
INSERT INTO t1 VALUES (2, 'hello,new,world')

statement ok
CREATE INDEX idx ON t1([FUNCTION 1074](b,','))

query IT
SELECT * FROM t1@idx WHERE [FUNCTION 1074](b, ',') = ARRAY['hello','new','world']
----
2  hello,new,world

# 814 is the OID of builtin function "length" with signature
# "length(val: string) -> int".
statement ok
ALTER TABLE t1 ADD CONSTRAINT c_len CHECK ([FUNCTION 814](b) > 2)

statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(length\(b\) > 2:::INT8\)
INSERT INTO t1 VALUES (3, 'a')

statement ok
CREATE FUNCTION f1() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$

let $fn_oid
SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f1'

query I
SELECT [FUNCTION $fn_oid]()
----
1

statement ok
CREATE FUNCTION f2(a STRING) RETURNS STRING LANGUAGE SQL AS $$ SELECT a $$

let $fn_oid
SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f2'

query T
SELECT [FUNCTION $fn_oid]('hello world')
----
hello world

# Make sure that argument types are still checked even we know which function
# overload to use.
statement error pgcode 42883 pq: unknown signature: public.f2\(int\)
SELECT [FUNCTION $fn_oid](123)

# Make sure that renaming does not break the reference.
statement ok
ALTER FUNCTION f2(STRING) RENAME TO f2_new;

query T
SELECT [FUNCTION $fn_oid]('hello world')
----
hello world

statement ok
CREATE SCHEMA sc1;

statement ok
ALTER FUNCTION f2_new(STRING) SET SCHEMA sc1;

query T
SELECT [FUNCTION $fn_oid]('hello world')
----
hello world

# Make sure that function dropped cannot be resolved.
statement ok
DROP FUNCTION sc1.f2_new(STRING);

statement error pgcode 42883 function 108 does not exist
SELECT [FUNCTION $fn_oid]('maybe')

# Referencing UDF OID within a UDF

statement ok
CREATE FUNCTION f_in_udf() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$;

let $fn_oid
SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f_in_udf'

statement ok
CREATE FUNCTION f_using_udf() RETURNS INT LANGUAGE SQL AS $$ SELECT [FUNCTION $fn_oid]() $$;

query I
SELECT f_using_udf()
----
1

# 814 is the OID of builtin function "length" with signature, and it's ok to
# call it from a UDF.
statement ok
CREATE FUNCTION f_using_udf_2() RETURNS INT LANGUAGE SQL AS $$ SELECT [FUNCTION 814]('abc') $$;

query I
SELECT f_using_udf_2();
----
3

# Make sure cross-db reference by OID is ok

statement ok
CREATE DATABASE db1

statement ok
USE db1

statement ok
CREATE FUNCTION f_cross_db() RETURNS INT LANGUAGE SQL AS $$ SELECT 321 $$;

let $fn_oid
SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f_cross_db'

statement ok
USE test

query I
SELECT [FUNCTION $fn_oid]()
----
321
