short-circuit in WHERE clause with mutiple conditions connected by AND
- 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 query:
```sql
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
```
There are two conditions in the `WHERE` clause, connected by `AND`, which means that if any one of the conditions is not satisfied, it is `false`. Therefore, when we calculate two conditions, if we calculate one of the conditions to be `false`, we can short-circuit, which can avoid unnecessary calculations. However, TiDB does not short-circuit to optimize such queries. Specially, short-circuiting is necessary when the computation of the condition is expensive.
### 1. Minimal reproduce step (Required)
(1) Create tables and insert data.
```sql
CREATE TABLE t1(c1 INT8);
CREATE TABLE t2(c2 INT8);
INSERT INTO t1 VALUES(1);
CREATE TEMPORARY TABLE digits (d INT);
INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
-- insert into t2 with 10,000,000 rows
INSERT INTO t2
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 another subqueries (which does not need to be executed) and triggers `ERROR 8175 (HY000)`.
```sql
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1);
Empty set (0.00 sec)
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
Empty set (1.79 sec)
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
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=402653194]
```
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 * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
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=402653194]
```
Thus, I think there are two bugs in the above case: (1) TiDB continues to evaluate another subquery (which is unnecessary). (2) `EXPLAIN` statement actually executes the query.
### 2. What did you expect to see? (Required)
```sql
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
Empty set (0.00 sec)
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
Empty set (0.00 sec)
EXPLAIN SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
-- Expect: TiDB returns query plan.
```
### 3. What did you see instead (Required)
```sql
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
Empty set (1.79 sec)
SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
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=402653194]
EXPLAIN SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(a.c2) FROM t2 a CROSS JOIN t2 b);
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=402653194]
```
### 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 |
### 5. Additional data
I did a differential test among MySQL, MariaDB, and TiDB (Sorry for my rudeness, and I think all of them are great DBMSs).
The following is the case for MySQL and MariaDB:
```sql
MariaDB [test]> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
Empty set (0.014 sec)
MariaDB [test]> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1);
Empty set (0.001 sec)
MariaDB [test]> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c2) FROM t2);
Empty set (2.563 sec)
mysql> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1) AND c1 > (SELECT AVG(c2) FROM t2);
Empty set (0.003 sec)
mysql> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c1) FROM t1);
Empty set (0.001 sec)
mysql> SELECT * FROM t1 WHERE c1 > (SELECT AVG(c2) FROM t2);
Empty set (4.442 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.