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-24953] [SQL] Prune a branch in CaseWhen if previously seen #21904

Closed
wants to merge 3 commits into from

Conversation

dbtsai
Copy link
Member

@dbtsai dbtsai commented Jul 27, 2018

What changes were proposed in this pull request?

If a condition in a branch is previously seen, this branch can be pruned.

If the outputs of two adjacent branches are the same, two branches can be combined.

How was this patch tested?

Tests added.

@SparkQA
Copy link

SparkQA commented Jul 28, 2018

Test build #93705 has finished for PR 21904 at commit 4ac3d1e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -416,6 +416,29 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
// these branches can be pruned away
val (h, t) = branches.span(_._1 != TrueLiteral)
CaseWhen( h :+ t.head, None)

case e @ CaseWhen(branches, _) =>
val newBranches = branches.foldLeft(List[(Expression, Expression)]()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about using ArrayBuffer? So we don't have to recreate the List at every iteration...

newBranches
} else if (newBranches.nonEmpty && newBranches.last._2.semanticEquals(branch._2)) {
// If the outputs of two adjacent branches are the same, two branches can be combined.
newBranches.take(newBranches.length - 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newBranches.init

} else if (newBranches.nonEmpty && newBranches.last._2.semanticEquals(branch._2)) {
// If the outputs of two adjacent branches are the same, two branches can be combined.
newBranches.take(newBranches.length - 1)
.:+((Or(newBranches.last._1, branch._1), newBranches.last._2))
Copy link
Member

Choose a reason for hiding this comment

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

Can this provide any benefit?

Copy link
Member Author

Choose a reason for hiding this comment

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

For example, the following case can be benefitted from this rule,

      CaseWhen((UnresolvedAttribute("a"), Literal(1)) ::
        (Not(UnresolvedAttribute("a")), Literal(1)) ::
        (LessThan(Rand(1), Literal(0.5)), Literal(3)) ::
        (NonFoldableLiteral(true), Literal(4)) ::
        (NonFoldableLiteral(false), Literal(5)) ::
        Nil,
        None),
      Literal(1))

// TODO: In fact, if a condition is a sub-condition of the previous one,
// TODO: it can be pruned. This is less strict and can be implemented
// TODO: by decomposing seen conditions.
newBranches
Copy link
Member

Choose a reason for hiding this comment

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

This seems good as the branch is useless. Removing it should simplify code and query plan.

e.copy(branches = pruneSeenBranches(branches).get)

case e @ CaseWhen(branches, _) if combineAdjacentBranches(branches).nonEmpty =>
e.copy(branches = combineAdjacentBranches(branches).get)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe:

case e @ CaseWhen(branches, _) =>
  val prunedBranches = pruneSeenBranches(branches).getOrElse(branches)
  val newBranches = combineAdjacentBranches(prunedBranches).getOrElse(prunedBranches)
  if (newBranches.length < branches.length) {
    e.copy(branches = newBranches)
  } else {
    e
  }

Copy link
Member

Choose a reason for hiding this comment

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

If pruneSeenBranches and combineAdjacentBranches both return input branches if no changes applied, this maybe more simplified:

case e @ CaseWhen(branches, _) =>
  val prunedBranches = pruneSeenBranches(branches)
  val newBranches = combineAdjacentBranches(prunedBranches)
  if (newBranches.length < branches.length) {
    e.copy(branches = newBranches)
  } else {
    e
  }

Copy link
Member Author

Choose a reason for hiding this comment

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

This will work. But my only concern is that this will be caught all the CaseWhen cases; as a result, no more rule can be added after this.

@SparkQA
Copy link

SparkQA commented Jul 31, 2018

Test build #93804 has finished for PR 21904 at commit 6915647.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@dbtsai dbtsai closed this Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants