pingcap / pingcap/tidb

Impossible WHERE with inequality-only contradictions not optimized

Open
#69,058 2 comments 0 reactions 0 assignees View on GitHub
affects-8.1 affects-8.5 affects-9.0 contribution severity/moderate sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Hi, TiDB developers, I find a missed optimization in TiDB .

TiDB detects impossible WHERE conditions when a column is compared to a constant using both equality and an inequality (e.g., c0 > 0 AND c0 = 0), producing a `estRows: 0.00` result without table access. However, contradictory conditions that involve only inequalities (e.g., c0 > 0 AND c0 < 0, c0 >= 0 AND c0 < 0, c0 > 0 AND c0 <= 0) are not recognized as so. Instead, a full table scan with a filter is performed, which is wasteful for large tables.

### 1. Minimal reproduce step (Required)

```sql
CREATE TEMPORARY TABLE digits (d INT);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

-- insert into t0 with values 0-999999
CREATE TABLE t0(c0 INT);
INSERT INTO t0
SELECT d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 AS num
FROM digits d1, digits d2, digits d3, digits d4, digits d5, digits d6
WHERE d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 <= 1000000;

-- positive cases
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 > 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 < 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 <> 0 AND t0.c0 = 0 \G
*************************** 1. row ***************************
id: TableDual_5
estRows: 0.00
actRows: 0
1 row in set (0.00 sec)

-- missed optimization
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 > 0 AND t0.c0 < 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 > 0 AND t0.c0 <= 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 >= 0 AND t0.c0 < 0 \G
*************************** 1. row ***************************
id: TableReader_7
estRows: 0.00
actRows: 0
task: root
access object:
execution info: ...
operator info: data:Selection_6
*************************** 2. row ***************************
id: └─Selection_6
estRows: 0.00
actRows: 0
task: cop[tikv]
access object:
execution info: ...
operator info: gt(test.t0.c0, 0), lt(test.t0.c0, 0); gt(test.t0.c0, 0), le(test.t0.c0, 0); ge(test.t0.c0, 0), lt(test.t0.c0, 0)
*************************** 3. row ***************************
id: └─TableFullScan_5
estRows: 1000000.00
actRows: 1000000
task: cop[tikv]
access object: table:t0
execution info: ...
operator info: keep order:false
3 rows in set (0.28 sec)
```

### 2. What did you expect to see? (Required)
Producing a `estRows: 0.00` result without table access for these queries.
```sql
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 > 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 < 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 <> 0 AND t0.c0 = 0 \G
*************************** 1. row ***************************
id: TableDual_5
estRows: 0.00
actRows: 0
1 row in set (0.00 sec)
```

### 3. What did you see instead (Required)
`TableFullScan` occurs in their query plans.
```sql
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 > 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 < 0 AND t0.c0 = 0 \G
EXPLAIN ANALYZE SELECT * FROM t0 WHERE t0.c0 <> 0 AND t0.c0 = 0 \G
....
*************************** 3. row ***************************
id: └─TableFullScan_5
estRows: 1000000.00
actRows: 1000000
...
3 rows in set (0.28 sec)
```
### 4. What is your TiDB version? (Required)
8.0.11-TiDB-v8.5.6

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.