1717
1818package org .apache .spark .sql .catalyst .optimizer
1919
20- import org .apache .spark .sql .Row
2120import org .apache .spark .sql .catalyst .analysis .EliminateSubqueryAliases
2221import org .apache .spark .sql .catalyst .dsl .expressions ._
2322import org .apache .spark .sql .catalyst .dsl .plans ._
@@ -38,8 +37,7 @@ class ConstantPropagationSuite extends PlanTest {
3837 Batch (" ConstantPropagation" , FixedPoint (10 ),
3938 ConstantPropagation ,
4039 ConstantFolding ,
41- BooleanSimplification ,
42- PruneFilters ) :: Nil
40+ BooleanSimplification ) :: Nil
4341 }
4442
4543 val testRelation = LocalRelation (' a .int, ' b .int, ' c .int, ' x .boolean)
@@ -157,106 +155,89 @@ class ConstantPropagationSuite extends PlanTest {
157155 val query = testRelation
158156 .where(
159157 columnA === Literal (1 ) && columnA === Literal (2 ) && columnB === Add (columnA, Literal (3 )))
158+ .analyze
160159
161- val correctAnswer = testRelation
160+ val correctAnswer = testRelation.where( Literal . FalseLiteral )
162161
163162 comparePlans(Optimize .execute(query.analyze), correctAnswer)
164163 }
165164
166- val data = {
167- val intElements = Seq (null , 1 , 2 , 3 )
168- val booleanElements = Seq (null , true , false )
169- for {
170- a <- intElements
171- b <- intElements
172- c <- intElements
173- x <- booleanElements
174- } yield (a, b, c, x)
175- }
176-
177- val testRelationWithData = LocalRelation .fromExternalRows(testRelation.output, data.map(Row (_)))
178-
179165 private def testPropagation (
180- input : Expression ,
181- expectEmptyRelation : Boolean ,
182- expectedConstraints : Seq [Expression ] = Seq .empty) = {
183- val originalQuery = testRelationWithData.where(input).analyze
166+ inputConstraints : Expression ,
167+ expectedConstraints : Seq [Expression ]) = {
168+ val originalQuery = testRelation.where(inputConstraints).analyze
184169 val optimized = Optimize .execute(originalQuery)
185- val correctAnswer = if (expectEmptyRelation) {
186- testRelation
187- } else {
188- testRelationWithData.where(expectedConstraints.reduce(And )).analyze
189- }
170+ val correctAnswer = testRelation.where(expectedConstraints.reduce(And )).analyze
190171 comparePlans(optimized, correctAnswer)
191172 }
192173
193174 test(" Constant propagation" ) {
194- testPropagation(' a < 2 && Literal (2 ) === ' a , true )
195- testPropagation(' a < 2 && Literal (2 ) <=> ' a , true )
196- testPropagation(' a <= 2 && Literal (2 ) === ' a , false , Seq (' a === 2 ))
197- testPropagation(' a <= 2 && Literal (2 ) <=> ' a , false , Seq (' a <=> 2 ))
198- testPropagation(' a === 2 && Literal (2 ) < ' a , true )
199- testPropagation(' a === 2 && Literal (2 ) <= ' a , false , Seq (' a === 2 ))
200- testPropagation(' a === 2 && Literal (2 ) === ' a , false , Seq (' a === 2 ))
201- testPropagation(' a === 2 && Literal (2 ) <=> ' a , false , Seq (' a === 2 ))
202- testPropagation(' a === 2 && Literal (2 ) >= ' a , false , Seq (' a === 2 ))
203- testPropagation(' a === 2 && Literal (2 ) > ' a , true )
204- testPropagation(' a <=> 2 && Literal (2 ) < ' a , true )
205- testPropagation(' a <=> 2 && Literal (2 ) <= ' a , false , Seq (' a <=> 2 ))
206- testPropagation(' a <=> 2 && Literal (2 ) === ' a , false , Seq (' a <=> 2 ))
207- testPropagation(' a <=> 2 && Literal (2 ) <=> ' a , false , Seq (' a <=> 2 ))
208- testPropagation(' a <=> 2 && Literal (2 ) >= ' a , false , Seq (' a <=> 2 ))
209- testPropagation(' a <=> 2 && Literal (2 ) > ' a , true )
210- testPropagation(' a >= 2 && Literal (2 ) === ' a , false , Seq (' a === 2 ))
211- testPropagation(' a >= 2 && Literal (2 ) <=> ' a , false , Seq (' a <=> 2 ))
212- testPropagation(' a > 2 && Literal (2 ) === ' a , true )
213- testPropagation(' a > 2 && Literal (2 ) <=> ' a , true )
214-
215- testPropagation((' x || ' a < 2 ) && Literal (2 ) === ' a , false , Seq (' x , Literal (2 ) === ' a ))
216- testPropagation((' x || ' a < 2 ) && Literal (2 ) <=> ' a , false , Seq (' x , Literal (2 ) <=> ' a ))
217- testPropagation((' x || ' a <= 2 ) && Literal (2 ) === ' a , false , Seq (Literal (2 ) === ' a ))
218- testPropagation((' x || ' a <= 2 ) && Literal (2 ) <=> ' a , false , Seq (Literal (2 ) <=> ' a ))
219- testPropagation((' x || ' a === 2 ) && Literal (2 ) === ' a , false , Seq (Literal (2 ) === ' a ))
220- testPropagation((' x || ' a === 2 ) && Literal (2 ) <=> ' a , false , Seq (Literal (2 ) <=> ' a ))
221- testPropagation((' x || ' a <=> 2 ) && Literal (2 ) === ' a , false , Seq (Literal (2 ) === ' a ))
222- testPropagation((' x || ' a <=> 2 ) && Literal (2 ) <=> ' a , false , Seq (Literal (2 ) <=> ' a ))
223- testPropagation((' x || ' a >= 2 ) && Literal (2 ) === ' a , false , Seq (Literal (2 ) === ' a ))
224- testPropagation((' x || ' a >= 2 ) && Literal (2 ) <=> ' a , false , Seq (Literal (2 ) <=> ' a ))
225- testPropagation((' x || ' a > 2 ) && Literal (2 ) === ' a , false , Seq (' x , Literal (2 ) === ' a ))
226- testPropagation((' x || ' a > 2 ) && Literal (2 ) <=> ' a , false , Seq (' x , Literal (2 ) <=> ' a ))
227-
228- testPropagation(' a < 2 && Literal (3 ) === ' a , true )
229- testPropagation(' a < 2 && Literal (3 ) <=> ' a , true )
230- testPropagation(' a <= 2 && Literal (3 ) === ' a , true )
231- testPropagation(' a <= 2 && Literal (3 ) <=> ' a , true )
232- testPropagation(' a === 2 && Literal (3 ) < ' a , true )
233- testPropagation(' a === 2 && Literal (3 ) <= ' a , true )
234- testPropagation(' a === 2 && Literal (3 ) === ' a , true )
235- testPropagation(' a === 2 && Literal (3 ) <=> ' a , true )
236- testPropagation(' a === 2 && Literal (3 ) >= ' a , false , Seq (' a === 2 ))
237- testPropagation(' a === 2 && Literal (3 ) > ' a , false , Seq (' a === 2 ))
238- testPropagation(' a <=> 2 && Literal (3 ) < ' a , true )
239- testPropagation(' a <=> 2 && Literal (3 ) <= ' a , true )
240- testPropagation(' a <=> 2 && Literal (3 ) === ' a , true )
241- testPropagation(' a <=> 2 && Literal (3 ) <=> ' a , true )
242- testPropagation(' a <=> 2 && Literal (3 ) >= ' a , false , Seq (' a <=> 2 ))
243- testPropagation(' a <=> 2 && Literal (3 ) > ' a , false , Seq (' a <=> 2 ))
244- testPropagation(' a >= 2 && Literal (3 ) === ' a , false , Seq (Literal (3 ) === ' a ))
245- testPropagation(' a >= 2 && Literal (3 ) <=> ' a , false , Seq (Literal (3 ) <=> ' a ))
246- testPropagation(' a > 2 && Literal (3 ) === ' a , false , Seq (Literal (3 ) === ' a ))
247- testPropagation(' a > 2 && Literal (3 ) <=> ' a , false , Seq (Literal (3 ) <=> ' a ))
248-
249- testPropagation((' x || ' a < 2 ) && Literal (3 ) === ' a , false , Seq (' x , Literal (3 ) === ' a ))
250- testPropagation((' x || ' a < 2 ) && Literal (3 ) <=> ' a , false , Seq (' x , Literal (3 ) <=> ' a ))
251- testPropagation((' x || ' a <= 2 ) && Literal (3 ) === ' a , false , Seq (' x , Literal (3 ) === ' a ))
252- testPropagation((' x || ' a <= 2 ) && Literal (3 ) <=> ' a , false , Seq (' x , Literal (3 ) <=> ' a ))
253- testPropagation((' x || ' a === 2 ) && Literal (3 ) === ' a , false , Seq (' x , Literal (3 ) === ' a ))
254- testPropagation((' x || ' a === 2 ) && Literal (3 ) <=> ' a , false , Seq (' x , Literal (3 ) <=> ' a ))
255- testPropagation((' x || ' a <=> 2 ) && Literal (3 ) === ' a , false , Seq (' x , Literal (3 ) === ' a ))
256- testPropagation((' x || ' a <=> 2 ) && Literal (3 ) <=> ' a , false , Seq (' x , Literal (3 ) <=> ' a ))
257- testPropagation((' x || ' a >= 2 ) && Literal (3 ) === ' a , false , Seq (Literal (3 ) === ' a ))
258- testPropagation((' x || ' a >= 2 ) && Literal (3 ) <=> ' a , false , Seq (Literal (3 ) <=> ' a ))
259- testPropagation((' x || ' a > 2 ) && Literal (3 ) === ' a , false , Seq (Literal (3 ) === ' a ))
260- testPropagation((' x || ' a > 2 ) && Literal (3 ) <=> ' a , false , Seq (Literal (3 ) <=> ' a ))
175+ testPropagation(' a < 2 && Literal (2 ) === ' a , Seq ( Literal . FalseLiteral ) )
176+ testPropagation(' a < 2 && Literal (2 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
177+ testPropagation(' a <= 2 && Literal (2 ) === ' a , Seq (' a === 2 ))
178+ testPropagation(' a <= 2 && Literal (2 ) <=> ' a , Seq (' a <=> 2 ))
179+ testPropagation(' a === 2 && Literal (2 ) < ' a , Seq ( Literal . FalseLiteral ) )
180+ testPropagation(' a === 2 && Literal (2 ) <= ' a , Seq (' a === 2 ))
181+ testPropagation(' a === 2 && Literal (2 ) === ' a , Seq (' a === 2 ))
182+ testPropagation(' a === 2 && Literal (2 ) <=> ' a , Seq (' a === 2 ))
183+ testPropagation(' a === 2 && Literal (2 ) >= ' a , Seq (' a === 2 ))
184+ testPropagation(' a === 2 && Literal (2 ) > ' a , Seq ( Literal . FalseLiteral ) )
185+ testPropagation(' a <=> 2 && Literal (2 ) < ' a , Seq ( Literal . FalseLiteral ) )
186+ testPropagation(' a <=> 2 && Literal (2 ) <= ' a , Seq (' a <=> 2 ))
187+ testPropagation(' a <=> 2 && Literal (2 ) === ' a , Seq (' a <=> 2 ))
188+ testPropagation(' a <=> 2 && Literal (2 ) <=> ' a , Seq (' a <=> 2 ))
189+ testPropagation(' a <=> 2 && Literal (2 ) >= ' a , Seq (' a <=> 2 ))
190+ testPropagation(' a <=> 2 && Literal (2 ) > ' a , Seq ( Literal . FalseLiteral ) )
191+ testPropagation(' a >= 2 && Literal (2 ) === ' a , Seq (' a === 2 ))
192+ testPropagation(' a >= 2 && Literal (2 ) <=> ' a , Seq (' a <=> 2 ))
193+ testPropagation(' a > 2 && Literal (2 ) === ' a , Seq ( Literal . FalseLiteral ) )
194+ testPropagation(' a > 2 && Literal (2 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
195+
196+ testPropagation((' x || ' a < 2 ) && Literal (2 ) === ' a , Seq (' x , Literal (2 ) === ' a ))
197+ testPropagation((' x || ' a < 2 ) && Literal (2 ) <=> ' a , Seq (' x , Literal (2 ) <=> ' a ))
198+ testPropagation((' x || ' a <= 2 ) && Literal (2 ) === ' a , Seq (Literal (2 ) === ' a ))
199+ testPropagation((' x || ' a <= 2 ) && Literal (2 ) <=> ' a , Seq (Literal (2 ) <=> ' a ))
200+ testPropagation((' x || ' a === 2 ) && Literal (2 ) === ' a , Seq (Literal (2 ) === ' a ))
201+ testPropagation((' x || ' a === 2 ) && Literal (2 ) <=> ' a , Seq (Literal (2 ) <=> ' a ))
202+ testPropagation((' x || ' a <=> 2 ) && Literal (2 ) === ' a , Seq (Literal (2 ) === ' a ))
203+ testPropagation((' x || ' a <=> 2 ) && Literal (2 ) <=> ' a , Seq (Literal (2 ) <=> ' a ))
204+ testPropagation((' x || ' a >= 2 ) && Literal (2 ) === ' a , Seq (Literal (2 ) === ' a ))
205+ testPropagation((' x || ' a >= 2 ) && Literal (2 ) <=> ' a , Seq (Literal (2 ) <=> ' a ))
206+ testPropagation((' x || ' a > 2 ) && Literal (2 ) === ' a , Seq (' x , Literal (2 ) === ' a ))
207+ testPropagation((' x || ' a > 2 ) && Literal (2 ) <=> ' a , Seq (' x , Literal (2 ) <=> ' a ))
208+
209+ testPropagation(' a < 2 && Literal (3 ) === ' a , Seq ( Literal . FalseLiteral ) )
210+ testPropagation(' a < 2 && Literal (3 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
211+ testPropagation(' a <= 2 && Literal (3 ) === ' a , Seq ( Literal . FalseLiteral ) )
212+ testPropagation(' a <= 2 && Literal (3 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
213+ testPropagation(' a === 2 && Literal (3 ) < ' a , Seq ( Literal . FalseLiteral ) )
214+ testPropagation(' a === 2 && Literal (3 ) <= ' a , Seq ( Literal . FalseLiteral ) )
215+ testPropagation(' a === 2 && Literal (3 ) === ' a , Seq ( Literal . FalseLiteral ) )
216+ testPropagation(' a === 2 && Literal (3 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
217+ testPropagation(' a === 2 && Literal (3 ) >= ' a , Seq (' a === 2 ))
218+ testPropagation(' a === 2 && Literal (3 ) > ' a , Seq (' a === 2 ))
219+ testPropagation(' a <=> 2 && Literal (3 ) < ' a , Seq ( Literal . FalseLiteral ) )
220+ testPropagation(' a <=> 2 && Literal (3 ) <= ' a , Seq ( Literal . FalseLiteral ) )
221+ testPropagation(' a <=> 2 && Literal (3 ) === ' a , Seq ( Literal . FalseLiteral ) )
222+ testPropagation(' a <=> 2 && Literal (3 ) <=> ' a , Seq ( Literal . FalseLiteral ) )
223+ testPropagation(' a <=> 2 && Literal (3 ) >= ' a , Seq (' a <=> 2 ))
224+ testPropagation(' a <=> 2 && Literal (3 ) > ' a , Seq (' a <=> 2 ))
225+ testPropagation(' a >= 2 && Literal (3 ) === ' a , Seq (Literal (3 ) === ' a ))
226+ testPropagation(' a >= 2 && Literal (3 ) <=> ' a , Seq (Literal (3 ) <=> ' a ))
227+ testPropagation(' a > 2 && Literal (3 ) === ' a , Seq (Literal (3 ) === ' a ))
228+ testPropagation(' a > 2 && Literal (3 ) <=> ' a , Seq (Literal (3 ) <=> ' a ))
229+
230+ testPropagation((' x || ' a < 2 ) && Literal (3 ) === ' a , Seq (' x , Literal (3 ) === ' a ))
231+ testPropagation((' x || ' a < 2 ) && Literal (3 ) <=> ' a , Seq (' x , Literal (3 ) <=> ' a ))
232+ testPropagation((' x || ' a <= 2 ) && Literal (3 ) === ' a , Seq (' x , Literal (3 ) === ' a ))
233+ testPropagation((' x || ' a <= 2 ) && Literal (3 ) <=> ' a , Seq (' x , Literal (3 ) <=> ' a ))
234+ testPropagation((' x || ' a === 2 ) && Literal (3 ) === ' a , Seq (' x , Literal (3 ) === ' a ))
235+ testPropagation((' x || ' a === 2 ) && Literal (3 ) <=> ' a , Seq (' x , Literal (3 ) <=> ' a ))
236+ testPropagation((' x || ' a <=> 2 ) && Literal (3 ) === ' a , Seq (' x , Literal (3 ) === ' a ))
237+ testPropagation((' x || ' a <=> 2 ) && Literal (3 ) <=> ' a , Seq (' x , Literal (3 ) <=> ' a ))
238+ testPropagation((' x || ' a >= 2 ) && Literal (3 ) === ' a , Seq (Literal (3 ) === ' a ))
239+ testPropagation((' x || ' a >= 2 ) && Literal (3 ) <=> ' a , Seq (Literal (3 ) <=> ' a ))
240+ testPropagation((' x || ' a > 2 ) && Literal (3 ) === ' a , Seq (Literal (3 ) === ' a ))
241+ testPropagation((' x || ' a > 2 ) && Literal (3 ) <=> ' a , Seq (Literal (3 ) <=> ' a ))
261242 }
262243}
0 commit comments