Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sdks/python/apache_beam/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,10 @@ def apply(
self._infer_result_type(transform, tuple(inputs.values()), result)

assert isinstance(result.producer.inputs, tuple)
# The DoOutputsTuple adds the PCollection to the outputs when accessed
# except for the main tag. Add the main tag here.
if isinstance(result, pvalue.DoOutputsTuple):
current.add_output(result, result._main_tag)
for tag, pc in list(result._pcolls.items()):
if tag not in current.outputs:
current.add_output(pc, tag)
continue

# If there is already a tag with the same name, increase a counter for
Expand Down
53 changes: 53 additions & 0 deletions sdks/python/apache_beam/pipeline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,59 @@ def file_artifact(path, hash, staged_name):

self.assertEqual(len(proto.components.environments), 6)

def test_multiple_outputs_composite_ptransform(self):
"""
Test that a composite PTransform with multiple outputs is represented
correctly in the pipeline proto.
"""
class SalesSplitter(beam.DoFn):
def process(self, element):
price = element['price']
if price > 100:
yield beam.pvalue.TaggedOutput('premium_sales', element)
else:
yield beam.pvalue.TaggedOutput('standard_sales', element)

class ParentSalesSplitter(beam.PTransform):
def expand(self, pcoll):
return pcoll | beam.ParDo(SalesSplitter()).with_outputs(
'premium_sales', 'standard_sales')

sales_data = [
{
'item': 'Laptop', 'price': 1200
},
{
'item': 'Mouse', 'price': 25
},
{
'item': 'Keyboard', 'price': 75
},
{
'item': 'Monitor', 'price': 350
},
{
'item': 'Headphones', 'price': 90
},
]

with beam.Pipeline() as pipeline:
sales_records = pipeline | 'Create Sales' >> beam.Create(sales_data)
_ = sales_records | 'Split Sales' >> ParentSalesSplitter()
current_transforms = list(pipeline.transforms_stack)
all_applied_transforms = {
xform.full_label: xform
for xform in current_transforms
}
while current_transforms:
xform = current_transforms.pop()
all_applied_transforms[xform.full_label] = xform
current_transforms.extend(xform.parts)
xform = all_applied_transforms['Split Sales']
# Confirm that Split Sales correctly has two outputs as specified by
# ParDo.with_outputs in ParentSalesSplitter.
assert len(xform.outputs) == 2


if __name__ == '__main__':
unittest.main()
Loading