pingcap / pingcap/tidb

The opposite WHERE clause intersects and is always an empty set. It should perform no action.

Open
#60,847 1 comment 1 reaction 0 assignees View on GitHub
severity/minor sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Dear TiDB Developer:

Since q1 and q2 have opposite WHERE clause (e.g., = and !=), q1 INTERSECT q2 should always produce empty set and without physical read.

q1: SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
q2: SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME = COLUMNS.TABLE_NAME;

However, TiDB actually executed such an unnecessary query. I think such queries should be optimized.

Best regard,

### 1. Minimal reproduce step (Required)

```
mysql> use INFORMATION_SCHEMA;
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME = COLUMNS.TABLE_NAME;
Empty set (34.72 sec)
```
### 2. What did you expect to see? (Required)
```
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME = COLUMNS.TABLE_NAME;
Empty set (00.00 sec)
```
### 3. What did you see instead (Required)
```
mysql> SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME != COLUMNS.TABLE_NAME
INTERSECT
SELECT * FROM TABLES CROSS JOIN COLUMNS WHERE TABLES.TABLE_NAME = COLUMNS.TABLE_NAME;
Empty set (34.72 sec)
```
### 4. What is your TiDB version? (Required)
| Release Version: v8.5.1
Edition: Community
Git Commit Hash: https://github.com/pingcap/tidb/commit/fea86c8e35ad4a86a5e1160701f99493c2ee547c
Git Branch: HEAD
UTC Build Time: 2025-01-16 07:38:34
GoVersion: go1.23.4
Race Enabled: false
Check Table Before Drop: false
Store: tikv |

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.