forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectutils.py
202 lines (169 loc) · 6.65 KB
/
connectutils.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#
# 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 shutil
import tempfile
import typing
import os
import functools
import unittest
import uuid
import contextlib
from pyspark.testing import (
grpc_requirement_message,
have_grpc,
grpc_status_requirement_message,
have_grpc_status,
googleapis_common_protos_requirement_message,
have_googleapis_common_protos,
graphviz_requirement_message,
have_graphviz,
connect_requirement_message,
should_test_connect,
)
from pyspark import Row, SparkConf
from pyspark.util import is_remote_only
from pyspark.testing.utils import PySparkErrorTestUtils
from pyspark.testing.sqlutils import (
have_pandas,
pandas_requirement_message,
pyarrow_requirement_message,
SQLTestUtils,
)
from pyspark.sql.session import SparkSession as PySparkSession
if should_test_connect:
from pyspark.sql.connect.dataframe import DataFrame
from pyspark.sql.connect.plan import Read, Range, SQL, LogicalPlan
from pyspark.sql.connect.session import SparkSession
class MockRemoteSession:
def __init__(self):
self.hooks = {}
self.session_id = str(uuid.uuid4())
self.is_mock_session = True
def set_hook(self, name, hook):
self.hooks[name] = hook
def drop_hook(self, name):
self.hooks.pop(name)
def __getattr__(self, item):
if item not in self.hooks:
raise LookupError(f"{item} is not defined as a method hook in MockRemoteSession")
return functools.partial(self.hooks[item])
@unittest.skipIf(not should_test_connect, connect_requirement_message)
class PlanOnlyTestFixture(unittest.TestCase, PySparkErrorTestUtils):
if should_test_connect:
class MockDF(DataFrame):
"""Helper class that must only be used for the mock plan tests."""
def __init__(self, plan: LogicalPlan, session: SparkSession):
super().__init__(plan, session)
def __getattr__(self, name):
"""All attributes are resolved to columns, because none really exist in the
mocked DataFrame."""
return self[name]
@classmethod
def _read_table(cls, table_name):
return cls._df_mock(Read(table_name))
@classmethod
def _udf_mock(cls, *args, **kwargs):
return "internal_name"
@classmethod
def _df_mock(cls, plan: LogicalPlan) -> MockDF:
return PlanOnlyTestFixture.MockDF(plan, cls.connect)
@classmethod
def _session_range(
cls,
start,
end,
step=1,
num_partitions=None,
):
return cls._df_mock(Range(start, end, step, num_partitions))
@classmethod
def _session_sql(cls, query):
return cls._df_mock(SQL(query))
if have_pandas:
@classmethod
def _with_plan(cls, plan):
return cls._df_mock(plan)
@classmethod
def setUpClass(cls):
cls.connect = MockRemoteSession()
cls.session = SparkSession.builder.remote().getOrCreate()
cls.tbl_name = "test_connect_plan_only_table_1"
cls.connect.set_hook("readTable", cls._read_table)
cls.connect.set_hook("range", cls._session_range)
cls.connect.set_hook("sql", cls._session_sql)
cls.connect.set_hook("with_plan", cls._with_plan)
@classmethod
def tearDownClass(cls):
cls.connect.drop_hook("readTable")
cls.connect.drop_hook("range")
cls.connect.drop_hook("sql")
cls.connect.drop_hook("with_plan")
@unittest.skipIf(not should_test_connect, connect_requirement_message)
class ReusedConnectTestCase(unittest.TestCase, SQLTestUtils, PySparkErrorTestUtils):
"""
Spark Connect version of :class:`pyspark.testing.sqlutils.ReusedSQLTestCase`.
"""
@classmethod
def conf(cls):
"""
Override this in subclasses to supply a more specific conf
"""
conf = SparkConf(loadDefaults=False)
# Make the server terminate reattachable streams every 1 second and 123 bytes,
# to make the tests exercise reattach.
if conf._jconf is not None:
conf._jconf.remove("spark.master")
conf.set("spark.connect.execute.reattachable.senderMaxStreamDuration", "1s")
conf.set("spark.connect.execute.reattachable.senderMaxStreamSize", "123")
# Set a static token for all tests so the parallelism doesn't overwrite each
# tests' environment variables
conf.set("spark.connect.authenticate.token", "deadbeef")
# Make the max size of ML Cache larger, to avoid CONNECT_ML.CACHE_INVALID issues
# in tests.
conf.set("spark.connect.session.connectML.mlCache.maxSize", "1g")
return conf
@classmethod
def master(cls):
return os.environ.get("SPARK_CONNECT_TESTING_REMOTE", "local[4]")
@classmethod
def setUpClass(cls):
cls.spark = (
PySparkSession.builder.config(conf=cls.conf())
.appName(cls.__name__)
.remote(cls.master())
.getOrCreate()
)
cls._legacy_sc = None
if not is_remote_only():
cls._legacy_sc = PySparkSession._instantiatedSession._sc
cls.tempdir = tempfile.NamedTemporaryFile(delete=False)
os.unlink(cls.tempdir.name)
cls.testData = [Row(key=i, value=str(i)) for i in range(100)]
cls.df = cls.spark.createDataFrame(cls.testData)
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.tempdir.name, ignore_errors=True)
cls.spark.stop()
def test_assert_remote_mode(self):
from pyspark.sql import is_remote
self.assertTrue(is_remote())
def quiet(self):
from pyspark.testing.utils import QuietTest
if self._legacy_sc is not None:
return QuietTest(self._legacy_sc)
else:
return contextlib.nullcontext()