Conversation
CreateSortedView sorted a defensive copy from getTotalItemsArray() but still called setLevelZeroSorted(true) on the heap sketch. Serialization then claimed level-0 was sorted, so heapify/wrap skipped sorting and returned a corrupted sorted view. Do not set the flag when only the copy was sorted. Doubles are unaffected because getDoubleItemsArray() returns the live array. Fixes apache#756.
leerho
approved these changes
Sep 17, 2026
leerho
left a comment
Member
There was a problem hiding this comment.
Thank you for finding and fixing this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #756.
Problem
Querying a heap
KllItemsSketchbefore serializing it makesheapify/wrapreturn a corrupted sorted view (wrong quantiles, duplicated min/max, wrong retained count in the view).Root cause
KllItemsSketch.CreateSortedView.getSV()sorts level-0 and then records that it did:For the heap Items variant,
KllHeapItemsSketch.getTotalItemsArray()returns a defensive copy, so the sort lands on the copy whilesetLevelZeroSorted(true)is set on the live sketch. Serialization writes that flag;heapify/wraptrust it and skip sorting.The Doubles path is fine because
KllHeapDoublesSketch.getDoubleItemsArray()returns the live array, which is what makes the comment atKllDoublesSketchCreateSortedView true.Fix
Minimal change: do not call
setLevelZeroSorted(true)in ItemsCreateSortedView. Only a copy was sorted, so the live level-0 remains unsorted and the serialized flag must stay false. An alternative would be returning the live array fromgetTotalItemsArray()to match Doubles; this PR prefers not claiming the live sketch was sorted when it was not.Tests
Added regression tests in
KllItemsSketchSerDeTest:heapify— sorted view quantiles/weights and probed ranks matchwrap— samek=8,n=8) — heapify and wrap matchAlso asserts
isLevelZeroSorted()stays false after the query on the heap sketch.AI disclosure
I used Cursor / Grok to help draft the patch and open this PR via the GitHub API (no local clone). I verified the root cause against the sources cited in #756 before editing.