Skip to content

fix(sql): limited subqueries within a union not skipping rows correctly#6395

Merged
bluestreak01 merged 3 commits intomasterfrom
nw_limit_bug
Nov 24, 2025
Merged

fix(sql): limited subqueries within a union not skipping rows correctly#6395
bluestreak01 merged 3 commits intomasterfrom
nw_limit_bug

Conversation

@nwoolmer
Copy link
Copy Markdown
Contributor

Fixes #6381

There are some conditions within the Limit factory that interplay strangely. In need of a second pair of eyes 👀

@nwoolmer nwoolmer added Bug Incorrect or unexpected behavior SQL Issues or changes relating to SQL execution labels Nov 13, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 13, 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.

Walkthrough

This PR fixes incorrect LIMIT result handling when negative LIMIT values are used within subqueries and UNION operations. A state management issue in LimitRecordCursorFactory is addressed by resetting skipToRows to -1 after row skipping, and a corresponding test validates the fix.

Changes

Cohort / File(s) Summary
Bug fix in limit cursor reset
core/src/main/java/io/questdb/griffin/engine/LimitRecordCursorFactory.java
Modified LimitRecordCursor.toTop() method to explicitly reset skipToRows to -1 after calling skipRows(skippedRows), ensuring negative limit handling is preserved during cursor state resets
Test coverage for negative limits in unions
core/src/test/java/io/questdb/test/griffin/UnionTest.java
Added testUnionWithNegativeLimitReturnsLastNRows() test method that validates LIMIT -1 behavior (returning last N rows) in UNION queries, verifying both result correctness and execution plan structure

Sequence Diagram

sequenceDiagram
    participant Cursor as LimitRecordCursor
    participant SkipRows as skipRows()
    
    Cursor->>SkipRows: skipRows(skippedRows)
    Note over SkipRows: May modify skipToRows<br/>during execution
    SkipRows-->>Cursor: Returns control
    
    rect rgb(240, 248, 255)
        Note over Cursor: FIX: Reset state
        Cursor->>Cursor: skipToRows = -1
    end
    
    Note over Cursor: State preserved for<br/>negative limit handling
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • The core fix is a single-line assignment ensuring proper state reset in a localized method
  • Test method addition follows standard patterns and is straightforward to validate
  • Minimal surface area: two distinct changes with clear purposes
  • The fix addresses a specific state management concern rather than broad refactoring

Areas requiring attention:

  • Verify that resetting skipToRows = -1 after skipRows() does not interfere with other execution paths or cursor lifecycle states
  • Confirm the test's assertion logic correctly validates both the result set (expected timestamps) and the execution plan structure for negative LIMIT handling
  • Ensure the fix applies universally to all contexts where toTop() is called, particularly in nested/union query scenarios

Suggested reviewers

  • RaphDal

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main fix: addressing incorrect row skipping in LIMIT subqueries within UNION operations, directly matching the changeset's scope.
Description check ✅ Passed The description is concise and directly references the related issue (#6381), mentioning the specific problem with LIMIT factory conditions, which aligns with the changeset's purpose.
Linked Issues check ✅ Passed The code changes address the core issue: resetting skipToRows to -1 after row skipping, and adding a test that validates LIMIT -1 behavior in UNION contexts, fulfilling the requirements from issue #6381.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the LIMIT bug in UNION queries: one production code fix and one test specifically validating the negative LIMIT behavior described in the linked issue.

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.

@bluestreak01
Copy link
Copy Markdown
Member

@CodeRabbit review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 21, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 1 / 1 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/griffin/engine/LimitRecordCursorFactory.java 1 1 100.00%

@bluestreak01 bluestreak01 merged commit c50837c into master Nov 24, 2025
41 checks passed
@bluestreak01 bluestreak01 deleted the nw_limit_bug branch November 24, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Incorrect or unexpected behavior SQL Issues or changes relating to SQL execution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

incorrect limit result when using within subquery and union

3 participants