cockroachdb / cockroachdb/cockroach
sql: UDF cannot contain cast of parameter to user-defined type
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
The example below errors with `ERROR: column "a" does not exist`.
```sql
CREATE TYPE my_type AS ENUM ('foo');
CREATE FUNCTION get_region(a STRING) RETURNS TEXT LANGUAGE SQL as $$
SELECT a::my_type
$$;
```
I haven't confirmed, but I believe the problem is related to #105259 and #107654. From https://github.com/cockroachdb/cockroach/issues/107654#issuecomment-1652752619:
> We really shouldn't be re-type-checking the AST outside of the optimizer - that's the real offender here. Unfortunately, in order to stop doing that, we'll need to do something like annotating the AST during type checking with information that can be used to serialize enum values and types during formatting. I went down this path and think there's potential, but I'd be hard-pressed to back port it.
This problem keeps cropping up and the bandaid solutions aren't robust, so I think we'll need to to fix the root cause of this sooner rather than later. If we do, it'll make things like replacing table and column identifiers with immutable IDs easier (see #101755).
Jira issue: CRDB-30516
Epic CRDB-49018
Contributor guide
Assessment
This issue has not been assessed yet.