|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import unittest |
| 19 | + |
| 20 | +from pyspark.sql.functions import udf |
| 21 | +from pyspark.sql.tests.test_udf import BaseUDFTests |
| 22 | +from pyspark.testing.sqlutils import ( |
| 23 | + have_pandas, |
| 24 | + have_pyarrow, |
| 25 | + pandas_requirement_message, |
| 26 | + pyarrow_requirement_message, |
| 27 | + ReusedSQLTestCase, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +@unittest.skipIf( |
| 32 | + not have_pandas or not have_pyarrow, pandas_requirement_message or pyarrow_requirement_message |
| 33 | +) |
| 34 | +class PythonUDFArrowTests(BaseUDFTests, ReusedSQLTestCase): |
| 35 | + @classmethod |
| 36 | + def setUpClass(cls): |
| 37 | + super(PythonUDFArrowTests, cls).setUpClass() |
| 38 | + cls.spark.conf.set("spark.sql.execution.pythonUDF.arrow.enabled", "true") |
| 39 | + |
| 40 | + @unittest.skip("Unrelated test, and it fails when it runs duplicatedly.") |
| 41 | + def test_broadcast_in_udf(self): |
| 42 | + super(PythonUDFArrowTests, self).test_broadcast_in_udf() |
| 43 | + |
| 44 | + @unittest.skip("Unrelated test, and it fails when it runs duplicatedly.") |
| 45 | + def test_register_java_function(self): |
| 46 | + super(PythonUDFArrowTests, self).test_register_java_function() |
| 47 | + |
| 48 | + @unittest.skip("Unrelated test, and it fails when it runs duplicatedly.") |
| 49 | + def test_register_java_udaf(self): |
| 50 | + super(PythonUDFArrowTests, self).test_register_java_udaf() |
| 51 | + |
| 52 | + @unittest.skip("Struct input types are not supported with Arrow optimization") |
| 53 | + def test_udf_input_serialization_valuecompare_disabled(self): |
| 54 | + super(PythonUDFArrowTests, self).test_udf_input_serialization_valuecompare_disabled() |
| 55 | + |
| 56 | + def test_nested_input_error(self): |
| 57 | + with self.assertRaisesRegexp( |
| 58 | + Exception, "NotImplementedError: Struct input type are not supported" |
| 59 | + ): |
| 60 | + self.spark.range(1).selectExpr("struct(1, 2) as struct").select( |
| 61 | + udf(lambda x: x)("struct") |
| 62 | + ).collect() |
| 63 | + |
| 64 | + def test_complex_input_types(self): |
| 65 | + row = ( |
| 66 | + self.spark.range(1) |
| 67 | + .selectExpr("array(1, 2, 3) as array", "map('a', 'b') as map") |
| 68 | + .select( |
| 69 | + udf(lambda x: str(x))("array"), |
| 70 | + udf(lambda x: str(x))("map"), |
| 71 | + ) |
| 72 | + .first() |
| 73 | + ) |
| 74 | + |
| 75 | + # The input is NumPy array when the optimization is on. |
| 76 | + self.assertEquals(row[0], "[1 2 3]") |
| 77 | + self.assertEquals(row[1], "{'a': 'b'}") |
| 78 | + |
| 79 | + def test_use_arrow(self): |
| 80 | + # useArrow=True |
| 81 | + row_true = ( |
| 82 | + self.spark.range(1) |
| 83 | + .selectExpr( |
| 84 | + "array(1, 2, 3) as array", |
| 85 | + ) |
| 86 | + .select( |
| 87 | + udf(lambda x: str(x), useArrow=True)("array"), |
| 88 | + ) |
| 89 | + .first() |
| 90 | + ) |
| 91 | + |
| 92 | + # useArrow=None |
| 93 | + row_none = ( |
| 94 | + self.spark.range(1) |
| 95 | + .selectExpr( |
| 96 | + "array(1, 2, 3) as array", |
| 97 | + ) |
| 98 | + .select( |
| 99 | + udf(lambda x: str(x), useArrow=None)("array"), |
| 100 | + ) |
| 101 | + .first() |
| 102 | + ) |
| 103 | + |
| 104 | + # The input is a NumPy array when the Arrow optimization is on. |
| 105 | + self.assertEquals(row_true[0], row_none[0]) # "[1 2 3]" |
| 106 | + |
| 107 | + # useArrow=False |
| 108 | + row_false = ( |
| 109 | + self.spark.range(1) |
| 110 | + .selectExpr( |
| 111 | + "array(1, 2, 3) as array", |
| 112 | + ) |
| 113 | + .select( |
| 114 | + udf(lambda x: str(x), useArrow=False)("array"), |
| 115 | + ) |
| 116 | + .first() |
| 117 | + ) |
| 118 | + self.assertEquals(row_false[0], "[1, 2, 3]") |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + from pyspark.sql.tests.test_arrow_python_udf import * # noqa: F401 |
| 123 | + |
| 124 | + try: |
| 125 | + import xmlrunner |
| 126 | + |
| 127 | + testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) |
| 128 | + except ImportError: |
| 129 | + testRunner = None |
| 130 | + unittest.main(testRunner=testRunner, verbosity=2) |
0 commit comments