Skip to content

Commit 4362127

Browse files
authored
Merge pull request #486 from apache/fix_issue_484
Fix Issue 484.
2 parents 4d43fc3 + 62721f7 commit 4362127

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
lines changed

src/main/java/org/apache/datasketches/kll/KllDirectCompactItemsSketch.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.reflect.Array;
3333
import java.util.Comparator;
3434

35+
import org.apache.datasketches.common.ArrayOfBooleansSerDe;
3536
import org.apache.datasketches.common.ArrayOfItemsSerDe;
3637
import org.apache.datasketches.common.SketchesArgumentException;
3738
import org.apache.datasketches.memory.Memory;
@@ -154,6 +155,7 @@ byte[] getMinMaxByteArr() { //this is only used by COMPACT_FULL
154155
@Override
155156
int getMinMaxSizeBytes() { //this is only used by COMPACT_FULL
156157
final int offset = DATA_START_ADR + getNumLevels() * Integer.BYTES;
158+
if (serDe instanceof ArrayOfBooleansSerDe) { return 2; }
157159
return serDe.sizeOf(mem, offset, 2);
158160
}
159161

src/main/java/org/apache/datasketches/kll/KllHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,13 @@ static byte[] toByteArray(final KllSketch srcSk, final boolean updatable) {
432432
final byte m = (byte) srcSk.getM();
433433

434434
//load first 8 bytes
435-
wbuf.putByte(preInts);
435+
wbuf.putByte(preInts); //byte 0
436436
wbuf.putByte(serVer);
437437
wbuf.putByte(famId);
438438
wbuf.putByte(flags);
439439
wbuf.putShort(k);
440440
wbuf.putByte(m);
441-
wbuf.incrementPosition(1);
441+
wbuf.incrementPosition(1); //byte 7 is unused
442442

443443
if (tgtStructure == COMPACT_EMPTY) {
444444
return bytesOut;

src/main/java/org/apache/datasketches/kll/KllMemoryValidate.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.apache.datasketches.kll.KllSketch.SketchType.FLOATS_SKETCH;
3737
import static org.apache.datasketches.kll.KllSketch.SketchType.ITEMS_SKETCH;
3838

39+
import org.apache.datasketches.common.ArrayOfBooleansSerDe;
3940
import org.apache.datasketches.common.ArrayOfItemsSerDe;
4041
import org.apache.datasketches.common.Family;
4142
import org.apache.datasketches.common.SketchesArgumentException;
@@ -175,7 +176,11 @@ static int computeSketchBytes( //for COMPACT_FULL or UPDATABLE only
175176

176177
int offsetBytes = DATA_START_ADR + levelsLen * Integer.BYTES;
177178
if (sketchType == ITEMS_SKETCH) {
178-
offsetBytes += serDe.sizeOf(srcMem, offsetBytes, numItems + 2); //2 for min & max
179+
if (serDe instanceof ArrayOfBooleansSerDe) {
180+
offsetBytes += serDe.sizeOf(srcMem, offsetBytes, numItems) + 2; //2 for min & max
181+
} else {
182+
offsetBytes += serDe.sizeOf(srcMem, offsetBytes, numItems + 2); //2 for min & max
183+
}
179184
} else {
180185
final int typeBytes = sketchType.getBytes();
181186
offsetBytes += (numItems + 2) * typeBytes; //2 for min & max

src/test/java/org/apache/datasketches/common/ArrayOfXSerDeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void checkBooleanItems() {
3434
WritableMemory wmem;
3535
ArrayOfBooleansSerDe serDe = new ArrayOfBooleansSerDe();
3636

37-
Boolean[] items = {true,false,true,true,false,false};
37+
Boolean[] items = {true,false,true,false,true,false,true,false,true,false};
3838
bytes = serDe.sizeOf(items);
3939
byteArr = serDe.serializeToByteArray(items);
4040
assertEquals(byteArr.length, bytes);

src/test/java/org/apache/datasketches/kll/KllMiscItemsTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Comparator;
2929

30+
import org.apache.datasketches.common.ArrayOfBooleansSerDe;
3031
import org.apache.datasketches.common.ArrayOfStringsSerDe;
3132
import org.apache.datasketches.common.SketchesArgumentException;
3233
import org.apache.datasketches.common.Util;
@@ -437,6 +438,33 @@ public void checkGetSingleItem() {
437438
assertEquals(skCompact.getSingleItem(), "1");
438439
}
439440

441+
@Test
442+
public void checkIssue484() {
443+
Boolean[] items = { true,false,true,false,true,false,true,false,true,false };
444+
KllItemsSketch<Boolean> sketch = KllItemsSketch.newHeapInstance(Boolean::compareTo, new ArrayOfBooleansSerDe());
445+
for (int i = 0; i < items.length; i++) { sketch.update(items[i]); }
446+
byte[] serialized = sketch.toByteArray();
447+
KllItemsSketch<Boolean> deserialized =
448+
KllItemsSketch.wrap(Memory.wrap(serialized), Boolean::compareTo, new ArrayOfBooleansSerDe());
449+
checkSketchesEqual(sketch, deserialized);
450+
}
451+
452+
private static <T> void checkSketchesEqual(KllItemsSketch<T> expected, KllItemsSketch<T> actual) {
453+
KllItemsSketchSortedView<T> expSV = expected.getSortedView();
454+
KllItemsSketchSortedView<T> actSV = actual.getSortedView();
455+
int N = (int)actSV.getN();
456+
long[] expCumWts = expSV.getCumulativeWeights();
457+
Boolean[] expItemsArr = (Boolean[])expSV.getQuantiles();
458+
long[] actCumWts = actSV.getCumulativeWeights();
459+
Boolean[] actItemsArr = (Boolean[])actSV.getQuantiles();
460+
printf("%3s %8s %8s\n", "i","Actual", "Expected");
461+
for (int i = 0; i < N; i++) {
462+
printf("%3d %8s %8s\n", i, actItemsArr[i].toString(), expItemsArr[i].toString());
463+
}
464+
assertEquals(actCumWts, expCumWts);
465+
assertEquals(actItemsArr, expItemsArr);
466+
}
467+
440468
private final static boolean enablePrinting = false;
441469

442470
/**

src/test/java/org/apache/datasketches/quantiles/ItemsSketchTest.java

+41-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.Comparator;
3232

33+
import org.apache.datasketches.common.ArrayOfBooleansSerDe;
3334
import org.apache.datasketches.common.ArrayOfDoublesSerDe;
3435
import org.apache.datasketches.common.ArrayOfItemsSerDe;
3536
import org.apache.datasketches.common.ArrayOfLongsSerDe;
@@ -647,16 +648,53 @@ public void getQuantiles() {
647648
assertEquals(quantiles1, quantiles2);
648649
}
649650

651+
@Test
652+
public void checkIssue484() {
653+
Boolean[] items = { true,false,true,false,true,false,true,false,true,false };
654+
ItemsSketch<Boolean> sketch = ItemsSketch.getInstance(Boolean.class, Boolean::compareTo);
655+
for (int i = 0; i < items.length; i++) { sketch.update(items[i]); }
656+
byte[] serialized = sketch.toByteArray(new ArrayOfBooleansSerDe());
657+
ItemsSketch<Boolean> deserialized =
658+
ItemsSketch.getInstance(Boolean.class, Memory.wrap(serialized), Boolean::compareTo, new ArrayOfBooleansSerDe());
659+
checkSketchesEqual(sketch, deserialized);
660+
}
661+
662+
private static <T> void checkSketchesEqual(ItemsSketch<T> expected, ItemsSketch<T> actual) {
663+
ItemsSketchSortedView<T> expSV = expected.getSortedView();
664+
ItemsSketchSortedView<T> actSV = actual.getSortedView();
665+
int N = (int)actSV.getN();
666+
long[] expCumWts = expSV.getCumulativeWeights();
667+
Boolean[] expItemsArr = (Boolean[])expSV.getQuantiles();
668+
long[] actCumWts = actSV.getCumulativeWeights();
669+
Boolean[] actItemsArr = (Boolean[])actSV.getQuantiles();
670+
printf("%3s %8s %8s\n", "i","Actual", "Expected");
671+
for (int i = 0; i < N; i++) {
672+
printf("%3d %8s %8s\n", i, actItemsArr[i].toString(), expItemsArr[i].toString());
673+
}
674+
assertEquals(actCumWts, expCumWts);
675+
assertEquals(actItemsArr, expItemsArr);
676+
}
677+
650678
@Test
651679
public void printlnTest() {
652680
println("PRINTING: "+this.getClass().getName());
653681
}
654682

683+
private final static boolean enablePrinting = false;
684+
685+
/**
686+
* @param format the format
687+
* @param args the args
688+
*/
689+
private static final void printf(final String format, final Object ...args) {
690+
if (enablePrinting) { System.out.printf(format, args); }
691+
}
692+
655693
/**
656-
* @param s value to print
694+
* @param o the Object to println
657695
*/
658-
static void println(final String s) {
659-
//System.out.println(s); //disable here
696+
private static final void println(final Object o) {
697+
if (enablePrinting) { System.out.println(o.toString()); }
660698
}
661699

662700
}

0 commit comments

Comments
 (0)