Skip to content

Commit

Permalink
sql: Implemented check constraint validation for new schema changer
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiang-Gu committed Oct 11, 2022
1 parent 083c44d commit 397d5da
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,42 @@ func (e InvalidIndexesError) Error() string {
return fmt.Sprintf("found %d invalid indexes", len(e.Indexes))
}

// ValidateCheckConstraint validates the check constraint against all rows
// in the table.
func ValidateCheckConstraint(
ctx context.Context,
tableDesc catalog.TableDescriptor,
constraint *descpb.ConstraintDetail,
sessionData *sessiondata.SessionData,
runHistoricalTxn descs.HistoricalInternalExecTxnRunner,
execOverride sessiondata.InternalExecutorOverride,
) (err error) {
if constraint.CheckConstraint == nil {
return errors.AssertionFailedf("%v is not a check constraint", constraint.GetConstraintName())
}

tableDesc, err = tableDesc.MakeFirstMutationPublic(catalog.IgnoreConstraints)
if err != nil {
return err
}

// The check operates at the historical timestamp.
return runHistoricalTxn(ctx, func(
ctx context.Context, txn *kv.Txn, ie sqlutil.InternalExecutor, descriptors *descs.Collection,
) error {
// Use the DistSQLTypeResolver because we need to resolve types by ID.
resolver := descs.NewDistSQLTypeResolver(descriptors, txn)
semaCtx := tree.MakeSemaContext()
semaCtx.TypeResolver = &resolver
defer func() { descriptors.ReleaseAll(ctx) }()

return ie.WithSyntheticDescriptors([]catalog.Descriptor{tableDesc}, func() error {
return validateCheckExpr(ctx, &semaCtx, txn, sessionData, constraint.CheckConstraint.Expr,
tableDesc.(*tabledesc.Mutable), ie)
})
})
}

// ValidateInvertedIndexes checks that the indexes have entries for
// all the items of data in rows.
//
Expand Down Expand Up @@ -1618,19 +1654,6 @@ func ValidateInvertedIndexes(
return nil
}

// ValidateCheckConstraint checks all rows satisfy the check constraint.
func ValidateCheckConstraint(
ctx context.Context,
tableDesc catalog.TableDescriptor,
constraint *descpb.ConstraintDetail,
sessionData *sessiondata.SessionData,
runHistoricalTxn descs.HistoricalInternalExecTxnRunner,
execOverride sessiondata.InternalExecutorOverride,
) (err error) {
// TODO (xiang): implement this.
return nil
}

func countExpectedRowsForInvertedIndex(
ctx context.Context,
tableDesc catalog.TableDescriptor,
Expand Down

0 comments on commit 397d5da

Please sign in to comment.