Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: always add deletable index predicates to table metadata #58714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/sql/opt/optbuilder/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ func (mb *mutationBuilder) buildUpsert(returning tree.ReturningExprs) {
// Add the partial index predicate expressions to the table metadata.
// These expressions are used to prune fetch columns during
// normalization.
mb.b.addPartialIndexPredicatesForTable(mb.md.TableMeta(mb.tabID), nil /* scan */, true /* includeDeletable */)
mb.b.addPartialIndexPredicatesForTable(mb.md.TableMeta(mb.tabID), nil /* scan */)

// Project partial index PUT and DEL boolean columns.
//
Expand Down
11 changes: 2 additions & 9 deletions pkg/sql/opt/optbuilder/partial_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,9 @@ import (
// outputs all the ordinary columns in the table, we avoid constructing a new
// scan. A scan and its logical properties are required in order to fully
// normalize the partial index predicates.
func (b *Builder) addPartialIndexPredicatesForTable(
tabMeta *opt.TableMeta, scan memo.RelExpr, includeDeletable bool,
) {
func (b *Builder) addPartialIndexPredicatesForTable(tabMeta *opt.TableMeta, scan memo.RelExpr) {
tab := tabMeta.Table
var numIndexes int
if includeDeletable {
numIndexes = tab.DeletableIndexCount()
} else {
numIndexes = tab.IndexCount()
}
numIndexes := tab.DeletableIndexCount()

// Find the first partial index.
indexOrd := 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/optbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (b *Builder) buildScan(
// logical properties of the scan to fully normalize the index predicates.
// We don't need to add deletable partial index predicates in the context of
// a scan.
b.addPartialIndexPredicatesForTable(tabMeta, outScope.expr, false /* includeDeletable */)
b.addPartialIndexPredicatesForTable(tabMeta, outScope.expr)

if !virtualColIDs.Empty() {
// Project the expressions for the virtual columns (and pass through all
Expand Down
8 changes: 6 additions & 2 deletions pkg/sql/opt/optbuilder/testdata/delete
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,12 @@ delete partial_indexes
│ └── partial index predicates
│ ├── secondary: filters
│ │ └── c:7 = 'foo'
│ └── secondary: filters
│ └── (a:5 > b:6) AND (c:7 = 'bar')
│ ├── secondary: filters
│ │ └── (a:5 > b:6) AND (c:7 = 'bar')
│ ├── b: filters
│ │ └── c:7 = 'delete-only'
│ └── b: filters
│ └── c:7 = 'write-only'
└── projections
├── c:7 = 'foo' [as=partial_index_del1:9]
├── (a:5 > b:6) AND (c:7 = 'bar') [as=partial_index_del2:10]
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/opt/optbuilder/testdata/update
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,12 @@ update partial_indexes
│ │ └── partial index predicates
│ │ ├── secondary: filters
│ │ │ └── c:7 = 'foo'
│ │ └── secondary: filters
│ │ └── (a:5 > b:6) AND (c:7 = 'bar')
│ │ ├── secondary: filters
│ │ │ └── (a:5 > b:6) AND (c:7 = 'bar')
│ │ ├── b: filters
│ │ │ └── c:7 = 'delete-only'
│ │ └── b: filters
│ │ └── c:7 = 'write-only'
│ └── projections
│ └── 1 [as=a_new:9]
└── projections
Expand Down Expand Up @@ -1712,8 +1716,12 @@ update partial_indexes
│ │ └── partial index predicates
│ │ ├── secondary: filters
│ │ │ └── c:7 = 'foo'
│ │ └── secondary: filters
│ │ └── (a:5 > b:6) AND (c:7 = 'bar')
│ │ ├── secondary: filters
│ │ │ └── (a:5 > b:6) AND (c:7 = 'bar')
│ │ ├── b: filters
│ │ │ └── c:7 = 'delete-only'
│ │ └── b: filters
│ │ └── c:7 = 'write-only'
│ └── projections
│ └── a:5 + 5 [as=a_new:9]
└── projections
Expand Down
32 changes: 24 additions & 8 deletions pkg/sql/opt/optbuilder/testdata/upsert
Original file line number Diff line number Diff line change
Expand Up @@ -1932,8 +1932,12 @@ insert partial_indexes
│ │ │ │ │ │ │ └── partial index predicates
│ │ │ │ │ │ │ ├── secondary: filters
│ │ │ │ │ │ │ │ └── c:10 = 'foo'
│ │ │ │ │ │ │ └── secondary: filters
│ │ │ │ │ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ │ │ │ │ ├── secondary: filters
│ │ │ │ │ │ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ │ │ │ │ ├── b: filters
│ │ │ │ │ │ │ │ └── c:10 = 'delete-only'
│ │ │ │ │ │ │ └── b: filters
│ │ │ │ │ │ │ └── c:10 = 'write-only'
│ │ │ │ │ │ └── filters
│ │ │ │ │ │ └── column1:5 = a:8
│ │ │ │ │ └── filters
Expand All @@ -1948,8 +1952,12 @@ insert partial_indexes
│ │ │ │ └── partial index predicates
│ │ │ │ ├── secondary: filters
│ │ │ │ │ └── c:14 = 'foo'
│ │ │ │ └── secondary: filters
│ │ │ │ └── (a:12 > b:13) AND (c:14 = 'bar')
│ │ │ │ ├── secondary: filters
│ │ │ │ │ └── (a:12 > b:13) AND (c:14 = 'bar')
│ │ │ │ ├── b: filters
│ │ │ │ │ └── c:14 = 'delete-only'
│ │ │ │ └── b: filters
│ │ │ │ └── c:14 = 'write-only'
│ │ │ └── filters
│ │ │ ├── column2:6 = b:13
│ │ │ └── column3:7 = c:14
Expand Down Expand Up @@ -2003,8 +2011,12 @@ upsert partial_indexes
│ │ │ │ └── partial index predicates
│ │ │ │ ├── secondary: filters
│ │ │ │ │ └── c:10 = 'foo'
│ │ │ │ └── secondary: filters
│ │ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ │ ├── secondary: filters
│ │ │ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ │ ├── b: filters
│ │ │ │ │ └── c:10 = 'delete-only'
│ │ │ │ └── b: filters
│ │ │ │ └── c:10 = 'write-only'
│ │ │ └── filters
│ │ │ ├── column2:6 = b:9
│ │ │ └── column3:7 = c:10
Expand Down Expand Up @@ -2064,8 +2076,12 @@ upsert partial_indexes
│ │ │ └── partial index predicates
│ │ │ ├── secondary: filters
│ │ │ │ └── c:10 = 'foo'
│ │ │ └── secondary: filters
│ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ ├── secondary: filters
│ │ │ │ └── (a:8 > b:9) AND (c:10 = 'bar')
│ │ │ ├── b: filters
│ │ │ │ └── c:10 = 'delete-only'
│ │ │ └── b: filters
│ │ │ └── c:10 = 'write-only'
│ │ └── filters
│ │ └── column1:5 = a:8
│ └── projections
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/optbuilder/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (mb *mutationBuilder) buildUpdate(returning tree.ReturningExprs) {
// Add the partial index predicate expressions to the table metadata.
// These expressions are used to prune fetch columns during
// normalization.
mb.b.addPartialIndexPredicatesForTable(mb.md.TableMeta(mb.tabID), nil /* scan */, true /* includeDeletable */)
mb.b.addPartialIndexPredicatesForTable(mb.md.TableMeta(mb.tabID), nil /* scan */)

// Project partial index PUT and DEL boolean columns.
mb.projectPartialIndexPutAndDelCols(preCheckScope, mb.fetchScope)
Expand Down