cockroachdb / cockroachdb/cockroach

sql: allow UDFs in EXECUTE arguments

Open
#108,215 1 comment 0 reactions 0 assignees View on GitHub
A-prepared-stmts A-sql-routine C-enhancement T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

We currently disallow UDFs from being used in EXECUTE arguments (see #99008, #108213). UDF calls cannot be simply evaluated like placeholder expressions are currently: https://github.com/cockroachdb/cockroach/blob/f59f0f408f95b50a9300545e966b3ef9a5db7f72/pkg/sql/opt/norm/factory.go#L355

UDFs must be built by the optimizer and executed as routines. #107767 attempted to address this by replacing placeholders with expressions instead of constants. There were some major issues with this approach though:

1. If the UDF references any database objects, like tables, indexes, columns, or views, then the catalog must be initialized to build the expression. During placeholder assignment, the catalog is not initialized.
2. Inlining the UDF will result in the UDF being effectively evaluated each time the argument is referenced in the prepared statement. For example, if the prepared statement looks like `PREPARE p AS SELECT $1::INT + $1::INT`, then `f` will be evaluated twice when running `EXECUTE p(f())`. This is fine for stable, immutable, and leakproof functions, but causes unexpected behavior for volatile functions. See the related issue #108027.

Jira issue: CRDB-30365

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.