allisonwang-db commented on code in PR #41316: URL: https://github.com/apache/spark/pull/41316#discussion_r1222484804
########## 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"): + func(lit(1)).collect() + + def test_udtf_eval_returning_non_generator(self): + class TestUDTF: + def eval(self, a: int): + return (a,) + + func = udtf(TestUDTF, returnType="a: int") + with self.assertRaisesRegex(PythonException, "Unexpected tuple 1 with StructType"): + func(lit(1)).collect() + + def test_udtf_eval_with_no_return(self): + @udtf(returnType="a: int") + class TestUDTF: + def eval(self, a: int): + ... + + # TODO(SPARK-43967): Support Python UDTFs with empty return values + with self.assertRaisesRegex( + PythonException, "TypeError: 'NoneType' object is not iterable" + ): + TestUDTF(lit(1)).collect() + + @udtf(returnType="a: int") + class TestUDTF: + def eval(self, a: int): + return + + with self.assertRaisesRegex( + PythonException, "TypeError: 'NoneType' object is not iterable" + ): + TestUDTF(lit(1)).collect() + + def test_udtf_with_conditional_return(self): + class TestUDTF: + def eval(self, a: int): + if a > 5: + yield a, + + func = udtf(TestUDTF, returnType="a: int") + self.spark.udtf.register("test_udtf", func) + self.assertEqual( + self.spark.sql("SELECT * FROM range(0, 8) JOIN LATERAL test_udtf(id)").collect(), + [Row(id=6, a=6), Row(id=7, a=7)], + ) + + def test_udtf_with_empty_yield(self): + @udtf(returnType="a: int") + class TestUDTF: + def eval(self, a: int): + yield + + with self.assertRaisesRegex(Py4JJavaError, "java.lang.NullPointerException"): + TestUDTF(lit(1)).collect() + + def test_udtf_with_none_output(self): + @udtf(returnType="a: int") + class TestUDTF: + def eval(self, a: int): + yield a, + yield None, + + self.assertEqual(TestUDTF(lit(1)).collect(), [Row(a=1), Row(a=None)]) + df = self.spark.createDataFrame([(0, 1), (1, 2)], schema=["a", "b"]) + self.assertEqual(TestUDTF(lit(1)).join(df, "a", "inner").collect(), [Row(a=1, b=2)]) + self.assertEqual( + TestUDTF(lit(1)).join(df, "a", "left").collect(), [Row(a=None, b=None), Row(a=1, b=2)] + ) + + def test_udtf_with_none_input(self): + @udtf(returnType="a: int") + class TestUDTF: + def eval(self, a: int): + yield a, + + self.assertEqual(TestUDTF(lit(None)).collect(), [Row(a=None)]) + self.spark.udtf.register("testUDTF", TestUDTF) + df = self.spark.sql("SELECT * FROM testUDTF(null)") + self.assertEqual(df.collect(), [Row(a=None)]) + + def test_udtf_with_wrong_num_output(self): + err_msg = ( + "java.lang.IllegalStateException: Input row doesn't have expected number of " Review Comment: This error message is rather confusing. This test is actually testing the mismatch between the output values and the output schema. I will add one for the input mismatch. Also added a TODO to improve this error msg. -- 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]
