Skip to content
Snippets Groups Projects
Commit da56c4e7 authored by Liang-Chi Hsieh's avatar Liang-Chi Hsieh Committed by Michael Armbrust
Browse files

[SPARK-8794] [SQL] Make PrunedScan work for Sample

JIRA: https://issues.apache.org/jira/browse/SPARK-8794

Currently `PrunedScan` works only when followed by project or filter operations. However, even if there is a `Sample` between these operations and `PrunedScan`, `PrunedScan` should work too.

Author: Liang-Chi Hsieh <viirya@appier.com>
Author: Liang-Chi Hsieh <viirya@gmail.com>

Closes #7228 from viirya/sample_prunedscan and squashes the following commits:

ede7cd8 [Liang-Chi Hsieh] Keep PrunedScanSuite untouched.
6f05d30 [Liang-Chi Hsieh] Move unit test to FilterPushdownSuite.
5f32473 [Liang-Chi Hsieh] Merge remote-tracking branch 'upstream/master' into sample_prunedscan
7e4ba76 [Liang-Chi Hsieh] Use Optimzier for push down projection and filter.
0686830 [Liang-Chi Hsieh] Merge remote-tracking branch 'upstream/master' into sample_prunedscan
df82785 [Liang-Chi Hsieh] Make PrunedScan work on Sample.
parent 3bf20c27
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,7 @@ object DefaultOptimizer extends Optimizer {
Batch("Operator Optimizations", FixedPoint(100),
// Operator push down
UnionPushDown,
SamplePushDown,
PushPredicateThroughJoin,
PushPredicateThroughProject,
PushPredicateThroughGenerate,
......@@ -65,6 +66,23 @@ object DefaultOptimizer extends Optimizer {
ConvertToLocalRelation) :: Nil
}
/**
* Pushes operations down into a Sample.
*/
object SamplePushDown extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
// Push down filter into sample
case Filter(condition, s @ Sample(lb, up, replace, seed, child)) =>
Sample(lb, up, replace, seed,
Filter(condition, child))
// Push down projection into sample
case Project(projectList, s @ Sample(lb, up, replace, seed, child)) =>
Sample(lb, up, replace, seed,
Project(projectList, child))
}
}
/**
* Pushes operations to either side of a Union.
*/
......
......@@ -34,6 +34,7 @@ class FilterPushdownSuite extends PlanTest {
Batch("Subqueries", Once,
EliminateSubQueries) ::
Batch("Filter Pushdown", Once,
SamplePushDown,
CombineFilters,
PushPredicateThroughProject,
BooleanSimplification,
......@@ -593,4 +594,19 @@ class FilterPushdownSuite extends PlanTest {
comparePlans(optimized1, analysis.EliminateSubQueries(correctAnswer1))
}
test("push project and filter down into sample") {
val x = testRelation.subquery('x)
val originalQuery =
Sample(0.0, 0.6, false, 11L, x).select('a)
val originalQueryAnalyzed = EliminateSubQueries(analysis.SimpleAnalyzer.execute(originalQuery))
val optimized = Optimize.execute(originalQueryAnalyzed)
val correctAnswer =
Sample(0.0, 0.6, false, 11L, x.select('a))
comparePlans(optimized, correctAnswer.analyze)
}
}
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