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-20124][SQL] Join reorder should keep the same order of final project attributes #17453

Closed
wants to merge 2 commits into from
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 @@ -40,10 +40,10 @@ case class CostBasedJoinReorder(conf: SQLConf) extends Rule[LogicalPlan] with Pr
val result = plan transformDown {
// Start reordering with a joinable item, which is an InnerLike join with conditions.
case j @ Join(_, _, _: InnerLike, Some(cond)) =>
reorder(j, j.outputSet)
reorder(j, j.output)
case p @ Project(projectList, Join(_, _, _: InnerLike, Some(cond)))
if projectList.forall(_.isInstanceOf[Attribute]) =>
reorder(p, p.outputSet)
reorder(p, p.output)
}
// After reordering is finished, convert OrderedJoin back to Join
result transformDown {
Expand All @@ -52,7 +52,7 @@ case class CostBasedJoinReorder(conf: SQLConf) extends Rule[LogicalPlan] with Pr
}
}

private def reorder(plan: LogicalPlan, output: AttributeSet): LogicalPlan = {
private def reorder(plan: LogicalPlan, output: Seq[Attribute]): LogicalPlan = {
val (items, conditions) = extractInnerJoins(plan)
// TODO: Compute the set of star-joins and use them in the join enumeration
// algorithm to prune un-optimal plan choices.
Expand Down Expand Up @@ -140,7 +140,7 @@ object JoinReorderDP extends PredicateHelper with Logging {
conf: SQLConf,
items: Seq[LogicalPlan],
conditions: Set[Expression],
topOutput: AttributeSet): LogicalPlan = {
output: Seq[Attribute]): LogicalPlan = {

val startTime = System.nanoTime()
// Level i maintains all found plans for i + 1 items.
Expand All @@ -152,9 +152,10 @@ object JoinReorderDP extends PredicateHelper with Logging {

// Build plans for next levels until the last level has only one plan. This plan contains
// all items that can be joined, so there's no need to continue.
val topOutputSet = AttributeSet(output)
while (foundPlans.size < items.length && foundPlans.last.size > 1) {
// Build plans for the next level.
foundPlans += searchLevel(foundPlans, conf, conditions, topOutput)
foundPlans += searchLevel(foundPlans, conf, conditions, topOutputSet)
}

val durationInMs = (System.nanoTime() - startTime) / (1000 * 1000)
Expand All @@ -163,7 +164,14 @@ object JoinReorderDP extends PredicateHelper with Logging {

// The last level must have one and only one plan, because all items are joinable.
assert(foundPlans.size == items.length && foundPlans.last.size == 1)
foundPlans.last.head._2.plan
foundPlans.last.head._2.plan match {
case p @ Project(projectList, j: Join) if projectList != output =>
assert(topOutputSet == p.outputSet)
// Keep the same order of final output attributes.
p.copy(projectList = output)
case finalPlan =>
finalPlan
}
}

/** Find all possible plans at the next level, based on existing levels. */
Expand Down Expand Up @@ -254,10 +262,10 @@ object JoinReorderDP extends PredicateHelper with Logging {
val collectedJoinConds = joinConds ++ oneJoinPlan.joinConds ++ otherJoinPlan.joinConds
val remainingConds = conditions -- collectedJoinConds
val neededAttr = AttributeSet(remainingConds.flatMap(_.references)) ++ topOutput
val neededFromNewJoin = newJoin.outputSet.filter(neededAttr.contains)
val neededFromNewJoin = newJoin.output.filter(neededAttr.contains)
val newPlan =
if ((newJoin.outputSet -- neededFromNewJoin).nonEmpty) {
Project(neededFromNewJoin.toSeq, newJoin)
Project(neededFromNewJoin, newJoin)
} else {
newJoin
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ class JoinReorderSuite extends PlanTest with StatsEstimationTestBase {
assertEqualPlans(originalPlan, bestPlan)
}

test("keep the order of attributes in the final output") {
val outputLists = Seq("t1.k-1-2", "t1.v-1-10", "t3.v-1-100").permutations
while (outputLists.hasNext) {
val expectedOrder = outputLists.next().map(nameToAttr)
val expectedPlan =
t1.join(t3, Inner, Some(nameToAttr("t1.v-1-10") === nameToAttr("t3.v-1-100")))
.join(t2, Inner, Some(nameToAttr("t1.k-1-2") === nameToAttr("t2.k-1-5")))
.select(expectedOrder: _*)
// The plan should not change after optimization
assertEqualPlans(expectedPlan, expectedPlan)
}
}

private def assertEqualPlans(
originalPlan: LogicalPlan,
groundTruthBestPlan: LogicalPlan): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ abstract class PlanTest extends SparkFunSuite with PredicateHelper {
(sameJoinPlan(j1.left, j2.left) && sameJoinPlan(j1.right, j2.right)) ||
(sameJoinPlan(j1.left, j2.right) && sameJoinPlan(j1.right, j2.left))
case _ if plan1.children.nonEmpty && plan2.children.nonEmpty =>
(plan1.children, plan2.children).zipped.forall { case (c1, c2) => sameJoinPlan(c1, c2) }
plan1 == plan2 &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Why should they be equal? This implies that the entire subree is the same as well right?

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 you are right. There's some confusion here.

(plan1.children, plan2.children).zipped.forall { case (c1, c2) => sameJoinPlan(c1, c2) }
case _ =>
plan1 == plan2
}
Expand Down