polardb / polardb/polardbx-sql

PolarDB crashes (Docker container exits) when executing a complex SQL query with nested JOINs, LATERAL subqueries, and EXIST

Open
#274 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.7k
Forks
337
PR merge metrics
No merged PRs in 30d

Description

Description:
When executing a highly complex SQL query involving multiple nested JOINs, LATERAL subqueries, and EXISTS conditions, the PolarDB server running inside a Docker container report:

ERROR 2013 (HY000): Lost connection to MySQL server during query

The server must be reconnect to recover.

How to repeat:

-- 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 SQL
SELECT 1
FROM (
    SELECT ref_0.age AS c0, ref_0.id AS c1, ref_0.id AS c2, ref_0.email AS c3
    FROM users ref_0
    WHERE ref_0.created_at IS NOT NULL AND false
    LIMIT 192
) subq_0
RIGHT JOIN users ref_1
LEFT JOIN comments ref_2
    LEFT JOIN posts ref_3
        INNER JOIN comments ref_4 ON true
        RIGHT JOIN posts ref_11
            RIGHT JOIN comments ref_12 ON true
        ON ref_3.views = ref_12.id
    ON false
    INNER JOIN (
        SELECT ref_13.created_at AS c0,
               (SELECT content FROM comments LIMIT 1 OFFSET 80) AS c1,
               (SELECT amount FROM orders LIMIT 1 OFFSET 5) AS c3
        FROM orders ref_13
        WHERE true
    ) subq_1 ON true
ON true
ON EXISTS (
    SELECT 1
    FROM comments ref_18
    LEFT JOIN comments ref_19
        ON (false OR ref_11.likes IS NULL OR subq_1.c1 IS NOT NULL)
    WHERE EXISTS (
        SELECT 1
        FROM posts ref_20
        WHERE ref_2.content IS NULL
        LIMIT 90
    )
),
LATERAL (
    SELECT ref_12.content AS c0
    FROM (
        SELECT ref_2.user_id AS c5
        FROM users ref_21
        INNER JOIN orders ref_22
            ON (63 IS NOT NULL OR (SELECT amount FROM orders LIMIT 1 OFFSET 4) IS NOT NULL)
        WHERE ref_21.username IS NOT NULL
    ) subq_3
    WHERE ref_1.username IS NOT NULL
    LIMIT 25
) subq_4,
LATERAL (
    SELECT 6 AS c6
    FROM users ref_25
    INNER JOIN orders ref_26
        INNER JOIN posts ref_27
            LEFT JOIN orders ref_28 ON true
            LEFT JOIN posts ref_31 ON true
        ON ref_31.content IS NULL
    ON ref_25.id = ref_27.id
    WHERE (
        true AND (
            false AND (true AND false AND ref_28.created_at IS NOT NULL)
            AND subq_1.c3 IS NOT NULL
            AND ref_4.content IS NOT NULL
            AND false
            OR true
        )
        OR EXISTS (
            SELECT 1
            FROM users ref_46
            WHERE ref_31.views IS NULL
            LIMIT 55
        )
        OR (
            (subq_0.c0 IS NOT NULL OR ref_3.title IS NULL)
            AND ref_3.title IS NULL
        )
        OR EXISTS (
            SELECT 1
            FROM orders ref_47
            RIGHT JOIN users ref_48 ON true
            WHERE true
               OR EXISTS (
                    SELECT 1
                    FROM posts ref_49,
                         LATERAL (
                             SELECT 1
                             FROM comments ref_50
                             WHERE ref_1.username IS NULL
                         ) subq_11
                    WHERE true
                )
            LIMIT 75
        )
        OR EXISTS (
            SELECT ref_25.id
            FROM comments ref_53
            WHERE true
              AND ref_1.status IS NULL
              AND true
            LIMIT 59
        )
        OR EXISTS (
            SELECT 1
            FROM comments ref_56
            WHERE EXISTS (
                SELECT 1
                FROM posts ref_57
                WHERE ref_27.created_at IS NOT NULL
                LIMIT 53
            )
            AND ref_28.created_at IS NOT NULL
            LIMIT 112
        )
    )
    LIMIT 142
) subq_12
WHERE EXISTS (
    SELECT 1
    FROM users ref_99,
         LATERAL (
             SELECT ref_12.id AS c0, ref_99.age AS c1, subq_12.c6 AS c2, ref_99.username AS c3
             FROM users ref_100
             WHERE subq_0.c3 IS NULL
             LIMIT 71
         ) subq_26
    WHERE EXISTS (
        SELECT 1
        FROM users ref_101
        LEFT JOIN orders ref_102 ON true
        WHERE EXISTS (
            SELECT 1
            FROM users ref_103
            WHERE ref_1.score IS NULL
            LIMIT 38
        )
        LIMIT 110
    )
    LIMIT 11
)
LIMIT 176;

version info:

MySQL [test]> select version();
+----------------------------------+
| version()                        |
+----------------------------------+
| 8.0.32-X-Cluster-8.4.19-20250825 |
+----------------------------------+
1 row in set (0.00 sec)

MySQL [test]> select polardb_version();
+----------------------------------------------------------+
| polardb_version()                                        |
+----------------------------------------------------------+
| PolarDB V2.0_2.4.2_8.4.19-20250825 (Distributed Edition) |
+----------------------------------------------------------+
1 row in set (0.00 sec)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named. Start by running the supplied schema and query in a Docker PolarDB instance matching the listed MySQL and PolarDB versions, then inspect server logs and the connection state; done means the query no longer crashes the server or drops the client connection.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.