Skip to content
Snippets Groups Projects
Commit 31a229aa authored by Wenchen Fan's avatar Wenchen Fan Committed by Michael Armbrust
Browse files

[SPARK-10475] [SQL] improve column prunning for Project on Sort

Sometimes we can't push down the whole `Project` though `Sort`, but we still have a chance to push down part of it.

Author: Wenchen Fan <cloud0fan@outlook.com>

Closes #8644 from cloud-fan/column-prune.
parent 841972e2
No related branches found
No related tags found
No related merge requests found
......@@ -228,10 +228,21 @@ object ColumnPruning extends Rule[LogicalPlan] {
case Project(projectList, Limit(exp, child)) =>
Limit(exp, Project(projectList, child))
// Push down project if possible when the child is sort
case p @ Project(projectList, s @ Sort(_, _, grandChild))
if s.references.subsetOf(p.outputSet) =>
s.copy(child = Project(projectList, grandChild))
// Push down project if possible when the child is sort.
case p @ Project(projectList, s @ Sort(_, _, grandChild)) =>
if (s.references.subsetOf(p.outputSet)) {
s.copy(child = Project(projectList, grandChild))
} else {
val neededReferences = s.references ++ p.references
if (neededReferences == grandChild.outputSet) {
// No column we can prune, return the original plan.
p
} else {
// Do not use neededReferences.toSeq directly, should respect grandChild's output order.
val newProjectList = grandChild.output.filter(neededReferences.contains)
p.copy(child = s.copy(child = Project(newProjectList, grandChild)))
}
}
// Eliminate no-op Projects
case Project(projectList, child) if child.output == projectList => child
......
......@@ -80,5 +80,16 @@ class ColumnPruningSuite extends PlanTest {
comparePlans(optimized, correctAnswer)
}
test("Column pruning for Project on Sort") {
val input = LocalRelation('a.int, 'b.string, 'c.double)
val query = input.orderBy('b.asc).select('a).analyze
val optimized = Optimize.execute(query)
val correctAnswer = input.select('a, 'b).orderBy('b.asc).select('a).analyze
comparePlans(optimized, correctAnswer)
}
// todo: add more tests for column pruning
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment