Skip to content

chore: DH-24543: Generate MegaMerge kernels with JavaPoet - #9319

Open
cpwright wants to merge 4 commits into
deephaven:mainfrom
cpwright:engine/cpw/DH-24543-megamerge-javapoet
Open

cpwright wants to merge 4 commits into
deephaven:mainfrom
cpwright:engine/cpw/DH-24543-megamerge-javapoet

Conversation

@cpwright

@cpwright cpwright commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Jira: DH-24543

Summary

The MegaMerge kernels are now generated with JavaPoet. Until now, ReplicateSortKernel produced them by replicating the text of the hand-written CharLongMegaMergeKernel and rewriting comparisons with regexes for the descending variants. A second commit fixes a truncating long-to-int cast in the merge loop.

Commit 1: generate the kernels with JavaPoet

  • Adds GenerateMegaMergeKernels to replication/reflective. It is registered as generateMegaMergeKernels and runs under replicateAll. It emits all 16 kernels (char, byte, short, int, long, float, double and Object; ascending and descending) into engine/table/src/replicated/generateMegaMergeKernels/.
  • It reuses the per-type tables from GenerateTimsortKernels (chunk families, the *Comparisons classes, and the direction-aware comparison expressions). The merge kernels and the timsort kernels whose output they merge now share one definition of ordering. Those KeyKind constants were private and are now package-private.
  • Deletes the CharLongMegaMergeKernel template from src/main and the MegaMerge replication from ReplicateSortKernel.

The generated kernels are behaviorally identical to the replicated ones. I stripped comments, imports, annotations and whitespace from all 16 old and new kernels and compared their members as sets: they match token for token. The only difference is ObjectArraySource<Object> in place of the raw type. Re-running generateMegaMergeKernels, generateTimsortKernels and replicateSortKernel reproduces the checked-in sources exactly.

Commit 2: fix the truncated merge-start comparison

The destination-wins branch of merge tested --destCursor < (int) mergeStartPosition. Both are long positions. For a merge start beyond 2^31, the cast produces a smaller value, so the loop runs past the merge start instead of breaking, and it reads the value just before the merge start.

With SortHelpers, which merges from offset 0, every such value is less than or equal to the chunk's smallest value. The extra iterations therefore always take from the chunk, and the result is still correct. A merge from a non-zero offset beyond 2^31, though, could copy a value from outside the destination run into the output.

TestMegaMergeBeyondIntegerMaxValue merges into a one-row destination run at (1L << 31) + 16, preceded by a value outside the run, in both directions. It checks the merged values, the row keys, and that the preceding row is unchanged. With the cast restored, both cases fail: the preceding value is merged into the run and overwritten. The test-only SparseArraySourceBlocks fixture sets up the sources so that only the block containing the run is allocated; the cost is the block-reference array, not the 16 GB that 2^31 dense rows of long row keys would take.

Layout of the generated code

The generator leaves code layout to JavaPoet: no hand-placed blank lines or line breaks, matching GenerateTimsortKernels. JavaPoet does not wrap javadoc, so the new ReplicationUtils.wrapJavadoc wraps it to 120 columns.

Testing

  • ./gradlew :engine-table:test --tests "io.deephaven.engine.table.impl.sort.megamerge.*": 35 tests, all pass, including the two new beyond-Integer.MAX_VALUE cases.
  • ./gradlew :engine-table:testOutOfBand --tests "*.QueryTableSortTest" --tests "*.QueryTableHugeSortTest": 35 tests, all pass.
  • spotlessCheck on :replication-reflective, :replication-static and :engine-table.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HgjU6BT2fMBP5buTa48oXP

@semanticdiff-com

Copy link
Copy Markdown

Review changes with  SemanticDiff

@cpwright
cpwright requested a balanced review from Copilot November 27, 2026 19:36
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

No docs changes detected for 480687b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A regression above Integer.MAX_VALUE is still needed to guard against cast-induced corruption, checking merged values, row keys, and an unchanged prefix in both sort directions.

1 open issue
What changed in this PR

Replaces text-based MegaMerge replication with JavaPoet generation, sharing type and ordering definitions with the engine’s timsort generator.

Changes:

  • Adds generateMegaMergeKernels to replicateAll and removes legacy MegaMerge replication.
  • Regenerates all 16 type/direction variants using shared comparison definitions.
  • Removes the narrowing cast from merge-start checks to preserve bounds beyond Integer.MAX_VALUE.
File Description
ReplicateSortKernel.java Removes legacy MegaMerge replication and fixups.
GenerateTimsortKernels.java Exposes type descriptors for generator reuse.
GenerateMegaMergeKernels.java Generates all kernels with corrected long-position checks.
replication/​reflective/​build.gradle Registers the new generation task.
ShortLongMegaMergeKernel.java Regenerates ascending short merges with corrected bounds.
ShortLongMegaMergeDescendingKernel.java Regenerates descending short merges with corrected bounds.
ObjectLongMegaMergeKernel.java Regenerates ascending Object merges; parameterizes the source and corrects bounds.
ObjectLongMegaMergeDescendingKernel.java Regenerates descending Object merges; parameterizes the source and corrects bounds.
LongLongMegaMergeKernel.java Regenerates ascending long merges with corrected bounds.
LongLongMegaMergeDescendingKernel.java Regenerates descending long merges with corrected bounds.
IntLongMegaMergeKernel.java Regenerates ascending int merges with corrected bounds.
IntLongMegaMergeDescendingKernel.java Regenerates descending int merges with corrected bounds.
FloatLongMegaMergeKernel.java Regenerates ascending float merges with corrected bounds.
FloatLongMegaMergeDescendingKernel.java Regenerates descending float merges with corrected bounds.
DoubleLongMegaMergeKernel.java Regenerates ascending double merges with corrected bounds.
DoubleLongMegaMergeDescendingKernel.java Regenerates descending double merges with corrected bounds.
CharLongMegaMergeKernel.java Replaces the handwritten template with generated code and corrected bounds.
CharLongMegaMergeDescendingKernel.java Regenerates descending char merges with corrected bounds.
ByteLongMegaMergeKernel.java Regenerates ascending byte merges with corrected bounds.
ByteLongMegaMergeDescendingKernel.java Regenerates descending byte merges with corrected bounds.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review reports no active issues with the MegaMerge generation modernization and large-offset merge fix.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cpwright
cpwright marked this pull request as ready for review November 27, 2026 20:17
@cpwright
cpwright requested a review from lbooker42 November 27, 2026 20:18
Copilot AI balanced review requested due to automatic review settings November 27, 2026 21:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the MegaMerge generation and large-offset merge-boundary changes.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants