Skip to content

fix(sql): fix false timestamp ordering error with nested SAMPLE BY and ORDER BY#6572

Merged
bluestreak01 merged 1 commit intomasterfrom
vi_fix_order
Dec 22, 2025
Merged

fix(sql): fix false timestamp ordering error with nested SAMPLE BY and ORDER BY#6572
bluestreak01 merged 1 commit intomasterfrom
vi_fix_order

Conversation

@bluestreak01
Copy link
Copy Markdown
Member

@bluestreak01 bluestreak01 commented Dec 22, 2025

Problem

Nested SAMPLE BY queries with an ORDER BY clause in between incorrectly failed with:

ASC order over TIMESTAMP column is required but not provided

Example of failing query:

SELECT timestamp, count()
FROM (
    (
        SELECT timestamp, symbol, count(bids[1][1]) as total
        FROM eq_equities_market_data
        WHERE symbol = 'HSBC'
        SAMPLE BY 10s
    )
    ORDER BY timestamp
)
SAMPLE BY 10m

Root Cause

When SAMPLE BY needs ordered timestamp data, it pushes timestampRequired=true onto a context stack. This flag propagates to nested models and throws an error if ASC timestamp order isn't guaranteed.

However, when there's an ORDER BY clause between the outer and inner SAMPLE BY, the inner model's ordering is irrelevant since ORDER BY will re-sort the data anyway.

Fix

When an ORDER BY clause is present, push timestampRequired=false to signal that nested models don't need to maintain timestamp ordering.

🤖 Generated with Claude Code

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 22, 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 vi_fix_order

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 bluestreak01 changed the title fix(sql): fix timestamp related SQL error with nested sample-by expressions fix(sql): fix false timestamp ordering error with nested SAMPLE BY and ORDER BY Dec 22, 2025
@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 5 / 5 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/griffin/SqlCodeGenerator.java 5 5 100.00%

@bluestreak01 bluestreak01 merged commit ed4f35e into master Dec 22, 2025
44 checks passed
@bluestreak01 bluestreak01 deleted the vi_fix_order branch December 22, 2025 10:48
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