|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.expressions.{CaseWhen, If} |
| 21 | +import org.apache.spark.sql.execution.LocalTableScanExec |
| 22 | +import org.apache.spark.sql.functions.{lit, when} |
| 23 | +import org.apache.spark.sql.test.SharedSQLContext |
| 24 | + |
| 25 | +class ReplaceNullWithFalseEndToEndSuite extends QueryTest with SharedSQLContext { |
| 26 | + import testImplicits._ |
| 27 | + |
| 28 | + test("SPARK-25860: Replace Literal(null, _) with FalseLiteral whenever possible") { |
| 29 | + withTable("t1", "t2") { |
| 30 | + Seq((1, true), (2, false)).toDF("l", "b").write.saveAsTable("t1") |
| 31 | + Seq(2, 3).toDF("l").write.saveAsTable("t2") |
| 32 | + val df1 = spark.table("t1") |
| 33 | + val df2 = spark.table("t2") |
| 34 | + |
| 35 | + val q1 = df1.where("IF(l > 10, false, b AND null)") |
| 36 | + checkAnswer(q1, Seq.empty) |
| 37 | + checkPlanIsEmptyLocalScan(q1) |
| 38 | + |
| 39 | + val q2 = df1.where("CASE WHEN l < 10 THEN null WHEN l > 40 THEN false ELSE null END") |
| 40 | + checkAnswer(q2, Seq.empty) |
| 41 | + checkPlanIsEmptyLocalScan(q2) |
| 42 | + |
| 43 | + val q3 = df1.join(df2, when(df1("l") > df2("l"), lit(null)).otherwise(df1("b") && lit(null))) |
| 44 | + checkAnswer(q3, Seq.empty) |
| 45 | + checkPlanIsEmptyLocalScan(q3) |
| 46 | + |
| 47 | + val q4 = df1.where("IF(IF(b, null, false), true, null)") |
| 48 | + checkAnswer(q4, Seq.empty) |
| 49 | + checkPlanIsEmptyLocalScan(q4) |
| 50 | + |
| 51 | + val q5 = df1.selectExpr("IF(l > 1 AND null, 5, 1) AS out") |
| 52 | + checkAnswer(q5, Row(1) :: Row(1) :: Nil) |
| 53 | + q5.queryExecution.executedPlan.foreach { p => |
| 54 | + assert(p.expressions.forall(e => e.find(_.isInstanceOf[If]).isEmpty)) |
| 55 | + } |
| 56 | + |
| 57 | + val q6 = df1.selectExpr("CASE WHEN (l > 2 AND null) THEN 3 ELSE 2 END") |
| 58 | + checkAnswer(q6, Row(2) :: Row(2) :: Nil) |
| 59 | + q6.queryExecution.executedPlan.foreach { p => |
| 60 | + assert(p.expressions.forall(e => e.find(_.isInstanceOf[CaseWhen]).isEmpty)) |
| 61 | + } |
| 62 | + |
| 63 | + checkAnswer(df1.where("IF(l > 10, false, b OR null)"), Row(1, true)) |
| 64 | + } |
| 65 | + |
| 66 | + def checkPlanIsEmptyLocalScan(df: DataFrame): Unit = df.queryExecution.executedPlan match { |
| 67 | + case s: LocalTableScanExec => assert(s.rows.isEmpty) |
| 68 | + case p => fail(s"$p is not LocalTableScanExec") |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments