pingcap / pingcap/tidb

Incorrect results of SELECT caused by subquery and logical operations

Open
#42,617 9 comments 0 reactions 2 assignees Claimed by @hawkingrei View on GitHub
may-affects-4.0 may-affects-5.0 may-affects-5.1 may-affects-5.2 may-affects-5.3 may-affects-5.4 may-affects-6.1 may-affects-6.5 severity/moderate sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

**Setup the environment:**

```
CREATE TABLE t0 (pkey int, c6 varchar(100));
INSERT INTO t0 VALUES (1,NULL);
```

**Testcase 1**
```
select *
from
t0 as ref_0
where
(((ref_0.c6 is null) and false) and null) xor (true or ('111' >= (
select
null as c0
from
t0 as ref_15
where exists (
select *
from
t0 as ref_16
where false xor ((ref_16.pkey <= (
select
ref_0.pkey as c0
from
t0 as ref_30
order by c0 asc limit 1))
and false))
order by c0 desc limit 1)));
```
Testcase 1 outputs empty (0 rows).

Then, I replace `(ref_0.c6 is null) and false` with `false`. The results should not be changed because `anything and false` is `false`. The test case becomes:

**Testcase 2**
```
select *
from
t0 as ref_0
where
((false) and null) xor (true or ('111' >= (
select
null as c0
from
t0 as ref_15
where exists (
select *
from
t0 as ref_16
where false xor ((ref_16.pkey <= (
select
ref_0.pkey as c0
from
t0 as ref_30
order by c0 asc limit 1))
and false))
order by c0 desc limit 1)));
```

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

Testcase 1 and Testcase 2 should output the same results.

### 3. What did you see instead (Required)

Testcase 2 outputs
```
+------+------+
| pkey | c6 |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.00 sec)
```
While Testcase 1 outputs empty set:
`Empty set (0.00 sec)`

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

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v7.1.0-alpha-27-gf5ca27ef3
Edition: Community
Git Commit Hash: f5ca27ef326e94fb29b4ba35a5d59aa575f66880
Git Branch: master
UTC Build Time: 2023-03-23 13:57:53
GoVersion: go1.20.2
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.