Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: implement function OID reference #96442

Merged

Conversation

chengxiong-ruan
Copy link
Contributor

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

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@chengxiong-ruan chengxiong-ruan marked this pull request as ready for review February 6, 2023 16:57
@chengxiong-ruan chengxiong-ruan requested a review from a team as a code owner February 6, 2023 16:57
@chengxiong-ruan chengxiong-ruan requested review from a team February 6, 2023 16:57
@chengxiong-ruan chengxiong-ruan requested review from a team as code owners February 6, 2023 16:57
@chengxiong-ruan chengxiong-ruan requested review from shermanCRL and mgartner and removed request for a team February 6, 2023 16:57
Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 26 of 27 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @chengxiong-ruan, @mgartner, and @shermanCRL)


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):


query I
SELECT [FUNCTION $fn_oid]()

Can you add a case where we attempt to use this syntax from a different database? And maybe also one or two where the function has been altered or dropped.

@chengxiong-ruan chengxiong-ruan force-pushed the implement-function-reference-by-oid branch 3 times, most recently from 26a3fb3 to 6c9468e Compare February 6, 2023 19:30
@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Can you add a case where we attempt to use this syntax from a different database? And maybe also one or two where the function has been altered or dropped.

Good call! Yeah, we need to disallow cross-db references event referencing by id. Fixed that and added test case for it.
Also add cases for renaming a function, altering function's parent schema and dropping a function

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd defer to @mgartner for approval, but :lgtm:

Reviewed 9 of 9 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @chengxiong-ruan, @mgartner, and @shermanCRL)


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

Good call! Yeah, we need to disallow cross-db references event referencing by id. Fixed that and added test case for it.
Also add cases for renaming a function, altering function's parent schema and dropping a function

Nice fix! See also #96674 - we have similar problems with UDTs and tables referenced by OID.

@chengxiong-ruan chengxiong-ruan force-pushed the implement-function-reference-by-oid branch from 6c9468e to 92a9df6 Compare February 7, 2023 04:34
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I just left a couple questions and suggestions.

Reviewed 16 of 27 files at r1, 9 of 9 files at r2, 1 of 2 files at r3, 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @chengxiong-ruan, @DrewKimball, and @shermanCRL)


pkg/sql/sem/tree/function_name.go line 162 at r4 (raw file):

func (*OIDFunctionReference) functionReference()       {}

type OIDFunctionReference struct {

nit: Maybe call this just FunctionOID.


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Nice fix! See also #96674 - we have similar problems with UDTs and tables referenced by OID.

Why do we need to disallow cross-database references with this syntax? Are OIDs of functions only unique within a database? That might cause problems when joining two tables from different databases that have computed columns/partial index predictes/etc. that reference UDFs.


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 2 at r4 (raw file):

query T
SELECT [FUNCTION 1074]('hello,world', ',')

Can you add a comment explaining what function this is? Is it a builtin?


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 106 at r4 (raw file):


statement error pq: cross database function references are not supported
SELECT [FUNCTION $fn_oid]('hello world')

Can you add a test with a function OID reference within a UDF? That should error for now.

@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Why do we need to disallow cross-database references with this syntax? Are OIDs of functions only unique within a database? That might cause problems when joining two tables from different databases that have computed columns/partial index predictes/etc. that reference UDFs.

Thanks for pointing this out, I thought cross-db references are not encouraged in CRDB for some reason. I'll relax this constrain for OID reference since there is a need.

@mgartner
Copy link
Collaborator

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

Thanks for pointing this out, I thought cross-db references are not encouraged in CRDB for some reason. I'll relax this constrain for OID reference since there is a need.

I think you're correct about them being discouraged - it's not supported by PG by design, AFAIK. I don't know why we support it - but we do need to make sure that we either produce expected results with cross-db references or have nice errors messages for behaviors we don't support.

@chengxiong-ruan chengxiong-ruan force-pushed the implement-function-reference-by-oid branch from 92a9df6 to b2d2977 Compare February 13, 2023 19:10
@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/sem/tree/function_name.go line 162 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

nit: Maybe call this just FunctionOID.

done, good idea

@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 2 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Can you add a comment explaining what function this is? Is it a builtin?

yes, it's a builtin, comment added.

@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 106 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Can you add a test with a function OID reference within a UDF? That should error for now.

thanks for pointing this out, actually fixed a bug where resolver can be nil.

Copy link
Contributor Author

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball, @mgartner, and @shermanCRL)


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I think you're correct about them being discouraged - it's not supported by PG by design, AFAIK. I don't know why we support it - but we do need to make sure that we either produce expected results with cross-db references or have nice errors messages for behaviors we don't support.

Done.

@chengxiong-ruan chengxiong-ruan removed the request for review from shermanCRL February 13, 2023 20:05
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: I don't think the behavior of cross-database references has been solved in this PR - that's fine, but let's create an issue to track that work at the least.

Reviewed 11 of 17 files at r5.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @chengxiong-ruan and @DrewKimball)


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

Done.

I'm confused - I don't see tests for these. Did you mean to add them?


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 100 at r5 (raw file):

SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f_in_udf'

statement error pq: function 100110 not found: function undefined

You need a regex like \d+ instead of 100110 to prevent this test from flaking, right? Maybe a leave a TODO above this test here too - since we want to allow this in the future, right?


pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 104 at r5 (raw file):


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

nit: add a comment explaining that this is allowed because it is a built-in.

@chengxiong-ruan
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/udf_oid_ref line 41 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I'm confused - I don't see tests for these. Did you mean to add them?

oh weird, I thought I had a case to test to make sure that's allowed, I'll add one

@chengxiong-ruan chengxiong-ruan force-pushed the implement-function-reference-by-oid branch from b2d2977 to 41e5e6a Compare February 14, 2023 20:43
@chengxiong-ruan
Copy link
Contributor Author

TFTRs!
bors r+

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: cockroachdb#83231

Release note: None
@chengxiong-ruan chengxiong-ruan force-pushed the implement-function-reference-by-oid branch from 41e5e6a to 1c7a82b Compare February 14, 2023 23:37
@craig
Copy link
Contributor

craig bot commented Feb 14, 2023

Canceled.

@chengxiong-ruan
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Feb 15, 2023

Build succeeded:

@craig craig bot merged commit f886e7c into cockroachdb:master Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UDF: Support Function OIDs and Function reference with OIDs in expressions
4 participants