Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mgartner opened this issue Jan 14, 2022 · 1 comment · Fixed by #81071
Closed

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

mgartner opened this issue Jan 14, 2022 · 1 comment · Fixed by #81071
Assignees
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) sync-me-8 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team

Comments

@mgartner
Copy link
Collaborator

mgartner commented Jan 14, 2022

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

@mgartner mgartner added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Jan 14, 2022
@mgartner mgartner added this to Triage in SQL Queries via automation Jan 14, 2022
@mgartner mgartner added this to Triage in SQL Sessions - Deprecated via automation Jan 14, 2022
@blathers-crl blathers-crl bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team labels Jan 14, 2022
@mgartner mgartner moved this from Triage to Backlog in SQL Queries Jan 14, 2022
@mgartner
Copy link
Collaborator Author

Related context: #70605 (comment)

@rafiss rafiss moved this from Triage to Longer term backlog in SQL Sessions - Deprecated Feb 7, 2022
@rafiss rafiss moved this from Longer term backlog to Potentially for 22.2 in SQL Sessions - Deprecated Mar 15, 2022
@rafiss rafiss moved this from 22.2 Now to 22.2 Later in SQL Sessions - Deprecated Apr 21, 2022
@e-mbrown e-mbrown self-assigned this May 4, 2022
@craig craig bot closed this as completed in e09ce91 Jun 9, 2022
SQL Sessions - Deprecated automation moved this from 22.2 Later to Done Jun 9, 2022
SQL Queries automation moved this from Backlog to Done Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) sync-me-8 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants