-
Notifications
You must be signed in to change notification settings - Fork 28k
/
test_connect_plan_only.py
136 lines (115 loc) · 6.01 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
#
# 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 unittest
from pyspark.testing.connectutils import PlanOnlyTestFixture
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
class SparkConnectTestsPlanOnly(PlanOnlyTestFixture):
"""These test cases exercise the interface to the proto plan
generation but do not call Spark."""
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_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_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.seed, -1)
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.common.alias, "table_alias")
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)
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)