-
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.
schemachanger: Implement
ALTER TABLE .. VALIDATE CONSTRAINT
The main idea is to drop the unvalidated element and add a vanilla counterpart in the builder state.
- Loading branch information
Showing
16 changed files
with
880 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
142 changes: 142 additions & 0 deletions
142
pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_validate_constraint.go
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,142 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package scbuildstmt | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/privilege" | ||
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
func alterTableValidateConstraint( | ||
b BuildCtx, tn *tree.TableName, tbl *scpb.Table, t *tree.AlterTableValidateConstraint, | ||
) { | ||
constraintElems := b.ResolveConstraint(tbl.TableID, t.Constraint, ResolveParams{ | ||
IsExistenceOptional: false, | ||
RequiredPrivilege: privilege.CREATE, | ||
}) | ||
|
||
// 1. We can only validate non-index-backed constraints. Panic if not. | ||
_, _, constraintNameElem := scpb.FindConstraintWithoutIndexName(constraintElems) | ||
if constraintNameElem == nil { | ||
panic(pgerror.Newf(pgcode.WrongObjectType, | ||
"constraint %q of relation %q is not a foreign key, check, or unique without index"+ | ||
" constraint", tree.ErrString(&t.Constraint), tree.ErrString(tn))) | ||
} | ||
|
||
// 2. Return if the constraint is already validated. | ||
constraintID := constraintNameElem.ConstraintID | ||
if !shouldValidateConstraint(b, tbl.TableID, constraintID) { | ||
return | ||
} | ||
|
||
// 3. Drop the not-valid constraint and old constraint name element | ||
// Add a new sibling constraint and a new constraint name element. | ||
validateConstraint(b, tbl.TableID, validateConstraintSpec{ | ||
constraintNameElem: constraintNameElem, | ||
ckNotValidElem: retrieveCheckConstraintUnvalidatedElem(b, tbl.TableID, constraintID), | ||
uwiNotValidElem: retrieveUniqueWithoutIndexConstraintUnvalidatedElem(b, tbl.TableID, constraintID), | ||
fkNotValidElem: retrieveForeignKeyConstraintUnvalidatedElem(b, tbl.TableID, constraintID), | ||
}) | ||
} | ||
|
||
func retrieveCheckConstraintUnvalidatedElem( | ||
b BuildCtx, tableID catid.DescID, constraintID catid.ConstraintID, | ||
) (CheckConstraintUnvalidatedElem *scpb.CheckConstraintUnvalidated) { | ||
scpb.ForEachCheckConstraintUnvalidated(b.QueryByID(tableID), func( | ||
current scpb.Status, target scpb.TargetStatus, e *scpb.CheckConstraintUnvalidated, | ||
) { | ||
if e.ConstraintID == constraintID { | ||
CheckConstraintUnvalidatedElem = e | ||
} | ||
}) | ||
return CheckConstraintUnvalidatedElem | ||
} | ||
|
||
func retrieveUniqueWithoutIndexConstraintUnvalidatedElem( | ||
b BuildCtx, tableID catid.DescID, constraintID catid.ConstraintID, | ||
) (UniqueWithoutIndexConstraintUnvalidatedElem *scpb.UniqueWithoutIndexConstraintUnvalidated) { | ||
scpb.ForEachUniqueWithoutIndexConstraintUnvalidated(b.QueryByID(tableID), func( | ||
current scpb.Status, target scpb.TargetStatus, e *scpb.UniqueWithoutIndexConstraintUnvalidated, | ||
) { | ||
if e.ConstraintID == constraintID { | ||
UniqueWithoutIndexConstraintUnvalidatedElem = e | ||
} | ||
}) | ||
return UniqueWithoutIndexConstraintUnvalidatedElem | ||
} | ||
|
||
func retrieveForeignKeyConstraintUnvalidatedElem( | ||
b BuildCtx, tableID catid.DescID, constraintID catid.ConstraintID, | ||
) (ForeignKeyConstraintUnvalidatedElem *scpb.ForeignKeyConstraintUnvalidated) { | ||
scpb.ForEachForeignKeyConstraintUnvalidated(b.QueryByID(tableID), func( | ||
current scpb.Status, target scpb.TargetStatus, e *scpb.ForeignKeyConstraintUnvalidated, | ||
) { | ||
if e.ConstraintID == constraintID { | ||
ForeignKeyConstraintUnvalidatedElem = e | ||
} | ||
}) | ||
return ForeignKeyConstraintUnvalidatedElem | ||
} | ||
|
||
type validateConstraintSpec struct { | ||
constraintNameElem *scpb.ConstraintWithoutIndexName | ||
ckNotValidElem *scpb.CheckConstraintUnvalidated | ||
uwiNotValidElem *scpb.UniqueWithoutIndexConstraintUnvalidated | ||
fkNotValidElem *scpb.ForeignKeyConstraintUnvalidated | ||
} | ||
|
||
func validateConstraint(b BuildCtx, tableID catid.DescID, spec validateConstraintSpec) { | ||
nextConstraintID := b.NextTableConstraintID(tableID) | ||
if spec.ckNotValidElem != nil { | ||
b.Drop(spec.ckNotValidElem) | ||
b.Add(&scpb.CheckConstraint{ | ||
TableID: tableID, | ||
ConstraintID: nextConstraintID, | ||
ColumnIDs: spec.ckNotValidElem.ColumnIDs, | ||
Expression: spec.ckNotValidElem.Expression, | ||
FromHashShardedColumn: false, | ||
IndexIDForValidation: getIndexIDForValidationForConstraint(b, tableID), | ||
}) | ||
} | ||
if spec.uwiNotValidElem != nil { | ||
b.Drop(spec.uwiNotValidElem) | ||
b.Add(&scpb.UniqueWithoutIndexConstraint{ | ||
TableID: tableID, | ||
ConstraintID: nextConstraintID, | ||
ColumnIDs: spec.uwiNotValidElem.ColumnIDs, | ||
Predicate: spec.uwiNotValidElem.Predicate, | ||
}) | ||
} | ||
if spec.fkNotValidElem != nil { | ||
b.Drop(spec.fkNotValidElem) | ||
b.Add(&scpb.ForeignKeyConstraint{ | ||
TableID: tableID, | ||
ConstraintID: nextConstraintID, | ||
ColumnIDs: spec.fkNotValidElem.ColumnIDs, | ||
ReferencedTableID: spec.fkNotValidElem.ReferencedTableID, | ||
ReferencedColumnIDs: spec.fkNotValidElem.ReferencedColumnIDs, | ||
OnUpdateAction: spec.fkNotValidElem.OnUpdateAction, | ||
OnDeleteAction: spec.fkNotValidElem.OnDeleteAction, | ||
CompositeKeyMatchMethod: spec.fkNotValidElem.CompositeKeyMatchMethod, | ||
IndexIDForValidation: getIndexIDForValidationForConstraint(b, tableID), | ||
}) | ||
} | ||
b.Drop(spec.constraintNameElem) | ||
b.Add(&scpb.ConstraintWithoutIndexName{ | ||
TableID: tableID, | ||
ConstraintID: nextConstraintID, | ||
Name: spec.constraintNameElem.Name, | ||
}) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.