-
Notifications
You must be signed in to change notification settings - Fork 4.1k
sql: ALTER COLUMN TYPE general for column that has a check constraint not supported. #48288
Copy link
Copy link
Open
Labels
A-schema-changesA-sql-pgcompatSemantic compatibility with PostgreSQLSemantic compatibility with PostgreSQLC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Description
Currently, ALTER COLUMN TYPE for a column that has a check constraint is not supported.
Steps to implement:
- On creation of new column, copy constraints and create constraint for new column. Each comparison to the column has to be casted to the new type. Comparisons that have the column inside a nested expression must also be converted.
- For example, if (x INT) has check (x + 5 > 10), after float conversion, the constraint would be (x + 5::float > 10::float).
-pg source code for re-adding constraint
- For example, if (x INT) has check (x + 5 > 10), after float conversion, the constraint would be (x + 5::float > 10::float).
- Need to check if constraint is valid (aka casts are valid and comparisons can be done), otherwise fail schema change.
- After swapping the column, need to drop constraints for old column.
- Need to rename the constraints during/after the swap since we can't have duplicate constraint names if we want to keep the constraint name the same.
Example:
If the original column constraint was (x > 0),
after a cast to string, the new constraint should be (x > 0::string).
This is postgres behaviour.
Jira issue: CRDB-4338
Epic CRDB-44826
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-schema-changesA-sql-pgcompatSemantic compatibility with PostgreSQLSemantic compatibility with PostgreSQLC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)T-sql-foundationsSQL Foundations Team (formerly SQL Schema + SQL Sessions)SQL Foundations Team (formerly SQL Schema + SQL Sessions)