cockroachdb / cockroachdb/cockroach

plpgsql: type coercion truncates when value is too wide

Open
#115,385 1 comment 0 reactions 0 assignees View on GitHub
A-sql-plpgsql C-enhancement docs-done P-3 T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

This issue tracks a difference between postgres and CRDB when coercing a PLpgSQL expression into a type that is too narrow. For context:
Explicit casts succeed, and truncate the value:
```
postgres=# SELECT ('foo'::TEXT)::CHAR;
bpchar
--------
f
(1 row)
```
Assignment casts fail with an error:
```
postgres=# CREATE TABLE t (c CHAR);
CREATE TABLE
postgres=# INSERT INTO t VALUES ('foo'::TEXT);
ERROR: value too long for type character(1)
```
Finally, PLpgSQL type coercion succeeds, but does not truncate:
```
postgres=# CREATE OR REPLACE FUNCTION f() RETURNS CHAR AS $$
BEGIN
RETURN 'abcd'::TEXT;
END
$$ LANGUAGE PLpgSQL;
CREATE FUNCTION
postgres=# SELECT f();
f
------
abcd
(1 row)
```
CRDB matches behavior for the first two cases, but truncates the value for the last case:
```
CREATE OR REPLACE FUNCTION f() RETURNS CHAR AS $$
BEGIN
RETURN 'abcd'::TEXT;
END
$$ LANGUAGE PLpgSQL;

query T
SELECT f();
----
a
```

Jira issue: CRDB-34004

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.