pingcap / pingcap/tidb

Unexpected Estimated Rows by WHERE clause

Open
#38,721 0 comments 0 reactions 2 assignees Claimed by @time-and-fate View on GitHub
fuzz/sqlancer severity/minor sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

### 1. Minimal reproduce step (Required)

```sql
CREATE TABLE t0(c0 INT);
CREATE VIEW v0(c1, c2) AS SELECT t0.c0, DATE_FORMAT('2022-10-28 16:23:00', '%W %M %Y') FROM t0 WHERE t0.c0;
INSERT INTO t0 VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
ANALYZE TABLE t0;

EXPLAIN SELECT t0.c0 FROM v0 STRAIGHT_JOIN t0 ON v0.c1; -- estimated rows: 66.57, real rows: 90
EXPLAIN SELECT t0.c0 FROM v0 STRAIGHT_JOIN t0 ON v0.c1 WHERE v0.c2; -- estimated rows: 80, real rows: 0
```

### 2. What did you expect to see? (Required)
The second query returns no more estimated rows than the first query.

Attach both query plans:
```
> EXPLAIN SELECT t0.c0 FROM v0 STRAIGHT_JOIN t0 ON v0.c1;
id estRows task access object operator info
HashJoin_11 66.57 root CARTESIAN inner join
├─TableReader_14(Build) 6.66 root data:Selection_13
│ └─Selection_13 6.66 cop[tikv] database1.t0.c0
│ └─TableFullScan_12 10.00 cop[tikv] table:t0 keep order:false
└─TableReader_16(Probe) 10.00 root data:TableFullScan_15
└─TableFullScan_15 10.00 cop[tikv] table:t0 keep order:false
```
```
> EXPLAIN SELECT t0.c0 FROM v0 STRAIGHT_JOIN t0 ON v0.c1;
id estRows task access object operator info
HashJoin_12 80.00 root CARTESIAN inner join
├─Projection_13(Build) 8.00 root 1->Column#8
│ └─TableDual_14 8.00 root rows:0
└─TableReader_16(Probe) 10.00 root data:TableFullScan_15
└─TableFullScan_15 10.00 cop[tikv] table:t0 keep order:false
```

### 3. What did you see instead (Required)
The second query returns more estimated rows than the first query.

### 4. What is your TiDB version? (Required)

```
| Release Version: v6.4.0-alpha-154-g636a363f7
Edition: Community
Git Commit Hash: 636a363f7b5f6aa9a268efaf72b170770405dcc3
Git Branch: master
UTC Build Time: 2022-10-28 05:18:57
GoVersion: go1.19.1
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: unistore |
```

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.