Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -103,6 +103,7 @@ abstract class Optimizer(catalogManager: CatalogManager)
RemoveDispensableExpressions,
SimplifyBinaryComparison,
ReplaceNullWithFalseInPredicate,
SimplifyConditionalInPredicate,
PruneFilters,
SimplifyCasts,
SimplifyCaseConversionExpressions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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._
import org.apache.spark.util.Utils

/*
* Optimization rules defined in this file should not affect the structure of the logical plan.
Expand Down Expand Up @@ -570,6 +571,33 @@ object PushFoldableIntoBranches extends Rule[LogicalPlan] with PredicateHelper {
}


object SimplifyConditionalInPredicate extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case f @ Filter(cond, _) => f.copy(condition = simplifyConditional(cond))
}

private def simplifyConditional(e: Expression): Expression = e match {
case cw @ CaseWhen(branches, elseValue) if cw.dataType == BooleanType && branches.size == 1 &&
elseValue.forall(_.semanticEquals(FalseLiteral)) =>
val (whenVal, thenVal) = branches.head
And(whenVal, thenVal)
case i @ If(pred, trueVal, FalseLiteral) if i.dataType == BooleanType =>
And(pred, trueVal)
case e if e.dataType == BooleanType =>
e
case e =>
val message = "Expected a Boolean type expression in simplifyConditional, " +
s"but got the type `${e.dataType.catalogString}` in `${e.sql}`."
if (Utils.isTesting) {
throw new IllegalArgumentException(message)
} else {
logWarning(message)
e
}
}
}


/**
* Simplifies LIKE expressions that do not need full regular expressions to evaluate the condition.
* For example, when the expression is just checking to see if a string starts with a given
Expand Down