Skip to content

Commit

Permalink
opt: add test constraining partial index on virtual column
Browse files Browse the repository at this point in the history
No code changes were necessary in order to generate a constrained scan
for a partial index on a virtual column.

Release note: None
  • Loading branch information
mgartner committed Feb 17, 2021
1 parent ef900a2 commit 7c3275f
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions pkg/sql/opt/xform/testdata/rules/select
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ CREATE TABLE virtual (
s STRING,
lower_s STRING AS (lower(s)) VIRTUAL,
INDEX (a) WHERE c IN (10, 20, 30),
INDEX (lower_s)
INDEX (lower_s),
INDEX (c) WHERE c > 100
)
----

Expand Down Expand Up @@ -370,8 +371,10 @@ project
│ │ └── lower_s:6
│ │ └── lower(s:5)
│ ├── partial index predicates
│ │ ├── secondary: filters
│ │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable]
│ │ └── secondary: filters
│ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable]
│ │ └── b:3 > 90 [outer=(3), constraints=(/3: [/91 - ]; tight)]
│ ├── key: (1)
│ └── fd: (1)-->(3)
└── filters
Expand Down Expand Up @@ -1714,17 +1717,70 @@ project
│ │ └── lower_s:6
│ │ └── lower(s:5)
│ ├── partial index predicates
│ │ ├── secondary: filters
│ │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable]
│ │ └── secondary: filters
│ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable]
│ │ └── b:3 > 90 [outer=(3), constraints=(/3: [/91 - ]; tight)]
│ ├── key: (1)
│ └── fd: (1)-->(2,3)
└── filters
├── (a:2 > 10) AND (a:2 < 20) [outer=(2), constraints=(/2: [/11 - /19]; tight)]
└── b:3 = 10 [outer=(3), constraints=(/3: [/10 - /10]; tight), fd=()-->(3)]

# Constrained partial index scan with an indexed virtual computed column also in
# the predicate.
# TODO(mgartner): Teach indexConstraintCtx.simplifyFilter to remove b=140 from
# the remaining filters after the index constraint is built. This will eliminate
# the unnecessary index-join and select expressions.
opt
SELECT k FROM virtual WHERE c = 150
----
project
├── columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null b:3!null
├── key: (1)
├── fd: ()-->(3)
├── index-join virtual
│ ├── columns: k:1!null b:3
│ ├── key: (1)
│ ├── fd: (1)-->(3)
│ └── scan virtual@secondary,partial
│ ├── columns: k:1!null
│ ├── constraint: /4/1: [/150 - /150]
│ └── key: (1)
└── filters
└── b:3 = 140 [outer=(3), constraints=(/3: [/140 - /140]; tight), fd=()-->(3)]

# Constrain a scan on a partial index on virtual column c constraining the
# dependent column b.
# TODO(mgartner): Teach indexConstraintCtx.simplifyFilter to remove b=140 from
# the remaining filters after the index constraint is built. This will eliminate
# the unnecessary index-join and select expressions.
opt
SELECT k FROM virtual WHERE b = 140
----
project
├── columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null b:3!null
├── key: (1)
├── fd: ()-->(3)
├── index-join virtual
│ ├── columns: k:1!null b:3
│ ├── key: (1)
│ ├── fd: (1)-->(3)
│ └── scan virtual@secondary,partial
│ ├── columns: k:1!null
│ ├── constraint: /4/1: [/150 - /150]
│ └── key: (1)
└── filters
└── b:3 = 140 [outer=(3), constraints=(/3: [/140 - /140]; tight), fd=()-->(3)]

# Regression test for #55387. GenerateConstrainedScans should not reduce the
# input filters when proving partial index implication.

exec-ddl
CREATE TABLE t55387 (
k INT PRIMARY KEY,
Expand Down

0 comments on commit 7c3275f

Please sign in to comment.