|
26 | 26 | from apache_beam.testing.util import assert_that |
27 | 27 | from apache_beam.testing.util import equal_to |
28 | 28 | from apache_beam.typehints import row_type |
| 29 | +from apache_beam.typehints import schemas |
29 | 30 |
|
30 | 31 |
|
31 | 32 | class RowTypeTest(unittest.TestCase): |
@@ -85,6 +86,94 @@ def generate(num: int): |
85 | 86 | | 'Count Elements' >> beam.Map(self._check_key_type_and_count)) |
86 | 87 | assert_that(result, equal_to([10] * 100)) |
87 | 88 |
|
| 89 | + def test_group_by_key_namedtuple_union(self): |
| 90 | + Tuple1 = typing.NamedTuple("Tuple1", [("id", int)]) |
| 91 | + |
| 92 | + Tuple2 = typing.NamedTuple("Tuple2", [("id", int), ("name", str)]) |
| 93 | + |
| 94 | + def generate(num: int): |
| 95 | + for i in range(2): |
| 96 | + yield (Tuple1(i), num) |
| 97 | + yield (Tuple2(i, 'a'), num) |
| 98 | + |
| 99 | + pipeline = TestPipeline(is_integration_test=False) |
| 100 | + |
| 101 | + with pipeline as p: |
| 102 | + result = ( |
| 103 | + p |
| 104 | + | 'Create' >> beam.Create([i for i in range(2)]) |
| 105 | + | 'Generate' >> beam.ParDo(generate).with_output_types( |
| 106 | + tuple[(Tuple1 | Tuple2), int]) |
| 107 | + | 'GBK' >> beam.GroupByKey() |
| 108 | + | 'Count' >> beam.Map(lambda x: len(x[1]))) |
| 109 | + assert_that(result, equal_to([2] * 4)) |
| 110 | + |
| 111 | + # Union of dataclasses as type hint currently result in FastPrimitiveCoder |
| 112 | + # fails at GBK |
| 113 | + @unittest.skip("https://github.com/apache/beam/issues/22085") |
| 114 | + def test_group_by_key_inherited_dataclass_union(self): |
| 115 | + @dataclass |
| 116 | + class DataClassInt: |
| 117 | + id: int |
| 118 | + |
| 119 | + @dataclass |
| 120 | + class DataClassStr(DataClassInt): |
| 121 | + name: str |
| 122 | + |
| 123 | + beam.coders.typecoders.registry.register_coder( |
| 124 | + DataClassInt, beam.coders.RowCoder) |
| 125 | + beam.coders.typecoders.registry.register_coder( |
| 126 | + DataClassStr, beam.coders.RowCoder) |
| 127 | + |
| 128 | + def generate(num: int): |
| 129 | + for i in range(10): |
| 130 | + yield (DataClassInt(i), num) |
| 131 | + yield (DataClassStr(i, 'a'), num) |
| 132 | + |
| 133 | + pipeline = TestPipeline(is_integration_test=False) |
| 134 | + |
| 135 | + with pipeline as p: |
| 136 | + result = ( |
| 137 | + p |
| 138 | + | 'Create' >> beam.Create([i for i in range(2)]) |
| 139 | + | 'Generate' >> beam.ParDo(generate).with_output_types( |
| 140 | + tuple[(DataClassInt | DataClassStr), int]) |
| 141 | + | 'GBK' >> beam.GroupByKey() |
| 142 | + | 'Count Elements' >> beam.Map(self._check_key_type_and_count)) |
| 143 | + assert_that(result, equal_to([2] * 4)) |
| 144 | + |
| 145 | + def test_derived_dataclass_schema_id(self): |
| 146 | + @dataclass |
| 147 | + class BaseDataClass: |
| 148 | + id: int |
| 149 | + |
| 150 | + @dataclass |
| 151 | + class DerivedDataClass(BaseDataClass): |
| 152 | + name: str |
| 153 | + |
| 154 | + self.assertFalse(hasattr(BaseDataClass, row_type._BEAM_SCHEMA_ID)) |
| 155 | + schema_for_base = schemas.schema_from_element_type(BaseDataClass) |
| 156 | + self.assertTrue(hasattr(BaseDataClass, row_type._BEAM_SCHEMA_ID)) |
| 157 | + self.assertEqual( |
| 158 | + schema_for_base.id, getattr(BaseDataClass, row_type._BEAM_SCHEMA_ID)) |
| 159 | + |
| 160 | + # Getting the schema for BaseDataClass sets the _beam_schema_id |
| 161 | + schemas.typing_to_runner_api( |
| 162 | + BaseDataClass, schema_registry=schemas.SchemaTypeRegistry()) |
| 163 | + |
| 164 | + # We create a RowTypeConstraint from DerivedDataClass. |
| 165 | + # It should not inherit the _beam_schema_id from BaseDataClass! |
| 166 | + derived_row_type = row_type.RowTypeConstraint.from_user_type( |
| 167 | + DerivedDataClass) |
| 168 | + self.assertIsNone(derived_row_type._schema_id) |
| 169 | + |
| 170 | + schema_for_derived = schemas.schema_from_element_type(DerivedDataClass) |
| 171 | + self.assertTrue(hasattr(DerivedDataClass, row_type._BEAM_SCHEMA_ID)) |
| 172 | + self.assertEqual( |
| 173 | + schema_for_derived.id, |
| 174 | + getattr(DerivedDataClass, row_type._BEAM_SCHEMA_ID)) |
| 175 | + self.assertNotEqual(schema_for_derived.id, schema_for_base.id) |
| 176 | + |
88 | 177 |
|
89 | 178 | if __name__ == '__main__': |
90 | 179 | unittest.main() |
0 commit comments