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: constrain expression indexes with IS NULL expressions #83619

Merged
merged 1 commit into from
Jul 11, 2022
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
4 changes: 1 addition & 3 deletions pkg/sql/opt/xform/general_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,7 @@ func (c *CustomFuncs) findConstantFilterCols(
}

datum := span.StartKey().Value(0)
if datum != tree.DNull {
constFilterCols[colID] = c.e.f.ConstructConstVal(datum, colTyp)
}
constFilterCols[colID] = c.e.f.ConstructConstVal(datum, colTyp)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/opt/xform/select_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (c *CustomFuncs) GenerateConstrainedScans(

sb.Init(c, scanPrivate.Table)

// Check constraint and computed column filters
// Build optional filters from check constraint and computed column filters.
optionalFilters, filterColumns :=
c.GetOptionalFiltersAndFilterColumns(explicitFilters, scanPrivate)

Expand All @@ -424,7 +424,8 @@ func (c *CustomFuncs) GenerateConstrainedScans(
iter.Init(c.e.evalCtx, c.e.f, c.e.mem, &c.im, scanPrivate, explicitFilters, rejectInvertedIndexes)
iter.ForEach(func(index cat.Index, filters memo.FiltersExpr, indexCols opt.ColSet, isCovering bool, constProj memo.ProjectionsExpr) {

// A structure describing which index partitions are local to the gateway region
// Create a prefix sorter that describes which index partitions are
// local to the gateway region.
prefixSorter, _ := tabMeta.IndexPartitionLocality(scanPrivate.Index, index, c.e.evalCtx)

// Build Constraints to scan a subset of the table Spans.
Expand Down Expand Up @@ -491,7 +492,7 @@ func (c *CustomFuncs) GenerateConstrainedScans(
// tryFoldComputedCol tries to reduce the computed column with the given column
// ID into a constant value, by evaluating it with respect to a set of other
// columns that are constant. If the computed column is constant, enter it into
// the constCols map and return false. Otherwise, return false.
// the constCols map and return true. Otherwise, return false.
//
func (c *CustomFuncs) tryFoldComputedCol(
tabMeta *opt.TableMeta, computedColID opt.ColumnID, constCols constColsMap,
Expand Down
40 changes: 33 additions & 7 deletions pkg/sql/opt/xform/testdata/rules/computed
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,14 @@ select
└── filters
└── (k_int:1 = 2) OR (k_int:1 = 3) [outer=(1), constraints=(/1: [/2 - /2] [/3 - /3]; tight)]

# Don't constrain the index for a NULL value.
# Constrain the index for a NULL value.
opt
SELECT k_int FROM t_int WHERE k_int IS NULL
----
select
scan t_int@c_int_index
├── columns: k_int:1
├── fd: ()-->(1)
├── scan t_int@c_int_index
│ └── columns: k_int:1
└── filters
└── k_int:1 IS NULL [outer=(1), constraints=(/1: [/NULL - /NULL]; tight), fd=()-->(1)]
├── constraint: /2/1/4: [/NULL/NULL - /NULL/NULL]
└── fd: ()-->(1)


# Don't constrain the index when the computed column has a volatile function.
Expand Down Expand Up @@ -267,3 +264,32 @@ project
│ └── key: (1)
└── filters
└── d:4 = 1 [outer=(4), immutable, constraints=(/4: [/1 - /1]; tight), fd=()-->(4)]

# Regression test for #83390. The optimizer should constrain an expression index
# with an IS NULL expression.
exec-ddl
CREATE TABLE t83390 (
k INT PRIMARY KEY,
a INT,
INDEX idx ((a IS NULL))
)
----

opt
SELECT * FROM t83390@idx WHERE a IS NULL
----
select
├── columns: k:1!null a:2
├── key: (1)
├── fd: ()-->(2)
├── index-join t83390
│ ├── columns: k:1!null a:2
│ ├── key: (1)
│ ├── fd: (1)-->(2)
│ └── scan t83390@idx
│ ├── columns: k:1!null
│ ├── constraint: /5/1: [/true - /true]
│ ├── flags: force-index=idx
│ └── key: (1)
└── filters
└── a:2 IS NULL [outer=(2), constraints=(/2: [/NULL - /NULL]; tight), fd=()-->(2)]