forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 52
/
BinaryComparisonSimplificationSuite.scala
219 lines (193 loc) · 8.9 KB
/
BinaryComparisonSimplificationSuite.scala
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
/*
* 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.
*/
package org.apache.spark.sql.catalyst.optimizer
import org.apache.spark.sql.catalyst.analysis._
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral}
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
class BinaryComparisonSimplificationSuite extends PlanTest {
object Optimize extends RuleExecutor[LogicalPlan] {
val batches =
Batch("AnalysisNodes", Once,
EliminateSubqueryAliases) ::
Batch("Infer Filters", Once,
InferFiltersFromConstraints) ::
Batch("Constant Folding", FixedPoint(50),
NullPropagation,
ConstantFolding,
BooleanSimplification,
SimplifyBinaryComparison,
PruneFilters) :: Nil
}
private def checkCondition(rel: LocalRelation, input: Expression, expected: Expression): Unit =
comparePlans(Optimize.execute(rel.where(input).analyze), rel.where(expected).analyze)
val nullableRelation = LocalRelation('a.int.withNullability(true))
val nonNullableRelation = LocalRelation('a.int.withNullability(false))
val boolRelation = LocalRelation('a.boolean, 'b.boolean)
test("Preserve nullable exprs when constraintPropagation is false") {
withSQLConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED.key -> "false") {
val a = Symbol("a")
for (e <- Seq(a === a, a <= a, a >= a, a < a, a > a)) {
val plan = nullableRelation.where(e).analyze
val actual = Optimize.execute(plan)
val correctAnswer = plan
comparePlans(actual, correctAnswer)
}
}
}
test("Preserve non-deterministic exprs") {
val plan = nonNullableRelation
.where(Rand(0) === Rand(0) && Rand(1) <=> Rand(1)).analyze
val actual = Optimize.execute(plan)
val correctAnswer = plan
comparePlans(actual, correctAnswer)
}
test("Nullable Simplification Primitive: <=>") {
val plan = nullableRelation.select('a <=> 'a).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nullableRelation.select(Alias(TrueLiteral, "(a <=> a)")()).analyze
comparePlans(actual, correctAnswer)
}
test("Non-Nullable Simplification Primitive") {
val plan = nonNullableRelation
.select('a === 'a, 'a <=> 'a, 'a <= 'a, 'a >= 'a, 'a < 'a, 'a > 'a).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nonNullableRelation
.select(
Alias(TrueLiteral, "(a = a)")(),
Alias(TrueLiteral, "(a <=> a)")(),
Alias(TrueLiteral, "(a <= a)")(),
Alias(TrueLiteral, "(a >= a)")(),
Alias(FalseLiteral, "(a < a)")(),
Alias(FalseLiteral, "(a > a)")())
.analyze
comparePlans(actual, correctAnswer)
}
test("Expression Normalization") {
val plan = nonNullableRelation.where(
'a * Literal(100) + Pi() === Pi() + Literal(100) * 'a &&
DateAdd(CurrentDate(), 'a + Literal(2)) <= DateAdd(CurrentDate(), Literal(2) + 'a))
.analyze
val actual = Optimize.execute(plan)
val correctAnswer = nonNullableRelation.analyze
comparePlans(actual, correctAnswer)
}
test("SPARK-26402: accessing nested fields with different cases in case insensitive mode") {
val expId = NamedExpression.newExprId
val qualifier = Seq.empty[String]
val structType = StructType(
StructField("a", StructType(StructField("b", IntegerType, false) :: Nil), false) :: Nil)
val fieldA1 = GetStructField(
GetStructField(
AttributeReference("data1", structType, false)(expId, qualifier),
0, Some("a1")),
0, Some("b1"))
val fieldA2 = GetStructField(
GetStructField(
AttributeReference("data2", structType, false)(expId, qualifier),
0, Some("a2")),
0, Some("b2"))
// GetStructField with different names are semantically equal; thus, `EqualTo(fieldA1, fieldA2)`
// will be optimized to `TrueLiteral` by `SimplifyBinaryComparison`.
val originalQuery = nonNullableRelation.where(EqualTo(fieldA1, fieldA2))
val optimized = Optimize.execute(originalQuery)
val correctAnswer = nonNullableRelation.analyze
comparePlans(optimized, correctAnswer)
}
test("Simplify null and nonnull with filter constraints") {
val a = Symbol("a")
Seq(a === a, a <= a, a >= a, a < a, a > a).foreach { condition =>
val plan = nonNullableRelation.where(condition).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nonNullableRelation.analyze
comparePlans(actual, correctAnswer)
}
// infer filter constraints will add IsNotNull
Seq(a === a, a <= a, a >= a).foreach { condition =>
val plan = nullableRelation.where(condition).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nullableRelation.where('a.isNotNull).analyze
comparePlans(actual, correctAnswer)
}
Seq(a < a, a > a).foreach { condition =>
val plan = nullableRelation.where(condition).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nullableRelation.analyze
comparePlans(actual, correctAnswer)
}
}
test("Simplify nullable without constraints propagation") {
withSQLConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED.key -> "false") {
val a = Symbol("a")
Seq(And(a === a, a.isNotNull),
And(a <= a, a.isNotNull),
And(a >= a, a.isNotNull)).foreach { condition =>
val plan = nullableRelation.where(condition).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nullableRelation.where('a.isNotNull).analyze
comparePlans(actual, correctAnswer)
}
Seq(And(a < a, a.isNotNull), And(a > a, a.isNotNull))
.foreach { condition =>
val plan = nullableRelation.where(condition).analyze
val actual = Optimize.execute(plan)
val correctAnswer = nullableRelation.analyze
comparePlans(actual, correctAnswer)
}
}
}
test("SPARK-36359: Coalesce drop all expressions after the first non nullable expression") {
val testRelation = LocalRelation(
'a.int.withNullability(false),
'b.int.withNullability(true),
'c.int.withNullability(false),
'd.int.withNullability(true))
comparePlans(
Optimize.execute(testRelation.select(Coalesce(Seq('a, 'b, 'c, 'd)).as("out")).analyze),
testRelation.select('a.as("out")).analyze)
comparePlans(
Optimize.execute(testRelation.select(Coalesce(Seq('a, 'c)).as("out")).analyze),
testRelation.select('a.as("out")).analyze)
comparePlans(
Optimize.execute(testRelation.select(Coalesce(Seq('b, 'c, 'd)).as("out")).analyze),
testRelation.select(Coalesce(Seq('b, 'c)).as("out")).analyze)
comparePlans(
Optimize.execute(testRelation.select(Coalesce(Seq('b, 'd)).as("out")).analyze),
testRelation.select(Coalesce(Seq('b, 'd)).as("out")).analyze)
}
test("SPARK-36721: Simplify boolean equalities if one side is literal") {
checkCondition(boolRelation, And('a, 'b) === TrueLiteral, And('a, 'b))
checkCondition(boolRelation, TrueLiteral === And('a, 'b), And('a, 'b))
checkCondition(boolRelation, And('a, 'b) === FalseLiteral, Or(Not('a), Not('b)))
checkCondition(boolRelation, FalseLiteral === And('a, 'b), Or(Not('a), Not('b)))
checkCondition(boolRelation, IsNull('a) <=> TrueLiteral, IsNull('a))
checkCondition(boolRelation, TrueLiteral <=> IsNull('a), IsNull('a))
checkCondition(boolRelation, IsNull('a) <=> FalseLiteral, IsNotNull('a))
checkCondition(boolRelation, FalseLiteral <=> IsNull('a), IsNotNull('a))
// Should not optimize for nullable <=> Literal
checkCondition(boolRelation, And('a, 'b) <=> TrueLiteral, And('a, 'b) <=> TrueLiteral)
checkCondition(boolRelation, TrueLiteral <=> And('a, 'b), TrueLiteral <=> And('a, 'b))
checkCondition(boolRelation, And('a, 'b) <=> FalseLiteral, And('a, 'b) <=> FalseLiteral)
checkCondition(boolRelation, FalseLiteral <=> And('a, 'b), FalseLiteral <=> And('a, 'b))
}
}