cockroachdb / cockroachdb/cockroach

opt: produce tight constraints for NOT IN expressions

Open
#117,424 0 comments 0 reactions 1 assignee Claimed by @DrewKimball View on GitHub
C-enhancement E-quick-win O-support P-3 T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

The optimizer constraint-building code current handles `IN`, but not `NOT IN` expressions. The constraint for `NOT IN` should just be the inverse of that for `IN`, with the exception of `NULL` values, which are filtered in both cases. The following is an example of a query which is unable to use a partial index due to this limitation, even though the query filter is compatible:
```
exec-ddl
CREATE TABLE foo (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT foo_pkey PRIMARY KEY (a ASC),
INDEX foo_idx (a ASC) STORING (b) WHERE b NOT IN (1)
)
----

opt
SELECT a,b FROM foo@foo_idx WHERE b NOT IN (1, 2)
----
select
├── columns: a:1!null b:2
├── key: (1)
├── fd: (1)-->(2)
├── scan foo
│ ├── columns: a:1!null b:2
│ ├── partial index predicates
│ │ └── foo_idx: filters
│ │ └── b:2 != 1 [outer=(2), constraints=(/2: (/NULL - /0] [/2 - ]; tight)]
│ ├── flags: force-index=foo_idx
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── b:2 NOT IN (1, 2) [outer=(2)]
```

Jira issue: CRDB-35170

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.