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-23973][SQL] Remove consecutive Sorts #21072

Closed
wants to merge 6 commits into from

Conversation

mgaido91
Copy link
Contributor

What changes were proposed in this pull request?

In SPARK-23375 we introduced the ability of removing Sort operation during query optimization if the data is already sorted. In this follow-up we remove also a Sort which is followed by another Sort: in this case the first sort is not needed and can be safely removed.

The PR starts from @henryr's comment: #20560 (comment). So credit should be given to him.

How was this patch tested?

added UT

@mgaido91
Copy link
Contributor Author

cc @cloud-fan @henryr

@SparkQA
Copy link

SparkQA commented Apr 14, 2018

Test build #89367 has finished for PR 21072 at commit 6ba4186.

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

*/
object RemoveRedundantSorts extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case Sort(orders, true, child) if SortOrder.orderingSatisfies(child.outputOrdering, orders) =>
child
case s @ Sort(_, _, Sort(_, _, child)) => s.copy(child = child)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for doing this! It might be useful to generalise this to any pair of sorts separated by 0 or more projections or filters. I did this for my SPARK-23975 PR, see: henryr@bb992c2#diff-a636a87d8843eeccca90140be91d4fafR322

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it makes sense. I will do, thanks.

@SparkQA
Copy link

SparkQA commented Apr 16, 2018

Test build #89388 has finished for PR 21072 at commit ff7d412.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Apr 16, 2018

Test build #89395 has finished for PR 21072 at commit ac03bed.

  • This patch fails PySpark pip packaging tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@mgaido91
Copy link
Contributor Author

retest this please

val optimized = Optimize.execute(orderedTwice.analyze)
val correctAnswer = testRelation.orderBy('b.desc).analyze
comparePlans(optimized, correctAnswer)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test for three consecutive sorts? Two is the base case, three will help us show the inductive case :)

@@ -98,4 +98,31 @@ class RemoveRedundantSortsSuite extends PlanTest {
val correctAnswer = groupedAndResorted.analyze
comparePlans(optimized, correctAnswer)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test which explicitly confirms that sort.limit.sort is not simplified? I know the above two tests cover that case, but it's good to have one dedicated to testing this important property.

* Removes Sort operation if the child is already sorted
* Removes redundant Sort operation. This can happen:
* 1) if the child is already sorted
* 2) if the there is another Sort operator separated by 0...n Project/Filter operators
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: 'the there'

@SparkQA
Copy link

SparkQA commented Apr 16, 2018

Test build #89413 has finished for PR 21072 at commit ac03bed.

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

@mgaido91
Copy link
Contributor Author

@henryr thanks, I added the test cases you suggested :)

@SparkQA
Copy link

SparkQA commented Apr 17, 2018

Test build #89445 has finished for PR 21072 at commit 1d6ca1e.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@mgaido91
Copy link
Contributor Author

retest this please

@SparkQA
Copy link

SparkQA commented Apr 17, 2018

Test build #89449 has finished for PR 21072 at commit 1d6ca1e.

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

@mgaido91
Copy link
Contributor Author

anymore comments @henryr ? comments @cloud-fan ?


def recursiveRemoveSort(plan: LogicalPlan): LogicalPlan = plan match {
case Project(fields, child) => Project(fields, recursiveRemoveSort(child))
case Filter(condition, child) => Filter(condition, recursiveRemoveSort(child))
Copy link
Contributor

Choose a reason for hiding this comment

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

we should at least add ResolvedHint. To easily expand the white list in the future, I'd like to change the code style to

def recursiveRemoveSort(plan: LogicalPlan): LogicalPlan = plan match {
  case s: Sort => recursiveRemoveSort(s.child)
  case other if canEliminateSort(other) => other.withNewChildren(other.children.map(recursiveRemoveSort))
  case _ => plan
}

def canEliminateSort(plan: LogicalPlan): Boolean = plan match {
  case p: Project => p.projectList.forall(_.deterministic)
  case f: Filter => f.condition.deterministic
  case _: ResolvedHint => true
  ...
  case _ => false
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why do you think we should check for the filter condition and the projected items to be deterministic?

Copy link
Contributor

Choose a reason for hiding this comment

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

by the definition of deterministic, the entire input is the stats of the expression. It's very likely we will get a different result if we remove sort before filter, e.g. rowId() < 10 will get the first 10 rows, if you sort the input, the first 10 rows changed.

I think we should be conservative about deterministic expressions.

* Removes Sort operation if the child is already sorted
* Removes redundant Sort operation. This can happen:
* 1) if the child is already sorted
* 2) if there is another Sort operator separated by 0...n Project/Filter operators
*/
object RemoveRedundantSorts extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: now it's more efficient to do transformDown

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isn't it the same?

Copy link
Contributor

@cloud-fan cloud-fan Apr 23, 2018

Choose a reason for hiding this comment

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

assume the plan is

Sort
  Filter
    Sort
      Filter
        Sort
          OtherPlan

If we do transformUp, we hit the rule 3 times, which has some unnecessary transformation(OtherPlan is transformed 3 times). If it's transformDown, it's one-shot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but I saw that transfrom actually does transformDown. Anyway, I see that this might change and here we best have transformDown

@cloud-fan
Copy link
Contributor

LGTM

@SparkQA
Copy link

SparkQA commented Apr 23, 2018

Test build #89721 has finished for PR 21072 at commit e7391f3.

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

@SparkQA
Copy link

SparkQA commented Apr 23, 2018

Test build #89726 has finished for PR 21072 at commit e2f4d4d.

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

@cloud-fan
Copy link
Contributor

thanks, merging to master!

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