Skip to content

Commit

Permalink
sqlerrors: Refactor columnExistsInIndex into one place
Browse files Browse the repository at this point in the history
This is pure mechanical change where we moved creating a new error for
columnExistsInIndex into one place in sqlerrors. This error is used
whenever we create an index that stores a column that's already in
the primary key columns.

Release note: None
  • Loading branch information
Xiang-Gu committed Dec 12, 2023
1 parent 1d4fc67 commit 085e759
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func checkColumnDoesNotExist(
col.GetName())
}
if col.Public() {
return true, sqlerrors.NewColumnAlreadyExistsError(tree.ErrString(&name), tableDesc.GetName())
return true, sqlerrors.NewColumnAlreadyExistsInRelationError(tree.ErrString(&name), tableDesc.GetName())
}
if col.Adding() {
return false, pgerror.Newf(pgcode.DuplicateColumn,
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/catalog/tabledesc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_library(
"//pkg/sql/sem/semenumpb",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/volatility",
"//pkg/sql/sqlerrors",
"//pkg/sql/types",
"//pkg/util",
"//pkg/util/errorutil/unimplemented",
Expand Down
10 changes: 4 additions & 6 deletions pkg/sql/catalog/tabledesc/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
Expand Down Expand Up @@ -740,16 +741,13 @@ func (desc *Mutable) allocateIndexIDs(columnNames map[string]descpb.ColumnID) er
if primaryColIDs.Contains(col.GetID()) && idx.GetEncodingType() == catenumpb.SecondaryIndexEncoding {
// If the primary index contains a stored column, we don't need to
// store it - it's already part of the index.
err = pgerror.Newf(pgcode.DuplicateColumn,
"index %q already contains column %q", idx.GetName(), col.GetName())
err = errors.WithDetailf(err,
err = errors.WithDetailf(
sqlerrors.NewColumnAlreadyExistsInIndexError(idx.GetName(), col.GetName()),
"column %q is part of the primary index and therefore implicit in all indexes", col.GetName())
return err
}
if colIDs.Contains(col.GetID()) {
return pgerror.Newf(
pgcode.DuplicateColumn,
"index %q already contains column %q", idx.GetName(), col.GetName())
return sqlerrors.NewColumnAlreadyExistsInIndexError(idx.GetName(), col.GetName())
}
if indexHasOldStoredColumns {
idx.IndexDesc().KeySuffixColumnIDs = append(idx.IndexDesc().KeySuffixColumnIDs, col.GetID())
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/catalog/tabledesc/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,9 @@ func (desc *wrapper) validateTableIndexes(columnsByID map[descpb.ColumnID]catalo
idx.GetName(), idx.IndexDesc().EncodingType, catenumpb.SecondaryIndexEncoding)
}
}
// Ensure that index column ID subsets are well formed.

// Ensure that an index column ID shows up at most once in `keyColumnIDs`,
// `keySuffixColumnIDs`, and `storeColumnIDs`.
if idx.GetVersion() < descpb.StrictIndexColumnIDGuaranteesVersion {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func alterTableAddColumn(
"column name %q conflicts with a system column name",
d.Name))
}
panic(sqlerrors.NewColumnAlreadyExistsError(string(d.Name), tn.Object()))
panic(sqlerrors.NewColumnAlreadyExistsInRelationError(string(d.Name), tn.Object()))
}
}
if d.IsSerial {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/storageparam"
"github.com/cockroachdb/cockroach/pkg/sql/storageparam/indexstorageparam"
"github.com/cockroachdb/cockroach/pkg/sql/types"
Expand Down Expand Up @@ -556,8 +557,7 @@ func addColumnsForSecondaryIndex(
for _, storingNode := range n.Storing {
colName := storingNode.String()
if _, found := columnRefs[colName]; found {
panic(pgerror.Newf(pgcode.InvalidObjectDefinition,
"index %q already contains column %q", n.Name, colName))
panic(sqlerrors.NewColumnAlreadyExistsInIndexError(string(n.Name), colName))
}
columnRefs[colName] = struct{}{}
}
Expand Down Expand Up @@ -616,8 +616,7 @@ func addColumnsForSecondaryIndex(
// earlier so this covers any extra columns.
columnName := mustRetrieveColumnNameElem(b, e.TableID, e.ColumnID)
if _, found := columnRefs[columnName.Name]; found {
panic(pgerror.Newf(pgcode.InvalidObjectDefinition,
"index %q already contains column %q", n.Name, columnName.Name))
panic(sqlerrors.NewColumnAlreadyExistsInIndexError(string(n.Name), columnName.Name))
}
columnRefs[columnName.Name] = struct{}{}
keySuffixColumns = append(keySuffixColumns, e)
Expand Down
9 changes: 7 additions & 2 deletions pkg/sql/sqlerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,16 @@ func NewUndefinedRelationError(name tree.NodeFormatter) error {
"relation %q does not exist", tree.ErrString(name))
}

// NewColumnAlreadyExistsError creates an error for a preexisting column.
func NewColumnAlreadyExistsError(name, relation string) error {
// NewColumnAlreadyExistsInRelationError creates an error for a preexisting column in relation.
func NewColumnAlreadyExistsInRelationError(name, relation string) error {
return pgerror.Newf(pgcode.DuplicateColumn, "column %q of relation %q already exists", name, relation)
}

// NewColumnAlreadyExistsInIndexError creates an error for a preexisting column in index.
func NewColumnAlreadyExistsInIndexError(idxName, colName string) error {
return pgerror.Newf(pgcode.DuplicateColumn, "index %q already contains column %q", idxName, colName)
}

// NewDatabaseAlreadyExistsError creates an error for a preexisting database.
func NewDatabaseAlreadyExistsError(name string) error {
return pgerror.Newf(pgcode.DuplicateDatabase, "database %q already exists", name)
Expand Down

0 comments on commit 085e759

Please sign in to comment.