forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_parity_udtf.py
More file actions
170 lines (137 loc) · 5.88 KB
/
test_parity_udtf.py
File metadata and controls
170 lines (137 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#
# 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 os
import unittest
from pyspark.testing.connectutils import should_test_connect
from pyspark.sql.tests.test_udtf import (
BaseUDTFTestsMixin,
UDTFArrowTestsMixin,
LegacyUDTFArrowTestsMixin,
)
from pyspark.testing.connectutils import ReusedConnectTestCase
if should_test_connect:
from pyspark import sql
from pyspark.sql.connect.udtf import UserDefinedTableFunction
sql.udtf.UserDefinedTableFunction = UserDefinedTableFunction
from pyspark.sql.connect.functions import lit, udtf
from pyspark.errors.exceptions.connect import (
PickleException,
PythonException,
InvalidPlanInput,
)
class UDTFParityTests(BaseUDTFTestsMixin, ReusedConnectTestCase):
@classmethod
def setUpClass(cls):
super(UDTFParityTests, cls).setUpClass()
cls.spark.conf.set("spark.sql.execution.pythonUDTF.arrow.enabled", "false")
@classmethod
def tearDownClass(cls):
try:
cls.spark.conf.unset("spark.sql.execution.pythonUDTF.arrow.enabled")
finally:
super(UDTFParityTests, cls).tearDownClass()
def test_struct_output_type_casting_row(self):
self.check_struct_output_type_casting_row(PickleException)
def test_udtf_with_invalid_return_type(self):
@udtf(returnType="int")
class TestUDTF:
def eval(self, a: int):
yield a + 1,
with self.assertRaisesRegex(
InvalidPlanInput, "Invalid Python user-defined table function return type."
):
TestUDTF(lit(1)).collect()
@unittest.skip("Spark Connect does not support broadcast but the test depends on it.")
def test_udtf_with_analyze_using_broadcast(self):
super().test_udtf_with_analyze_using_broadcast()
@unittest.skip("Spark Connect does not support accumulator but the test depends on it.")
def test_udtf_with_analyze_using_accumulator(self):
super().test_udtf_with_analyze_using_accumulator()
def test_udtf_with_analyze_using_archive(self):
super().check_udtf_with_analyze_using_archive(".")
def test_udtf_with_analyze_using_file(self):
super().check_udtf_with_analyze_using_file(".")
@unittest.skip("pyspark-connect can serialize SparkSession, but fails on executor")
def test_udtf_access_spark_session(self):
super().test_udtf_access_spark_session()
def _add_pyfile(self, path):
self.spark.addArtifacts(path, pyfile=True)
def _add_archive(self, path):
self.spark.addArtifacts(path, archive=True)
def _add_file(self, path):
self.spark.addArtifacts(path, file=True)
class LegacyArrowUDTFParityTests(LegacyUDTFArrowTestsMixin, UDTFParityTests):
@classmethod
def setUpClass(cls):
super(LegacyArrowUDTFParityTests, cls).setUpClass()
cls.spark.conf.set("spark.sql.execution.pythonUDTF.arrow.enabled", "true")
cls.spark.conf.set(
"spark.sql.legacy.execution.pythonUDTF.pandas.conversion.enabled", "true"
)
@classmethod
def tearDownClass(cls):
try:
cls.spark.conf.unset("spark.sql.execution.pythonUDTF.arrow.enabled")
cls.spark.conf.unset("spark.sql.legacy.execution.pythonUDTF.pandas.conversion.enabled")
finally:
super(LegacyArrowUDTFParityTests, cls).tearDownClass()
def test_udtf_access_spark_session_connect(self):
df = self.spark.range(10)
@udtf(returnType="x: int")
class TestUDTF:
def eval(self):
df.collect()
yield 1,
with self.assertRaisesRegex(PythonException, "NO_ACTIVE_SESSION"):
TestUDTF().collect()
@unittest.skipIf(
os.environ.get("SPARK_SKIP_CONNECT_COMPAT_TESTS") == "1",
"Python UDTF with Arrow is still under development.",
)
class ArrowUDTFParityTests(UDTFArrowTestsMixin, UDTFParityTests):
@classmethod
def setUpClass(cls):
super(ArrowUDTFParityTests, cls).setUpClass()
cls.spark.conf.set("spark.sql.execution.pythonUDTF.arrow.enabled", "true")
cls.spark.conf.set(
"spark.sql.legacy.execution.pythonUDTF.pandas.conversion.enabled", "false"
)
@classmethod
def tearDownClass(cls):
try:
cls.spark.conf.unset("spark.sql.execution.pythonUDTF.arrow.enabled")
cls.spark.conf.unset("spark.sql.legacy.execution.pythonUDTF.pandas.conversion.enabled")
finally:
super(ArrowUDTFParityTests, cls).tearDownClass()
def test_udtf_access_spark_session_connect(self):
df = self.spark.range(10)
@udtf(returnType="x: int")
class TestUDTF:
def eval(self):
df.collect()
yield 1,
with self.assertRaisesRegex(PythonException, "NO_ACTIVE_SESSION"):
TestUDTF().collect()
if __name__ == "__main__":
import unittest
from pyspark.sql.tests.connect.test_parity_udtf import * # noqa: F401
try:
import xmlrunner # type: ignore[import]
testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2)
except ImportError:
testRunner = None
unittest.main(testRunner=testRunner, verbosity=2)