forked from apache/spark
/
test_connect_basic.py
214 lines (181 loc) · 7.85 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
#
# 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
from pyspark.testing.sqlutils import have_pandas
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
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):
"""Parent test fixture class for all Spark Connect related
test cases."""
if have_pandas:
connect: RemoteSparkSession
tbl_name: 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()
@classmethod
def spark_connect_load_test_data(cls: Any):
# Setup Remote Spark Session
cls.connect = RemoteSparkSession(user_id="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_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_head(self):
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):
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:
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_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_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))
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)