cockroachdb / cockroachdb/cockroach

sql: allow casting a polymorphic routine parameter

Open
#123,536 2 comments 0 reactions 0 assignees View on GitHub
A-sql-pgcompat A-sql-routine A-sql-typing C-enhancement docs-done docs-known-limitation T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Since there is no cast from polymorphic types to other types (for example, TEXT), CRDB currently returns an error during function creation for a case like the following:
```
root@localhost:26257/defaultdb> CREATE FUNCTION f(x ANYELEMENT) RETURNS TEXT AS $$ SELECT x::TEXT; $$ LANGUAGE SQL;
ERROR: invalid cast: anyelement -> string
SQLSTATE: 42846
```
Postgres, on the other hand, allows the function to be created and called:
```
postgres=# CREATE FUNCTION f(x ANYELEMENT) RETURNS TEXT AS $$ SELECT x::TEXT; $$ LANGUAGE SQL;
CREATE FUNCTION
postgres=# SELECT f('hello world'::TEXT);
f
-------------
hello world
(1 row)
```
We should fix this, either by propagating some context through `SemaContext` to make type-checking ignore polymorphic types in this case, or by always allowing casts from polymorphic types.

Jira issue: CRDB-38387

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.