Skip to content

fix(sql): WINDOW JOIN dropping prevailing row on cross-partition#6871

Merged
bluestreak01 merged 5 commits intomasterfrom
rd_fix_window_join_prevailing2
Mar 13, 2026
Merged

fix(sql): WINDOW JOIN dropping prevailing row on cross-partition#6871
bluestreak01 merged 5 commits intomasterfrom
rd_fix_window_join_prevailing2

Conversation

@RaphDal
Copy link
Copy Markdown
Contributor

@RaphDal RaphDal commented Mar 12, 2026

Summary

  • WindowJoinTimeFrameHelper.findRowLo() drops the INCLUDE PREVAILING row when the bookmarked-frame optimization kicks in and the prevailing candidate sits in a prior partition. The bookmarked path skipped initialization of prevailingFrameIndex/prevailingRowIndex, leaving them at -1/Long.MIN_VALUE. Downstream backward scans (findPrevailingForMasterRow, WindowJoinPrevailingCache) saw -1 and returned immediately, silently omitting the prevailing row from results.
  • WindowJoinWithPrevailingFastRecordCursor.hasNext() compared slaveTimestamps.get(rowLo) against masterTimestampHi instead of slaveTimestampLo when deciding whether to include the prevailing value. This caused it to skip prevailing inclusion when the first in-index row fell between slaveTimestampLo and masterTimestampHi.

Detail

The first bug affects all four sync cursor variants that call findRowLo(lo, hi, true):

  • WindowJoinWithPrevailingAndJoinFilterRecordCursor
  • WindowJoinWithPrevailingAndJoinFilterFastRecordCursor
  • WindowJoinWithPrevailingFastRecordCursor
  • WindowJoinFastVectRecordCursor (when includePrevailing=true)

The base WindowJoinRecordCursor (prevailing without filter) is not affected because it uses the separate findRowLoWithPrevailing() method, which maintains its own local prevailing tracking and handles the bookmarked path correctly.

The fix seeds prevailingFrameIndex/prevailingRowIndex when entering via the bookmark path. If the bookmark is past the frame start, the row immediately before the bookmark becomes the prevailing candidate. If the bookmark is at the frame start, the prevailing index is set to rowLo - 1, which signals findPrevailingForMasterRow to skip to the previous frame.

The second bug is a simple comparison target error in WindowJoinWithPrevailingFastRecordCursor: the condition that triggers prevailing inclusion should check whether the first row's timestamp exceeds the window low boundary (slaveTimestampLo), not the window high boundary (masterTimestampHi).

Test plan

  • Add testPrevailingWithFilterCrossPartition regression test to WindowJoinTest: master rows on day 2, slave rows on day 1 and day 2, join filter on id column, 1-second window with INCLUDE PREVAILING. Verifies the cross-partition prevailing row is counted. Fails without the fix, passes with it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 12, 2026

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.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9edbc1e1-08ab-4046-9667-c40cf72e419d

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

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch rd_fix_window_join_prevailing2
📝 Coding Plan
  • Generate coding plan for human review comments

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.

@RaphDal RaphDal requested a review from puzpuzpuz March 12, 2026 10:10
@puzpuzpuz puzpuzpuz added Bug Incorrect or unexpected behavior SQL Issues or changes relating to SQL execution labels Mar 13, 2026
@puzpuzpuz puzpuzpuz changed the title fix(sql): Fix WINDOW JOIN dropping prevailing row on cross-partition fix(sql): WINDOW JOIN dropping prevailing row on cross-partition Mar 13, 2026
@bluestreak01 bluestreak01 merged commit ae67e0b into master Mar 13, 2026
5 of 6 checks passed
@bluestreak01 bluestreak01 deleted the rd_fix_window_join_prevailing2 branch March 13, 2026 13:26
@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 0 / 0 (0%)

maciulis pushed a commit to maciulis/questdb that referenced this pull request Mar 16, 2026
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.

4 participants