Skip to content

Commit

Permalink
sql/catalog/tabledesc: relaxed validation for virtual col in SUFFIX cols
Browse files Browse the repository at this point in the history
One of the validation rule says that "we don't allow virtual columns to
be in the SUFFIX columns of a secondary index", except for one case:
`ALTER PRIMARY KYE USING HASH`, where the implicitly created virtual,
shard column, will need to appear in the SUFFIX columns of the
implicitly created unique, secondary index of the old PK key columns (
which btw is a CRDB unique feature).

The validation has logic to exempt us from this special case but it's
written specifically to the legacy schema changer. Namely, it uses the
canonical `PrimaryKeySwap` mutation type as the signal but we don't have
that in the declarative schema changer. This PR addresses this issue and
allows the validation logic to also exempt the exact same case but
in the declarative schema changer.

Release note: None
  • Loading branch information
Xiang-Gu committed Aug 10, 2022
1 parent 24906bd commit 31d8c22
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/sql/catalog/tabledesc/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,20 @@ func (desc *wrapper) validateTableIndexes(
newPKColIDs.UnionWith(newPK.CollectKeyColumnIDs())
}
}
if newPKColIDs.Empty() {
// Sadly, if the `ALTER PRIMARY KEY USING HASH` is from declarative schema changer,
// we won't find the `PrimaryKeySwap` mutation. In that case, we will attempt to
// find a mutation of adding a primary index and allow its key columns to be used
// as SUFFIX columns in other indexes, even if they are virtual.
for _, mut := range desc.Mutations {
if pidx := mut.GetIndex(); pidx != nil &&
pidx.EncodingType == descpb.PrimaryIndexEncoding &&
mut.Direction == descpb.DescriptorMutation_ADD &&
!mut.Rollback {
newPKColIDs.UnionWith(catalog.MakeTableColSet(pidx.KeyColumnIDs...))
}
}
}
for _, colID := range idx.IndexDesc().KeySuffixColumnIDs {
if !vea.IsActive(clusterversion.Start22_1) {
if col := columnsByID[colID]; col != nil && col.IsVirtual() {
Expand Down

0 comments on commit 31d8c22

Please sign in to comment.