pingcap / pingcap/tidb

Optimizer fails to convert ABS(column) < constant to range scan, resulting in full table scan

Open
#69,158 2 comments 0 reactions 0 assignees View on GitHub
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 wanna recommend an optimization about `ABS()`.
When querying with `ABS(t0.c0) < 10`, TiDB performs a full table scan even though the equivalent condition `t0.c0 > -10 AND t0.c0 < 10` can be executed as a range scan. The optimizer does not transform the `ABS()` predicate into a range condition, preventing efficient index usage.

### 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);

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;

EXPLAIN SELECT c0 FROM t0 WHERE ABS(t0.c0)<10;
+-------------------------+------------+-----------+---------------+-------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+------------+-----------+---------------+-------------------------+
| TableReader_7 | 800000.00 | root | | data:Selection_6 |
| └─Selection_6 | 800000.00 | cop[tikv] | | lt(abs(test.t0.c0), 10) |
| └─TableFullScan_5 | 1000000.00 | cop[tikv] | table:t0 | keep order:false |
+-------------------------+------------+-----------+---------------+-------------------------+

EXPLAIN SELECT c0 FROM t0 WHERE t0.c0>-10 AND t0.c0<10;
+------------------------+---------+-----------+---------------+----------------------------------+
| id | estRows | task | access object | operator info |
+------------------------+---------+-----------+---------------+----------------------------------+
| TableReader_6 | 0.00 | root | | data:TableRangeScan_5 |
| └─TableRangeScan_5 | 0.00 | cop[tikv] | table:t0 | range:(-10,10), keep order:false |
+------------------------+---------+-----------+---------------+----------------------------------+
```

### 2. What did you expect to see? (Required)

```sql
EXPLAIN SELECT c0 FROM t0 WHERE ABS(t0.c0)<10;
+------------------------+---------+-----------+---------------+----------------------------------+
| id | estRows | task | access object | operator info |
+------------------------+---------+-----------+---------------+----------------------------------+
| TableReader_6 | 0.00 | root | | data:TableRangeScan_5 |
| └─TableRangeScan_5 | 0.00 | cop[tikv] | table:t0 | range:(-10,10), keep order:false |
+------------------------+---------+-----------+---------------+----------------------------------+
```

### 3. What did you see instead (Required)
```sql
EXPLAIN SELECT c0 FROM t0 WHERE ABS(t0.c0)<10;
+-------------------------+------------+-----------+---------------+-------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+------------+-----------+---------------+-------------------------+
| TableReader_7 | 800000.00 | root | | data:Selection_6 |
| └─Selection_6 | 800000.00 | cop[tikv] | | lt(abs(test.t0.c0), 10) |
| └─TableFullScan_5 | 1000000.00 | cop[tikv] | table:t0 | keep order:false |
+-------------------------+------------+-----------+---------------+-------------------------+
```
### 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.