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: add support for executing simple UDFs #85674

Merged
merged 5 commits into from
Aug 8, 2022

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Aug 5, 2022

opt: fix outdated comment in execbuilder

Release note: None

sql: add helpful eval_gen instructions

Release note: None

sql: add support for executing simple UDFs

This commit adds basic support for executing single-statement,
zero-argument, scalar user-defined functions. It sets up some basic
components that are needed. There's quite a few TODOs scattered
throughout the commit that will be addressed in future commits.

Evaluation of a UDF is performed similarly to the execution of
apply-joins. During the execbuilder phase, a tree.RoutineExpr is
created to represent the invocation of a UDF. This expression contains a
closure that can generate an optimized plan for each statement in the
UDF body. During evaluation, a plan is generated and executed for each
statement sequentially (however, only a single-statement is currently
supported). The results of the last statement are returned as the
result.

Release note: None

opt: fully optimize statements in UDFs

The optimizer now creates required properties that are used when
optimizing each statement in a body UDF during evaluation of the UDF.
These props must be used to ensure that properties such as ordering and
presentation are maintained during optimization.

Release note: None

sql: execute all statements in a UDF during evaluation

All statements in a UDF body are now executed when a UDF is evaluated.

Release note: None

@mgartner mgartner requested review from a team as code owners August 5, 2022 15:00
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner mgartner force-pushed the udf-simple-execution branch 2 times, most recently from 2b1e650 to 4759880 Compare August 5, 2022 16:06
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice work! No comments :)

Reviewed 1 of 1 files at r1, 3 of 3 files at r2, 19 of 19 files at r3, 6 of 6 files at r4, 9 of 9 files at r5, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner, @msirek, and @rytaft)


pkg/sql/sem/tree/routine.go line 45 at r3 (raw file):

}

// NewTypedRoutineExpr returns a new RoutineExpr the is well-typed.

nit: probably s/the is/that is/.

@mgartner mgartner requested a review from DrewKimball August 7, 2022 14:02
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.

Cool! :LGTM:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner, @msirek, and @rytaft)


pkg/sql/logictest/testdata/logic_test/udf line 293 at r5 (raw file):


query III colnames
SELECT *, one() FROM ab WHERE a = one()

[nit] Is it possible to use a udf as a table source right now? If so, that case could use a test.

This commit adds basic support for executing single-statement,
zero-argument, scalar user-defined functions. It sets up some basic
components that are needed. There's quite a few TODOs scattered
throughout the commit that will be addressed in future commits.

Evaluation of a UDF is performed similarly to the execution of
apply-joins. During the execbuilder phase, a `tree.RoutineExpr` is
created to represent the invocation of a UDF. This expression contains a
closure that can generate an optimized plan for each statement in the
UDF body. During evaluation, a plan is generated and executed for each
statement sequentially (however, only a single-statement is currently
supported). The results of the last statement are returned as the
result.

Release note: None
The optimizer now creates required properties that are used when
optimizing each statement in a body UDF during evaluation of the UDF.
These props must be used to ensure that properties such as ordering and
presentation are maintained during optimization.

Release note: None
All statements in a UDF body are now executed when a UDF is evaluated.

Release note: None
Copy link
Collaborator Author

@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.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball, @msirek, and @rytaft)


pkg/sql/logictest/testdata/logic_test/udf line 293 at r5 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] Is it possible to use a udf as a table source right now? If so, that case could use a test.

Good catch! Done.


pkg/sql/sem/tree/routine.go line 45 at r3 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: probably s/the is/that is/.

Done.

@mgartner mgartner force-pushed the udf-simple-execution branch from 4759880 to 1665a0d Compare August 8, 2022 14:54
@mgartner
Copy link
Collaborator Author

mgartner commented Aug 8, 2022

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 8, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 8, 2022

Build succeeded:

@craig craig bot merged commit 3aea0c4 into cockroachdb:master Aug 8, 2022
@mgartner mgartner deleted the udf-simple-execution branch August 8, 2022 23:23
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.

4 participants