pingcap / pingcap/tidb

Unexpected behavior for `case when {false condition} then {subquery...} else {...} end`

Open
#35,706 1 comment 0 reactions 0 assignees View on GitHub
affects-4.0 affects-5.0 affects-5.1 affects-5.2 affects-5.3 affects-5.4 affects-6.0 affects-6.1 severity/moderate sig/execution 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!

ref #34217

### 1. Minimal reproduce step (Required)

```
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
create table t1(c1 int);
insert into t1 values(2), (3);

select * from t1 where (case when 0 <> 0 then (select c1 from t1) else 0 end) > 1;
explain select * from t1 where (case when 0 <> 0 then (select c1 from t1) else 0 end) > 1;
select * from t1 where (case when (select count(1) from t1) = 0 then (select c1 from t1) else 0 end) > 1;
```

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

MySQL

```
mysql> select * from t1 where (case when 0 <> 0 then (select c1 from t1) else 0 end) > 1;
Empty set (0.00 sec)
```

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

```
MySQL [test]> select * from t1 where (case when 0 <> 0 then (select c1 from t1) else 0 end) > 1;
ERROR 1242 (21000): Subquery returns more than 1 row

MySQL [test]> explain select * from t1 where (case when 0 <> 0 then (select c1 from t1) else 0 end) > 1;
ERROR 1242 (21000): Subquery returns more than 1 row

MySQL [test]> select * from t1 where (case when (select count(1) from t1) = 0 then (select c1 from t1) else 0 end) > 1;
ERROR 1242 (21000): Subquery returns more than 1 row
```

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

>= release-4.0

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.