cockroachdb / cockroachdb/cockroach
opt: not able to plan lookup join under filter with subquery
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
In the example below, we avoid a full scan of `cd` by using a lookup join into `cd@primary`:
```sql
CREATE TABLE abc (
a INT PRIMARY KEY,
b STRING,
c INT,
INDEX (b)
);
CREATE TABLE cd (
c INT PRIMARY KEY,
d INT[]
);
EXPLAIN SELECT * FROM abc JOIN cd USING (c) WHERE b LIKE 'foo%' AND d @> ARRAY[123];
```
```
demo@127.0.0.1:26257/demoapp/defaultdb> EXPLAIN SELECT * FROM abc JOIN cd USING (c) WHERE b LIKE 'foo%' AND d @> ARRAY[123];
info
--------------------------------------
distribution: local
vectorized: true
• lookup join
│ table: cd@cd_pkey
│ equality: (c) = (c)
│ equality cols are key
│ pred: d @> ARRAY[123]
│
└── • index join
│ table: abc@abc_pkey
│
└── • scan
missing stats
table: abc@abc_b_idx
spans: [/'foo' - /'fop')
(16 rows)
```
But if we change the `d @> ARRAY[123]` predicate to use a correlated subquery over a set-generating function such as `123 = ANY (SELECT unnest(d))` then it's no longer possible to plan a lookup join, and instead we get a full scan of `cd`:
```
demo@127.0.0.1:26257/demoapp/defaultdb> EXPLAIN SELECT * FROM abc INNER LOOKUP JOIN cd USING (c) WHERE b LIKE 'foo%' AND 123 = ANY (SELECT unnest(d));
ERROR: could not produce a query plan conforming to the LOOKUP JOIN hint
demo@127.0.0.1:26257/demoapp/defaultdb> EXPLAIN SELECT * FROM abc JOIN cd USING (c) WHERE b LIKE 'foo%' AND 123 = ANY (SELECT unnest(d));
info
------------------------------------------------------------------------------------------------
distribution: local
vectorized: true
• hash join
│ estimated row count: 1
│ equality: (c) = (c)
│ right cols are key
│
├── • index join
│ │ estimated row count: 1
│ │ table: abc@abc_pkey
│ │
│ └── • scan
│ estimated row count: 1 (100% of the table; stats collected 54 seconds ago)
│ table: abc@abc_b_idx
│ spans: [/'foo' - /'fop')
│
└── • group (hash)
│ estimated row count: 1
│ group by: c
│
└── • filter
│ estimated row count: 10
│ filter: unnest = 123
│
└── • project set
│ estimated row count: 10
│
└── • scan
estimated row count: 1 (100% of the table; stats collected 55 seconds ago)
table: cd@cd_pkey
spans: FULL SCAN
(32 rows)
```
Jira issue: CRDB-40119
Contributor guide
Assessment
This issue has not been assessed yet.