dianfu commented on a change in pull request #8561: [FLINK-12588][python] Add 
TableSchema for Python Table API.
URL: https://github.com/apache/flink/pull/8561#discussion_r288496123
 
 

 ##########
 File path: flink-python/pyflink/table/table_schema.py
 ##########
 @@ -0,0 +1,236 @@
+################################################################################
+#  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 sys
+
+from pyflink.java_gateway import get_gateway
+from pyflink.table.types import _to_java_type, _to_python_type
+from pyflink.util.utils import to_jarray
+
+if sys.version >= '3':
+    unicode = str
+
+
+class TableSchema(object):
+    """
+    A table schema that represents a table's structure with field names and 
data types.
+    """
+
+    def __init__(self, field_names=None, data_types=None, java_object=None):
+        if java_object is None:
+            gateway = get_gateway()
+            j_field_names = to_jarray(gateway.jvm.String, field_names)
+            j_data_types = to_jarray(gateway.jvm.TypeInformation,
+                                     [_to_java_type(item) for item in 
data_types])
+            self._j_table_schema = gateway.jvm.TableSchema(j_field_names, 
j_data_types)
+        else:
+            self._j_table_schema = java_object
+
+    def copy(self):
+        """
+        Returns a deep copy of the table schema.
+
+        :return: A deep copy of the table schema.
+        """
+        return TableSchema(java_object=self._j_table_schema.copy())
+
+    def get_field_types(self):
+        """
+        Returns all field data types as an array.
+
+        .. note::
+            Deprecated.
+            Use 
:func:`pyflink.table.table_schema.TableSchema.get_field_data_types` instead.
+
+
+        :return: A list of all field data types.
+        """
+        return [_to_python_type(item) for item in 
self._j_table_schema.getFieldTypes()]
+
+    def get_field_data_types(self):
+        """
+        Returns all field data types as an array.
+
+        :return: A list of all field data types.
+        """
+        return [_to_python_type(item) for item in 
self._j_table_schema.getFieldDataTypes()]
+
+    def get_field_data_type(self, field):
+        """
+        Returns the specified data type for the given field index or field 
name.
+
+        :param field: The index of the field or the name of the field.
+        :return: The specified data type.
+        """
+        if not isinstance(field, (int, str, unicode)):
+            raise TypeError("not supported data type: %s" % type(field))
 
 Review comment:
   "data type" here is confusing. What about TypeError("expected field index or 
field name, got %s" field)?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to