forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_connect_plan_only.py
348 lines (298 loc) · 15.3 KB
/
test_connect_plan_only.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#
# 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 cast
import unittest
from pyspark.testing.connectutils import PlanOnlyTestFixture
from pyspark.testing.sqlutils import have_pandas, pandas_requirement_message
if have_pandas:
import pyspark.sql.connect.proto as proto
from pyspark.sql.connect.readwriter import DataFrameReader
from pyspark.sql.connect.function_builder import UserDefinedFunction, udf
from pyspark.sql.types import StringType
@unittest.skipIf(not have_pandas, cast(str, pandas_requirement_message))
class SparkConnectTestsPlanOnly(PlanOnlyTestFixture):
"""These test cases exercise the interface to the proto plan
generation but do not call Spark."""
def test_sql_project(self):
plan = self.connect.sql("SELECT 1")._plan.to_proto(self.connect)
self.assertEqual(plan.root.sql.query, "SELECT 1")
def test_simple_project(self):
plan = self.connect.readTable(table_name=self.tbl_name)._plan.to_proto(self.connect)
self.assertIsNotNone(plan.root, "Root relation must be set")
self.assertIsNotNone(plan.root.read)
def test_join_using_columns(self):
left_input = self.connect.readTable(table_name=self.tbl_name)
right_input = self.connect.readTable(table_name=self.tbl_name)
plan = left_input.join(other=right_input, on="join_column")._plan.to_proto(self.connect)
self.assertEqual(len(plan.root.join.using_columns), 1)
plan2 = left_input.join(other=right_input, on=["col1", "col2"])._plan.to_proto(self.connect)
self.assertEqual(len(plan2.root.join.using_columns), 2)
def test_join_condition(self):
left_input = self.connect.readTable(table_name=self.tbl_name)
right_input = self.connect.readTable(table_name=self.tbl_name)
plan = left_input.join(
other=right_input, on=left_input.name == right_input.name
)._plan.to_proto(self.connect)
self.assertIsNotNone(plan.root.join.join_condition)
def test_filter(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3)._plan.to_proto(self.connect)
self.assertIsNotNone(plan.root.filter)
self.assertTrue(
isinstance(
plan.root.filter.condition.unresolved_function, proto.Expression.UnresolvedFunction
)
)
self.assertEqual(plan.root.filter.condition.unresolved_function.parts, [">"])
self.assertEqual(len(plan.root.filter.condition.unresolved_function.arguments), 2)
def test_fill_na(self):
# SPARK-41128: Test fill na
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.fillna(value=1)._plan.to_proto(self.connect)
self.assertEqual(len(plan.root.fill_na.values), 1)
self.assertEqual(plan.root.fill_na.values[0].i64, 1)
self.assertEqual(plan.root.fill_na.cols, [])
plan = df.na.fill(value="xyz")._plan.to_proto(self.connect)
self.assertEqual(len(plan.root.fill_na.values), 1)
self.assertEqual(plan.root.fill_na.values[0].string, "xyz")
self.assertEqual(plan.root.fill_na.cols, [])
plan = df.na.fill(value="xyz", subset=["col_a", "col_b"])._plan.to_proto(self.connect)
self.assertEqual(len(plan.root.fill_na.values), 1)
self.assertEqual(plan.root.fill_na.values[0].string, "xyz")
self.assertEqual(plan.root.fill_na.cols, ["col_a", "col_b"])
plan = df.na.fill(value=True, subset=("col_a", "col_b", "col_c"))._plan.to_proto(
self.connect
)
self.assertEqual(len(plan.root.fill_na.values), 1)
self.assertEqual(plan.root.fill_na.values[0].boolean, True)
self.assertEqual(plan.root.fill_na.cols, ["col_a", "col_b", "col_c"])
plan = df.fillna({"col_a": 1.5, "col_b": "abc"})._plan.to_proto(self.connect)
self.assertEqual(len(plan.root.fill_na.values), 2)
self.assertEqual(plan.root.fill_na.values[0].fp64, 1.5)
self.assertEqual(plan.root.fill_na.values[1].string, "abc")
self.assertEqual(plan.root.fill_na.cols, ["col_a", "col_b"])
def test_summary(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3).summary()._plan.to_proto(self.connect)
self.assertEqual(plan.root.summary.statistics, [])
plan = (
df.filter(df.col_name > 3)
.summary("count", "mean", "stddev", "min", "25%")
._plan.to_proto(self.connect)
)
self.assertEqual(
plan.root.summary.statistics,
["count", "mean", "stddev", "min", "25%"],
)
def test_crosstab(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3).crosstab("col_a", "col_b")._plan.to_proto(self.connect)
self.assertEqual(plan.root.crosstab.col1, "col_a")
self.assertEqual(plan.root.crosstab.col2, "col_b")
plan = df.stat.crosstab("col_a", "col_b")._plan.to_proto(self.connect)
self.assertEqual(plan.root.crosstab.col1, "col_a")
self.assertEqual(plan.root.crosstab.col2, "col_b")
def test_limit(self):
df = self.connect.readTable(table_name=self.tbl_name)
limit_plan = df.limit(10)._plan.to_proto(self.connect)
self.assertEqual(limit_plan.root.limit.limit, 10)
def test_offset(self):
df = self.connect.readTable(table_name=self.tbl_name)
offset_plan = df.offset(10)._plan.to_proto(self.connect)
self.assertEqual(offset_plan.root.offset.offset, 10)
def test_sample(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3).sample(fraction=0.3)._plan.to_proto(self.connect)
self.assertEqual(plan.root.sample.lower_bound, 0.0)
self.assertEqual(plan.root.sample.upper_bound, 0.3)
self.assertEqual(plan.root.sample.with_replacement, False)
self.assertEqual(plan.root.sample.HasField("seed"), False)
plan = (
df.filter(df.col_name > 3)
.sample(withReplacement=True, fraction=0.4, seed=-1)
._plan.to_proto(self.connect)
)
self.assertEqual(plan.root.sample.lower_bound, 0.0)
self.assertEqual(plan.root.sample.upper_bound, 0.4)
self.assertEqual(plan.root.sample.with_replacement, True)
self.assertEqual(plan.root.sample.seed, -1)
def test_sort(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3).sort("col_a", "col_b")._plan.to_proto(self.connect)
self.assertEqual(
[
f.expression.unresolved_attribute.unparsed_identifier
for f in plan.root.sort.sort_fields
],
["col_a", "col_b"],
)
self.assertEqual(plan.root.sort.is_global, True)
plan = (
df.filter(df.col_name > 3)
.sortWithinPartitions("col_a", "col_b")
._plan.to_proto(self.connect)
)
self.assertEqual(
[
f.expression.unresolved_attribute.unparsed_identifier
for f in plan.root.sort.sort_fields
],
["col_a", "col_b"],
)
self.assertEqual(plan.root.sort.is_global, False)
def test_drop(self):
# SPARK-41169: test drop
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.filter(df.col_name > 3).drop("col_a", "col_b")._plan.to_proto(self.connect)
self.assertEqual(
[f.unresolved_attribute.unparsed_identifier for f in plan.root.drop.cols],
["col_a", "col_b"],
)
plan = df.filter(df.col_name > 3).drop(df.col_x, "col_b")._plan.to_proto(self.connect)
self.assertEqual(
[f.unresolved_attribute.unparsed_identifier for f in plan.root.drop.cols],
["col_x", "col_b"],
)
def test_deduplicate(self):
df = self.connect.readTable(table_name=self.tbl_name)
distinct_plan = df.distinct()._plan.to_proto(self.connect)
self.assertEqual(distinct_plan.root.deduplicate.all_columns_as_keys, True)
self.assertEqual(len(distinct_plan.root.deduplicate.column_names), 0)
deduplicate_on_all_columns_plan = df.dropDuplicates()._plan.to_proto(self.connect)
self.assertEqual(deduplicate_on_all_columns_plan.root.deduplicate.all_columns_as_keys, True)
self.assertEqual(len(deduplicate_on_all_columns_plan.root.deduplicate.column_names), 0)
deduplicate_on_subset_columns_plan = df.dropDuplicates(["name", "height"])._plan.to_proto(
self.connect
)
self.assertEqual(
deduplicate_on_subset_columns_plan.root.deduplicate.all_columns_as_keys, False
)
self.assertEqual(len(deduplicate_on_subset_columns_plan.root.deduplicate.column_names), 2)
def test_relation_alias(self):
df = self.connect.readTable(table_name=self.tbl_name)
plan = df.alias("table_alias")._plan.to_proto(self.connect)
self.assertEqual(plan.root.subquery_alias.alias, "table_alias")
self.assertIsNotNone(plan.root.subquery_alias.input)
def test_range(self):
plan = self.connect.range(start=10, end=20, step=3, num_partitions=4)._plan.to_proto(
self.connect
)
self.assertEqual(plan.root.range.start, 10)
self.assertEqual(plan.root.range.end, 20)
self.assertEqual(plan.root.range.step, 3)
self.assertEqual(plan.root.range.num_partitions, 4)
plan = self.connect.range(start=10, end=20)._plan.to_proto(self.connect)
self.assertEqual(plan.root.range.start, 10)
self.assertEqual(plan.root.range.end, 20)
self.assertEqual(plan.root.range.step, 1)
self.assertFalse(plan.root.range.HasField("num_partitions"))
def test_datasource_read(self):
reader = DataFrameReader(self.connect)
df = reader.load(path="test_path", format="text", schema="id INT", op1="opv", op2="opv2")
plan = df._plan.to_proto(self.connect)
data_source = plan.root.read.data_source
self.assertEqual(data_source.format, "text")
self.assertEqual(data_source.schema, "id INT")
self.assertEqual(len(data_source.options), 3)
self.assertEqual(data_source.options.get("path"), "test_path")
self.assertEqual(data_source.options.get("op1"), "opv")
self.assertEqual(data_source.options.get("op2"), "opv2")
def test_simple_udf(self):
u = udf(lambda x: "Martin", StringType())
self.assertIsNotNone(u)
expr = u("ThisCol", "ThatCol", "OtherCol")
self.assertTrue(isinstance(expr, UserDefinedFunction))
u_plan = expr.to_plan(self.connect)
self.assertIsNotNone(u_plan)
def test_all_the_plans(self):
df = self.connect.readTable(table_name=self.tbl_name)
df = df.select(df.col1).filter(df.col2 == 2).sort(df.col3.asc())
plan = df._plan.to_proto(self.connect)
self.assertIsNotNone(plan.root, "Root relation must be set")
self.assertIsNotNone(plan.root.read)
def test_union(self):
df1 = self.connect.readTable(table_name=self.tbl_name)
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.union(df2)._plan.to_proto(self.connect)
self.assertTrue(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan1.root.set_op.set_op_type)
plan2 = df1.union(df2)._plan.to_proto(self.connect)
self.assertTrue(plan2.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan2.root.set_op.set_op_type)
plan3 = df1.unionByName(df2, True)._plan.to_proto(self.connect)
self.assertTrue(plan3.root.set_op.by_name)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan3.root.set_op.set_op_type)
def test_except(self):
# SPARK-41010: test `except` API for Python client.
df1 = self.connect.readTable(table_name=self.tbl_name)
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.exceptAll(df2)._plan.to_proto(self.connect)
self.assertTrue(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_EXCEPT, plan1.root.set_op.set_op_type)
def test_intersect(self):
# SPARK-41010: test `intersect` API for Python client.
df1 = self.connect.readTable(table_name=self.tbl_name)
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.intersect(df2)._plan.to_proto(self.connect)
self.assertFalse(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_INTERSECT, plan1.root.set_op.set_op_type)
plan2 = df1.intersectAll(df2)._plan.to_proto(self.connect)
self.assertTrue(plan2.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_INTERSECT, plan2.root.set_op.set_op_type)
def test_coalesce_and_repartition(self):
# SPARK-41026: test Coalesce and Repartition API in Python client.
df = self.connect.readTable(table_name=self.tbl_name)
plan1 = df.coalesce(10)._plan.to_proto(self.connect)
self.assertEqual(10, plan1.root.repartition.num_partitions)
self.assertFalse(plan1.root.repartition.shuffle)
plan2 = df.repartition(20)._plan.to_proto(self.connect)
self.assertTrue(plan2.root.repartition.shuffle)
with self.assertRaises(ValueError) as context:
df.coalesce(-1)._plan.to_proto(self.connect)
self.assertTrue("numPartitions must be positive" in str(context.exception))
with self.assertRaises(ValueError) as context:
df.repartition(-1)._plan.to_proto(self.connect)
self.assertTrue("numPartitions must be positive" in str(context.exception))
def test_unsupported_functions(self):
# SPARK-41225: Disable unsupported functions.
df = self.connect.readTable(table_name=self.tbl_name)
for f in (
"rdd",
"unpersist",
"cache",
"persist",
"withWatermark",
"observe",
"foreach",
"foreachPartition",
"toLocalIterator",
"checkpoint",
"localCheckpoint",
"_repr_html_",
"semanticHash",
"sameSemantics",
):
with self.assertRaises(NotImplementedError):
getattr(df, f)()
if __name__ == "__main__":
from pyspark.sql.tests.connect.test_connect_plan_only 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)