Skip to content

Nested union with LIMIT handles incorrectly #49874

@breezewish

Description

@breezewish

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE a(PK int);
CREATE TABLE b(PK int);

INSERT INTO a VALUES (1), (2);
INSERT INTO b VALUES (3), (4);

select * from a where PK = 0
union all
  (
    select * from b where PK = 0
    union all
    (select * from b where PK != 0)
    order by pk
    limit 1
  )
order by pk
limit 2;

2. What did you expect to see? (Required)

MySQL 8.0:

+------+
| PK   |
+------+
|    3 |
+------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> select * from a where PK = 0
    -> union all
    ->   (
    ->     select * from b where PK = 0
    ->     union all
    ->     (select * from b where PK != 0)
    ->     order by pk
    ->     limit 1
    ->   )
    -> order by pk
    -> limit 2;
+------+
| PK   |
+------+
|    3 |
|    4 |
+------+
2 rows in set (0.00 sec)

4. What is your TiDB version? (Required)

master

Metadata

Metadata

Assignees

Labels

affects-5.4This bug affects the 5.4.x(LTS) versions.affects-6.1This bug affects the 6.1.x(LTS) versions.affects-6.5This bug affects the 6.5.x(LTS) versions.affects-7.1This bug affects the 7.1.x(LTS) versions.affects-7.5This bug affects the 7.5.x(LTS) versions.severity/criticalsig/sql-infraSIG: SQL Infratype/bugThe issue is confirmed as a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions