Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
58433: sql: fix assertion failure for CREATE TABLE with duplicate index names r=postamar a=postamar

Previously, since version 20.2, executing a statement such as
```
CREATE TABLE t (a INT, b INT, INDEX idx (a), INDEX idx (b))
```
yields an assertion failure during the validation of the table descriptor.
This is a regression from version 20.1 which yields a proper error:
```
ERROR: duplicate index name: "idx"
```
This patch fixes this regression and restores the original behaviour.

Fixes cockroachdb#57630.

Release note (bug fix): A CREATE TABLE statement with indexes with
duplicate names will no longer result in an assertion failure. This bug
was present since version 20.2.

Co-authored-by: Marius Posta <[email protected]>
  • Loading branch information
craig[bot] and Marius Posta committed Jan 5, 2021
2 parents 28aead8 + 3e187d9 commit d887df7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/sql/catalog/tabledesc/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -2337,7 +2337,7 @@ func (desc *wrapper) validateTableIndexes(columnNames map[string]descpb.ColumnID
if _, indexNameExists := indexNames[index.Name]; indexNameExists {
for i := range desc.Indexes {
if desc.Indexes[i].Name == index.Name {
// This error should be caught in MakeIndexDescriptor.
// This error should be caught in MakeIndexDescriptor or NewTableDesc.
return errors.HandleAsAssertionFailure(fmt.Errorf("duplicate index name: %q", index.Name))
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,11 @@ func NewTableDesc(
// pass, handled above.

case *tree.IndexTableDef:
// If the index is named, ensure that the name is unique.
// Unnamed indexes will be given a unique auto-generated name later on.
if d.Name != "" && desc.ValidateIndexNameIsUnique(d.Name.String()) != nil {
return nil, pgerror.Newf(pgcode.DuplicateRelation, "duplicate index name: %q", d.Name)
}
idx := descpb.IndexDescriptor{
Name: string(d.Name),
StoreColumnNames: d.Storing.ToStrings(),
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/create_table
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,8 @@ CREATE TABLE error (a INT, b INT, UNIQUE WITHOUT INDEX (a) PARTITION BY LIST (b)

statement error pgcode 0A000 unique constraints with a predicate but without an index are not supported
CREATE TABLE error (a INT, b INT, UNIQUE WITHOUT INDEX (a) WHERE b > 5)

subtest regression_57630

statement error pgcode 42P07 duplicate index name: \"idx\"
CREATE TABLE error (a INT, b INT, INDEX idx (a), INDEX idx (b))

0 comments on commit d887df7

Please sign in to comment.