You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "rangadi (via GitHub)" <gi...@apache.org> on 2023/08/18 19:20:41 UTC

[GitHub] [spark] rangadi commented on a diff in pull request #42563: [SPARK-44877][CONNECT] Support python protobuf functions for Spark Connect

rangadi commented on code in PR #42563:
URL: https://github.com/apache/spark/pull/42563#discussion_r1298781546


##########
dev/sparktestsupport/modules.py:
##########
@@ -271,27 +271,27 @@ def __hash__(self):
     ],
 )
 
-connect = Module(

Review Comment:
   Did you swap the order of `protobuf` and `connect`? The diff looks confusing :). 



##########
python/pyspark/sql/connect/protobuf/functions.py:
##########
@@ -0,0 +1,164 @@
+#
+# 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.
+#
+
+"""
+A collections of builtin protobuf functions
+"""
+
+from pyspark.sql.connect.utils import check_dependencies
+
+check_dependencies(__name__)
+
+from typing import Dict, Optional, TYPE_CHECKING
+
+from pyspark.sql.protobuf import functions as PyProtobufFunctions
+
+from pyspark.sql.connect.column import Column
+from pyspark.sql.connect.functions import _invoke_function, _to_col, _options_to_col, lit
+
+if TYPE_CHECKING:
+    from pyspark.sql.connect._typing import ColumnOrName
+
+
+def from_protobuf(
+    data: "ColumnOrName",
+    messageName: str,
+    descFilePath: Optional[str] = None,
+    options: Optional[Dict[str, str]] = None,
+    binaryDescriptorSet: Optional[bytes] = None,
+) -> Column:
+    binary_proto = None
+    if binaryDescriptorSet is not None:
+        binary_proto = binaryDescriptorSet
+    elif descFilePath is not None:
+        binary_proto = _read_descriptor_set_file(descFilePath)
+
+    if binary_proto is not None:
+        if options is None:
+            return _invoke_function(
+                "from_protobuf", _to_col(data), lit(messageName), lit(binary_proto)
+            )
+        else:
+            return _invoke_function(
+                "from_protobuf",
+                _to_col(data),
+                lit(messageName),
+                lit(binary_proto),
+                _options_to_col(options),
+            )
+    else:
+        if options is None:
+            return _invoke_function("from_protobuf", _to_col(data), lit(messageName))
+        else:
+            return _invoke_function(
+                "from_protobuf", _to_col(data), lit(messageName), _options_to_col(options)
+            )
+
+
+from_protobuf.__doc__ = PyProtobufFunctions.from_protobuf.__doc__
+
+
+def to_protobuf(
+    data: "ColumnOrName",
+    messageName: str,
+    descFilePath: Optional[str] = None,
+    options: Optional[Dict[str, str]] = None,
+    binaryDescriptorSet: Optional[bytes] = None,
+) -> Column:
+    binary_proto = None
+    if binaryDescriptorSet is not None:
+        binary_proto = binaryDescriptorSet
+    elif descFilePath is not None:
+        binary_proto = _read_descriptor_set_file(descFilePath)
+
+    if binary_proto is not None:
+        if options is None:
+            return _invoke_function(
+                "to_protobuf", _to_col(data), lit(messageName), lit(binary_proto)
+            )
+        else:
+            return _invoke_function(
+                "to_protobuf",
+                _to_col(data),
+                lit(messageName),
+                lit(binary_proto),
+                _options_to_col(options),
+            )
+    else:
+        if options is None:
+            return _invoke_function("to_protobuf", _to_col(data), lit(messageName))
+        else:
+            return _invoke_function(
+                "to_protobuf", _to_col(data), lit(messageName), _options_to_col(options)
+            )
+
+
+to_protobuf.__doc__ = PyProtobufFunctions.to_protobuf.__doc__
+
+
+def _read_descriptor_set_file(filePath: str) -> bytes:
+    with open(filePath, "rb") as f:
+        return f.read()
+
+
+def _test() -> None:
+    import os
+    import sys
+    from pyspark.testing.utils import search_jar
+
+    protobuf_jar = search_jar("connector/protobuf", "spark-protobuf-assembly-", "spark-protobuf")
+    if protobuf_jar is None:
+        print(
+            "Skipping all Protobuf Python tests as the optional Protobuf project was "
+            "not compiled into a JAR. To run these tests, "
+            "you need to build Spark with 'build/sbt package' or "
+            "'build/mvn package' before running this test."
+        )
+        sys.exit(0)

Review Comment:
   This should not fail like this right?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org