Skip to content

Commit

Permalink
sql: support the ADD COLUMN ... REFERENCES syntax
Browse files Browse the repository at this point in the history
Fixes #32917.

This PR adds support for the add column references
statement by allowing the foreign key building code
to use columns and indexes added in the current txn.
The schema changer already understands how to add
the combination of the three in the same transaction.

Release note (sql change): This PR adds support for the
`ALTER TABLE ... ADD COLUMN ... REFERENCES ...` syntax
for tables that are empty.
  • Loading branch information
rohany committed Apr 30, 2020
1 parent ef2b897 commit fa3cfff
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 66 deletions.
10 changes: 1 addition & 9 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ func (n *alterTableNode) startExec(params runParams) error {
switch t := cmd.(type) {
case *tree.AlterTableAddColumn:
d := t.ColumnDef
if d.HasFKConstraint() {
return unimplemented.NewWithIssue(32917,
"adding a REFERENCES constraint while also adding a column via ALTER not supported")
}
version := params.ExecCfg().Settings.Version.ActiveVersionOrEmpty(params.ctx)
if supported, err := isTypeSupportedInVersion(version, d.Type); err != nil {
return err
Expand Down Expand Up @@ -317,12 +313,8 @@ func (n *alterTableNode) startExec(params runParams) error {

case *tree.ForeignKeyConstraintTableDef:
for _, colName := range d.FromCols {
col, err := n.tableDesc.FindActiveColumnByName(string(colName))
col, err := n.tableDesc.FindActiveOrNewColumnByName(colName)
if err != nil {
if _, dropped, inactiveErr := n.tableDesc.FindColumnByName(colName); inactiveErr == nil && !dropped {
return unimplemented.NewWithIssue(32917,
"adding a REFERENCES constraint while the column is being added not supported")
}
return err
}

Expand Down
60 changes: 30 additions & 30 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,16 +608,16 @@ func ResolveFK(
validationBehavior tree.ValidationBehavior,
settings *cluster.Settings,
) error {
originColumnIDs := make(sqlbase.ColumnIDs, len(d.FromCols))
originCols := make([]*sqlbase.ColumnDescriptor, len(d.FromCols))
for i, col := range d.FromCols {
col, _, err := tbl.FindColumnByName(col)
col, err := tbl.FindActiveOrNewColumnByName(col)
if err != nil {
return err
}
if err := col.CheckCanBeFKRef(); err != nil {
return err
}
originColumnIDs[i] = col.ID
originCols[i] = col
}

target, err := ResolveMutableExistingObject(ctx, sc, &d.Table, true /*required*/, ResolveRequireTableDesc)
Expand Down Expand Up @@ -658,33 +658,28 @@ func ResolveFK(
}
}

srcCols, err := tbl.FindActiveColumnsByNames(d.FromCols)
if err != nil {
return err
}

targetColNames := d.ToCols
referencedColNames := d.ToCols
// If no columns are specified, attempt to default to PK.
if len(targetColNames) == 0 {
targetColNames = make(tree.NameList, len(target.PrimaryIndex.ColumnNames))
if len(referencedColNames) == 0 {
referencedColNames = make(tree.NameList, len(target.PrimaryIndex.ColumnNames))
for i, n := range target.PrimaryIndex.ColumnNames {
targetColNames[i] = tree.Name(n)
referencedColNames[i] = tree.Name(n)
}
}

targetCols, err := target.FindActiveColumnsByNames(targetColNames)
referencedCols, err := target.FindActiveColumnsByNames(referencedColNames)
if err != nil {
return err
}

if len(targetCols) != len(srcCols) {
if len(referencedCols) != len(originCols) {
return pgerror.Newf(pgcode.Syntax,
"%d columns must reference exactly %d columns in referenced table (found %d)",
len(srcCols), len(srcCols), len(targetCols))
len(originCols), len(originCols), len(referencedCols))
}

for i := range srcCols {
if s, t := srcCols[i], targetCols[i]; !s.Type.Equivalent(&t.Type) {
for i := range originCols {
if s, t := originCols[i], referencedCols[i]; !s.Type.Equivalent(&t.Type) {
return pgerror.Newf(pgcode.DatatypeMismatch,
"type of %q (%s) does not match foreign key %q.%q (%s)",
s.Name, s.Type.String(), target.Name, t.Name, t.Type.String())
Expand Down Expand Up @@ -715,17 +710,17 @@ func ResolveFK(
}
}

targetColIDs := make(sqlbase.ColumnIDs, len(targetCols))
for i := range targetCols {
targetColIDs[i] = targetCols[i].ID
targetColIDs := make(sqlbase.ColumnIDs, len(referencedCols))
for i := range referencedCols {
targetColIDs[i] = referencedCols[i].ID
}

// Don't add a SET NULL action on an index that has any column that is NOT
// NULL.
if d.Actions.Delete == tree.SetNull || d.Actions.Update == tree.SetNull {
for _, sourceColumn := range srcCols {
if !sourceColumn.Nullable {
col := qualifyFKColErrorWithDB(ctx, txn, tbl.TableDesc(), sourceColumn.Name)
for _, originColumn := range originCols {
if !originColumn.Nullable {
col := qualifyFKColErrorWithDB(ctx, txn, tbl.TableDesc(), originColumn.Name)
return pgerror.Newf(pgcode.InvalidForeignKey,
"cannot add a SET NULL cascading action on column %q which has a NOT NULL constraint", col,
)
Expand All @@ -736,11 +731,11 @@ func ResolveFK(
// Don't add a SET DEFAULT action on an index that has any column that has
// a DEFAULT expression of NULL and a NOT NULL constraint.
if d.Actions.Delete == tree.SetDefault || d.Actions.Update == tree.SetDefault {
for _, sourceColumn := range srcCols {
for _, originColumn := range originCols {
// Having a default expression of NULL, and a constraint of NOT NULL is a
// contradiction and should never be allowed.
if sourceColumn.DefaultExpr == nil && !sourceColumn.Nullable {
col := qualifyFKColErrorWithDB(ctx, txn, tbl.TableDesc(), sourceColumn.Name)
if originColumn.DefaultExpr == nil && !originColumn.Nullable {
col := qualifyFKColErrorWithDB(ctx, txn, tbl.TableDesc(), originColumn.Name)
return pgerror.Newf(pgcode.InvalidForeignKey,
"cannot add a SET DEFAULT cascading action on column %q which has a "+
"NOT NULL constraint and a NULL default expression", col,
Expand All @@ -749,10 +744,15 @@ func ResolveFK(
}
}

originColumnIDs := make(sqlbase.ColumnIDs, len(originCols))
for i, col := range originCols {
originColumnIDs[i] = col.ID
}
var legacyOriginIndexID sqlbase.IndexID
// Search for an index on the origin table that matches. If one doesn't exist,
// we create one automatically if the table to alter is new or empty.
originIdx, err := sqlbase.FindFKOriginIndex(tbl.TableDesc(), originColumnIDs)
// we create one automatically if the table to alter is new or empty. We also
// search if an index for the set of columns was created in this transaction.
originIdx, err := sqlbase.FindFKOriginIndexInTxn(tbl, originColumnIDs)
if err == nil {
// If there was no error, we found a suitable index.
legacyOriginIndexID = originIdx.ID
Expand All @@ -775,7 +775,7 @@ func ResolveFK(
return pgerror.Newf(pgcode.ForeignKeyViolation,
"foreign key requires an existing index on columns %s", colNames.String())
}
id, err := addIndexForFK(tbl, srcCols, constraintName, ts)
id, err := addIndexForFK(tbl, originCols, constraintName, ts)
if err != nil {
return err
}
Expand Down Expand Up @@ -825,7 +825,7 @@ func ResolveFK(
// that will support using `srcCols` as the referencing (src) side of an FK.
func addIndexForFK(
tbl *sqlbase.MutableTableDescriptor,
srcCols []sqlbase.ColumnDescriptor,
srcCols []*sqlbase.ColumnDescriptor,
constraintName string,
ts FKTableState,
) (sqlbase.IndexID, error) {
Expand Down
Loading

0 comments on commit fa3cfff

Please sign in to comment.