Skip to content

fix(sql): not to ignore outer limit if a subquery has a filter and another limit#6533

Merged
bluestreak01 merged 3 commits intomasterfrom
ia_fix_limit
Dec 16, 2025
Merged

fix(sql): not to ignore outer limit if a subquery has a filter and another limit#6533
bluestreak01 merged 3 commits intomasterfrom
ia_fix_limit

Conversation

@glasstiger
Copy link
Copy Markdown
Contributor

@glasstiger glasstiger commented Dec 14, 2025

If a query has multiple LIMIT clauses and one of them is in a subquery, the outer LIMIT is ignored.
For example, the below query returns 4 rows instead of 2:

select * from (
    select timestamp, symbol, venue,
           bids[1][1] as bid_price,
           bids[2][1] as bid_size,
           asks[1][1] as ask_price,
           asks[2][1] as ask_size
    from eq_equities_market_data
    where symbol='AAPL' and timestamp in '1970'
    limit -4
) limit 2;

This change fixes the problem.
It was tracked by the model if the limit has been implemented by a factory already.
It used a boolean, which did not allow for tracking multiple LIMIT clauses.
It is not the best choice to track this on the model anyway.
The LIMIT clause is represented by a node expression, which is copied to multiple places on the model tree.
It is better to track this on the node itself, and this way each LIMIT is tracked by the node which represents it in the model tree.

Also removed the followedLimitAdvice() method from RecordCursorFactory, it served the exact same purpose as implementsLimit().


required by https://github.com/questdb/questdb-enterprise/pull/828

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 14, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ia_fix_limit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@glasstiger
Copy link
Copy Markdown
Contributor Author

[PR Coverage check]

😍 pass : 8 / 9 (88.89%)

file detail

path covered line new line coverage
🔵 io/questdb/griffin/model/ExpressionNode.java 3 4 75.00%
🔵 io/questdb/griffin/SqlCodeGenerator.java 3 3 100.00%
🔵 io/questdb/griffin/engine/table/AsyncFilteredRecordCursorFactory.java 1 1 100.00%
🔵 io/questdb/griffin/engine/table/AsyncJitFilteredRecordCursorFactory.java 1 1 100.00%

@bluestreak01 bluestreak01 merged commit 0d9a535 into master Dec 16, 2025
43 checks passed
@bluestreak01 bluestreak01 deleted the ia_fix_limit branch December 16, 2025 00:05
mtopolnik pushed a commit that referenced this pull request Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants