|
22 | 22 | import static java.lang.Math.ceil;
|
23 | 23 | import static org.apache.datasketches.kll.KllSketch.SketchStructure.*;
|
24 | 24 | import static org.apache.datasketches.kll.KllSketch.SketchType.*;
|
| 25 | +import static org.apache.datasketches.quantilescommon.LongsAsOrderableStrings.getString; |
25 | 26 | import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.EXCLUSIVE;
|
26 | 27 | import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.INCLUSIVE;
|
27 | 28 | import static org.testng.Assert.assertEquals;
|
|
31 | 32 | import static org.testng.Assert.fail;
|
32 | 33 |
|
33 | 34 | import java.util.Comparator;
|
| 35 | +import java.util.Random; |
34 | 36 |
|
35 | 37 | import org.apache.datasketches.common.ArrayOfStringsSerDe;
|
36 | 38 | import org.apache.datasketches.common.SketchesArgumentException;
|
|
42 | 44 | import org.apache.datasketches.quantilescommon.DoublesSortedView;
|
43 | 45 | import org.apache.datasketches.quantilescommon.GenericSortedView;
|
44 | 46 | import org.apache.datasketches.quantilescommon.GenericSortedViewIterator;
|
| 47 | +import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator; |
45 | 48 | import org.testng.annotations.Test;
|
46 | 49 |
|
47 | 50 | @SuppressWarnings("unused")
|
@@ -751,6 +754,34 @@ public void checkSortedViewAfterReset() {
|
751 | 754 | try { sk.getSortedView(); fail(); } catch (SketchesArgumentException e) { }
|
752 | 755 | }
|
753 | 756 |
|
| 757 | + @Test |
| 758 | + //There is no guarantee that L0 is sorted after a merge. |
| 759 | + //The issue is, during a merge, L0 must be sorted prior to a compaction to a higher level. |
| 760 | + //Otherwise the higher levels would not be sorted properly. |
| 761 | + public void checkL0SortDuringMerge() throws NumberFormatException { |
| 762 | + final Random rand = new Random(); |
| 763 | + final KllItemsSketch<String> sk1 = KllItemsSketch.newHeapInstance(8, Comparator.reverseOrder(), serDe); |
| 764 | + final KllItemsSketch<String> sk2 = KllItemsSketch.newHeapInstance(8, Comparator.reverseOrder(), serDe); |
| 765 | + final int n = 26; //don't change this |
| 766 | + for (int i = 1; i <= n; i++ ) { |
| 767 | + final int j = rand.nextInt(n) + 1; |
| 768 | + sk1.update(getString(j, 3)); |
| 769 | + sk2.update(getString(j +100, 3)); |
| 770 | + } |
| 771 | + sk1.merge(sk2); |
| 772 | + println(sk1.toString(true, true)); //L1 and above should be sorted in reverse. Ignore L0. |
| 773 | + final int lvl1size = sk1.levelsArr[2] - sk1.levelsArr[1]; |
| 774 | + final QuantilesGenericSketchIterator<String> itr = sk1.iterator(); |
| 775 | + itr.next(); |
| 776 | + int prev = Integer.parseInt(itr.getQuantile().trim()); |
| 777 | + for (int i = 1; i < lvl1size; i++) { |
| 778 | + if (itr.next()) { |
| 779 | + int v = Integer.parseInt(itr.getQuantile().trim()); |
| 780 | + assertTrue(v <= prev); |
| 781 | + prev = v; |
| 782 | + } |
| 783 | + } |
| 784 | + } |
754 | 785 |
|
755 | 786 | private final static boolean enablePrinting = false;
|
756 | 787 |
|
|
0 commit comments