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

ALTER COLUMN TYPE doesn’t work if the column being altered has a default value #119555

Open
dikshant opened this issue Feb 22, 2024 · 11 comments
Open
Labels
A-schema-changes C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-quick-win Likely to be a quick win for someone experienced. good first issue T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@dikshant
Copy link

dikshant commented Feb 22, 2024

Alter column type doesn’t work if the column has a default value.

For example:

CREATE TABLE test2 (
   testytest INT4 NOT NULL DEFAULT 0:::INT8
);
ALTER TABLE test2 ALTER COLUMN testytest TYPE int2 USING testytest::int2;

Will fail with:

ERROR: relation "test2" (104): computed column "testytest_1" cannot also have a DEFAULT expression
SQLSTATE: 42P16

But this will work without the DEFAULT

CREATE TABLE test2 (
   testytest INT4 NOT NULL
);
ALTER TABLE test2 ALTER COLUMN testytest TYPE int2 USING testytest::int2;

Epic CRDB-25314

Jira issue: CRDB-36224

@dikshant dikshant added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-schema-changes T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels Feb 22, 2024
@blathers-crl blathers-crl bot added this to Triage in SQL Foundations Feb 22, 2024
@rimadeodhar rimadeodhar added good first issue E-quick-win Likely to be a quick win for someone experienced. labels Feb 22, 2024
@uds5501
Copy link

uds5501 commented Feb 26, 2024

Hi @rimadeodhar, can I pick this issue to contribute? There doesn't seem to be any takers for this and seems perfect for a newcomer like me.

@cjhetzle
Copy link

@uds5501 have you been working on this? What is the new behavior going to do to the existing default?

If we want, we can remove the default or we can try to convert it as well.

@uds5501
Copy link

uds5501 commented Feb 28, 2024

@cjhetzle I am currently exploring the codebase and checking relevant files to change.
My primary thought was to remove the default altogether, however if conversion is feasible internally, I could explore that as well (however this might not be be the case everytime.)

@sahilwahi7
Copy link

func (node *AlterTableSetDefault) Format(ctx *FmtCtx) {
ctx.WriteString(" ALTER COLUMN ")
ctx.FormatNode(&node.Column)
if node.Default == nil {
ctx.WriteString(" DROP DEFAULT")
} else {
ctx.WriteString(" SET DEFAULT ")
ctx.FormatNode(node.Default)
}
}

This function should be responsible for ALTER table having default feature added in node need to fix logic here @uds5501 are you looking into similar part of code?

@uds5501
Copy link

uds5501 commented Mar 28, 2024

I think it's got to be AlterColumnType , writing tests to understand that.

@sahilwahi7
Copy link

sahilwahi7 commented Mar 29, 2024

@cjhetzle @uds5501 can you please let me do we need to remove the default one or need to update handling for that
Found the codebase causing the issue
In AlterColumnType we have a function called alterColumnTypeGeneral in which we have a check added for

  hasDefault := col.HasDefault()
	if hasDefault {
		if validCast := cast.ValidCast(col.GetType(), toType, cast.ContextAssignment); !validCast {
			return pgerror.Newf(
				pgcode.DatatypeMismatch,
				"default for column %q cannot be cast automatically to type %s",
				col.GetName(),
				toType.SQLString(),
			)
		}
	}

Here we need to remove the hasDefault Check

@uds5501
Copy link

uds5501 commented Mar 29, 2024

hey @sahilwahi7 , since you've debugged the issue in depth, you can carry on with the dev of the same. Feel free to raise an MR

@sahilwahi7
Copy link

@uds5501 please go ahead with raising PR , it was your initial hint from which i was able to debug

@uds5501
Copy link

uds5501 commented Apr 1, 2024

Hi @sahilwahi7 , I am not able to setup cockroach locally over the weekend and don't want myself to be a blocker if someone else can contribute to this. Please feel free to raise the MR.

@krishnakeshan
Copy link
Contributor

krishnakeshan commented Apr 17, 2024

@dikshant In your example:

CREATE TABLE test2 (
   testytest INT4 NOT NULL DEFAULT 0:::INT8
);

why do we cast the default value to INT8 when the column is defined to be an INT4? Just curious to know if that's relevant to the issue?

@mohitsethia
Copy link

@sahilwahi7 @uds5501 the issue is not in alterColumnType, but in the validator pkg>sql>catalog>tabledesc>validate.go (line 1144).

    if column.IsComputed() {
			if column.HasDefault() {
				return pgerror.Newf(pgcode.InvalidTableDefinition,
					"computed column %q cannot also have a DEFAULT expression",
					column.GetName(),
				)
			}
			if column.HasOnUpdate() {
				return pgerror.Newf(pgcode.InvalidTableDefinition,
					"computed column %q cannot also have an ON UPDATE expression",
					column.GetName(),
				)
			}
	}

@dikshant I am new here, and not sure what needs to be done, as it was a bug with clusters & cluster backup which contain tables with such columns as described here. What should be the new behaviour/additional check that would resolve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-changes C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-quick-win Likely to be a quick win for someone experienced. good first issue T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
SQL Foundations
  
Triage
Development

No branches or pull requests

7 participants