pingcap / pingcap/tidb

TiDB does not short-circuit in CASE statement

Open
#61,846 2 comments 0 reactions 1 assignee Claimed by @hawkingrei View on GitHub
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, please consider such a case:
```sql
SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN
(SELECT 0)
ELSE
(SELECT COUNT(*) FROM t1 CROSS JOIN t2)
END AS result;
```
Here, `t0` is an empty table, and TiDB has collected the statistics of `t0`. Thus, `(SELECT COUNT(*) FROM t0) = 0` is `true` and TiDB should not go to execute the `else` branch. However, TiDB does it.

### 1. Minimal reproduce step (Required)
(1) Create tables and insert data.
```sql
CREATE TABLE t0(c0 INT8);
CREATE TABLE t1(c1 INT8);

CREATE TEMPORARY TABLE digits (d INT);
INSERT INTO digits VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);

-- insert into t1 with 10,000,000 rows
INSERT INTO t1
SELECT
d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 + d7.d*1000000 AS num
FROM
digits d1, digits d2, digits d3, digits d4, digits d5, digits d6, digits d7
WHERE
d1.d + d2.d*10 + d3.d*100 + d4.d*1000 + d5.d*10000 + d6.d*100000 + d7.d*1000000 <= 10000000;
```
(2) Execute queries
TiDB executes the `else` branch (which does not need to be executed) and triggers `ERROR 8175 (HY000)`.
```sql
SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
ERROR 8175 (HY000): Your query has been cancelled due to exceeding the allowed memory limit for a single SQL query. Please try narrowing your query scope or increase the tidb_mem_quota_query limit and try again.[conn=295698438]
```
Moreover, the `explain` statement also triggers the same bug. Does this mean that the explain statement will actually execute the query?
```sql
explain SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
ERROR 8175 (HY000): Your query has been cancelled due to exceeding the allowed memory limit for a single SQL query. Please try narrowing your query scope or increase the tidb_mem_quota_query limit and try again.[conn=295698438]
```

### 2. What did you expect to see? (Required)
```sql
SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
+------+
| num |
+------+
| 0 |
+------+
1 row in set (0.00x sec)

explain SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
-- Expect: TiDB returns query plan.
```
### 3. What did you see instead (Required)
```sql
SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
ERROR 8175 (HY000): Your query has been cancelled due to exceeding the allowed memory limit for a single SQL query. Please try narrowing your query scope or increase the tidb_mem_quota_query limit and try again.[conn=295698438]

explain SELECT
CASE
WHEN (SELECT COUNT(*) FROM t0) = 0 THEN (SELECT 0)
ELSE (SELECT COUNT(*) FROM t1 a CROSS JOIN t1 b)
END AS num;
ERROR 8175 (HY000): Your query has been cancelled due to exceeding the allowed memory limit for a single SQL query. Please try narrowing your query scope or increase the tidb_mem_quota_query limit and try again.[conn=295698438]
```
### 4. What is your TiDB version? (Required)

| Release Version: v8.5.2
Edition: Community
Git Commit Hash: f43a13324440f92209e2a9f04c0bbe9cf763978d
Git Branch: HEAD
UTC Build Time: 2025-05-29 03:30:55
GoVersion: go1.23.8
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.