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 @@ -647,6 +647,11 @@ object LimitPushDown extends Rule[LogicalPlan] {
// There is a Project between LocalLimit and Join if they do not have the same output.
case LocalLimit(exp, project @ Project(_, join: Join)) =>
LocalLimit(exp, project.copy(child = pushLocalLimitThroughJoin(exp, join)))
// Push down limit 1 through Aggregate and turn Aggregate into Project if it is group only.
case Limit(le @ IntegerLiteral(1), a: Aggregate) if a.groupOnly =>
Limit(le, Project(a.output, LocalLimit(le, a.child)))
case Limit(le @ IntegerLiteral(1), p @ Project(_, a: Aggregate)) if a.groupOnly =>
Limit(le, p.copy(child = Project(a.output, LocalLimit(le, a.child))))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,28 @@ class LimitPushdownSuite extends PlanTest {
Limit(5, LocalLimit(5, x).join(y, LeftOuter, joinCondition).select("x.a".attr)).analyze
comparePlans(optimized, correctAnswer)
}

test("SPARK-36183: Push down limit 1 through Aggregate if it is group only") {
// Push down when it is group only and limit 1.
comparePlans(
Optimize.execute(x.groupBy("x.a".attr)("x.a".attr).limit(1).analyze),
LocalLimit(1, x).select("x.a".attr).limit(1).analyze)

comparePlans(
Optimize.execute(x.groupBy("x.a".attr)("x.a".attr).select("x.a".attr).limit(1).analyze),
LocalLimit(1, x).select("x.a".attr).select("x.a".attr).limit(1).analyze)

comparePlans(
Optimize.execute(x.union(y).groupBy("x.a".attr)("x.a".attr).limit(1).analyze),
LocalLimit(1, LocalLimit(1, x).union(LocalLimit(1, y))).select("x.a".attr).limit(1).analyze)

// No push down
comparePlans(
Optimize.execute(x.groupBy("x.a".attr)("x.a".attr).limit(2).analyze),
x.groupBy("x.a".attr)("x.a".attr).limit(2).analyze)

comparePlans(
Optimize.execute(x.groupBy("x.a".attr)("x.a".attr, count("x.a".attr)).limit(1).analyze),
x.groupBy("x.a".attr)("x.a".attr, count("x.a".attr)).limit(1).analyze)
}
}