-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix GroupBy snippet tests for issue #30778 #37672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4ea0131
cd0a09d
d9dec2e
bc469b3
4648f05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,22 +43,23 @@ | |
| beam.Row(recipe='pie', fruit='blueberry', quantity=1, unit_price=2.00), | ||
| beam.Row(recipe='muffin', fruit='blueberry', quantity=2, unit_price=2.00), | ||
| beam.Row(recipe='muffin', fruit='banana', quantity=3, unit_price=1.00), | ||
| beam.Row(recipe='pie', fruit='strawberry', quantity=3, unit_price=1.50), | ||
| ] | ||
| # [END groupby_table] | ||
|
|
||
|
|
||
| def groupby_attr_expr(test=None): | ||
| with beam.Pipeline() as p: | ||
| # [START groupby_attr_expr] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not remove these attributes |
||
| grouped = ( | ||
| p | ||
| | beam.Create(GROCERY_LIST) | ||
| | beam.GroupBy('recipe', is_berry=lambda x: 'berry' in x.fruit) | ||
| | beam.Map(print)) | ||
| # [END groupby_attr_expr] | ||
| ) | ||
|
|
||
| if test: | ||
| test(grouped) | ||
| if test: | ||
| test(grouped) | ||
|
|
||
| grouped | beam.Map(print) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,27 +36,49 @@ | |
|
|
||
| import apache_beam as beam | ||
|
|
||
| # [START groupby_table] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not remove this either |
||
| GROCERY_LIST = [ | ||
| beam.Row(recipe='pie', fruit='raspberry', quantity=1, unit_price=3.50), | ||
| beam.Row(recipe='pie', fruit='blackberry', quantity=1, unit_price=4.00), | ||
| beam.Row(recipe='pie', fruit='blueberry', quantity=1, unit_price=2.00), | ||
| beam.Row(recipe='muffin', fruit='blueberry', quantity=2, unit_price=2.00), | ||
| beam.Row(recipe='muffin', fruit='banana', quantity=3, unit_price=1.00), | ||
| beam.Row(recipe='pie', fruit='strawberry', quantity=3, unit_price=1.50), | ||
| ] | ||
| # [END groupby_table] | ||
|
|
||
|
|
||
| def simple_aggregate(test=None): | ||
| def to_grocery_row(x): | ||
| # If it's already a Beam Row / schema object, keep it | ||
| if hasattr(x, 'recipe') and hasattr(x, 'fruit') and hasattr( | ||
| x, 'quantity') and hasattr(x, 'unit_price'): | ||
| return beam.Row( | ||
| recipe=x.recipe, | ||
| fruit=x.fruit, | ||
| quantity=x.quantity, | ||
| unit_price=x.unit_price) | ||
|
|
||
| # If dict | ||
| if isinstance(x, dict): | ||
| return beam.Row( | ||
| recipe=x['recipe'], | ||
| fruit=x['fruit'], | ||
| quantity=x['quantity'], | ||
| unit_price=x['unit_price'], | ||
| ) | ||
|
|
||
| # If tuple/list (recipe, fruit, quantity, unit_price) | ||
| return beam.Row(recipe=x[0], fruit=x[1], quantity=x[2], unit_price=x[3]) | ||
|
|
||
| with beam.Pipeline() as p: | ||
| # [START simple_aggregate] | ||
| grouped = ( | ||
| p | ||
| | beam.Create(GROCERY_LIST) | ||
| | 'ToGroceryRows' >> beam.Map(to_grocery_row) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above, this is redundant, I don't think we need it |
||
| | beam.GroupBy('fruit').aggregate_field( | ||
| 'quantity', sum, 'total_quantity') | ||
| | beam.Map(print)) | ||
| 'quantity', sum, 'total_quantity')) | ||
| # [END simple_aggregate] | ||
|
|
||
| if test: | ||
| test(grouped) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we've fixed this, we should
beam/sdks/python/apache_beam/examples/snippets/transforms/aggregation/groupby_test.py
Line 44 in 4fe6173