Skip to content

Commit

Permalink
importccl: fix bug when mixing old and new tables during import
Browse files Browse the repository at this point in the history
There was an indexing bug which would have been triggered if the
resolution of new and old table descriptors was ever mixed within the
same import. This situation has not hit any users because an IMPORT only
allows writing to new tables, while an IMPORT INTO only allows writing
to existing (old) tables, thereby never mixing the two.

This change fixes that bug.

Closes #50733

Release note: None
  • Loading branch information
adityamaru committed Jul 8, 2020
1 parent a6a58f4 commit 0d23c26
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions pkg/ccl/importccl/import_stmt.go
Original file line number Diff line number Diff line change
@@ -858,14 +858,15 @@ func prepareNewTableDescsForIngestion(
importTables []jobspb.ImportDetails_Table,
parentID sqlbase.ID,
) ([]*sqlbase.TableDescriptor, error) {
var tableDescs []*sqlbase.TableDescriptor
for _, i := range importTables {
tableDescs := make([]*sqlbase.TableDescriptor, len(importTables))
for i := range importTables {
// TODO (rohany): Use keys.PublicSchemaID for now, revisit this once we
// support user defined schemas.
if err := backupccl.CheckObjectExists(ctx, txn, p.ExecCfg().Codec, parentID, keys.PublicSchemaID, i.Desc.Name); err != nil {
if err := backupccl.CheckObjectExists(ctx, txn, p.ExecCfg().Codec, parentID,
keys.PublicSchemaID, importTables[i].Desc.Name); err != nil {
return nil, err
}
tableDescs = append(tableDescs, i.Desc)
tableDescs[i] = importTables[i].Desc
}

// Verification steps have passed, generate a new table ID if we're
@@ -914,7 +915,8 @@ func prepareNewTableDescsForIngestion(
// Write the new TableDescriptors and flip the namespace entries over to
// them. After this call, any queries on a table will be served by the newly
// imported data.
if err := backupccl.WriteDescriptors(ctx, txn, nil /* databases */, tables, nil, tree.RequestedDescriptors, p.ExecCfg().Settings, seqValKVs); err != nil {
if err := backupccl.WriteDescriptors(ctx, txn, nil /* databases */, tables, nil,
tree.RequestedDescriptors, p.ExecCfg().Settings, seqValKVs); err != nil {
return nil, errors.Wrapf(err, "creating importTables")
}

@@ -1014,12 +1016,15 @@ func (r *importResumer) prepareTableDescsForIngestion(
if err != nil {
return err
}
for i, table := range res {
importDetails.Tables[i] = jobspb.ImportDetails_Table{Desc: table,
Name: details.Tables[i].Name,
SeqVal: details.Tables[i].SeqVal,
IsNew: details.Tables[i].IsNew,
TargetCols: details.Tables[i].TargetCols}

for _, desc := range res {
i := newTableDescToIdx[desc]
table := details.Tables[i]
importDetails.Tables[i] = jobspb.ImportDetails_Table{Desc: desc,
Name: table.Name,
SeqVal: table.SeqVal,
IsNew: table.IsNew,
TargetCols: table.TargetCols}
}
}

0 comments on commit 0d23c26

Please sign in to comment.