allisonwang-db commented on code in PR #41316: URL: https://github.com/apache/spark/pull/41316#discussion_r1222481573
########## python/pyspark/sql/tests/test_udtf.py: ########## @@ -0,0 +1,366 @@ +# +# 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 unittest + +from typing import Iterator + +from py4j.protocol import Py4JJavaError + +from pyspark.errors import PythonException, AnalysisException +from pyspark.sql.functions import lit, udtf +from pyspark.sql.types import Row +from pyspark.testing.sqlutils import ReusedSQLTestCase + + +class UDTFTestsMixin(ReusedSQLTestCase): + def test_simple_udtf(self): + class TestUDTF: + def eval(self): + yield "hello", "world" + + func = udtf(TestUDTF, returnType="c1: string, c2: string") + rows = func().collect() + self.assertEqual(rows, [Row(c1="hello", c2="world")]) + + def test_udtf_yield_single_row_col(self): + class TestUDTF: + def eval(self, a: int): + yield a, + + func = udtf(TestUDTF, returnType="a: int") + rows = func(lit(1)).collect() + self.assertEqual(rows, [Row(a=1)]) + + def test_udtf_yield_multi_cols(self): + class TestUDTF: + def eval(self, a: int): + yield a, a + 1 + + func = udtf(TestUDTF, returnType="a: int, b: int") + rows = func(lit(1)).collect() + self.assertEqual(rows, [Row(a=1, b=2)]) + + def test_udtf_yield_multi_rows(self): + class TestUDTF: + def eval(self, a: int): + yield a, + yield a + 1, + + func = udtf(TestUDTF, returnType="a: int") + rows = func(lit(1)).collect() + self.assertEqual(rows, [Row(a=1), Row(a=2)]) + + def test_udtf_yield_multi_row_col(self): + class TestUDTF: + def eval(self, a: int, b: int): + yield a, b, a + b + yield a, b, a - b + yield a, b, b - a + + func = udtf(TestUDTF, returnType="a: int, b: int, c: int") + rows = func(lit(1), lit(2)).collect() + self.assertEqual(rows, [Row(a=1, b=2, c=3), Row(a=1, b=2, c=-1), Row(a=1, b=2, c=1)]) + + def test_udtf_decorator(self): + @udtf(returnType="a: int, b: int") + class TestUDTF: + def eval(self, a: int): + yield a, a + 1 + + rows = TestUDTF(lit(1)).collect() + self.assertEqual(rows, [Row(a=1, b=2)]) + + def test_udtf_registration(self): + class TestUDTF: + def eval(self, a: int, b: int): + yield a, b, a + b + yield a, b, a - b + yield a, b, b - a + + func = udtf(TestUDTF, returnType="a: int, b: int, c: int") + self.spark.udtf.register("testUDTF", func) + df = self.spark.sql("SELECT * FROM testUDTF(1, 2)") + self.assertEqual( + df.collect(), [Row(a=1, b=2, c=3), Row(a=1, b=2, c=-1), Row(a=1, b=2, c=1)] + ) + + def test_udtf_with_lateral_join(self): + class TestUDTF: + def eval(self, a: int, b: int) -> Iterator: + yield a, b, a + b + yield a, b, a - b + + func = udtf(TestUDTF, returnType="a: int, b: int, c: int") + self.spark.udtf.register("testUDTF", func) + df = self.spark.sql( + "SELECT f.* FROM values (0, 1), (1, 2) t(a, b), LATERAL testUDTF(a, b) f" + ) + expected = self.spark.createDataFrame( + [(0, 1, 1), (0, 1, -1), (1, 2, 3), (1, 2, -1)], schema=["a", "b", "c"] + ) + self.assertEqual(df.collect(), expected.collect()) + + def test_udtf_eval_with_return_stmt(self): + class TestUDTF: + def eval(self, a: int, b: int): + return [(a, a + 1), (b, b + 1)] + + func = udtf(TestUDTF, returnType="a: int, b: int") + rows = func(lit(1), lit(2)).collect() + self.assertEqual(rows, [Row(a=1, b=2), Row(a=2, b=3)]) + + def test_udtf_eval_returning_non_tuple(self): + class TestUDTF: + def eval(self, a: int): + yield a + + func = udtf(TestUDTF, returnType="a: int") + with self.assertRaisesRegex(PythonException, "Unexpected tuple 1 with StructType"): Review Comment: Yes I am planning to fix this error message (created a JIRA: SPARK-44005) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
