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 @@ -50,11 +50,27 @@ object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] {
}.filter(_.nonEmpty)
}

/**
* Add a new ExpressionSet S into distinctKeys D.
* To minimize the size of D:
* 1. If there is a subset of S in D, return D.
* 2. Otherwise, remove all the ExpressionSet containing S from D, and add the new one.
*/
private def addDistinctKey(
keys: Set[ExpressionSet],
newExpressionSet: ExpressionSet): Set[ExpressionSet] = {
if (keys.exists(_.subsetOf(newExpressionSet))) {
keys
} else {
keys.filterNot(s => newExpressionSet.subsetOf(s)) + newExpressionSet
}
}

override def default(p: LogicalPlan): Set[ExpressionSet] = Set.empty[ExpressionSet]

override def visitAggregate(p: Aggregate): Set[ExpressionSet] = {
val groupingExps = ExpressionSet(p.groupingExpressions) // handle group by a, a
projectDistinctKeys(Set(groupingExps), p.aggregateExpressions)
projectDistinctKeys(addDistinctKey(p.child.distinctKeys, groupingExps), p.aggregateExpressions)
}

override def visitDistinct(p: Distinct): Set[ExpressionSet] = Set(ExpressionSet(p.output))
Expand All @@ -70,7 +86,7 @@ object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] {

override def visitGlobalLimit(p: GlobalLimit): Set[ExpressionSet] = {
p.maxRows match {
case Some(value) if value <= 1 => Set(ExpressionSet(p.output))
case Some(value) if value <= 1 => p.output.map(attr => ExpressionSet(Seq(attr))).toSet
case _ => p.child.distinctKeys
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class DistinctKeyVisitorSuite extends PlanTest {
Set(ExpressionSet(Seq(a)), ExpressionSet(Seq(d.toAttribute))))
checkDistinctAttributes(t1.groupBy(f.child, $"b")(f, $"b", sum($"c")),
Set(ExpressionSet(Seq(f.toAttribute, b))))

// Aggregate should also propagate distinct keys from child
checkDistinctAttributes(t1.limit(1).groupBy($"a", $"b")($"a", $"b"),
Set(ExpressionSet(Seq(a)), ExpressionSet(Seq(b))))
}

test("Distinct's distinct attributes") {
Expand All @@ -86,7 +90,8 @@ class DistinctKeyVisitorSuite extends PlanTest {
test("Limit's distinct attributes") {
checkDistinctAttributes(Distinct(t1).limit(10), Set(ExpressionSet(Seq(a, b, c))))
checkDistinctAttributes(LocalLimit(10, Distinct(t1)), Set(ExpressionSet(Seq(a, b, c))))
checkDistinctAttributes(t1.limit(1), Set(ExpressionSet(Seq(a, b, c))))
checkDistinctAttributes(t1.limit(1),
Set(ExpressionSet(Seq(a)), ExpressionSet(Seq(b)), ExpressionSet(Seq(c))))
}

test("Intersect's distinct attributes") {
Expand Down