Skip to content
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 @@ -65,6 +65,15 @@ trait PredicateHelper {
}
}

// Substitute any known alias from a map.
protected def replaceAlias(
condition: Expression,
aliases: AttributeMap[Expression]): Expression = {
condition.transform {
case a: Attribute => aliases.getOrElse(a, a)
}
}

/**
* Returns true if `expr` can be evaluated using only the output of `plan`. This method
* can be used to determine when it is acceptable to move expression evaluation within a query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,20 +640,14 @@ object PushPredicateThroughProject extends Rule[LogicalPlan] with PredicateHelpe
filter
} else {
// Push down the small conditions without nondeterministic expressions.
val pushedCondition = deterministic.map(replaceAlias(_, aliasMap)).reduce(And)
val pushedCondition =
deterministic.map(replaceAlias(_, aliasMap)).reduce(And)
Filter(nondeterministic.reduce(And),
project.copy(child = Filter(pushedCondition, grandChild)))
}
}
}

// Substitute any attributes that are produced by the child projection, so that we safely
// eliminate it.
private def replaceAlias(condition: Expression, sourceAliases: AttributeMap[Expression]) = {
condition.transform {
case a: Attribute => sourceAliases.getOrElse(a, a)
}
}
}

/**
Expand Down Expand Up @@ -690,12 +684,24 @@ object PushPredicateThroughAggregate extends Rule[LogicalPlan] with PredicateHel
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case filter @ Filter(condition,
aggregate @ Aggregate(groupingExpressions, aggregateExpressions, grandChild)) =>
val (pushDown, stayUp) = splitConjunctivePredicates(condition).partition {
conjunct => conjunct.references subsetOf AttributeSet(groupingExpressions)

def hasAggregate(expression: Expression): Boolean = expression match {
case agg: AggregateExpression => true
case other => expression.children.exists(hasAggregate)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

a simplification: expr.find(_.isInstanceOf[AggregateExpression]).isDefined, but it's minor, may not worth another roundtrip.

// Create a map of Alias for expressions that does not have AggregateExpression
val aliasMap = AttributeMap(aggregateExpressions.collect {
case a: Alias if !hasAggregate(a.child) => (a.toAttribute, a.child)
})

val (pushDown, stayUp) = splitConjunctivePredicates(condition).partition { conjunct =>
val replaced = replaceAlias(conjunct, aliasMap)
replaced.references.subsetOf(grandChild.outputSet) && replaced.deterministic
}
if (pushDown.nonEmpty) {
val pushDownPredicate = pushDown.reduce(And)
val withPushdown = aggregate.copy(child = Filter(pushDownPredicate, grandChild))
val replaced = replaceAlias(pushDownPredicate, aliasMap)
val withPushdown = aggregate.copy(child = Filter(replaced, grandChild))
stayUp.reduceOption(And).map(Filter(_, withPushdown)).getOrElse(withPushdown)
} else {
filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,4 +697,57 @@ class FilterPushdownSuite extends PlanTest {

comparePlans(optimized, correctAnswer)
}

test("aggregate: push down filters with alias") {
val originalQuery = testRelation
.select('a, 'b)
.groupBy('a)(('a + 1) as 'aa, count('b) as 'c)
.where(('c === 2L || 'aa > 4) && 'aa < 3)

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer = testRelation
.select('a, 'b)
.where('a + 1 < 3)
.groupBy('a)(('a + 1) as 'aa, count('b) as 'c)
.where('c === 2L || 'aa > 4)
.analyze

comparePlans(optimized, correctAnswer)
}

test("aggregate: push down filters with literal") {
val originalQuery = testRelation
.select('a, 'b)
.groupBy('a)('a, count('b) as 'c, "s" as 'd)
.where('c === 2L && 'd === "s")

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer = testRelation
.select('a, 'b)
.where("s" === "s")
.groupBy('a)('a, count('b) as 'c, "s" as 'd)
.where('c === 2L)
.analyze

comparePlans(optimized, correctAnswer)
}

test("aggregate: don't push down filters which is nondeterministic") {
val originalQuery = testRelation
.select('a, 'b)
.groupBy('a)('a + Rand(10) as 'aa, count('b) as 'c, Rand(11).as("rnd"))
.where('c === 2L && 'aa + Rand(10).as("rnd") === 3 && 'rnd === 5)

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer = testRelation
.select('a, 'b)
.groupBy('a)('a + Rand(10) as 'aa, count('b) as 'c, Rand(11).as("rnd"))
.where('c === 2L && 'aa + Rand(10).as("rnd") === 3 && 'rnd === 5)
.analyze

comparePlans(optimized, correctAnswer)
}
}