-
Notifications
You must be signed in to change notification settings - Fork 4k
Closed
Labels
A-sql-routineUDFs and Stored ProceduresUDFs and Stored ProceduresC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)GA-blockerT-sql-queriesSQL Queries TeamSQL Queries Teambranch-release-25.3target-release-25.4.0v25.3.0
Description
User-defined functions cannot be resolved in view queries. Example:
root@localhost:26257/defaultdb> CREATE FUNCTION f() RETURNS BOOL LANGUAGE SQL AS $$ SELECT true; $$;
CREATE FUNCTION
root@localhost:26257/defaultdb> create table t (a int);
CREATE TABLE
root@localhost:26257/defaultdb> create view v as select a, f() from t;
ERROR: unknown function: f()
SQLSTATE: 42883
HINT: There is probably a typo in function name. Or the intention was to use a user-defined function in the view query, which is currently not supported.
This issue tracks support for that.
Epic CRDB-49018
Jira issue: CRDB-50398
Metadata
Metadata
Assignees
Labels
A-sql-routineUDFs and Stored ProceduresUDFs and Stored ProceduresC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)GA-blockerT-sql-queriesSQL Queries TeamSQL Queries Teambranch-release-25.3target-release-25.4.0v25.3.0
Type
Projects
Status
Done
Activity
rafiss commentedon May 8, 2025
Notes:
The "unknown function" error is encountered at this call to
GetBuiltinFuncDefinitionOrFail
:cockroach/pkg/sql/sem/tree/function_name.go
Lines 94 to 103 in 489d057
To avoid the error, we need to pass in a non-nil
resolver
to(*ResolvableFunctionReference).Resolve
when this function is called by the optimizer code:cockroach/pkg/sql/opt/optbuilder/scope.go
Line 1118 in f9efd1f
The
resolver
is nil at that point becausesemaCtx.FunctionResolver
is not set. After digging through some history, I found that afe78f9 modified the code so that we intentionally do not set theFunctionResolver
.cockroach/pkg/sql/opt/optbuilder/create_view.go
Lines 19 to 20 in be2cc31
Using a non-nil resolver is blocked by a different task: we'd need to update theThis rewrite functionality is not strictly needed, but can be done once the rest of this issue is completed. Previously, we added support for referencing functions by OID in #83231. This is the area of the code where we'd do the rewrite:CREATE VIEW
code so that we rewrite function references to refer by OID rather than by name.cockroach/pkg/sql/create_view.go
Lines 426 to 439 in 2932e59
That work is captured under #87699, but we can leave this issue open since this one is specific to views, but that one is more general.
While resolving this issue, we should add tests to make sure the view definition is serialized correctly, and also verify that dropping a function that is referenced by a view is blocked.
We also need to update this area of the code to add the dependency on the function to
desc.DependsOnFunctions
:cockroach/pkg/sql/create_view.go
Lines 283 to 296 in 2932e59
In order to do that, the function dependencies need to be correctly set by the optimizer in this code:
cockroach/pkg/sql/opt_exec_factory.go
Line 1977 in 89d6c57
The SQL Queries team is best equipped for that task.
dikshant commentedon May 9, 2025
Speaking to @rafiss, his thoughts are:
We could have that same behavior for views. in that case, all the work here is for Queries.
Merge #144442 #146239 #146461 #146468
dikshant commentedon May 27, 2025
@rytaft @michae2 I am moving this issue to SQL Queries based on Rafi's latest comment. We can triage it in our next triage.
sql,schemachanger: add remaining handling for routine references in v…
24 remaining items
rafiss commentedon Jul 7, 2025
re-opening for backport
rafiss commentedon Jul 7, 2025
No, #146475 depends on this. I think we should make sure to include #146475 in the release as well, otherwise we will need to write a migration at some point to convert all the view queries that reference UDFs.
rytaft commentedon Jul 11, 2025
Assigning to @rafiss since @DrewKimball's PR is merged now.
rafiss commentedon Jul 14, 2025
As per discussion in the SQL Foundations weekly, we will consider this issue resolved.
The remaining work to use by-ID references for UDFs used by views is tracked in #87699 and will be done in a later release. At that time, we will need to add a migration to rewrite any existing UDF references that are in descriptors.