Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
beliefer committed Nov 12, 2020
1 parent 72ceacc commit 68d3388
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,8 @@ object CollapseRepartition extends Rule[LogicalPlan] {
object OptimizeWindowFunctions extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan resolveExpressions {
case we @ WindowExpression(AggregateExpression(first: First, _, _, _, _), spec)
if !spec.orderSpec.isEmpty =>
if spec.orderSpec.nonEmpty &&
spec.frameSpecification.asInstanceOf[SpecifiedWindowFrame].frameType == RowFrame =>
we.copy(windowFunction = NthValue(first.child, Literal(1), first.ignoreNulls))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class OptimizeWindowFunctionsSuite extends PlanTest {
val b = testRelation.output(1)
val c = testRelation.output(2)

test("replace first(col) by nth_value(col, 1) if the window frame is ordered") {
test("replace first(col) by nth_value(col, 1)") {
val inputPlan = testRelation.select(
WindowExpression(
First(a, false).toAggregateExpression(),
Expand All @@ -52,6 +52,17 @@ class OptimizeWindowFunctionsSuite extends PlanTest {
assert(optimized == correctAnswer)
}

test("can't replace first(col) by nth_value(col, 1) if the window frame type is row") {
val inputPlan = testRelation.select(
WindowExpression(
First(a, false).toAggregateExpression(),
WindowSpecDefinition(b :: Nil, c.asc :: Nil,
SpecifiedWindowFrame(RangeFrame, UnboundedPreceding, CurrentRow))))

val optimized = Optimize.execute(inputPlan)
assert(optimized == inputPlan)
}

test("can't replace first(col) by nth_value(col, 1) if the window frame isn't ordered") {
val inputPlan = testRelation.select(
WindowExpression(
Expand Down

0 comments on commit 68d3388

Please sign in to comment.