TiDB server crashes when executing a complex CTE followed by a simple SELECT query.
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
### 1. Minimal reproduce step (Required)
```SQL
-- SCHEMA
CREATE TABLE users (
id INT,
username VARCHAR(100),
email VARCHAR(255),
age INT,
status VARCHAR(20),
created_at TIMESTAMP NULL,
score DOUBLE
);
CREATE TABLE posts (
id INT,
user_id INT,
title VARCHAR(255),
content VARCHAR(1000),
views INT,
likes INT,
created_at TIMESTAMP NULL,
rating DOUBLE
);
CREATE TABLE comments (
id INT,
post_id INT,
user_id INT,
content VARCHAR(1000),
is_spam INT,
created_at TIMESTAMP NULL
);
CREATE TABLE orders (
id INT,
user_id INT,
amount DOUBLE,
status VARCHAR(20),
created_at TIMESTAMP NULL
);
INSERT INTO users VALUES
(1, 'alice', 'alice@test.com', 20, 'active', '2022-01-01 10:00:00', 88.5),
(2, 'bob', 'bob@test.com', 30, 'active', '2022-01-02 11:00:00', 92.3),
(3, 'carol', NULL, NULL, 'banned','2022-01-03 12:00:00', NULL),
(4, 'dave', 'dave@test.com', 45, 'active', '2022-01-04 13:00:00', 65.2),
(5, NULL, 'null@test.com', 18, 'inactive','2022-01-05 14:00:00', 70.0);
INSERT INTO posts VALUES
(1, 1, 'Hello World', 'First post', 100, 10, '2022-01-10 10:00:00', 4.5),
(2, 1, 'Another Post', NULL, 150, 20, '2022-01-11 11:00:00', 3.0),
(3, 2, 'Bob Post', 'Content', NULL, 5, '2022-01-12 12:00:00', NULL),
(4, 3, NULL, 'Empty', 50, 2, '2022-01-13 13:00:00', 5.0),
(5, 4, 'Last Post', 'Last', 300, 30,'2022-01-14 14:00:00', 4.9);
INSERT INTO comments VALUES
(1, 1, 2, 'Nice post', 0, '2022-01-20 10:00:00'),
(2, 1, 3, 'Spam here', 1, '2022-01-21 11:00:00'),
(3, 2, 1, 'Thanks', 0, '2022-01-22 12:00:00'),
(4, 4, 5, NULL, 0, '2022-01-23 13:00:00');
INSERT INTO orders VALUES
(1, 1, 100.00, 'paid', '2022-02-01 09:00:00'),
(2, 1, 200.50, 'shipped', '2022-02-02 10:00:00'),
(3, 2, NULL, 'failed', '2022-02-03 11:00:00'),
(4, 3, 50.00, 'paid', '2022-02-04 12:00:00'),
(5, 5, 999.99, 'paid', '2022-02-05 13:00:00');
-- TRIGGER SQLs:
-- 1. WITH...SELECT...
WITH jennifer_0 AS (
SELECT
subq_0.c0,
(SELECT id FROM orders LIMIT 1 OFFSET 6) AS c5,
MIN(95) OVER (PARTITION BY subq_0.c0) AS c7,
subq_0.c0 AS c12
FROM (
SELECT ref_1.created_at AS c0
FROM orders ref_0
LEFT JOIN comments ref_1
ON (ref_1.created_at IS NOT NULL OR TRUE)
WHERE FALSE
) subq_0
WHERE subq_0.c0 IS NOT NULL
LIMIT 22
)
SELECT
subq_1.c10 AS c0,
subq_1.c4 AS c1,
subq_1.c1 AS c2,
subq_1.c11 AS c3,
VAR_SAMP(subq_1.c9) OVER (PARTITION BY subq_1.c12) AS c4,
(SELECT post_id FROM comments LIMIT 1 OFFSET 83) AS c5,
subq_1.c6 AS c6,
subq_1.c4 AS c7,
MIN(subq_1.c4) OVER (PARTITION BY subq_1.c9, subq_1.c11) AS c8
FROM (
SELECT
ref_4.c0 AS c12,
ref_4.c12 AS c6,
ref_4.c7 AS c4,
ref_4.c0 AS c1,
ref_4.c0 AS c10,
ref_4.c0 AS c11,
(SELECT VAR_SAMP(id) FROM orders) AS c9
FROM jennifer_0 ref_4
LIMIT 173
) subq_1
WHERE subq_1.c1 IS NOT NULL
LIMIT 145;
-- RESULT: ERROR 1105 (HY000): runtime error: index out of range [0] with length 0
-- 2. SELECT
SELECT version();
-- RESULT: crash and output the result
```
### 2. What did you expect to see? (Required)
The second query should execute normally and return a result set (possibly empty), without causing any server crash or connection reset.
### 3. What did you see instead (Required)
The server crashed first, then reconnected and returned the result set.
### 4. What is your TiDB version? (Required)
8.0.11-TiDB-v7.5.1
Contributor guide
Assessment
This issue has not been assessed yet.