From d34e3e9d15d1e736430c9f6ac09569790c5cbdf5 Mon Sep 17 00:00:00 2001 From: sasmith-14 <210313780+sasmith-14@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:51:40 +0530 Subject: [PATCH] Fix JavaReservoirSampleOperator returning the whole input when the dataset size is unknown #884 The reservoir operator short-circuited to "return all" whenever sampleSize >= datasetSize, but datasetSize defaults to UNKNOWN_DATASET_SIZE (0) when the size is not known, so the comparison was always true and the operator returned the entire input instead of a sample. Guard the shortcut with isDataSetSizeKnown(), matching JavaRandomSampleOperator; the streaming reservoir algorithm already handles inputs smaller than the sample size. Fixes #884 --- .../JavaReservoirSampleOperator.java | 2 +- .../JavaReservoirSampleOperatorTest.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/JavaReservoirSampleOperator.java b/wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/JavaReservoirSampleOperator.java index 66730afba..992f906c0 100644 --- a/wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/JavaReservoirSampleOperator.java +++ b/wayang-platforms/wayang-java/src/main/java/org/apache/wayang/java/operators/JavaReservoirSampleOperator.java @@ -81,7 +81,7 @@ public Tuple, Collection> eval int sampleSize = this.getSampleSize(operatorContext); - if (sampleSize >= datasetSize) { //return all + if (this.isDataSetSizeKnown() && sampleSize >= datasetSize) { //return all ((CollectionChannel.Instance) outputs[0]).accept(((JavaChannelInstance) inputs[0]).provideStream().collect(Collectors.toList())); } else { diff --git a/wayang-platforms/wayang-java/src/test/java/org/apache/wayang/java/operators/JavaReservoirSampleOperatorTest.java b/wayang-platforms/wayang-java/src/test/java/org/apache/wayang/java/operators/JavaReservoirSampleOperatorTest.java index 79f93271b..8680c9e14 100644 --- a/wayang-platforms/wayang-java/src/test/java/org/apache/wayang/java/operators/JavaReservoirSampleOperatorTest.java +++ b/wayang-platforms/wayang-java/src/test/java/org/apache/wayang/java/operators/JavaReservoirSampleOperatorTest.java @@ -59,6 +59,33 @@ void testExecution() { assertEquals(sampleSize, result.size()); } + @Test + void testExecutionWithUnknownDatasetSize() { + // Prepare test data. The dataset size is deliberately left unknown + // (setDatasetSize is never called), which is the normal case for a + // reservoir sample over a stream. + Stream inputStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + final int sampleSize = 5; + + // Build the operator. + JavaReservoirSampleOperator sampleOperator = + new JavaReservoirSampleOperator<>( + iterationNumber -> sampleSize, + DataSetType.createDefaultUnchecked(Integer.class), + iterationNumber -> 42L + ); + + JavaChannelInstance[] inputs = new JavaChannelInstance[]{createStreamChannelInstance(inputStream)}; + JavaChannelInstance[] outputs = new JavaChannelInstance[]{createCollectionChannelInstance()}; + + // Execute. + evaluate(sampleOperator, inputs, outputs); + + // Verify the outcome: a sample of the requested size, not the whole input. + final List result = outputs[0].provideStream().collect(Collectors.toList()); + assertEquals(sampleSize, result.size()); + } + @Test void testLargerSampleExecution() { // Prepare test data.