-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2435 from tamird/sql-alter-table
sql: ALTER TABLE ADD {,COLUMN,CONSTRAINT} support
- Loading branch information
Showing
17 changed files
with
5,857 additions
and
5,368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2015 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. See the AUTHORS file | ||
// for names of contributors. | ||
// | ||
// Author: Tamir Duberstein (tamird@gmail.com) | ||
|
||
package sql | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/sql/parser" | ||
"github.com/cockroachdb/cockroach/sql/privilege" | ||
"github.com/cockroachdb/cockroach/util" | ||
) | ||
|
||
// AlterTable creates a table. | ||
// Privileges: CREATE on table. | ||
// notes: postgres requires CREATE on the table. | ||
// mysql requires ALTER, CREATE, INSERT on the table. | ||
func (p *planner) AlterTable(n *parser.AlterTable) (planNode, error) { | ||
if err := n.Table.NormalizeTableName(p.session.Database); err != nil { | ||
return nil, err | ||
} | ||
|
||
dbDesc, err := p.getDatabaseDesc(n.Table.Database()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Check if table exists. | ||
tbKey := tableKey{dbDesc.ID, n.Table.Table()}.Key() | ||
gr, err := p.txn.Get(tbKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !gr.Exists() { | ||
if n.IfExists { | ||
// Noop. | ||
return &valuesNode{}, nil | ||
} | ||
// Key does not exist, but we want it to: error out. | ||
return nil, fmt.Errorf("table %q does not exist", n.Table.Table()) | ||
} | ||
|
||
tableDesc, err := p.getTableDesc(n.Table) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := p.checkPrivilege(tableDesc, privilege.CREATE); err != nil { | ||
return nil, err | ||
} | ||
|
||
nextIndexID := tableDesc.NextIndexID | ||
|
||
for _, cmd := range n.Cmds { | ||
switch t := cmd.(type) { | ||
case *parser.AlterTableAddColumn: | ||
d := t.ColumnDef | ||
col, idx, err := makeColumnDefDescs(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tableDesc.AddColumn(*col) | ||
if idx != nil { | ||
if err := tableDesc.AddIndex(*idx, d.PrimaryKey); err != nil { | ||
return nil, err | ||
} | ||
} | ||
case *parser.AlterTableAddConstraint: | ||
d := t.ConstraintDef | ||
idx := IndexDescriptor{ | ||
Name: string(d.Name), | ||
Unique: d.Unique, | ||
ColumnNames: d.Columns, | ||
StoreColumnNames: d.Storing, | ||
} | ||
if err := tableDesc.AddIndex(idx, d.PrimaryKey); err != nil { | ||
return nil, err | ||
} | ||
default: | ||
return nil, util.Errorf("unsupported alter cmd: %T", cmd) | ||
} | ||
} | ||
|
||
if err := tableDesc.AllocateIDs(); err != nil { | ||
return nil, err | ||
} | ||
|
||
// These changed on us when we called `tableDesc.AllocateIDs()`. | ||
var newIndexes []IndexDescriptor | ||
for _, index := range append(tableDesc.Indexes, tableDesc.PrimaryIndex) { | ||
if index.ID >= nextIndexID { | ||
newIndexes = append(newIndexes, index) | ||
} | ||
} | ||
|
||
b, err := p.makeBackfillBatch(n.Table, tableDesc, newIndexes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
b.Put(MakeDescMetadataKey(tableDesc.GetID()), tableDesc) | ||
// Mark transaction as operating on the system DB. | ||
p.txn.SetSystemDBTrigger() | ||
|
||
if err := p.txn.Run(&b); err != nil { | ||
return nil, convertBatchError(tableDesc, b, err) | ||
} | ||
|
||
return &valuesNode{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2015 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. See the AUTHORS file | ||
// for names of contributors. | ||
// | ||
// Author: Tamir Duberstein (tamird@gmail.com) | ||
|
||
package sql | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/client" | ||
"github.com/cockroachdb/cockroach/sql/parser" | ||
"github.com/cockroachdb/cockroach/util/log" | ||
) | ||
|
||
func (p *planner) makeBackfillBatch(tableName *parser.QualifiedName, tableDesc *TableDescriptor, indexDescs ...IndexDescriptor) (client.Batch, error) { | ||
var b client.Batch | ||
// Get all the rows affected. | ||
// TODO(vivek): Avoid going through Select. | ||
// TODO(tamird): Support partial indexes? | ||
row, err := p.Select(&parser.Select{ | ||
Exprs: parser.SelectExprs{parser.StarSelectExpr()}, | ||
From: parser.TableExprs{&parser.AliasedTableExpr{Expr: tableName}}, | ||
}) | ||
if err != nil { | ||
return b, err | ||
} | ||
|
||
// Construct a map from column ID to the index the value appears at within a | ||
// row. | ||
colIDtoRowIndex := map[ColumnID]int{} | ||
for i, name := range row.Columns() { | ||
c, err := tableDesc.FindColumnByName(name) | ||
if err != nil { | ||
return b, err | ||
} | ||
colIDtoRowIndex[c.ID] = i | ||
} | ||
|
||
// TODO(tamird): This will fall down in production use. We need to do | ||
// something better (see #2036). In particular, this implementation | ||
// has the following problems: | ||
// - Very large tables will generate an enormous batch here. This | ||
// isn't really a problem in itself except that it will exacerbate | ||
// the other issue: | ||
// - Any non-quiescent table that this runs against will end up with | ||
// an inconsistent index. This is because as inserts/updates continue | ||
// to roll in behind this operation's read front, the written index | ||
// will become incomplete/stale before it's written. | ||
|
||
for row.Next() { | ||
rowVals := row.Values() | ||
|
||
for _, indexDesc := range indexDescs { | ||
secondaryIndexEntries, err := encodeSecondaryIndexes( | ||
tableDesc.ID, []IndexDescriptor{indexDesc}, colIDtoRowIndex, rowVals) | ||
if err != nil { | ||
return b, err | ||
} | ||
|
||
for _, secondaryIndexEntry := range secondaryIndexEntries { | ||
if log.V(2) { | ||
log.Infof("CPut %q -> %v", secondaryIndexEntry.key, secondaryIndexEntry.value) | ||
} | ||
b.CPut(secondaryIndexEntry.key, secondaryIndexEntry.value, nil) | ||
} | ||
} | ||
} | ||
|
||
return b, row.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.