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-10475][SQL] improve column prunning for Project on Sort #8644

Closed
wants to merge 1 commit 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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}