Skip to content

incorrect limit result when using within subquery and union #6381

@nwoolmer

Description

@nwoolmer

To reproduce

DECLARE
  @tbl := trades
SELECT * FROM (
  (SELECT timestamp FROM @tbl LIMIT 1)
  UNION ALL
  (SELECT timestamp FROM @tbl LIMIT -1)
);
| timestamp                   |
| --------------------------- |
| 2022-03-08T18:03:57.609765Z |
| 2022-03-08T18:03:57.609765Z |

The plan:

Union All
    Limit lo: 1 skip-over-rows: 0 limit: 1
        PageFrame
            Row forward scan
            Frame forward scan on: trades
    Limit lo: -1 skip-over-rows: 0 limit: 1
        PageFrame
            Row forward scan
            Frame forward scan on: trades

but it should be ...

DECLARE
  @tbl := trades
SELECT * FROM (
  (SELECT min(timestamp) FROM @tbl)
  UNION ALL
  (SELECT max(timestamp) FROM @tbl)
);
| min(timestamp)              |
| --------------------------- |
| 2022-03-08T18:03:57.609765Z |
| 2025-11-11T13:17:28.411000Z |

QuestDB version:

9.1.1

OS, in case of Docker specify Docker and the Host OS:

N/A

File System, in case of Docker specify Host File System:

N/A

Full Name:

Nick Woolmer

Affiliation:

QuestDB

Have you followed Linux, MacOs kernel configuration steps to increase Maximum open files and Maximum virtual memory areas limit?

  • Yes, I have

Additional context

No response

Metadata

Metadata

Assignees

Labels

BugIncorrect or unexpected behaviorSQLIssues or changes relating to SQL execution

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions