HyukjinKwon commented on code in PR #36342: URL: https://github.com/apache/spark/pull/36342#discussion_r858234670
########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDSampler.func + rdd = self.sc.parallelize(range(20), 2) + sample_count = rdd.mapPartitionsWithIndex(RDDSampler(False, 0.4, 10).func).count() + self.assertGreater(sample_count, 4) + self.assertLess(sample_count, 10) + sample_data = rdd.mapPartitionsWithIndex(RDDSampler(True, 0.4, 10).func).collect() + sample_data.sort() + is_repetition_contain = False + # check if at least one element is repeated. + for index in range(1, len(sample_data)): + if sample_data[index] == sample_data[index - 1]: + is_repetition_contain = True + break + self.assertTrue(is_repetition_contain) + + def test_rdd_stratified_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler Review Comment: ```suggestion # SPARK-38879: Test case to improve test coverage for RDDSampler ``` ########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDSampler.func + rdd = self.sc.parallelize(range(20), 2) + sample_count = rdd.mapPartitionsWithIndex(RDDSampler(False, 0.4, 10).func).count() + self.assertGreater(sample_count, 4) + self.assertLess(sample_count, 10) + sample_data = rdd.mapPartitionsWithIndex(RDDSampler(True, 0.4, 10).func).collect() Review Comment: ```suggestion sample_data = rdd.mapPartitionsWithIndex(RDDSampler(True, 1, 10).func).collect() ``` Maybe make it 1 and make sure it always returns a repeated value? ########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler Review Comment: ```suggestion # SPARK-38879: Test case to improve test coverage for RDDSampler ``` ########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDSampler.func + rdd = self.sc.parallelize(range(20), 2) + sample_count = rdd.mapPartitionsWithIndex(RDDSampler(False, 0.4, 10).func).count() + self.assertGreater(sample_count, 4) + self.assertLess(sample_count, 10) + sample_data = rdd.mapPartitionsWithIndex(RDDSampler(True, 0.4, 10).func).collect() + sample_data.sort() + is_repetition_contain = False + # check if at least one element is repeated. + for index in range(1, len(sample_data)): Review Comment: I think you can reread this part like: ```suggestion self.assertTrue(any( sample_data[i] == sample_data[i - 1] for i in range(1, len(sample_data)))) ``` ########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDSampler.func + rdd = self.sc.parallelize(range(20), 2) + sample_count = rdd.mapPartitionsWithIndex(RDDSampler(False, 0.4, 10).func).count() + self.assertGreater(sample_count, 4) + self.assertLess(sample_count, 10) + sample_data = rdd.mapPartitionsWithIndex(RDDSampler(True, 0.4, 10).func).collect() + sample_data.sort() + is_repetition_contain = False + # check if at least one element is repeated. + for index in range(1, len(sample_data)): + if sample_data[index] == sample_data[index - 1]: + is_repetition_contain = True + break + self.assertTrue(is_repetition_contain) + + def test_rdd_stratified_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDStratifiedSampler.func + + fractions = {"a": 0.8, "b": 0.2} + rdd = self.sc.parallelize(fractions.keys()).cartesian(self.sc.parallelize(range(0, 100))) + sample_data = dict( + rdd.mapPartitionsWithIndex( + RDDStratifiedSampler(False, fractions, 10).func, True + ).countByKey() + ) + self.assertGreater(sample_data["a"], sample_data["b"]) Review Comment: Can you add a couple of comments why we compare this like? ########## python/pyspark/tests/test_rddsampler.py: ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from pyspark.testing.utils import ReusedPySparkTestCase +from pyspark.rddsampler import RDDSampler, RDDStratifiedSampler + + +class RDDSamplerTests(ReusedPySparkTestCase): + def test_rdd_sampler_func(self): + # SPARK-38879:Test case to improve test coverage for RDDSampler + # RDDSampler.func + rdd = self.sc.parallelize(range(20), 2) + sample_count = rdd.mapPartitionsWithIndex(RDDSampler(False, 0.4, 10).func).count() + self.assertGreater(sample_count, 4) Review Comment: Maybe 3 to avoid potential flakiness ```suggestion self.assertGreater(sample_count, 3) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
