Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-15076][SQL] Add ReorderAssociativeOperator optimizer #12850

Closed
wants to merge 5 commits into from
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 @@ -94,6 +94,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: CatalystConf)
FoldablePropagation,
OptimizeIn(conf),
ConstantFolding,
ReorderAssociativeOperator,
LikeSimplification,
BooleanSimplification,
SimplifyConditionals,
Expand Down Expand Up @@ -737,6 +738,44 @@ object InferFiltersFromConstraints extends Rule[LogicalPlan] with PredicateHelpe
}
}

/**
* Reorder associative integral-type operators and fold all constants into one.
*/
object ReorderAssociativeOperator extends Rule[LogicalPlan] {
private def flattenAdd(e: Expression): Seq[Expression] = e match {
case Add(l, r) => flattenAdd(l) ++ flattenAdd(r)
case other => other :: Nil
}

private def flattenMultiply(e: Expression): Seq[Expression] = e match {
case Multiply(l, r) => flattenMultiply(l) ++ flattenMultiply(r)
case other => other :: Nil
}

def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case q: LogicalPlan => q transformExpressionsDown {
Copy link
Contributor

Choose a reason for hiding this comment

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

how about

def flattenAdd(e: Expression): Seq[Expression] = e match {
  case Add(l, r) => flattenAdd(l) ++ flattenAdd(r)
  case other => other
}

...
plan transformAllExpressions {
  case a: Add if a.deterministic && a.dataType.isInstanceOf[IntegralType] =>
    val (foldables, others) => flattenAdd(a).partition(_.foldable)
    if (foldables.size > 1) {
      val foldableExpr = foldables.reduce(Add(_, _))
      val c = Literal.create(foldableExpr.eval(), a.dataType)
      if (others.isEmpty) c else Add(others.reduce(Add(_, _)), c)
    } else {
      a
    }
}

We can duplicate some code for Multiply, and I think this maybe more readable than the current version.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. That could be.
We also need to add isSingleOperatorExpr there.
Otherwise, flattenAdd(Add(Multiply(1, 2), 3)) -> (3).

Copy link
Contributor

Choose a reason for hiding this comment

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

flattenAdd(Add(Multiply(1, 2), 3)) will become [Multiply(1, 2), 3], and we won't get wrong result

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I see. You generalize my PR again! Great!

case a: Add if a.deterministic && a.dataType.isInstanceOf[IntegralType] =>
val (foldables, others) = flattenAdd(a).partition(_.foldable)
if (foldables.size > 1) {
val foldableExpr = foldables.reduce((x, y) => Add(x, y))
val c = Literal.create(foldableExpr.eval(EmptyRow), a.dataType)
if (others.isEmpty) c else Add(others.reduce((x, y) => Add(x, y)), c)
} else {
a
}
case m: Multiply if m.deterministic && m.dataType.isInstanceOf[IntegralType] =>
val (foldables, others) = flattenMultiply(m).partition(_.foldable)
if (foldables.size > 1) {
val foldableExpr = foldables.reduce((x, y) => Multiply(x, y))
val c = Literal.create(foldableExpr.eval(EmptyRow), m.dataType)
if (others.isEmpty) c else Multiply(others.reduce((x, y) => Multiply(x, y)), c)
} else {
m
}
}
}
}

/**
* Replaces [[Expression Expressions]] that can be statically evaluated with
* equivalent [[Literal]] values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.catalyst.optimizer

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.RuleExecutor

class ReorderAssociativeOperatorSuite extends PlanTest {

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

val testRelation = LocalRelation('a.int, 'b.int, 'c.int)

test("Reorder associative operators") {
Copy link
Contributor

Choose a reason for hiding this comment

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

also add a test for nondeterministic case please

val originalQuery =
testRelation
.select(
(Literal(3) + ((Literal(1) + 'a) + 2)) + 4,
'b * 1 * 2 * 3 * 4,
('b + 1) * 2 * 3 * 4,
'a + 1 + 'b + 2 + 'c + 3,
'a + 1 + 'b * 2 + 'c + 3,
Rand(0) * 1 * 2 * 3 * 4)

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer =
testRelation
.select(
('a + 10).as("((3 + ((1 + a) + 2)) + 4)"),
('b * 24).as("((((b * 1) * 2) * 3) * 4)"),
(('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)
.analyze

comparePlans(optimized, correctAnswer)
}
}