[
https://issues.apache.org/jira/browse/BEAM-7886?focusedWorklogId=298929&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-298929
]
ASF GitHub Bot logged work on BEAM-7886:
----------------------------------------
Author: ASF GitHub Bot
Created on: 21/Aug/19 19:23
Start Date: 21/Aug/19 19:23
Worklog Time Spent: 10m
Work Description: udim commented on pull request #9188: [BEAM-7886] Make
row coder a standard coder and implement in Python
URL: https://github.com/apache/beam/pull/9188#discussion_r316299259
##########
File path: sdks/python/apache_beam/coders/row_coder.py
##########
@@ -0,0 +1,162 @@
+#
+# 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.
+#
+
+from __future__ import absolute_import
+
+import itertools
+from array import array
+
+from apache_beam.coders.coder_impl import StreamCoderImpl
+from apache_beam.coders.coders import BytesCoder
+from apache_beam.coders.coders import Coder
+from apache_beam.coders.coders import FastCoder
+from apache_beam.coders.coders import FloatCoder
+from apache_beam.coders.coders import IterableCoder
+from apache_beam.coders.coders import StrUtf8Coder
+from apache_beam.coders.coders import TupleCoder
+from apache_beam.coders.coders import VarIntCoder
+from apache_beam.portability import common_urns
+from apache_beam.portability.api import schema_pb2
+from apache_beam.typehints.schemas import named_tuple_from_schema
+from apache_beam.typehints.schemas import named_tuple_to_schema
+
+__all__ = ["RowCoder"]
+
+
+class RowCoder(FastCoder):
+ """ Coder for `typing.NamedTuple` instances.
+
+ Implements the beam:coder:row:v1 standard coder spec.
+ """
+
+ def __init__(self, schema):
+ self.schema = schema
+ self.components = [
+ coder_from_type(field.type) for field in self.schema.fields
+ ]
+
+ def _create_impl(self):
+ return RowCoderImpl(self.schema, self.components)
+
+ def is_deterministic(self):
+ return all(c.is_deterministic() for c in self.components)
+
+ def to_type_hint(self):
+ return named_tuple_from_schema(self.schema)
+
+ def as_cloud_object(self, coders_context=None):
+ raise NotImplementedError("TODO")
+
+ def __eq__(self, other):
+ return type(self) == type(other) and self.schema == other.schema
+
+ def __hash__(self):
+ return hash((type(self), self.schema.SerializePartialToString()))
+
+ def to_runner_api_parameter(self, unused_context):
+ return (common_urns.coders.ROW.urn, self.schema, [])
+
+ @staticmethod
+ def from_type_hint(named_tuple_type, registry):
+ return RowCoder(named_tuple_to_schema(named_tuple_type))
+
+
+def coder_from_type(type_):
+ type_info = type_.WhichOneof("type_info")
+ if type_info == "atomic_type":
+ if type_.atomic_type in (schema_pb2.AtomicType.INT32,
+ schema_pb2.AtomicType.INT64):
+ return VarIntCoder()
+ elif type_.atomic_type == schema_pb2.AtomicType.DOUBLE:
+ return FloatCoder()
+ elif type_.atomic_type == schema_pb2.AtomicType.STRING:
+ return StrUtf8Coder()
+ elif type_info == "array_type":
+ return IterableCoder(coder_from_type(type_.array_type.element_type))
+
+ # The Java SDK supports several more types, but the coders are not yet
+ # standard, and are not implemented in Python.
+ raise ValueError(
+ "Encountered a type that is not currently supported by RowCoder: %s" %
+ type_)
+
+
+# pylint: disable=unused-variable
+
[email protected]_urn(common_urns.coders.ROW.urn, schema_pb2.Schema)
+def from_runner_api_parameter(payload, components, unused_context):
Review comment:
This is typically implemented as a method of the coder (looking at other
coders in Beam). The decorator will make it a staticmethod.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 298929)
Time Spent: 4h 50m (was: 4h 40m)
> Make row coder a standard coder and implement in python
> -------------------------------------------------------
>
> Key: BEAM-7886
> URL: https://issues.apache.org/jira/browse/BEAM-7886
> Project: Beam
> Issue Type: Improvement
> Components: beam-model, sdk-java-core, sdk-py-core
> Reporter: Brian Hulette
> Assignee: Brian Hulette
> Priority: Major
> Time Spent: 4h 50m
> Remaining Estimate: 0h
>
--
This message was sent by Atlassian Jira
(v8.3.2#803003)