Skip to content

Commit

Permalink
sql: Fix panic adding unique, non-indexable column
Browse files Browse the repository at this point in the history
This change updates the `checkColumnsFor...Index` functions to use
`allNonDropColumns()` to validate against any proposed mutations.  Otherwise, a
`ColumnMutation` that adds a non-indexable column followed by an
`IndexMutation` creating an index on that column would would be incorrectly
accepted, leading to a panic.

Fixes #26483

Release note (sql change): Return an error to the user instead of panicing when
trying to add a column with a unique constraint when that column's type is not
indexable.
  • Loading branch information
bobvawter committed Jun 14, 2018
1 parent 0a3df5f commit 12b3ab7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,30 @@ decomputed_column CREATE TABLE decomputed_column (
FAMILY "primary" (a, b)
)

# Test for https://github.com/cockroachdb/cockroach/issues/26483
# We try to create a unique column on an un-indexable type.
statement ok
CREATE TABLE b26483()

statement error unimplemented: column c is of type ARRAY and thus is not indexable
ALTER TABLE b26483 ADD COLUMN c INT[] UNIQUE

# As above, but performed in a transaction
statement ok
BEGIN

statement ok
CREATE TABLE b26483_tx()

statement ok
ALTER TABLE b26483_tx ADD COLUMN c INT[]

statement error unimplemented: column c is of type ARRAY and thus is not indexable
CREATE INDEX on b26483_tx (c)

statement ok
ROLLBACK

# Verify that auditing can be enabled by root, and cannot be disabled by non-root.

statement ok
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ func notIndexableError(cols []ColumnDescriptor, inverted bool) error {
func checkColumnsValidForIndex(tableDesc *TableDescriptor, indexColNames []string) error {
invalidColumns := make([]ColumnDescriptor, 0, len(indexColNames))
for _, indexCol := range indexColNames {
for _, col := range tableDesc.Columns {
for _, col := range tableDesc.allNonDropColumns() {
if col.Name == indexCol {
if !columnTypeIsIndexable(col.Type) {
invalidColumns = append(invalidColumns, col)
Expand All @@ -1633,7 +1633,7 @@ func checkColumnsValidForInvertedIndex(tableDesc *TableDescriptor, indexColNames
}
invalidColumns := make([]ColumnDescriptor, 0, len(indexColNames))
for _, indexCol := range indexColNames {
for _, col := range tableDesc.Columns {
for _, col := range tableDesc.allNonDropColumns() {
if col.Name == indexCol {
if !columnTypeIsInvertedIndexable(col.Type) {
invalidColumns = append(invalidColumns, col)
Expand Down

0 comments on commit 12b3ab7

Please sign in to comment.