Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested union with LIMIT handles incorrectly #49874

Closed
breezewish opened this issue Dec 28, 2023 · 1 comment · Fixed by #49898
Closed

Nested union with LIMIT handles incorrectly #49874

breezewish opened this issue Dec 28, 2023 · 1 comment · Fixed by #49898
Assignees

Comments

@breezewish
Copy link
Member

breezewish commented Dec 28, 2023

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

@breezewish breezewish added the type/bug This issue is a bug. label Dec 28, 2023
@breezewish
Copy link
Member Author

This is similar to a fixed issue #49377 but is different.

I did a quick debug and looks like the parser missed the inner LIMIT in this case:

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   # <----- missing after parsing
  )
order by pk
limit 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants