Repository navigation
[YAML] Fix SQL-backed Combine generating invalid syntax when group_by is empty or omitted #41049
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64,7 +64,9 @@ def normalize_combine(spec): | |||||
| from apache_beam.yaml.yaml_transform import SafeLineLoader | ||||||
| if spec['type'] == 'Combine': | ||||||
| config = spec.get('config') | ||||||
| if isinstance(config.get('group_by'), str): | ||||||
| if not config.get('group_by'): | ||||||
| config['group_by'] = [] | ||||||
| elif isinstance(config['group_by'], str): | ||||||
| config['group_by'] = [config['group_by']] | ||||||
|
|
||||||
| def normalize_agg(dest, agg): | ||||||
|
|
@@ -103,11 +105,11 @@ class PyJsYamlCombine(beam.PTransform): | |||||
| """ | ||||||
| def __init__( | ||||||
| self, | ||||||
| group_by: Iterable[str], | ||||||
| combine: Mapping[str, Mapping[str, Any]], | ||||||
| group_by: Iterable[str] = (), | ||||||
| combine: Optional[Mapping[str, Mapping[str, Any]]] = None, | ||||||
| language: Optional[str] = None): | ||||||
| self._group_by = group_by | ||||||
| self._combine = combine | ||||||
| self._group_by = tuple(group_by or ()) | ||||||
| self._combine = combine or {} | ||||||
| self._language = language | ||||||
|
|
||||||
| def expand(self, pcoll): | ||||||
|
|
@@ -180,7 +182,12 @@ def extract_return_type(expr): | |||||
|
|
||||||
| @beam.ptransform.ptransform_fn | ||||||
| def _SqlCombineTransform( | ||||||
| pcoll, sql_transform_constructor, group_by, combine, language=None): | ||||||
| pcoll, | ||||||
| sql_transform_constructor, | ||||||
| group_by: Iterable[str] = (), | ||||||
|
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. 🟡 Medium: Similarly here for
Suggested change
|
||||||
| combine: Optional[Mapping[str, Any]] = None, | ||||||
| language: Optional[str] = None): | ||||||
| group_by = list(group_by or []) | ||||||
| all_fields = [ | ||||||
| x for x, _ in named_fields_from_element_type(pcoll.element_type) | ||||||
| ] | ||||||
|
|
@@ -198,14 +205,18 @@ def combine_col(dest, fn_spec): | |||||
| expr = fn_spec['fn']['type'] | ||||||
| return f'{expr} as {dest}' | ||||||
|
|
||||||
| return pcoll | sql_transform_constructor( | ||||||
| 'SELECT %s FROM PCOLLECTION GROUP BY %s' % ( | ||||||
| ', '.join( | ||||||
| list(group_by) + | ||||||
| [combine_col(dest, fn_spec) | ||||||
| for dest, fn_spec in combine.items()]), | ||||||
| ', '.join(group_by), | ||||||
| )) | ||||||
| select_cols = group_by + [ | ||||||
| combine_col(dest, fn_spec) for dest, fn_spec in (combine or {}).items() | ||||||
| ] | ||||||
| if group_by: | ||||||
| query = 'SELECT %s FROM PCOLLECTION GROUP BY %s' % ( | ||||||
| ', '.join(select_cols), | ||||||
| ', '.join(group_by), | ||||||
| ) | ||||||
| else: | ||||||
| query = 'SELECT %s FROM PCOLLECTION' % ', '.join(select_cols) | ||||||
|
|
||||||
| return pcoll | sql_transform_constructor(query) | ||||||
|
|
||||||
|
|
||||||
| def create_combine_providers(): | ||||||
|
|
||||||
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.
🟡 Medium:
InlineProvider.config_schema_from_callablederives config field nullability (used forrequiredinbeam_schema_to_json_schemaand(Optional)ingenerate_yaml_docs.py) directly fromOptional[...]in the type annotations viatyping_to_runner_api, rather than from default values.Annotating
group_byasIterable[str]leavesgroup_bymarked as required (nullable=False) in the generated schema/docs and conflicts with passinggroup_by=None. Annotatinggroup_byasOptional[Iterable[str]] = ()(matching_PyJsMapToFieldsinyaml_mapping.py) and keepingcombinenon-Optional(e.g.,combine: Mapping[str, Mapping[str, Any]] = {} # pylint: disable=dangerous-default-value) ensuresgroup_byis marked optional whilecombineremains required in the config schema.