Skip to content

Commit e532b7f

Browse files
committed
fix: respect negative values on deserialization in REQ sketch
1 parent db0cf79 commit e532b7f

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/main/java/org/apache/datasketches/req/ReqSerDe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static final Compactor extractCompactor(final PositionalSegment posSeg, final bo
205205
final float[] arr = new float[count];
206206
posSeg.getFloatArray(arr, 0, count);
207207
float minItem = Float.MAX_VALUE;
208-
float maxItem = Float.MIN_VALUE;
208+
float maxItem = -Float.MAX_VALUE;
209209
for (int i = 0; i < count; i++) {
210210
minItem = min(minItem, arr[i]);
211211
maxItem = max(maxItem, arr[i]);

src/test/java/org/apache/datasketches/req/ReqCompactorTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,47 @@ private static void checkSerDeImpl(final int k, final boolean hra) {
101101
assertEquals(fbuf2.getDelta(), delta);
102102
assertTrue(fbuf.isEqualTo(fbuf2));
103103
}
104+
105+
@Test
106+
public void checkSerDeWithNegativeValues() {
107+
checkSerDeNegativeImpl(12, false);
108+
checkSerDeNegativeImpl(12, true);
109+
}
110+
111+
private static void checkSerDeNegativeImpl(final int k, final boolean hra) {
112+
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
113+
final int nomCap = 2 * 3 * k;
114+
final FloatBuffer fbuf = c1.getBuffer();
115+
116+
for (int i = 1; i <= nomCap; i++) {
117+
fbuf.append(-i); //all negative values
118+
}
119+
final byte[] c1ser = c1.toByteArray();
120+
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
121+
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, fbuf.isSorted(), hra);
122+
assertEquals(compactor.minItem, -nomCap);
123+
assertEquals(compactor.maxItem, -1f);
124+
}
125+
126+
@Test
127+
public void checkSerDeWithMixedValues() {
128+
checkSerDeMixedImpl(12, false);
129+
checkSerDeMixedImpl(12, true);
130+
}
131+
132+
private static void checkSerDeMixedImpl(final int k, final boolean hra) {
133+
final ReqCompactor c1 = new ReqCompactor((byte)0, hra, k, null);
134+
final int nomCap = 2 * 3 * k;
135+
final int half = nomCap / 2;
136+
final FloatBuffer fbuf = c1.getBuffer();
137+
138+
for (int i = 0; i < nomCap; i++) {
139+
fbuf.append(i - half); // range: -half to half-1
140+
}
141+
final byte[] c1ser = c1.toByteArray();
142+
final PositionalSegment posSeg = PositionalSegment.wrap(MemorySegment.ofArray(c1ser));
143+
final Compactor compactor = ReqSerDe.extractCompactor(posSeg, fbuf.isSorted(), hra);
144+
assertEquals(compactor.minItem, (float) -half);
145+
assertEquals(compactor.maxItem, (float) (half - 1));
146+
}
104147
}

src/test/java/org/apache/datasketches/req/ReqSketchCrossLanguageTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ public void generateBinariesForCompatibilityTesting() throws IOException {
5151
}
5252
}
5353

54+
@Test(groups = {GENERATE_JAVA_FILES})
55+
public void generateNegativeBinariesForCompatibilityTesting() throws IOException {
56+
final int[] nArr = {1, 10};
57+
for (final int n: nArr) {
58+
final ReqSketch sk = ReqSketch.builder().build();
59+
for (int i = 1; i <= n; i++) {
60+
sk.update(-i);
61+
}
62+
putBytesToJavaPath("req_float_negative_n" + n + "_java.sk", sk.toByteArray());
63+
}
64+
}
65+
66+
@Test(groups = {GENERATE_JAVA_FILES})
67+
public void generateMixedBinariesForCompatibilityTesting() throws IOException {
68+
final int[] nArr = {1, 10};
69+
for (final int n: nArr) {
70+
final ReqSketch sk = ReqSketch.builder().build();
71+
for (int i = -n; i <= n; i++) {
72+
sk.update(i);
73+
}
74+
putBytesToJavaPath("req_float_mixed_n" + n + "_java.sk", sk.toByteArray());
75+
}
76+
}
77+
5478
@Test(groups = {CHECK_CPP_FILES})
5579
public void deserializeFromCpp() throws IOException {
5680
final int[] nArr = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};

0 commit comments

Comments
 (0)