-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit implements `[FUNCTION xxxx]` OID references syntax of functions. With this change, functions can be called with the new OID numerical representation. For example `SELECT [FUNCTION 123]('helloworld')`. The intention of this syntax is only for internal serialization of references to UDFs. But it's general enough for builtin functions as well. A new implementation of the `ResolvableFunctionReference` interface, `OIDFunctionReference` is introduced for function resolution purpose for the new syntax. The `ResolveFunctionByOID` method is also refactored to return a qualified name. Fixes: #83231 Release note: None
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# 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 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 pq: unknown signature: 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 function undefined | ||
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' | ||
|
||
# TODO(chengxiong,mgartner): Fix this test when we enable support of calling UDFs from UDFs. | ||
statement error pq: function \d+ not found: function undefined | ||
CREATE FUNCTION f_using_udf() RETURNS INT LANGUAGE SQL AS $$ SELECT [FUNCTION $fn_oid]() $$; | ||
|
||
# 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() RETURNS INT LANGUAGE SQL AS $$ SELECT [FUNCTION 814]('abc') $$; | ||
|
||
query I | ||
SELECT f_using_udf(); | ||
---- | ||
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.