pingcap / pingcap/tidb

Could TiDB please consider short-circuiting COALESCE()

Open
#61,853 1 comment 0 reactions 0 assignees View on GitHub
sig/planner type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Hi, TiDB developers,

In TiDB's [docs](https://docs.pingcap.com/tidb/stable/operators/), `COALESCE(...)` 's description is: Return the first non-NULL argument.

To improve the `COALESCE(...)` function, other DBMSs (e.g., MySQL, MariaDB, CockroachDB) usually short-circuit the function when the first non-null value is obtained, and do not continue to evaluate subsequent parameters.

However, TiDB does not make such optimizations. Could you please consider short-circuiting COALESCE(), I think it will improve the performance of `COALESCE(...)`. Moreover, when I use `EXPLAIN` statement on `COALESCE()`, **the `EXPLAIN` statement will actually execute the query**.

### 1. Minimal reproduce step (Required)

(1) Create tables and insert data.
```sql
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
Although the first non-null value has been obtained, TiDB continues to evaluate subsequent parameters.
```sql
SELECT COALESCE(null, 1) AS result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)

SELECT COALESCE(null, 1, (SELECT MAX(c1) FROM t1)) AS result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (1.01 sec)

SELECT COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
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. According to TiDB's docs on [EXPLAIN](https://docs.pingcap.com/tidb/stable/sql-statement-explain/): **The EXPLAIN statement shows the execution plan for a query without executing it**.
```sql
EXPLAIN SELECT COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
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]
```
Thus, I think there are two bugs in the above case: (1) TiDB continues to evaluate subsequent parameters (which is unnecessary). (2) EXPLAIN statement actually executes the query.

### 2. What did you expect to see? (Required)
```sql
SELECT COALESCE(null, 1, (SELECT MAX(c1) FROM t1)) AS result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)

SELECT COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)

EXPLAIN SELECT COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
-- Expect: TiDB returns query plan.
```

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

```sql
SELECT COALESCE(null, 1, (SELECT MAX(c1) FROM t1)) AS result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (1.01 sec)

SELECT COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
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 COALESCE(null, 1, (SELECT MAX(a.c1) FROM t1 a CROSS JOIN t1 b)) AS result;
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: https://github.com/pingcap/tidb/commit/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.