Skip to content
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

[YAML] Schema-producing Create. #29030

Merged
merged 2 commits into from
Oct 24, 2023
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
9 changes: 8 additions & 1 deletion sdks/python/apache_beam/yaml/yaml_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ def create_transform(self, type, args, yaml_create_transform):
}


def element_to_rows(e):
if isinstance(e, dict):
return dicts_to_rows(e)
else:
return beam.Row(element=dicts_to_rows(e))


def dicts_to_rows(o):
if isinstance(o, dict):
return beam.Row(**{k: dicts_to_rows(v) for k, v in o.items()})
Expand Down Expand Up @@ -487,7 +494,7 @@ def create(elements: Iterable[Any], reshuffle: bool = True):
reshuffle (optional): Whether to introduce a reshuffle if there is more
than one element in the collection. Defaults to True.
"""
return beam.Create(dicts_to_rows(elements), reshuffle)
return beam.Create([element_to_rows(e) for e in elements], reshuffle)

def with_schema(**args):
# TODO: This is preliminary.
Expand Down
24 changes: 14 additions & 10 deletions sdks/python/apache_beam/yaml/yaml_transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def __init__(self, limit, error_handling):
self._error_handling = error_handling

def expand(self, pcoll):
def raise_on_big(element):
if len(element) > self._limit:
raise ValueError(element)
def raise_on_big(row):
if len(row.element) > self._limit:
raise ValueError(row.element)
else:
return element
return row.element

good, bad = pcoll | beam.Map(raise_on_big).with_exception_handling()
return {'small_elements': good, self._error_handling['output']: bad}
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_implicit_flatten(self):
- type: PyMap
input: [CreateBig, CreateSmall]
config:
fn: "lambda x: x * x"
fn: "lambda x: x.element * x.element"
output: PyMap
''',
providers=TEST_PROVIDERS)
Expand Down Expand Up @@ -263,7 +263,7 @@ def test_name_is_not_ambiguous(self):
- type: PyMap
name: PyMap
config:
fn: "lambda elem: elem * elem"
fn: "lambda row: row.element * row.element"
input: Create
output: PyMap
''',
Expand Down Expand Up @@ -421,11 +421,14 @@ def test_mapping_errors(self):
- type: Create
config:
elements: [0, 1, 2, 4]
- type: PyMap
- type: MapToFields
name: ToRow
input: Create
config:
fn: "lambda x: beam.Row(num=x, str='a' * x or 'bbb')"
language: python
fields:
num: element
str: "'a' * element or 'bbb'"
- type: Filter
input: ToRow
config:
Expand Down Expand Up @@ -585,7 +588,8 @@ class AnnotatingProvider(yaml_provider.InlineProvider):
"""
def __init__(self, name, transform_names):
super().__init__({
transform_name: lambda: beam.Map(lambda x: (x or ()) + (name, ))
transform_name:
lambda: beam.Map(lambda x: (x if type(x) == tuple else ()) + (name, ))
for transform_name in transform_names.strip().split()
})
self._name = name
Expand Down Expand Up @@ -718,7 +722,7 @@ def __init__(self, a, b):
def expand(self, pcoll):
a = self._a
b = self._b
return pcoll | beam.Map(lambda x: a * x + b)
return pcoll | beam.Map(lambda x: a * x.element + b)


if __name__ == '__main__':
Expand Down
Loading