Skip to content

sql: allow DEFAULT/ON UPDATE types that can be assignment-cast to column type #74854

@mgartner

Description

@mgartner

Postgres allows creating a DEFAULT column expression of a type that is different from the column's type, as long as the type of the DEFAULT expression can be assignment-cast to the column type. CRDB does not currently allow this.

Example: FLOAT4 -> INT4 casts are allowed in assignment contexts

In Postgres:

marcus=# CREATE TABLE t (a INT4 DEFAULT 1.0::FLOAT4);
CREATE TABLE

In CRDB:

defaultdb> CREATE TABLE t (a INT4 DEFAULT 1.0::FLOAT4);
ERROR: expected DEFAULT expression to have type int4, but '1.0::FLOAT4' has type float4

We also support ON UPDATE expressions (which Postgres does not) and these should also allowed to be typed as any type that can be assignment-cast to the column's type.


We must take care to disallow altering a column to a type which cannot be assignment-casted from the types of the DEFAULT and ON UPDATE expressions.

Example: FLOAT4 -> BOOL casts are NOT allowed in assignment contexts

In Postgres:

marcus=# CREATE TABLE t (a INT4 DEFAULT 1.0::FLOAT4);
CREATE TABLE

marcus=# ALTER TABLE t ALTER COLUMN a TYPE BOOL USING a::BOOL;
ERROR:  42804: default for column "a" cannot be cast automatically to type boolean

Jira issue: CRDB-12293

Metadata

Metadata

Assignees

Labels

C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)T-sql-queriesSQL Queries Teamsync-me-8

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions