Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ abstract class Optimizer(catalogManager: CatalogManager)
EliminateAggregateFilter,
ReorderAssociativeOperator,
LikeSimplification,
NotPropagation,
BooleanSimplification,
SimplifyConditionals,
PushFoldableIntoBranches,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,53 +447,6 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
}


/**
* Move/Push `Not` operator if it's beneficial.
*/
object NotPropagation extends Rule[LogicalPlan] {
// Given argument x, return true if expression Not(x) can be simplified
// E.g. let x == Not(y), then canSimplifyNot(x) == true because Not(x) == Not(Not(y)) == y
// For the case of x = EqualTo(a, b), recursively check each child expression
// Extra nullable check is required for EqualNullSafe because
// Not(EqualNullSafe(e, null)) is different from EqualNullSafe(e, Not(null))
private def canSimplifyNot(x: Expression): Boolean = x match {
case Literal(_, BooleanType) | Literal(_, NullType) => true
case _: Not | _: IsNull | _: IsNotNull | _: And | _: Or => true
case _: GreaterThan | _: GreaterThanOrEqual | _: LessThan | _: LessThanOrEqual => true
case EqualTo(a, b) if canSimplifyNot(a) || canSimplifyNot(b) => true
case EqualNullSafe(a, b)
if !a.nullable && !b.nullable && (canSimplifyNot(a) || canSimplifyNot(b)) => true
case _ => false
}

def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
_.containsPattern(NOT), ruleId) {
case q: LogicalPlan => q.transformExpressionsDownWithPruning(_.containsPattern(NOT), ruleId) {
// Move `Not` from one side of `EqualTo`/`EqualNullSafe` to the other side if it's beneficial.
// E.g. `EqualTo(Not(a), b)` where `b = Not(c)`, it will become
// `EqualTo(a, Not(b))` => `EqualTo(a, Not(Not(c)))` => `EqualTo(a, c)`
// In addition, `if canSimplifyNot(b)` checks if the optimization can converge
// that avoids the situation two conditions are returning to each other.
case EqualTo(Not(a), b) if !canSimplifyNot(a) && canSimplifyNot(b) => EqualTo(a, Not(b))
case EqualTo(a, Not(b)) if canSimplifyNot(a) && !canSimplifyNot(b) => EqualTo(Not(a), b)
case EqualNullSafe(Not(a), b) if !canSimplifyNot(a) && canSimplifyNot(b) =>
EqualNullSafe(a, Not(b))
case EqualNullSafe(a, Not(b)) if canSimplifyNot(a) && !canSimplifyNot(b) =>
EqualNullSafe(Not(a), b)

// Push `Not` to one side of `EqualTo`/`EqualNullSafe` if it's beneficial.
// E.g. Not(EqualTo(x, false)) => EqualTo(x, true)
case Not(EqualTo(a, b)) if canSimplifyNot(b) => EqualTo(a, Not(b))
case Not(EqualTo(a, b)) if canSimplifyNot(a) => EqualTo(Not(a), b)
case Not(EqualNullSafe(a, b)) if !a.nullable && !b.nullable && canSimplifyNot(b) =>
EqualNullSafe(a, Not(b))
case Not(EqualNullSafe(a, b)) if !a.nullable && !b.nullable && canSimplifyNot(a) =>
EqualNullSafe(Not(a), b)
}
}
}


/**
* Simplifies binary comparisons with semantically-equal expressions:
* 1) Replace '<=>' with 'true' literal.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class NullDownPropagationSuite extends PlanTest with ExpressionEvalHelper {
ConstantFolding,
SimplifyConditionals,
BooleanSimplification,
NotPropagation,
PruneFilters) :: Nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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

import org.apache.spark.sql.test.SharedSparkSession

class NotInSubqueryEndToEndSuite extends QueryTest with SharedSparkSession {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think we already have test suite e.g. SubquerySuite. Can we just put the test into existing test suite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

import testImplicits._

val t = "test_table"

test("SPARK-38132: Avoid Optimizing Not(InSubquery)") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "Avoid Optimizing Not(InSubquery)" -> "Avoid optimizing Not IN subquery"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

withTable(t) {
Seq[(Integer, Integer)](
(1, 1),
(2, 2),
(3, 3),
(4, null),
(null, 0))
.toDF("c1", "c2").write.saveAsTable(t)
val df = spark.table(t)

checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t)) = true"), Seq.empty)
checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t WHERE c2 IS NOT NULL)) = true"),
Row(4, null) :: Nil)
checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t)) <=> true"), Seq.empty)
checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t WHERE c2 IS NOT NULL)) <=> true"),
Row(4, null) :: Nil)
checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t)) != false"), Seq.empty)
checkAnswer(df.where(s"(c1 NOT IN (SELECT c2 FROM $t WHERE c2 IS NOT NULL)) != false"),
Row(4, null) :: Nil)
checkAnswer(df.where(s"NOT((c1 NOT IN (SELECT c2 FROM $t)) <=> false)"), Seq.empty)
checkAnswer(df.where(s"NOT((c1 NOT IN (SELECT c2 FROM $t WHERE c2 IS NOT NULL)) <=> false)"),
Row(4, null) :: Nil)
}
}
}