You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/04/01 06:51:21 UTC

[GitHub] [spark] sadhen commented on a change in pull request #31735: [SPARK-34799][PYTHON][SQL] Return User-defined types from Pandas UDF

sadhen commented on a change in pull request #31735:
URL: https://github.com/apache/spark/pull/31735#discussion_r605415479



##########
File path: python/pyspark/sql/tests/test_pandas_udf_scalar.py
##########
@@ -1109,6 +1109,99 @@ def f3i(it):
 
             self.assertEqual(expected, df1.collect())
 
+    # SPARK-34799
+    def test_user_defined_types_with_udf(self):
+        """PandasUDF returns single UDT out.
+        """
+
+        # ExamplePointUDT uses ArrayType to present its sqlType.
+        @pandas_udf(ExamplePointUDT())
+        def create_vector(series: pd.Series) -> pd.Series:
+            vectors = []
+            for _, item in series.items():
+                vectors.append(ExamplePoint(item, item + 1))
+            return pd.Series(vectors)
+
+        # ExampleBoxUDT uses StructType to present its sqlType.
+        @pandas_udf(ExampleBoxUDT())
+        def create_boxes(series: pd.Series) -> pd.Series:
+            boxes = []
+            for _, item in series.items():
+                boxes.append(ExampleBox(item, item + 1, item + 2, item + 3))
+            return pd.Series(boxes)
+
+        df = self.spark.range(2)
+        df = (
+            df
+            .withColumn("vector", create_vector(col("id")))
+            .withColumn("box", create_boxes(col("id")))
+        )
+        self.assertEqual([
+            Row(id=0, vector=ExamplePoint(0, 1), box=ExampleBox(0, 1, 2, 3)),
+            Row(id=1, vector=ExamplePoint(1, 2), box=ExampleBox(1, 2, 3, 4))
+        ], df.collect())
+
+    # SPARK-34799
+    def test_user_defined_types_in_struct(self):
+        @pandas_udf(StructType([
+            StructField("vec", ArrayType(ExamplePointUDT())),
+            StructField("box", ArrayType(ExampleBoxUDT()))
+        ]))
+        def array_of_udt_structs(series: pd.Series) -> pd.DataFrame:
+            vectors = []
+            for _, i in series.items():
+                vectors.append({
+                    "vec": [ExamplePoint(i, i), ExamplePoint(i + 1, i + 1)],
+                    "box": [ExampleBox(*([i] * 4)), ExampleBox(*([i+1] * 4))],
+                })
+            return pd.DataFrame(vectors)
+
+        df = self.spark.range(1, 3)
+        df = df.withColumn("nested", array_of_udt_structs(df.id))
+        self.assertEqual([
+            Row(id=1, nested=Row(
+                vec=[ExamplePoint(1, 1), ExamplePoint(2, 2)],
+                box=[ExampleBox(1, 1, 1, 1), ExampleBox(2, 2, 2, 2)])),
+            Row(id=2, nested=Row(
+                vec=[ExamplePoint(2, 2), ExamplePoint(3, 3)],
+                box=[ExampleBox(2, 2, 2, 2), ExampleBox(3, 3, 3, 3)]))
+        ], df.collect())
+
+    # SPARK-34799
+    def test_user_defined_types_in_array(self):
+        @pandas_udf(ArrayType(ExamplePointUDT()))

Review comment:
       postponed




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



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