forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: allow creation of partial UNIQUE WITHOUT INDEX constraints
This commit allows creation of partial `UNIQUE WITHOUT INDEX` constraints. These partial unique constraints have a predicate to enforce uniqueness on the subset of rows in which the predicate evaluates to true, similar to a unique partial index. An example of a partial `UNIQUE WITHOUT INDEX` constraint: CREATE TABLE t ( a INT, UNIQUE WITHOUT INDEX (a) WHERE a > 0 ) **NOTE**: these constraints are not yet correctly enforcing uniqueness on a subset of rows in the table. This requires optimizer changes that will come in a follow-up commit. Specifically, this commit adds support for: - Creating these constraints in `CREATE TABLE` and `ALTER TABLE`. - Storing the predicates in table descriptor constraints. - Validating the predicates both during creation and during descriptor validation. - Displaying the predicates in `SHOW CREATE` and `SHOW CONSTRAINTS` output, and in the pg_catalog.pg_constraint table. - Accessing the predicates via the opt catalog. The primary motivation for this change is to simplify testing for implicitly partitioned partial unique indexes. An experimental session setting is required to use `UNIQUE WITHOUT INDEX` there are no current plans to make these constraints available to users by default. Informs cockroachdb#59195 There is no release note because these constraints are gated behind the experimental_enable_unique_without_index_constraints session variable. Release note: None
- Loading branch information
Showing
23 changed files
with
761 additions
and
512 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2021 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 schemaexpr | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/catalog" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
) | ||
|
||
// ValidateUniqueWithoutIndexPredicate verifies that an expression is a valid | ||
// unique without index predicate. If the expression is valid, it returns the | ||
// serialized expression with the columns dequalified. | ||
// | ||
// A predicate expression is valid if all of the following are true: | ||
// | ||
// - It results in a boolean. | ||
// - It refers only to columns in the table. | ||
// - It does not include subqueries. | ||
// - It does not include non-immutable, aggregate, window, or set returning | ||
// functions. | ||
// | ||
func ValidateUniqueWithoutIndexPredicate( | ||
ctx context.Context, | ||
tn tree.TableName, | ||
desc catalog.TableDescriptor, | ||
pred tree.Expr, | ||
semaCtx *tree.SemaContext, | ||
) (string, error) { | ||
expr, _, err := DequalifyAndValidateExpr( | ||
ctx, | ||
desc, | ||
pred, | ||
types.Bool, | ||
"unique without index predicate", | ||
semaCtx, | ||
tree.VolatilityImmutable, | ||
&tn, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
return expr, nil | ||
} |
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
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
Oops, something went wrong.