Skip to content

Commit

Permalink
scbuild: error on duplicate columns in ALTER PRIMARY KEY
Browse files Browse the repository at this point in the history
This check has been missing from the declarative schema changer builder.
This would result in execution errors in later stages when the new index
makes it into the table descriptor. Since these are internal errors, the
user experience is correspondingly poor. This patch fixes this.

Fixes #91301

Release note (bug fix): fixed a bug present only in earlier 22.2 release
candidates, in which an ALTER PRIMARY KEY USING COLUMNS (x, x) statement
would result in an internal error instead of the expected user-facing
error with a pg-code.
  • Loading branch information
Marius Posta committed Nov 8, 2022
1 parent 0aa1e8f commit 8aef54e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CREATE TABLE t (x INT PRIMARY KEY, y INT NOT NULL, z INT NOT NULL, w INT, INDEX
statement ok
INSERT INTO t VALUES (1, 2, 3, 4), (5, 6, 7, 8)

statement error pgcode 0A000 .* contains duplicate column \"y\"
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y, y)

statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y, z)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func checkForEarlyExit(b BuildCtx, tbl *scpb.Table, t alterPrimaryKeySpec) {
panic(err)
}

usedColumns := make(map[tree.Name]bool, len(t.Columns))
for _, col := range t.Columns {
if col.Column == "" && col.Expr != nil {
panic(errors.WithHint(
Expand All @@ -217,6 +218,11 @@ func checkForEarlyExit(b BuildCtx, tbl *scpb.Table, t alterPrimaryKeySpec) {
"use columns instead",
))
}
if usedColumns[col.Column] {
panic(pgerror.Newf(pgcode.FeatureNotSupported,
"new primary key contains duplicate column %q", col.Column))
}
usedColumns[col.Column] = true

colElems := b.ResolveColumn(tbl.TableID, col.Column, ResolveParams{
IsExistenceOptional: false,
Expand Down

0 comments on commit 8aef54e

Please sign in to comment.