Skip to content

Commit 3b32df2

Browse files
committed
fix: Deserialized TDigest NaN checks (prestodb#25907)
Summary: Pull Request resolved: prestodb#25907 Reviewed By: Yuhta Differential Revision: D81173686 Privacy Context Container: L1202870
1 parent ecf1344 commit 3b32df2

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

presto-main-base/src/main/java/com/facebook/presto/tdigest/TDigest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ public static TDigest createTDigest(Slice slice)
203203
r.mean = new double[r.activeCentroids];
204204
sliceInput.readBytes(wrappedDoubleArray(r.weight), r.activeCentroids * SIZE_OF_DOUBLE);
205205
sliceInput.readBytes(wrappedDoubleArray(r.mean), r.activeCentroids * SIZE_OF_DOUBLE);
206+
207+
// Validate deserialized TDigest data
208+
for (int i = 0; i < r.activeCentroids; i++) {
209+
checkArgument(!isNaN(r.mean[i]), "Deserialized t-digest contains NaN mean value");
210+
checkArgument(r.weight[i] > 0, "weight must be > 0");
211+
}
212+
206213
sliceInput.close();
207214
return r;
208215
}
@@ -712,6 +719,12 @@ public long estimatedInMemorySizeInBytes()
712719

713720
public Slice serialize()
714721
{
722+
// Validate data before serialization
723+
for (int i = 0; i < activeCentroids; i++) {
724+
checkArgument(!isNaN(mean[i]), "Cannot serialize t-digest with NaN mean value");
725+
checkArgument(weight[i] > 0, "Cannot serialize t-digest with non-positive weight");
726+
}
727+
715728
SliceOutput sliceOutput = new DynamicSliceOutput(toIntExact(estimatedSerializedSizeInBytes()));
716729

717730
sliceOutput.writeByte(1); // version 1 of T-Digest serialization

presto-main-base/src/test/java/com/facebook/presto/tdigest/TestTDigest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ public void testGeometricDistribution()
397397
}
398398
}
399399

400+
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*Cannot serialize t-digest with NaN mean value.*")
401+
public void testSerializationWithNaNMean()
402+
{
403+
double[] means = {1.0, Double.NaN, 3.0};
404+
double[] weights = {1.0, 1.0, 1.0};
405+
TDigest tDigest = createTDigest(means, weights, STANDARD_COMPRESSION_FACTOR, 1.0, 3.0, 6.0, 3);
406+
407+
tDigest.serialize();
408+
}
409+
400410
@Test(enabled = false)
401411
public void testPoissonDistribution()
402412
{

0 commit comments

Comments
 (0)