damccorm commented on code in PR #39921:
URL: https://github.com/apache/beam/pull/39921#discussion_r3893895617
##########
sdks/python/apache_beam/transforms/stats_test.py:
##########
@@ -219,6 +219,40 @@ def
test_approximate_unique_combine_fn_requires_compatible_coder(self):
self.assertRegex(e.exception.args[0], 'Runtime exception')
+ def test_approximate_unique_merge_accumulators_reuses_first(self):
+ sample_size = 16
+ combine_fn = ApproximateUniqueCombineFn(sample_size, coders.VarIntCoder())
+ accumulators = [combine_fn.create_accumulator() for _ in range(3)]
+ for accumulator, values in zip(
+ accumulators, [range(16), range(8, 24), range(24, 40)]):
+ for value in values:
+ accumulator.add(value)
+
+ later_accumulator_states = [(
+ list(accumulator._sample_heap),
+ set(accumulator._sample_set),
+ accumulator._min_hash) for accumulator in accumulators[1:]]
+
+ merged_accumulator = combine_fn.merge_accumulators(iter(accumulators))
+
+ self.assertIs(merged_accumulator, accumulators[0])
+ self.assertEqual(set(range(24, 40)), merged_accumulator._sample_set)
+ self.assertEqual(24, merged_accumulator._min_hash)
+ self.assertEqual(
+ later_accumulator_states,
+ [(
+ list(accumulator._sample_heap),
+ set(accumulator._sample_set),
+ accumulator._min_hash) for accumulator in accumulators[1:]])
Review Comment:
What is this assert checking? Isn't this just running against our test data
that we constructed?
##########
sdks/python/apache_beam/transforms/stats.py:
##########
@@ -262,11 +262,14 @@ def add_input(self, accumulator, element, *args,
**kwargs):
except Exception as e:
raise RuntimeError("Runtime exception: %s" % e)
- # created an issue https://github.com/apache/beam/issues/19459 to speed up
- # merge process.
Review Comment:
While this is an improvement, I'll note that it does not actually address
the core issue in https://github.com/apache/beam/issues/19459
This PR avoids creating an extra accumulator and doing one additional merge
operation, but it doesn't handle the efficient merging of 2 accumulators.
With that said, I don't think a fast merge is possible here because of the
uniqueness constraint, so we can still probably call it fixed
##########
sdks/python/apache_beam/transforms/stats_test.py:
##########
@@ -219,6 +219,40 @@ def
test_approximate_unique_combine_fn_requires_compatible_coder(self):
self.assertRegex(e.exception.args[0], 'Runtime exception')
+ def test_approximate_unique_merge_accumulators_reuses_first(self):
+ sample_size = 16
+ combine_fn = ApproximateUniqueCombineFn(sample_size, coders.VarIntCoder())
+ accumulators = [combine_fn.create_accumulator() for _ in range(3)]
+ for accumulator, values in zip(
+ accumulators, [range(16), range(8, 24), range(24, 40)]):
+ for value in values:
+ accumulator.add(value)
+
+ later_accumulator_states = [(
+ list(accumulator._sample_heap),
+ set(accumulator._sample_set),
+ accumulator._min_hash) for accumulator in accumulators[1:]]
+
+ merged_accumulator = combine_fn.merge_accumulators(iter(accumulators))
+
+ self.assertIs(merged_accumulator, accumulators[0])
Review Comment:
This is testing our specific implementation, not correctness. For example,
it would be equally valid (and maybe better) to use the largest accumulator
available as our starting point.
Let's update this test to just test correctness instead of the specific
behavior we've baked in.
##########
sdks/python/apache_beam/transforms/stats_test.py:
##########
@@ -219,6 +219,40 @@ def
test_approximate_unique_combine_fn_requires_compatible_coder(self):
self.assertRegex(e.exception.args[0], 'Runtime exception')
+ def test_approximate_unique_merge_accumulators_reuses_first(self):
+ sample_size = 16
+ combine_fn = ApproximateUniqueCombineFn(sample_size, coders.VarIntCoder())
+ accumulators = [combine_fn.create_accumulator() for _ in range(3)]
+ for accumulator, values in zip(
+ accumulators, [range(16), range(8, 24), range(24, 40)]):
+ for value in values:
+ accumulator.add(value)
+
+ later_accumulator_states = [(
+ list(accumulator._sample_heap),
+ set(accumulator._sample_set),
+ accumulator._min_hash) for accumulator in accumulators[1:]]
+
+ merged_accumulator = combine_fn.merge_accumulators(iter(accumulators))
+
+ self.assertIs(merged_accumulator, accumulators[0])
+ self.assertEqual(set(range(24, 40)), merged_accumulator._sample_set)
+ self.assertEqual(24, merged_accumulator._min_hash)
Review Comment:
Both of these asserts would succeed if the only accumulator merged was the
last one. Can we update to avoid this? An easy way to do so would be to make
the sample size 30 (and update the asserts)
##########
sdks/python/apache_beam/transforms/stats.py:
##########
@@ -262,11 +262,14 @@ def add_input(self, accumulator, element, *args,
**kwargs):
except Exception as e:
raise RuntimeError("Runtime exception: %s" % e)
- # created an issue https://github.com/apache/beam/issues/19459 to speed up
- # merge process.
def merge_accumulators(self, accumulators, *args, **kwargs):
- merged_accumulator = self.create_accumulator()
- for accumulator in accumulators:
+ accumulator_iter = iter(accumulators)
+ try:
+ merged_accumulator = next(accumulator_iter)
Review Comment:
I mentioned this below, but it would likely be more efficient to find the
largest accumulator and use that as the starting point.
Ideally, we'd look for the accumulator with the largest _sample_heap size.
If 2 are tied, then we'd look for the one with the larger _min_hash
--
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]