Skip to content

Commit

Permalink
opt: prune unnecessary columns in uniqueness checks
Browse files Browse the repository at this point in the history
Projects now wrap semi-joins of unique checks which pass-through the
columns of the unique constraint. This allows normalization rules to
prune unnecessary columns from the expression.

Release justification: This is a low-risk change to new functionality,
implicitly partitioned unique indexes.

Release note (performance improvement): The columns fetched for
uniqueness checks of implicitly partitioned unique indexes are now
pruned to only include columns necessary for determining uniqueness.
  • Loading branch information
mgartner committed Mar 3, 2021
1 parent 432d6dd commit 6355b06
Show file tree
Hide file tree
Showing 7 changed files with 2,383 additions and 2,141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ vectorized: true
│ │ spans: FULL SCAN
│ │
│ └── • scan buffer
│ estimated row count: 1
│ label: buffer 1
└── • constraint-check
Expand All @@ -674,7 +673,6 @@ vectorized: true
│ spans: FULL SCAN
└── • scan buffer
estimated row count: 1
label: buffer 1

statement ok
Expand Down
575 changes: 300 additions & 275 deletions pkg/sql/opt/exec/execbuilder/testdata/unique

Large diffs are not rendered by default.

392 changes: 202 additions & 190 deletions pkg/sql/opt/norm/testdata/rules/prune_cols

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions pkg/sql/opt/optbuilder/mutation_builder_unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (mb *mutationBuilder) hasUniqueWithoutIndexConstraints() bool {
// constraint are being updated (according to updateColIDs). When the unique
// constraint has a partial predicate, it also returns true if the predicate
// references any of the columns being updated.
func (mb *mutationBuilder) uniqueColsUpdated(uniqueOrdinal int) bool {
func (mb *mutationBuilder) uniqueColsUpdated(uniqueOrdinal cat.UniqueOrdinal) bool {
uc := mb.tab.Unique(uniqueOrdinal)

for i, n := 0, uc.ColumnCount(); i < n; i++ {
Expand Down Expand Up @@ -379,7 +379,13 @@ func (h *uniqueCheckHelper) buildInsertionCheck() memo.UniqueChecksItem {
keyCols = append(keyCols, withScanScope.cols[i].id)
}

return f.ConstructUniqueChecksItem(semiJoin, &memo.UniqueChecksItemPrivate{
// Create a Project that passes-through only the key columns. This allows
// normalization rules to prune any unnecessary columns from the expression.
// The key columns are always needed in order to display the constraint
// violation error.
project := f.ConstructProject(semiJoin, nil /* projections */, keyCols.ToSet())

return f.ConstructUniqueChecksItem(project, &memo.UniqueChecksItemPrivate{
Table: h.mb.tabID,
CheckOrdinal: h.uniqueOrdinal,
KeyCols: keyCols,
Expand Down
1,204 changes: 637 additions & 567 deletions pkg/sql/opt/optbuilder/testdata/unique-checks-insert

Large diffs are not rendered by default.

888 changes: 470 additions & 418 deletions pkg/sql/opt/optbuilder/testdata/unique-checks-update

Large diffs are not rendered by default.

1,453 changes: 766 additions & 687 deletions pkg/sql/opt/optbuilder/testdata/unique-checks-upsert

Large diffs are not rendered by default.

0 comments on commit 6355b06

Please sign in to comment.