Skip to content

Commit 5308aa7

Browse files
craig[bot]fqazi
andcommitted
Merge #150176
150176: sql: block DROP NOT NULL on generated identity columns r=fqazi a=fqazi Previously, we incorrectly allowed users to drop not null on generated identity columns. The error we generated was vague and not instructive as to what was incorrect in this scenario. This patch, adds logic to block this operation in the declarative schema changer and improves the error message in the legacy one. Fixes: #150021 Fixes: #148455 Release note: None Co-authored-by: Faizan Qazi <[email protected]>
2 parents ec83247 + 6464038 commit 5308aa7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

pkg/sql/alter_table.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,11 @@ func applyColumnMutation(
10581058
return pgerror.Newf(pgcode.InvalidTableDefinition,
10591059
`column "%s" is in a primary index`, col.GetName())
10601060
}
1061-
1061+
// Ensure that we are not dropping not-null on a generated column.
1062+
if col.GetGeneratedAsIdentityType() != catpb.GeneratedAsIdentityType_NOT_IDENTITY_COLUMN {
1063+
return pgerror.Newf(pgcode.Syntax,
1064+
`column "%s" of relation "%s" is an identity column`, col.GetName(), tn.ObjectName)
1065+
}
10621066
// See if there's already a mutation to add/drop a not null constraint.
10631067
for i := range tableDesc.Mutations {
10641068
if constraint := tableDesc.Mutations[i].GetConstraint(); constraint != nil &&

pkg/sql/logictest/testdata/logic_test/alter_table

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5182,3 +5182,21 @@ t_128420 CREATE TABLE public.t_128420 (
51825182
) WITH (schema_locked = true);
51835183

51845184
subtest end
5185+
5186+
# Validate that dropping not-null is not allowed on generated identity columns
5187+
# (#150021)
5188+
subtest drop_not_null_identity
5189+
5190+
statement ok
5191+
CREATE TABLE drop_not_null_identity (a int, b text) WITH (schema_locked = false);
5192+
5193+
statement ok
5194+
ALTER TABLE drop_not_null_identity ALTER COLUMN a SET NOT NULL;
5195+
5196+
statement ok
5197+
ALTER TABLE drop_not_null_identity ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
5198+
5199+
statement error pgcode 42601 column \"a\" of relation \"drop_not_null_identity\" is an identity column
5200+
ALTER TABLE drop_not_null_identity ALTER COLUMN a DROP NOT NULL
5201+
5202+
subtest end

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_drop_not_null.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package scbuildstmt
77

88
import (
9+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
910
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
1011
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1112
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
@@ -35,6 +36,13 @@ func alterTableDropNotNull(
3536
panic(pgerror.Newf(pgcode.InvalidTableDefinition,
3637
`column "%s" is in a primary index`, colName.Name))
3738
}
39+
// Ensure that we are not dropping not-null on a generated column.
40+
colEl := mustRetrieveColumnElem(b, tbl.TableID, columnID)
41+
if colEl.GeneratedAsIdentityType != catpb.GeneratedAsIdentityType_NOT_IDENTITY_COLUMN {
42+
colName := mustRetrieveColumnName(b, tbl.TableID, columnID)
43+
panic(pgerror.Newf(pgcode.Syntax,
44+
`column "%s" of relation "%s" is an identity column`, colName.Name, tn.ObjectName))
45+
}
3846
columNotNull := b.QueryByID(tbl.TableID).FilterColumnNotNull().Filter(func(current scpb.Status, target scpb.TargetStatus, e *scpb.ColumnNotNull) bool {
3947
return e.ColumnID == columnID
4048
}).MustGetZeroOrOneElement()

0 commit comments

Comments
 (0)