TheNeuralBit commented on code in PR #23455:
URL: https://github.com/apache/beam/pull/23455#discussion_r1007103358


##########
sdks/python/apache_beam/typehints/arrow_batching_microbenchmark.py:
##########
@@ -0,0 +1,73 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import argparse
+import logging
+
+import pyarrow as pa
+
+from apache_beam.portability.api import schema_pb2
+from apache_beam.tools import utils
+from apache_beam.typehints.arrow_type_compatibility import 
PyarrowBatchConverter
+from apache_beam.typehints.arrow_type_compatibility import 
beam_schema_from_arrow_schema
+from apache_beam.typehints.schemas import typing_from_runner_api
+
+
+def benchmark_produce_batch(size):
+  batch = pa.Table.from_pydict({
+      'foo': pa.array(range(size), type=pa.int64()),
+      'bar': pa.array([i / size for i in range(size)], type=pa.float64()),
+      'baz': pa.array([str(i) for i in range(size)], type=pa.string()),
+  })
+  beam_schema = beam_schema_from_arrow_schema(batch.schema)
+  element_type = typing_from_runner_api(
+      schema_pb2.FieldType(row_type=schema_pb2.RowType(schema=beam_schema)))
+
+  batch_converter = PyarrowBatchConverter.from_typehints(element_type, 
pa.Table)
+  elements = list(batch_converter.explode_batch(batch))
+
+  def _do_benchmark():
+    _ = batch_converter.produce_batch(elements)
+
+  return _do_benchmark
+
+
+def run_benchmark(
+    starting_point=1, num_runs=10, num_elements_step=300, verbose=True):
+  suite = [
+      utils.LinearRegressionBenchmarkConfig(
+          benchmark_produce_batch, starting_point, num_elements_step, num_runs)
+  ]
+  return utils.run_benchmarks(suite, verbose=verbose)
+
+
+if __name__ == '__main__':
+  logging.basicConfig()
+  #utils.check_compiled('apache_beam.runners.common')
+
+  parser = argparse.ArgumentParser()
+  parser.add_argument('--num_runs', default=10, type=int)
+  parser.add_argument('--starting_point', default=50, type=int)
+  parser.add_argument('--increment', default=1000, type=int)
+  parser.add_argument('--verbose', default=True, type=bool)
+  options = parser.parse_args()
+
+  run_benchmark(

Review Comment:
   Clarified the purpose in the docstring



##########
sdks/python/apache_beam/typehints/arrow_type_compatibility_test.py:
##########
@@ -0,0 +1,145 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Tests for arrow_type_compatibility."""
+
+import logging
+import unittest
+from typing import Optional
+
+import pyarrow as pa
+import pytest
+from parameterized import parameterized
+from parameterized import parameterized_class
+
+from apache_beam.typehints import row_type
+from apache_beam.typehints import typehints
+from apache_beam.typehints.arrow_type_compatibility import 
arrow_schema_from_beam_schema
+from apache_beam.typehints.arrow_type_compatibility import 
beam_schema_from_arrow_schema
+from apache_beam.typehints.batch import BatchConverter
+from apache_beam.typehints.schemas_test import get_test_beam_schemas_protos
+
+
[email protected]_pyarrow
+class ArrowTypeCompatibilityTest(unittest.TestCase):
+  @parameterized.expand([(beam_schema, )
+                         for beam_schema in get_test_beam_schemas_protos()])
+  def test_beam_schema_survives_roundtrip(self, beam_schema):
+    roundtripped = beam_schema_from_arrow_schema(
+        arrow_schema_from_beam_schema(beam_schema))
+
+    self.assertEqual(beam_schema, roundtripped)
+
+@parameterized_class([
+    {
+        'batch_typehint': pa.Table,
+        'element_typehint': row_type.RowTypeConstraint.from_fields([
+            ('foo', Optional[int]),
+            ('bar', Optional[float]),
+            ('baz', Optional[str]),
+        ]),
+        'batch': pa.Table.from_pydict({
+            'foo': pa.array(range(100), type=pa.int64()),
+            'bar': pa.array([i / 100 for i in range(100)], type=pa.float64()),
+            'baz': pa.array([str(i) for i in range(100)], type=pa.string()),
+        }),
+    },
+    {
+        'batch_typehint': pa.Table,
+        'element_typehint': row_type.RowTypeConstraint.from_fields([
+            ('foo', Optional[int]),
+            (
+                'nested',
+                Optional[row_type.RowTypeConstraint.from_fields([
+                    ("bar", Optional[float]),  # noqa: F821
+                    ("baz", Optional[str]),  # noqa: F821
+                ])]),
+        ]),
+        'batch': pa.Table.from_pydict({
+            'foo': pa.array(range(100), type=pa.int64()),
+            'nested': pa.array([
+                None if i % 11 else {
+                    'bar': i / 100, 'baz': str(i)
+                } for i in range(100)
+            ]),
+        }),
+    },
+])
[email protected]_pyarrow
+class ArrowBatchConverterTest(unittest.TestCase):
+  def create_batch_converter(self):
+    return BatchConverter.from_typehints(
+        element_type=self.element_typehint, batch_type=self.batch_typehint)
+
+  def setUp(self):
+    self.converter = self.create_batch_converter()
+    self.normalized_batch_typehint = typehints.normalize(self.batch_typehint)
+    self.normalized_element_typehint = typehints.normalize(
+        self.element_typehint)
+
+  def equality_check(self, left, right):
+    self.assertEqual(left, right)
+
+  def test_typehint_validates(self):
+    typehints.validate_composite_type_param(self.batch_typehint, '')
+    typehints.validate_composite_type_param(self.element_typehint, '')
+
+  def test_type_check(self):
+    typehints.check_constraint(self.normalized_batch_typehint, self.batch)
+
+  def test_type_check_element(self):
+    for element in self.converter.explode_batch(self.batch):
+      typehints.check_constraint(self.normalized_element_typehint, element)
+
+  def test_explode_rebatch(self):
+    exploded = list(self.converter.explode_batch(self.batch))
+    rebatched = self.converter.produce_batch(exploded)
+
+    typehints.check_constraint(self.normalized_batch_typehint, rebatched)
+    self.equality_check(self.batch, rebatched)
+

Review Comment:
   Thanks! done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to