Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -249,6 +249,11 @@ object ReorderAssociativeOperator extends Rule[LogicalPlan] {
case _ => ExpressionSet(Seq.empty)
}

private def isSameInteger(expr: Expression, value: Int): Boolean = expr match {
case l: Literal => l.value == value
case _ => false
}

def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
_.containsPattern(BINARY_ARITHMETIC), ruleId) {
case q: LogicalPlan =>
Expand All @@ -259,32 +264,31 @@ object ReorderAssociativeOperator extends Rule[LogicalPlan] {
val groupingExpressionSet = collectGroupingExpressions(q)
q.transformExpressionsDownWithPruning(_.containsPattern(BINARY_ARITHMETIC)) {
case a @ Add(_, _, f) if a.deterministic && a.dataType.isInstanceOf[IntegralType] =>
val (foldables, others) = flattenAdd(a, groupingExpressionSet).partition(_.foldable)
if (foldables.nonEmpty) {
val foldableExpr = foldables.reduce((x, y) => Add(x, y, f))
val foldableValue = foldableExpr.eval(EmptyRow)
val (literals, others) = flattenAdd(a, groupingExpressionSet)
.partition(_.isInstanceOf[Literal])
if (literals.nonEmpty) {
val literalExpr = literals.reduce((x, y) => Add(x, y, f))
if (others.isEmpty) {
Literal.create(foldableValue, a.dataType)
} else if (foldableValue == 0) {
literalExpr
} else if (isSameInteger(literalExpr, 0)) {
others.reduce((x, y) => Add(x, y, f))
} else {
Add(others.reduce((x, y) => Add(x, y, f)), Literal.create(foldableValue, a.dataType), f)
Add(others.reduce((x, y) => Add(x, y, f)), literalExpr, f)
}
} else {
a
}
case m @ Multiply(_, _, f) if m.deterministic && m.dataType.isInstanceOf[IntegralType] =>
val (foldables, others) = flattenMultiply(m, groupingExpressionSet).partition(_.foldable)
if (foldables.nonEmpty) {
val foldableExpr = foldables.reduce((x, y) => Multiply(x, y, f))
val foldableValue = foldableExpr.eval(EmptyRow)
if (others.isEmpty || (foldableValue == 0 && !m.nullable)) {
Literal.create(foldableValue, m.dataType)
} else if (foldableValue == 1) {
val (literals, others) = flattenMultiply(m, groupingExpressionSet)
.partition(_.isInstanceOf[Literal])
if (literals.nonEmpty) {
val literalExpr = literals.reduce((x, y) => Multiply(x, y, f))
if (others.isEmpty || (isSameInteger(literalExpr, 0) && !m.nullable)) {
literalExpr
} else if (isSameInteger(literalExpr, 1)) {
others.reduce((x, y) => Multiply(x, y, f))
} else {
Multiply(others.reduce((x, y) => Multiply(x, y, f)),
Literal.create(foldableValue, m.dataType), f)
Multiply(others.reduce((x, y) => Multiply(x, y, f)), literalExpr, f)
}
} else {
m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ReorderAssociativeOperatorSuite extends PlanTest {

object Optimize extends RuleExecutor[LogicalPlan] {
val batches =
Batch("ReorderAssociativeOperator", Once,
Batch("ReorderAssociativeOperator", FixedPoint(10),
ConstantFolding,
ReorderAssociativeOperator) :: Nil
}

Expand All @@ -44,7 +45,7 @@ class ReorderAssociativeOperatorSuite extends PlanTest {
($"b" + 1) * 2 * 3 * 4,
$"a" + 1 + $"b" + 2 + $"c" + 3,
$"a" + 1 + $"b" * 2 + $"c" + 3,
Rand(0) * 1 * 2 * 3 * 4)
Rand(0) * 1.0 * 2.0 * 3.0 * 4.0)

val optimized = Optimize.execute(originalQuery.analyze)

Expand All @@ -56,7 +57,7 @@ class ReorderAssociativeOperatorSuite extends PlanTest {
(($"b" + 1) * 24).as("((((b + 1) * 2) * 3) * 4)"),
($"a" + $"b" + $"c" + 6).as("(((((a + 1) + b) + 2) + c) + 3)"),
($"a" + $"b" * 2 + $"c" + 4).as("((((a + 1) + (b * 2)) + c) + 3)"),
Rand(0) * 1 * 2 * 3 * 4)
Rand(0) * 1.0 * 2.0 * 3.0 * 4.0)
.analyze

comparePlans(optimized, correctAnswer)
Expand Down Expand Up @@ -106,4 +107,17 @@ class ReorderAssociativeOperatorSuite extends PlanTest {

comparePlans(optimized, correctAnswer)
}

test("SPARK-50380: conditional branches with error expression") {
val originalQuery1 = testRelation.select(If($"a" === 1, 1L, Literal(1).div(0) + $"b")).analyze
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for adding this.

val optimized1 = Optimize.execute(originalQuery1)
comparePlans(optimized1, originalQuery1)

val originalQuery2 = testRelation.select(
If($"a" === 1, 1, ($"b" + Literal(Int.MaxValue)) + 1).as("col")).analyze
val optimized2 = Optimize.execute(originalQuery2)
val correctAnswer2 = testRelation.select(
If($"a" === 1, 1, $"b" + (Literal(Int.MaxValue) + 1)).as("col")).analyze
comparePlans(optimized2, correctAnswer2)
Copy link
Member

Choose a reason for hiding this comment

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

Any difference between originalQuery2 and correctAnswer2?

Copy link
Member

Choose a reason for hiding this comment

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

The association is different.

  • originalQuery2: (A + B) + C
  • correctAnswer2: A + (B + C)

}
}