cockroachdb / cockroachdb/cockroach
Predicate simplification causes missing partial index access path
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Describe the problem**
A partial index which could be useful to speed up a query cannot be used because predicate simplification changes the predicate which matches the partial index predicate into something else.
**To Reproduce**
```sql
CREATE TABLE a (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT a PRIMARY KEY (a ASC),
INDEX b_idx (b ASC, a ASC) WHERE (a + b) IS NULL
);
EXPLAIN SELECT * from a@b_idx WHERE a+b IS NULL AND b = 9;
ERROR: index "b_idx" is a partial index that does not contain all the rows needed to execute this query
SQLSTATE: 42809
EXPLAIN SELECT * from a WHERE a+b IS NULL AND b = 9;
info
-------------------------------------------------------------
distribution: full
vectorized: true
• filter
│ filter: ((a + 9) IS NULL) AND (b = 9)
│
└── • scan
missing stats
table: a@a_pkey
spans: FULL SCAN
```
Here we can see the original `a+b IS NULL` predicate is changed to `(a + 9) IS NULL` due to the `b=9` term.
**Expected behavior**
A FULL SCAN should be avoided and a span of `b=9` should be used on partial index `b_idx`.
Perhaps instead of replacing predicates during simplication, the original predicates could be kept around, but marked as redundant. A redundant predicate could trigger a constrained index scan, but would be ignored as a filter if not consumed by the scan.
This example may be a little contrived, but one could imagine other cases such as:
```
INDEX b_east_idx (b ASC, a ASC) WHERE region_udf(b) = 'east'
INDEX b_central_idx (b ASC, a ASC) WHERE region_udf(b) = 'central'
INDEX b_west_idx (b ASC, a ASC) WHERE region_udf(b) = 'west'
```
If the query has terms like `b=123 AND region_udf(b) = 'east'`, `region_udf(b) = 'east'` would get converted to `region_udf(123) = 'east'` and not be able to use one of the partial indexes.
Jira issue: CRDB-27580
Contributor guide
Assessment
This issue has not been assessed yet.