forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_connect_basic.py
309 lines (257 loc) · 11.8 KB
/
test_connect_basic.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#
# 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.
#
from typing import Any
import unittest
import shutil
import tempfile
import grpc # type: ignore
from grpc._channel import _MultiThreadedRendezvous # type: ignore
from pyspark.testing.sqlutils import have_pandas, SQLTestUtils
if have_pandas:
import pandas
from pyspark.sql import SparkSession, Row
from pyspark.sql.types import StructType, StructField, LongType, StringType
if have_pandas:
from pyspark.sql.connect.client import RemoteSparkSession, ChannelBuilder
from pyspark.sql.connect.function_builder import udf
from pyspark.sql.connect.functions import lit
from pyspark.sql.dataframe import DataFrame
from pyspark.testing.connectutils import should_test_connect, connect_requirement_message
from pyspark.testing.utils import ReusedPySparkTestCase
@unittest.skipIf(not should_test_connect, connect_requirement_message)
class SparkConnectSQLTestCase(ReusedPySparkTestCase, SQLTestUtils):
"""Parent test fixture class for all Spark Connect related
test cases."""
if have_pandas:
connect: RemoteSparkSession
tbl_name: str
tbl_name_empty: str
df_text: "DataFrame"
@classmethod
def setUpClass(cls: Any):
ReusedPySparkTestCase.setUpClass()
cls.tempdir = tempfile.NamedTemporaryFile(delete=False)
cls.hive_available = True
# Create the new Spark Session
cls.spark = SparkSession(cls.sc)
cls.testData = [Row(key=i, value=str(i)) for i in range(100)]
cls.testDataStr = [Row(key=str(i)) for i in range(100)]
cls.df = cls.sc.parallelize(cls.testData).toDF()
cls.df_text = cls.sc.parallelize(cls.testDataStr).toDF()
cls.tbl_name = "test_connect_basic_table_1"
cls.tbl_name_empty = "test_connect_basic_table_empty"
# Cleanup test data
cls.spark_connect_clean_up_test_data()
# Load test data
cls.spark_connect_load_test_data()
@classmethod
def tearDownClass(cls: Any) -> None:
cls.spark_connect_clean_up_test_data()
ReusedPySparkTestCase.tearDownClass()
@classmethod
def spark_connect_load_test_data(cls: Any):
# Setup Remote Spark Session
cls.connect = RemoteSparkSession(userId="test_user")
df = cls.spark.createDataFrame([(x, f"{x}") for x in range(100)], ["id", "name"])
# Since we might create multiple Spark sessions, we need to create global temporary view
# that is specifically maintained in the "global_temp" schema.
df.write.saveAsTable(cls.tbl_name)
empty_table_schema = StructType(
[
StructField("firstname", StringType(), True),
StructField("middlename", StringType(), True),
StructField("lastname", StringType(), True),
]
)
emptyRDD = cls.spark.sparkContext.emptyRDD()
empty_df = cls.spark.createDataFrame(emptyRDD, empty_table_schema)
empty_df.write.saveAsTable(cls.tbl_name_empty)
@classmethod
def spark_connect_clean_up_test_data(cls: Any) -> None:
cls.spark.sql("DROP TABLE IF EXISTS {}".format(cls.tbl_name))
cls.spark.sql("DROP TABLE IF EXISTS {}".format(cls.tbl_name_empty))
class SparkConnectTests(SparkConnectSQLTestCase):
def test_simple_read(self):
df = self.connect.read.table(self.tbl_name)
data = df.limit(10).toPandas()
# Check that the limit is applied
self.assertEqual(len(data.index), 10)
def test_columns(self):
# SPARK-41036: test `columns` API for python client.
columns = self.connect.read.table(self.tbl_name).columns
self.assertEqual(["id", "name"], columns)
def test_collect(self):
df = self.connect.read.table(self.tbl_name)
data = df.limit(10).collect()
self.assertEqual(len(data), 10)
# Check Row has schema column names.
self.assertTrue("name" in data[0])
self.assertTrue("id" in data[0])
def test_simple_udf(self):
def conv_udf(x) -> str:
return "Martin"
u = udf(conv_udf)
df = self.connect.read.table(self.tbl_name)
result = df.select(u(df.id)).toPandas()
self.assertIsNotNone(result)
def test_simple_explain_string(self):
df = self.connect.read.table(self.tbl_name).limit(10)
result = df.explain()
self.assertGreater(len(result), 0)
def test_schema(self):
schema = self.connect.read.table(self.tbl_name).schema()
self.assertEqual(
StructType(
[StructField("id", LongType(), True), StructField("name", StringType(), True)]
),
schema,
)
def test_simple_binary_expressions(self):
"""Test complex expression"""
df = self.connect.read.table(self.tbl_name)
pd = df.select(df.id).where(df.id % lit(30) == lit(0)).sort(df.id.asc()).toPandas()
self.assertEqual(len(pd.index), 4)
res = pandas.DataFrame(data={"id": [0, 30, 60, 90]})
self.assert_(pd.equals(res), f"{pd.to_string()} != {res.to_string()}")
def test_limit_offset(self):
df = self.connect.read.table(self.tbl_name)
pd = df.limit(10).offset(1).toPandas()
self.assertEqual(9, len(pd.index))
pd2 = df.offset(98).limit(10).toPandas()
self.assertEqual(2, len(pd2.index))
def test_sql(self):
pdf = self.connect.sql("SELECT 1").toPandas()
self.assertEqual(1, len(pdf.index))
def test_head(self):
# SPARK-41002: test `head` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertIsNotNone(len(df.head()))
self.assertIsNotNone(len(df.head(1)))
self.assertIsNotNone(len(df.head(5)))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertIsNone(df2.head())
def test_first(self):
# SPARK-41002: test `first` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertIsNotNone(len(df.first()))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertIsNone(df2.first())
def test_take(self) -> None:
# SPARK-41002: test `take` API in Python Client
df = self.connect.read.table(self.tbl_name)
self.assertEqual(5, len(df.take(5)))
df2 = self.connect.read.table(self.tbl_name_empty)
self.assertEqual(0, len(df2.take(5)))
def test_subquery_alias(self) -> None:
# SPARK-40938: test subquery alias.
plan_text = self.connect.read.table(self.tbl_name).alias("special_alias").explain()
self.assertTrue("special_alias" in plan_text)
def test_range(self):
self.assertTrue(
self.connect.range(start=0, end=10)
.toPandas()
.equals(self.spark.range(start=0, end=10).toPandas())
)
self.assertTrue(
self.connect.range(start=0, end=10, step=3)
.toPandas()
.equals(self.spark.range(start=0, end=10, step=3).toPandas())
)
self.assertTrue(
self.connect.range(start=0, end=10, step=3, numPartitions=2)
.toPandas()
.equals(self.spark.range(start=0, end=10, step=3, numPartitions=2).toPandas())
)
def test_create_global_temp_view(self):
# SPARK-41127: test global temp view creation.
with self.tempView("view_1"):
self.connect.sql("SELECT 1 AS X LIMIT 0").createGlobalTempView("view_1")
self.connect.sql("SELECT 2 AS X LIMIT 1").createOrReplaceGlobalTempView("view_1")
self.assertTrue(self.spark.catalog.tableExists("global_temp.view_1"))
# Test when creating a view which is alreayd exists but
self.assertTrue(self.spark.catalog.tableExists("global_temp.view_1"))
with self.assertRaises(_MultiThreadedRendezvous):
self.connect.sql("SELECT 1 AS X LIMIT 0").createGlobalTempView("view_1")
def test_empty_dataset(self):
# SPARK-41005: Test arrow based collection with empty dataset.
self.assertTrue(
self.connect.sql("SELECT 1 AS X LIMIT 0")
.toPandas()
.equals(self.spark.sql("SELECT 1 AS X LIMIT 0").toPandas())
)
pdf = self.connect.sql("SELECT 1 AS X LIMIT 0").toPandas()
self.assertEqual(0, len(pdf)) # empty dataset
self.assertEqual(1, len(pdf.columns)) # one column
self.assertEqual("X", pdf.columns[0])
def test_session(self):
self.assertEqual(self.connect, self.connect.sql("SELECT 1").sparkSession())
def test_simple_datasource_read(self) -> None:
writeDf = self.df_text
tmpPath = tempfile.mkdtemp()
shutil.rmtree(tmpPath)
writeDf.write.text(tmpPath)
readDf = self.connect.read.format("text").schema("id STRING").load(path=tmpPath)
expectResult = writeDf.collect()
pandasResult = readDf.toPandas()
if pandasResult is None:
self.assertTrue(False, "Empty pandas dataframe")
else:
actualResult = pandasResult.values.tolist()
self.assertEqual(len(expectResult), len(actualResult))
class ChannelBuilderTests(ReusedPySparkTestCase):
def test_invalid_connection_strings(self):
invalid = [
"scc://host:12",
"http://host",
"sc:/host:1234/path",
"sc://host/path",
"sc://host/;parm1;param2",
]
for i in invalid:
self.assertRaises(AttributeError, ChannelBuilder, i)
def test_sensible_defaults(self):
chan = ChannelBuilder("sc://host")
self.assertFalse(chan.secure, "Default URL is not secure")
chan = ChannelBuilder("sc://host/;token=abcs")
self.assertTrue(chan.secure, "specifying a token must set the channel to secure")
chan = ChannelBuilder("sc://host/;use_ssl=abcs")
self.assertFalse(chan.secure, "Garbage in, false out")
def test_valid_channel_creation(self):
chan = ChannelBuilder("sc://host").toChannel()
self.assertIsInstance(chan, grpc.Channel)
# Sets up a channel without tokens because ssl is not used.
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc").toChannel()
self.assertIsInstance(chan, grpc.Channel)
chan = ChannelBuilder("sc://host/;use_ssl=true").toChannel()
self.assertIsInstance(chan, grpc.Channel)
def test_channel_properties(self):
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc;param1=120%2021")
self.assertEqual("host:15002", chan.endpoint)
self.assertEqual(True, chan.secure)
self.assertEqual("120 21", chan.get("param1"))
def test_metadata(self):
chan = ChannelBuilder("sc://host/;use_ssl=true;token=abc;param1=120%2021;x-my-header=abcd")
md = chan.metadata()
self.assertEqual([("param1", "120 21"), ("x-my-header", "abcd")], md)
if __name__ == "__main__":
from pyspark.sql.tests.connect.test_connect_basic import * # noqa: F401
try:
import xmlrunner # type: ignore
testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2)
except ImportError:
testRunner = None
unittest.main(testRunner=testRunner, verbosity=2)