cockroachdb / cockroachdb/cockroach
Use partial indexes in more cases
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Is your feature request related to a problem? Please describe.**
The following test case does not use the partial index, even though it could:
```sql
create table test_ilike (c0 int, c1 string);
create INDEX ilike_index on test_ilike (c1 ASC) STORING (c0) WHERE c1 ilike 'ABC%';
explain select * from test_ilike where c1 = 'ABC123';
distribution: local
vectorized: true
• filter
│ filter: c1 = 'ABC123'
│
└── • scan
missing stats
table: test_ilike@test_ilike_pkey
spans: FULL SCAN
```
Currently the index predicate must be a tight constraint in order for it to be used.
**Describe the solution you'd like**
Fold the index predicate using values from the query predicate.
If we have a query predicate such as `c1 = 'ABC123'` which equates a column with an exact value, the column reference can be replaced with the value in the index predicate, in this case `c1 ilike 'ABC%'` --> `ABC123 ilike 'ABC%'`. `CustomFuncs.FoldComparison` can then be called on that new predicate. If that evaluates to a constant boolean `true`, then the index can be used. Likewise if we have query predicate `c1 IN ('ABC123', 'ABC456'...)`, we replace column references with each of the IN list values and fold the expression multiple times. If all evaluations result in `true`, the index may be used.
**Describe alternatives you've considered**
None
Jira issue: CRDB-28162
Contributor guide
Assessment
This issue has not been assessed yet.