shuiqiangchen commented on a change in pull request #13983:
URL: https://github.com/apache/flink/pull/13983#discussion_r530090687



##########
File path: flink-python/pyflink/datastream/utils.py
##########
@@ -0,0 +1,75 @@
+################################################################################
+#  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/LICENSE2.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 ast
+import datetime
+import pickle
+
+from pyflink.common import typeinfo
+from pyflink.common.typeinfo import WrapperTypeInfo, RowTypeInfo, Types, 
BasicArrayTypeInfo, \
+    PrimitiveArrayTypeInfo
+from pyflink.java_gateway import get_gateway
+
+
+def convert_to_python_obj(data, type_info):
+    if type_info is None:
+        return pickle.loads(data)
+    else:
+        gateway = get_gateway()
+        pickle_bytes = gateway.jvm.PythonBridgeUtils. \
+            getPickledBytesFromJavaObject(data, type_info.get_java_type_info())
+        pickle_bytes = list(pickle_bytes[1:])
+        field_data = zip(pickle_bytes, type_info.types)
+        fields = []
+        for data, field_type in field_data:
+            if len(data) == 0:
+                fields.append(None)
+            else:
+                fields.append(java_to_python_converter(data, field_type))
+        return tuple(fields)
+
+
+def java_to_python_converter(data, field_type: WrapperTypeInfo):

Review comment:
       Here, the input data is not a Java object but pickled bytes, while 
naming the method as java_to_python_converter seems a bit confusing.

##########
File path: flink-python/pyflink/datastream/utils.py
##########
@@ -0,0 +1,75 @@
+################################################################################
+#  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/LICENSE2.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 ast
+import datetime
+import pickle
+
+from pyflink.common import typeinfo
+from pyflink.common.typeinfo import WrapperTypeInfo, RowTypeInfo, Types, 
BasicArrayTypeInfo, \
+    PrimitiveArrayTypeInfo
+from pyflink.java_gateway import get_gateway
+
+
+def convert_to_python_obj(data, type_info):
+    if type_info is None:
+        return pickle.loads(data)
+    else:
+        gateway = get_gateway()
+        pickle_bytes = gateway.jvm.PythonBridgeUtils. \
+            getPickledBytesFromJavaObject(data, type_info.get_java_type_info())
+        pickle_bytes = list(pickle_bytes[1:])
+        field_data = zip(pickle_bytes, type_info.types)
+        fields = []
+        for data, field_type in field_data:
+            if len(data) == 0:
+                fields.append(None)
+            else:
+                fields.append(java_to_python_converter(data, field_type))
+        return tuple(fields)
+
+
+def java_to_python_converter(data, field_type: WrapperTypeInfo):
+    if isinstance(field_type, RowTypeInfo):
+        data = zip(list(data[1:]), field_type.get_field_types())
+        fields = []
+        for d, d_type in data:
+            fields.append(java_to_python_converter(d, d_type))
+        return tuple(fields)

Review comment:
       Why return a tuple here?

##########
File path: 
flink-python/src/main/java/org/apache/flink/api/common/python/PythonBridgeUtils.java
##########
@@ -266,6 +278,92 @@ private static Object getPickledBytesFromJavaObject(Object 
obj, LogicalType data
                }
        }
 
+       public static Object getPickledBytesFromJavaObject(Object obj, 
TypeInformation<?> dataType) throws IOException {

Review comment:
       Seems a bit duplicated with getPickledBytesFromJavaObject(Row, 
DataType[])

##########
File path: flink-python/pyflink/common/typeinfo.py
##########
@@ -300,7 +325,7 @@ class RowTypeInfo(WrapperTypeInfo):
     TypeInformation for Row.
     """
 
-    def __init__(self, types: List[TypeInformation], field_names: List[str] = 
None):
+    def __init__(self, types: List[WrapperTypeInfo], field_names: List[str] = 
None):

Review comment:
       Exposing `WrapperTypeInfo` to users in API might not be appropriate.

##########
File path: flink-python/pyflink/datastream/utils.py
##########
@@ -0,0 +1,75 @@
+################################################################################
+#  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/LICENSE2.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 ast
+import datetime
+import pickle
+
+from pyflink.common import typeinfo
+from pyflink.common.typeinfo import WrapperTypeInfo, RowTypeInfo, Types, 
BasicArrayTypeInfo, \
+    PrimitiveArrayTypeInfo
+from pyflink.java_gateway import get_gateway
+
+
+def convert_to_python_obj(data, type_info):
+    if type_info is None:
+        return pickle.loads(data)
+    else:
+        gateway = get_gateway()
+        pickle_bytes = gateway.jvm.PythonBridgeUtils. \
+            getPickledBytesFromJavaObject(data, type_info.get_java_type_info())
+        pickle_bytes = list(pickle_bytes[1:])
+        field_data = zip(pickle_bytes, type_info.types)
+        fields = []
+        for data, field_type in field_data:
+            if len(data) == 0:
+                fields.append(None)
+            else:
+                fields.append(java_to_python_converter(data, field_type))
+        return tuple(fields)

Review comment:
       What if the type_info is just a single basic type like 
Types.STRING()/Types.INT()? but here we are returning a tuple.




----------------------------------------------------------------
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]


Reply via email to